UNPKG

contentful-management

Version:
139 lines (138 loc) 4.5 kB
import { toPlainObject, freezeSys } from 'contentful-sdk-core'; import copy from 'fast-copy'; import enhanceWithMethods from '../enhance-with-methods'; import entities from '.'; /** * @private */ function createResourceProviderApi(makeRequest) { const { wrapResourceType } = entities.resourceType; return { /** * Sends an update to the server with any changes made to the object's properties * @return Object returned from the server with updated changes. * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getOrganization('<org_id>') * .then((org) => org.getAppDefinition('<app_def_id>')) * .then((appDefinition) => appDefinition.getResourceProvider()) * .then((resourceProvider) => { * resourceProvider.function.sys.id = '<new_contentful_function_id>' * return resourceProvider.upsert() * }) * .catch(console.error) * ``` */ upsert: function upsert() { const data = this.toPlainObject(); return makeRequest({ entityType: 'ResourceProvider', action: 'upsert', params: getParams(data), headers: {}, payload: getUpsertParams(data) }).then(data => wrapResourceProvider(makeRequest, data)); }, /** * Deletes this object on the server. * @return Promise for the deletion. It contains no data, but the Promise error case should be handled. * @example ```javascript * const contentful = require('contentful-management') * * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * client.getOrganization('<org_id>') * .then((org) => org.getAppDefinition('<app_def_id>')) * .then((appDefinition) => appDefinition.getResourceProvider()) * .then((resourceProvider) => resourceProvider.delete()) * .catch(console.error) * ``` */ delete: function del() { const data = this.toPlainObject(); return makeRequest({ entityType: 'ResourceProvider', action: 'delete', params: getParams(data) }); }, getResourceType: function getResourceType(id) { return makeRequest({ entityType: 'ResourceType', action: 'get', params: { organizationId: this.sys.organization.sys.id, appDefinitionId: this.sys.appDefinition.sys.id, resourceTypeId: id } }).then(data => wrapResourceType(makeRequest, data)); }, upsertResourceType: function upsertResourceType(id, data) { return makeRequest({ entityType: 'ResourceType', action: 'upsert', params: { organizationId: this.sys.organization.sys.id, appDefinitionId: this.sys.appDefinition.sys.id, resourceTypeId: id }, headers: {}, payload: data }).then(data => wrapResourceType(makeRequest, data)); }, getResourceTypes: function getResourceTypes() { return makeRequest({ entityType: 'ResourceType', action: 'getMany', params: { organizationId: this.sys.organization.sys.id, appDefinitionId: this.sys.appDefinition.sys.id } }).then(data => { data.items = data.items.map(item => wrapResourceType(makeRequest, item)); return data; }); } }; } /** * @private * @param data - raw ResourceProvider Object * @return Object containing the http params for the ResourceProvider request: organizationId and appDefinitionId */ const getParams = data => ({ organizationId: data.sys.organization.sys.id, appDefinitionId: data.sys.appDefinition.sys.id }); /** * @private * @param data - raw ResourceProvider Object * @return UpsertResourceProviderProps */ const getUpsertParams = data => ({ sys: { id: data.sys.id }, type: data.type, function: data.function }); /** * @private * @param makeRequest - function to make requests via an adapter * @param data - Raw Resource Provider data * @return Wrapped Resource Provider data */ export function wrapResourceProvider(makeRequest, data) { const resourceProvider = toPlainObject(copy(data)); const ResourceProviderWithMethods = enhanceWithMethods(resourceProvider, createResourceProviderApi(makeRequest)); return freezeSys(ResourceProviderWithMethods); }