Skip to content

Commit

Permalink
Register Internal and Custom backend API's (#1011)
Browse files Browse the repository at this point in the history
* allow backend register

* handle multiple backends, including internal
  • Loading branch information
talves authored and erquhart committed Jan 25, 2018
1 parent 22d8475 commit 7053ccd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/backends/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import { sanitizeSlug } from "Lib/urlHelper";
import TestRepoBackend from "./test-repo/implementation";
import GitHubBackend from "./github/implementation";
import GitGatewayBackend from "./git-gateway/implementation";
import { registerBackend, getBackend } from 'Lib/registry';

/**
* Register internal backends
*/
registerBackend('git-gateway', GitGatewayBackend);
registerBackend('github', GitHubBackend);
registerBackend('test-repo', TestRepoBackend);


class LocalStorageAuthStore {
storageKey = "netlify-cms-user";
Expand Down Expand Up @@ -339,15 +348,10 @@ export function resolveBackend(config) {

const authStore = new LocalStorageAuthStore();

switch (name) {
case "test-repo":
return new Backend(new TestRepoBackend(config), name, authStore);
case "github":
return new Backend(new GitHubBackend(config), name, authStore);
case "git-gateway":
return new Backend(new GitGatewayBackend(config), name, authStore);
default:
throw new Error(`Backend not found: ${ name }`);
if (!getBackend(name)) {
throw new Error(`Backend not found: ${ name }`);
} else {
return new Backend(getBackend(name).init(config), name, authStore);
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { newEditorPlugin } from 'EditorWidgets/Markdown/MarkdownControl/plugins'
* Global Registry Object
*/
const registry = {
backends: { },
templates: {},
previewStyles: [],
widgets: {},
Expand All @@ -24,6 +25,8 @@ export default {
getEditorComponents,
registerWidgetValueSerializer,
getWidgetValueSerializer,
registerBackend,
getBackend,
};


Expand Down Expand Up @@ -87,3 +90,23 @@ export function registerWidgetValueSerializer(widgetName, serializer) {
export function getWidgetValueSerializer(widgetName) {
return registry.widgetValueSerializers[widgetName];
};

/**
* Backend API
*/
export function registerBackend(name, BackendClass) {
if (!name || !BackendClass) {
console.error("Backend parameters invalid. example: CMS.registerBackend('myBackend', BackendClass)");
} else if (registry.backends[name]) {
console.error(`Backend [${ name }] already registered. Please choose a different name.`);
} else {
registry.backends[name] = {
init: config => new BackendClass(config),
};
}
}

export function getBackend(name) {
return registry.backends[name];
}

0 comments on commit 7053ccd

Please sign in to comment.