UNPKG

@contentacms/contentajs

Version:

A nodejs server that proxies to Contenta CMS and holds custom code.

82 lines (67 loc) 1.94 kB
"use strict"; const _ = require('lodash'); const got = require('./got'); /** * A class for a JSON RPC executor. */ class ContentaJsonRpc { /** * The location of the CMS with auto-dicovery. */ /** * The list of methods available in the Contenta CMS side. */ /** * Constructs a ContentaJsonRpc object. * * @param {string} cmsHost * The location of the CMS with auto-dicovery. */ constructor(cmsHost) { this.host = cmsHost; } /** * Reads from the discovery endpoint and attaches the methods to the object. * * @return {Promise<void>} * Resolves when initialized. */ init() { return got(`${this.host}/jsonrpc/methods`).then(res => { const body = _.get(res, 'body'); this.methods = body.data.map(method => method.id).filter(i => i); }).then(() => {}); } /** * Executes the provided JSON RPC request against Contenta CMS. * * @param {JsonRpcRequest} request * The request object according to the spec. * * @return {Promise<JsonRpcResponse>} * The response from the Contenta CMS JSON-RPC server. */ execute(request) { // TODO: We probably want to validate input here with a JSON Schema. if (!Array.isArray(request) && this.methods.indexOf(request.method) === -1) { return Promise.reject(new Error(JSON.stringify({ jsonrpc: '2.0', error: { code: -32601, message: 'Method not found' } }))); } return got(`${this.host}/jsonrpc`, { query: { query: JSON.stringify(request) } }) // Reject if the request errored and is not a batched request. .then(res => { const body = _.get(res, 'body'); const isError = body && typeof _.get(body, 'error') !== 'undefined'; return isError ? Promise.reject(new Error(JSON.stringify(body))) : Promise.resolve(body); }); } } module.exports = ContentaJsonRpc;