2016-02-25 12:31:21 -08:00
|
|
|
import TestRepoBackend from './test-repo/Implementation';
|
2016-06-05 01:52:18 -07:00
|
|
|
import GitHubBackend from './github/Implementation';
|
2016-02-25 20:40:35 -08:00
|
|
|
import { resolveFormat } from '../formats/formats';
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
class LocalStorageAuthStore {
|
|
|
|
storageKey = 'nf-cms-user';
|
|
|
|
|
|
|
|
retrieve() {
|
|
|
|
const data = window.localStorage.getItem(this.storageKey);
|
|
|
|
return data && JSON.parse(data);
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
store(userData) {
|
|
|
|
window.localStorage.setItem(this.storageKey, JSON.stringify(userData));
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Backend {
|
|
|
|
constructor(implementation, authStore = null) {
|
|
|
|
this.implementation = implementation;
|
|
|
|
this.authStore = authStore;
|
|
|
|
if (this.implementation == null) {
|
|
|
|
throw 'Cannot instantiate a Backend with no implementation';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUser() {
|
|
|
|
if (this.user) { return this.user; }
|
2016-06-05 01:52:18 -07:00
|
|
|
const stored = this.authStore && this.authStore.retrieve();
|
|
|
|
if (stored) {
|
|
|
|
this.implementation.setUser(stored);
|
|
|
|
return stored;
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
authComponent() {
|
|
|
|
return this.implementation.authComponent();
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
authenticate(credentials) {
|
2016-05-30 16:55:32 -07:00
|
|
|
return this.implementation.authenticate(credentials).then((user) => {
|
|
|
|
if (this.authStore) { this.authStore.store(user); }
|
|
|
|
return user;
|
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
entries(collection, page, perPage) {
|
|
|
|
return this.implementation.entries(collection, page, perPage).then((response) => {
|
|
|
|
return {
|
|
|
|
pagination: response.pagination,
|
|
|
|
entries: response.entries.map(this.entryWithFormat(collection))
|
|
|
|
};
|
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
entry(collection, slug) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return this.implementation.entry(collection, slug).then(this.entryWithFormat(collection));
|
|
|
|
}
|
|
|
|
|
|
|
|
entryWithFormat(collection) {
|
|
|
|
return (entry) => {
|
|
|
|
const format = resolveFormat(collection, entry);
|
|
|
|
if (entry && entry.raw) {
|
|
|
|
entry.data = format && format.fromFile(entry.raw);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
export function resolveBackend(config) {
|
|
|
|
const name = config.getIn(['backend', 'name']);
|
|
|
|
if (name == null) {
|
|
|
|
throw 'No backend defined in configuration';
|
|
|
|
}
|
|
|
|
|
|
|
|
const authStore = new LocalStorageAuthStore();
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case 'test-repo':
|
|
|
|
return new Backend(new TestRepoBackend(config), authStore);
|
2016-06-05 01:52:18 -07:00
|
|
|
case 'github':
|
|
|
|
return new Backend(new GitHubBackend(config), authStore);
|
2016-05-30 16:55:32 -07:00
|
|
|
default:
|
|
|
|
throw `Backend not found: ${name}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
export const currentBackend = (function() {
|
|
|
|
let backend = null;
|
|
|
|
|
|
|
|
return (config) => {
|
|
|
|
if (backend) { return backend; }
|
|
|
|
if (config.get('backend')) {
|
|
|
|
return backend = resolveBackend(config);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|