2016-12-23 16:59:48 -02:00
|
|
|
import TestRepoBackend from "./test-repo/implementation";
|
|
|
|
import GitHubBackend from "./github/implementation";
|
|
|
|
import NetlifyAuthBackend from "./netlify-auth/implementation";
|
|
|
|
import { resolveFormat } from "../formats/formats";
|
|
|
|
import { selectListMethod, selectEntrySlug, selectEntryPath, selectAllowNewEntries } from "../reducers/collections";
|
|
|
|
import { createEntry } from "../valueObjects/Entry";
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
class LocalStorageAuthStore {
|
2016-12-23 17:30:27 -02:00
|
|
|
storageKey = "netlify-cms-user";
|
2016-05-30 16:55:32 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-11-01 14:35:20 +01:00
|
|
|
|
|
|
|
logout() {
|
|
|
|
window.localStorage.removeItem(this.storageKey);
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
const slugFormatter = (template = "{{slug}}", entryData) => {
|
2016-10-21 20:42:14 -02:00
|
|
|
const date = new Date();
|
2016-12-23 16:59:48 -02:00
|
|
|
const identifier = entryData.get("title", entryData.get("path"));
|
2016-10-28 11:42:31 -02:00
|
|
|
return template.replace(/\{\{([^\}]+)\}\}/g, (_, field) => {
|
|
|
|
switch (field) {
|
2016-12-23 16:59:48 -02:00
|
|
|
case "year":
|
2016-10-10 15:34:21 -03:00
|
|
|
return date.getFullYear();
|
2016-12-23 16:59:48 -02:00
|
|
|
case "month":
|
2016-10-21 20:42:14 -02:00
|
|
|
return (`0${ date.getMonth() + 1 }`).slice(-2);
|
2016-12-23 16:59:48 -02:00
|
|
|
case "day":
|
2016-10-21 20:42:14 -02:00
|
|
|
return (`0${ date.getDate() }`).slice(-2);
|
2016-12-23 16:59:48 -02:00
|
|
|
case "slug":
|
2017-03-14 13:39:56 -07:00
|
|
|
return identifier.trim().toLowerCase().replace(/[^a-z0-9\-_]+/gi, "-");
|
2016-10-10 15:34:21 -03:00
|
|
|
default:
|
2016-12-23 16:59:48 -02:00
|
|
|
return entryData.get(field, "").trim().toLowerCase().replace(/[^a-z0-9\.\-_]+/gi, "-");
|
2016-10-10 15:34:21 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
class Backend {
|
|
|
|
constructor(implementation, authStore = null) {
|
|
|
|
this.implementation = implementation;
|
|
|
|
this.authStore = authStore;
|
2016-09-05 12:12:38 -03:00
|
|
|
if (this.implementation === null) {
|
2016-12-23 16:59:48 -02:00
|
|
|
throw new Error("Cannot instantiate a Backend with no implementation");
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUser() {
|
|
|
|
if (this.user) { return this.user; }
|
2016-06-05 01:52:18 -07:00
|
|
|
const stored = this.authStore && this.authStore.retrieve();
|
|
|
|
if (stored) {
|
2016-12-23 16:59:48 -02:00
|
|
|
return Promise.resolve(this.implementation.setUser(stored)).then(() => stored);
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
return Promise.resolve(null);
|
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-11-01 14:35:20 +01:00
|
|
|
logout() {
|
|
|
|
if (this.authStore) {
|
|
|
|
this.authStore.logout();
|
|
|
|
} else {
|
2016-12-23 16:59:48 -02:00
|
|
|
throw new Error("User isn't authenticated.");
|
2016-11-01 14:35:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
getToken = () => this.implementation.getToken();
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
listEntries(collection) {
|
2016-11-11 17:54:58 -02:00
|
|
|
const listMethod = this.implementation[selectListMethod(collection)];
|
2016-10-27 14:23:36 +02:00
|
|
|
return listMethod.call(this.implementation, collection)
|
2016-10-21 20:42:14 -02:00
|
|
|
.then(loadedEntries => (
|
2016-10-27 14:23:36 +02:00
|
|
|
loadedEntries.map(loadedEntry => createEntry(
|
2016-12-23 16:59:48 -02:00
|
|
|
collection.get("name"),
|
2016-11-11 17:54:58 -02:00
|
|
|
selectEntrySlug(collection, loadedEntry.file.path),
|
2016-10-27 14:23:36 +02:00
|
|
|
loadedEntry.file.path,
|
|
|
|
{ raw: loadedEntry.data, label: loadedEntry.file.label }
|
|
|
|
))
|
2016-10-21 20:42:14 -02:00
|
|
|
))
|
|
|
|
.then(entries => (
|
|
|
|
{
|
|
|
|
entries: entries.map(this.entryWithFormat(collection)),
|
|
|
|
}
|
|
|
|
));
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:12:18 -02:00
|
|
|
getEntry(collection, slug) {
|
2016-11-11 17:54:58 -02:00
|
|
|
return this.implementation.getEntry(collection, slug, selectEntryPath(collection, slug))
|
2016-10-27 15:33:15 +02:00
|
|
|
.then(loadedEntry => this.entryWithFormat(collection, slug)(createEntry(
|
2016-12-23 16:59:48 -02:00
|
|
|
collection.get("name"),
|
2016-10-27 14:23:36 +02:00
|
|
|
slug,
|
|
|
|
loadedEntry.file.path,
|
|
|
|
{ raw: loadedEntry.data, label: loadedEntry.file.label }
|
2016-10-27 14:45:32 +02:00
|
|
|
))
|
2016-10-27 14:23:36 +02:00
|
|
|
);
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
entryWithFormat(collectionOrEntity) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return (entry) => {
|
2016-09-06 13:04:17 -03:00
|
|
|
const format = resolveFormat(collectionOrEntity, entry);
|
2016-06-05 01:52:18 -07:00
|
|
|
if (entry && entry.raw) {
|
2016-10-27 14:45:32 +02:00
|
|
|
return Object.assign(entry, { data: format && format.fromFile(entry.raw) });
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
2016-10-27 14:45:32 +02:00
|
|
|
return format.fromFile(entry);
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
2016-06-06 21:53:22 -03:00
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
unpublishedEntries(page, perPage) {
|
2016-10-27 15:27:39 -02:00
|
|
|
return this.implementation.unpublishedEntries(page, perPage)
|
2016-10-28 11:42:31 -02:00
|
|
|
.then(loadedEntries => loadedEntries.filter(entry => entry !== null))
|
|
|
|
.then(entries => (
|
|
|
|
entries.map((loadedEntry) => {
|
2017-01-11 20:58:15 -02:00
|
|
|
const entry = createEntry(loadedEntry.metaData.collection, loadedEntry.slug, loadedEntry.file.path, { raw: loadedEntry.data });
|
2016-10-27 15:27:39 -02:00
|
|
|
entry.metaData = loadedEntry.metaData;
|
|
|
|
return entry;
|
|
|
|
})
|
|
|
|
))
|
2016-10-28 11:42:31 -02:00
|
|
|
.then(entries => ({
|
|
|
|
pagination: 0,
|
2016-12-23 16:59:48 -02:00
|
|
|
entries: entries.map(this.entryWithFormat("editorialWorkflow")),
|
2016-10-28 11:42:31 -02:00
|
|
|
}));
|
2016-09-06 13:04:17 -03:00
|
|
|
}
|
|
|
|
|
2016-09-13 03:59:48 -03:00
|
|
|
unpublishedEntry(collection, slug) {
|
2016-10-27 15:27:39 -02:00
|
|
|
return this.implementation.unpublishedEntry(collection, slug)
|
2016-10-28 11:42:31 -02:00
|
|
|
.then((loadedEntry) => {
|
2016-12-23 16:59:48 -02:00
|
|
|
const entry = createEntry("draft", loadedEntry.slug, loadedEntry.file.path, { raw: loadedEntry.data });
|
2016-10-28 11:42:31 -02:00
|
|
|
entry.metaData = loadedEntry.metaData;
|
|
|
|
return entry;
|
|
|
|
})
|
|
|
|
.then(this.entryWithFormat(collection, slug));
|
2016-09-13 03:59:48 -03:00
|
|
|
}
|
|
|
|
|
2016-09-13 14:31:18 -03:00
|
|
|
persistEntry(config, collection, entryDraft, MediaFiles, options) {
|
2016-12-23 16:59:48 -02:00
|
|
|
const newEntry = entryDraft.getIn(["entry", "newRecord"]) || false;
|
2016-09-05 12:12:38 -03:00
|
|
|
|
|
|
|
const parsedData = {
|
2016-12-23 16:59:48 -02:00
|
|
|
title: entryDraft.getIn(["entry", "data", "title"], "No Title"),
|
|
|
|
description: entryDraft.getIn(["entry", "data", "description"], "No Description!"),
|
2016-09-05 12:12:38 -03:00
|
|
|
};
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
const entryData = entryDraft.getIn(["entry", "data"]).toJS();
|
2016-08-25 16:11:00 -03:00
|
|
|
let entryObj;
|
|
|
|
if (newEntry) {
|
2016-11-11 17:54:58 -02:00
|
|
|
if (!selectAllowNewEntries(collection)) {
|
2016-12-23 16:59:48 -02:00
|
|
|
throw (new Error("Not allowed to create new entries in this collection"));
|
2016-10-27 14:23:36 +02:00
|
|
|
}
|
2016-12-23 16:59:48 -02:00
|
|
|
const slug = slugFormatter(collection.get("slug"), entryDraft.getIn(["entry", "data"]));
|
2016-11-11 17:54:58 -02:00
|
|
|
const path = selectEntryPath(collection, slug);
|
2016-08-25 16:11:00 -03:00
|
|
|
entryObj = {
|
2016-10-28 04:51:37 +02:00
|
|
|
path,
|
2016-10-21 20:42:14 -02:00
|
|
|
slug,
|
2017-02-21 23:04:12 -08:00
|
|
|
raw: this.entryToRaw(collection, entryDraft.get("entry")),
|
2016-08-25 16:11:00 -03:00
|
|
|
};
|
|
|
|
} else {
|
2016-12-23 16:59:48 -02:00
|
|
|
const path = entryDraft.getIn(["entry", "path"]);
|
2017-02-21 23:04:12 -08:00
|
|
|
const slug = entryDraft.getIn(["entry", "slug"]);
|
2016-08-25 16:11:00 -03:00
|
|
|
entryObj = {
|
2016-10-28 04:51:37 +02:00
|
|
|
path,
|
2017-02-21 23:04:12 -08:00
|
|
|
slug,
|
|
|
|
raw: this.entryToRaw(collection, entryDraft.get("entry")),
|
2016-08-25 16:11:00 -03:00
|
|
|
};
|
|
|
|
}
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2017-03-17 01:09:52 +00:00
|
|
|
const commitMessage = `${ (newEntry ? "Create " : "Update ") +
|
2016-12-23 16:59:48 -02:00
|
|
|
collection.get("label") } “${ entryObj.slug }”`;
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
const mode = config.get("publish_mode");
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
const collectionName = collection.get("name");
|
2016-08-30 19:06:20 -03:00
|
|
|
|
2016-09-05 12:12:38 -03:00
|
|
|
return this.implementation.persistEntry(entryObj, MediaFiles, {
|
2016-10-21 20:42:14 -02:00
|
|
|
newEntry, parsedData, commitMessage, collectionName, mode, ...options,
|
2016-09-05 12:12:38 -03:00
|
|
|
});
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
|
|
|
|
2016-09-13 16:00:24 -03:00
|
|
|
persistUnpublishedEntry(config, collection, entryDraft, MediaFiles) {
|
|
|
|
return this.persistEntry(config, collection, entryDraft, MediaFiles, { unpublished: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUnpublishedEntryStatus(collection, slug, newStatus) {
|
|
|
|
return this.implementation.updateUnpublishedEntryStatus(collection, slug, newStatus);
|
2016-09-13 14:31:18 -03:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:58:15 -02:00
|
|
|
publishUnpublishedEntry(collection, slug) {
|
|
|
|
return this.implementation.publishUnpublishedEntry(collection, slug);
|
2016-09-14 18:25:45 -03:00
|
|
|
}
|
|
|
|
|
2017-03-11 13:47:36 -05:00
|
|
|
deleteUnpublishedEntry(collection, slug) {
|
|
|
|
return this.implementation.deleteUnpublishedEntry(collection, slug);
|
|
|
|
}
|
2016-09-13 14:31:18 -03:00
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
entryToRaw(collection, entry) {
|
2017-02-21 23:04:12 -08:00
|
|
|
const format = resolveFormat(collection, entry.toJS());
|
|
|
|
const fieldsOrder = this.fieldsOrder(collection, entry);
|
|
|
|
return format && format.toFile(entry.get("data").toJS(), fieldsOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldsOrder(collection, entry) {
|
|
|
|
const fields = collection.get('fields');
|
|
|
|
if (fields) {
|
|
|
|
return collection.get('fields').map(f => f.get('name')).toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = collection.get('files');
|
|
|
|
const file = (files || []).filter(f => f.get("name") === entry.get("slug")).get(0);
|
|
|
|
if (file == null) {
|
|
|
|
throw new Error(`No file found for ${ entry.get("slug") } in ${ collection.get('name') }`);
|
|
|
|
}
|
2017-02-21 23:59:34 -08:00
|
|
|
return file.get('fields').map(f => f.get('name')).toArray();
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
export function resolveBackend(config) {
|
2016-12-23 16:59:48 -02:00
|
|
|
const name = config.getIn(["backend", "name"]);
|
2016-05-30 16:55:32 -07:00
|
|
|
if (name == null) {
|
2016-12-23 16:59:48 -02:00
|
|
|
throw new Error("No backend defined in configuration");
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const authStore = new LocalStorageAuthStore();
|
|
|
|
|
|
|
|
switch (name) {
|
2016-12-23 16:59:48 -02:00
|
|
|
case "test-repo":
|
2016-10-27 13:12:18 -02:00
|
|
|
return new Backend(new TestRepoBackend(config), authStore);
|
2016-12-23 16:59:48 -02:00
|
|
|
case "github":
|
2016-10-27 13:12:18 -02:00
|
|
|
return new Backend(new GitHubBackend(config), authStore);
|
2016-12-23 16:59:48 -02:00
|
|
|
case "netlify-auth":
|
|
|
|
return new Backend(new NetlifyAuthBackend(config), authStore);
|
2016-05-30 16:55:32 -07:00
|
|
|
default:
|
2016-10-27 14:45:32 +02:00
|
|
|
throw new Error(`Backend not found: ${ name }`);
|
2016-05-30 16:55:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
export const currentBackend = (function () {
|
2016-02-25 12:31:21 -08:00
|
|
|
let backend = null;
|
|
|
|
|
|
|
|
return (config) => {
|
|
|
|
if (backend) { return backend; }
|
2016-12-23 16:59:48 -02:00
|
|
|
if (config.get("backend")) {
|
2016-02-25 12:31:21 -08:00
|
|
|
return backend = resolveBackend(config);
|
|
|
|
}
|
|
|
|
};
|
2016-10-21 20:42:14 -02:00
|
|
|
}());
|