Metadata retrieving
This commit is contained in:
parent
2f635944fd
commit
a2420647d5
@ -68,24 +68,43 @@ export default class API {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
storeMetadata(name, data) {
|
storeMetadata(key, data) {
|
||||||
this.checkMetadataBranch()
|
this.checkMetadataBranch()
|
||||||
.then((branchData) => {
|
.then((branchData) => {
|
||||||
const fileTree = {
|
const fileTree = {
|
||||||
[`${name}.json`]: {
|
[`${key}.json`]: {
|
||||||
path: `${name}.json`,
|
path: `${key}.json`,
|
||||||
raw: JSON.stringify(data),
|
raw: JSON.stringify(data),
|
||||||
file: true
|
file: true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.uploadBlob(fileTree[`${name}.json`])
|
return this.uploadBlob(fileTree[`${key}.json`])
|
||||||
.then(item => this.updateTree(branchData.sha, '/', fileTree))
|
.then(item => this.updateTree(branchData.sha, '/', fileTree))
|
||||||
.then(changeTree => this.commit(`Updating “${name}” metadata`, changeTree))
|
.then(changeTree => this.commit(`Updating “${key}” metadata`, changeTree))
|
||||||
.then(response => this.patchBranch('_netlify_cms', response.sha));
|
.then(response => this.patchBranch('_netlify_cms', response.sha));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
retrieveMetadata(key, data) {
|
||||||
|
const cache = LocalForage.getItem(`gh.meta.${key}`);
|
||||||
|
return cache.then((cached) => {
|
||||||
|
if (cached && cached.expires > Date.now()) { return cached.data; }
|
||||||
|
|
||||||
|
return this.request(`${this.repoURL}/contents/${key}.json?ref=_netlify_cms`, {
|
||||||
|
headers: { Accept: 'application/vnd.github.VERSION.raw' },
|
||||||
|
cache: 'no-store',
|
||||||
|
}).then((result) => {
|
||||||
|
console.log(result);
|
||||||
|
LocalForage.setItem(`gh.meta.${key}`, {
|
||||||
|
expires: Date.now() + 300000, // In 5 minutes
|
||||||
|
data: result,
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
readFile(path, sha) {
|
readFile(path, sha) {
|
||||||
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
|
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
|
||||||
return cache.then((cached) => {
|
return cache.then((cached) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user