static-cms/src/backends/github/implementation.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-08-24 21:36:44 -03:00
import { createEntry } from '../../valueObjects/Entry';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
export default class GitHub {
constructor(config) {
this.config = config;
if (config.getIn(['backend', 'repo']) == null) {
throw 'The GitHub backend needs a "repo" in the backend configuration.';
}
this.repo = config.getIn(['backend', 'repo']);
}
authComponent() {
return AuthenticationPage;
}
setUser(user) {
this.api = new API(user.token, this.repo, this.branch || 'master');
}
authenticate(state) {
this.api = new API(state.token, this.repo, this.branch || 'master');
return this.api.user().then((user) => {
user.token = state.token;
return user;
});
}
entries(collection) {
return this.api.listFiles(collection.get('folder')).then((files) => (
Promise.all(files.map((file) => (
this.api.readFile(file.path, file.sha).then((data) => {
2016-08-24 21:36:44 -03:00
return createEntry(file.path, file.path.split('/').pop().replace(/\.[^\.]+$/, ''), data);
})
)))
)).then((entries) => ({
pagination: {},
entries
}));
}
entry(collection, slug) {
return this.entries(collection).then((response) => (
response.entries.filter((entry) => entry.slug === slug)[0]
));
}
2016-07-19 17:11:22 -03:00
persistEntry(entry, mediaFiles = [], options = {}) {
return this.api.persistFiles(entry, mediaFiles, options);
2016-07-19 17:11:22 -03:00
}
}