title | weight | group |
---|---|---|
Creating a CMS Backend |
30 |
reference |
Netlify CMS exposes a window.CMS
global object that you can use to register custom backends. The same object is also the default export if you import Netify CMS as an npm module. The available backend extension methods are:
- registerBackend: lets you register a custom backend. The CMS expects you to pass it an ES6
class
or other object which it can callnew BackendClass()
on.
CMS.registerBackend('my-backend', MyBackendClass);
Following are the methods the CMS expects to see on your backend class. Currently they are very Git-centric and assume the backend is aware of the general flow of CMS internals, but this will be changing as a new API is rolled out.
Set up backend class based on user configuration.
constructor(config, { useWorkflow, updateUserCredentials, initialWorkflowStatus })
config
(Map
): entire CMS config fileuseWorkflow
(boolean): if editorial workflow is enabledupdateUserCredentials
(function): updates cached credentials (if user data or token is updated)initialWorkflowStatus
(constant): editorial workflow status for new posts
Return a login screen for the user to authenticate with the backend.
authComponent()
- returns a React Component: a login screen
Authenticate the user with credentials from the login screen. If it is successful, the returned user data will be cached locally until the user logs outof the CMS. Closing the browser does not implicitly log the user out. If you do not want the CMS to cache user data for your backend, you can choose not to return credentials along with the user metadata.
authenticate(credentials)
credentials
: credentials returned from login screen (seeauthComponent
)- returns a Promise:
- if the user's credentials are valid and the user has write access to the backend the CMS is configured to access, a resolved Promise containing user metadata (e.g. name, avatar) and credentials (that were passed in)
- otherwise a rejected Promise containing the reason for rejection
Authenticate the user with cached user data (see authenticate
).
restoreUser(user)
user
: user data returned fromauthenticate
- returns a Promise: (same return expected as for
authenticate
)
Forget user credentials. The CMS handles the cache seperately.
logout()
Returns user credentials. This token will be passed to integrations?
getToken()
- returns a Promise: resolving with user credentials
Returns contents of entries in a folder-type collection which have a specific file extension.
entriesByFolder(collection, extension)
collection
(Map
): collection configuration from config fileextension
(string): files with other extensions should be filtered out- returns a Promise: resolving with an array of files (like Promise.all). If the array is paginated and does not contain every file, the array should contain an cursor (see
traverseCursor
). Files should be objects with the following keys:file
(object): file metadata (at leastpath
)data
: (string): raw file content parsed as a Unicode string
Returns contents of all entries in a file-type collection.
entriesByFiles(collection)
collection
(Map
): (seeentriesByFolder
)- returns a Promise: (see
entriesByFolder
)
Get the contents of a single entry.
getEntry(collection, slug, path)
collection
(Map
): collection configuration from config fileslug
(string): entry slug (filename without extension)path
(string): file path (see returns forentriesByFolder
)- returns a Promise: resolving to an object in a
{ file, data }
format wherefile
consists of file metadata (at least the file path), anddata
is the raw file content parsed as a Unicode string.
Return the contents of the media_folder
from the config.
getMedia()
- returns a Promise: resolving with an array of media (like Promise.all). Media should be an object with the following keys:
id
(string): A unique key for the media file (a SHA is often appropriate)name
(string): media file namepath
(string): media file path- either
url
orgetBlobPromise
:url
(string): URL to a thumbnail of the media, if availablegetBlobPromise
(function): returns a Promise with resolves to a thumbnail of the, if available, in aBlob
format
Save a copy of the entry.
persistEntry(entry, mediaFiles, { newEntry, parsedData, collectionName, useWorkflow, commitMessage, hasAssetStore })
entry
(object):raw
(string): entry contentpath
(string): file path to entry
- deprecated in v0.7.0
mediaFiles
(array): a list of media files uploaded to this entry newEntry
(boolean): this is a newly created entryparsedData
(object):title
anddescription
parsed from raw entry content, if possiblecollectionName
(string):name
of collection from configuseWorkflow
(boolean): if Editorial Workflow is enabled, save draft -- do not publish directlycommitMessage
(string): Git commit message, if publishing entry directly.hasAssetStore
(boolean): if assets are being stored in a separate integration (instead of in the backend)- returns a Promise which resolves if persisting was successful
Save a copy of a media file.
persistMedia(mediaFile, { commitMessage })
mediaFile
(AssetProxy)commitMessage
(string): Git commit message, if publishing entry directly.- returns a Promise which resolves if persisting was successful
Delete an entry or media file.
deleteFile(path, commitMessage)
path
(string): file pathcommitMessage
(string): Git commit message.- returns a Promise which resolves if deletion was successful
Returns content of unpublished entries.
unpublishedEntries()
- returns a Promise: resolving with an array of files (like Promise.all). Files should be objects with the following keys:
slug
(string): entry slug (filename without extension)file
(object): file metadata (at leastpath
)metaData
(object): Editoral Workflow metadata (e.g. pull request)data
(string): raw file content parsed as a Unicode stringisModification
(boolean): if a published version of this entry exists
Returns content of an unpublished entry.
unpublishedEntries(collection, slug)
collection
(Map
): collection configuration from config fileslug
(string): entry slug (filename without extension)- returns a Promise: resolving with an array of files (like Promise.all). Files should be objects with the following keys:
slug
(string): entry slug (filename without extension)file
(object): file metadata (at leastpath
)metaData
(object): Editoral Workflow metadata (e.g. pull request)data
(string): raw file content parsed as a Unicode stringisModification
(boolean): if a published version of this entry exists
Update status marker on unpublished entry.
updateUnpublishedEntryStatus(collection, slug, newStatus)
collection
(Map
): collection configuration from config fileslug
(string): entry slug (filename without extension)newStatus
(constant): status to apply to entry metadata- returns a Promise: which resolves if status is updated successfully
Publish an Editorial Workflow draft entry.
publishUnpublishedEntry(collection, slug)
collection
(Map
): collection configuration from config fileslug
(string): entry slug (filename without extension)- returns a Promise: which resolves if entry is published successfully
Delete an Editorial Workflow draft entry. This does not affect any content which is already published.
deleteUnpublishedEntry(collection, slug)
collection
(Map
): collection configuration from config fileslug
(string): entry slug (filename without extension)- returns a Promise: which resolves if entry is deleted successfully