UNPKG

contentful-management

Version:
253 lines (250 loc) 10.1 kB
import { wrapAppBundle, wrapAppBundleCollection } from './entities/app-bundle.js'; import { wrapResourceProvider } from './entities/resource-provider.js'; import { wrapAppDefinition } from './entities/app-definition.js'; /** * @internal */ function createAppDefinitionApi(makeRequest) { const getParams = (data) => ({ appDefinitionId: data.sys.id, organizationId: data.sys.organization.sys.id, }); return { /** * Sends an update to the server with any changes made to the object's properties * @returns 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.name = 'New App Definition name' * return appDefinition.update() * }) * .then((appDefinition) => console.log(`App Definition ${appDefinition.sys.id} updated.`)) * .catch(console.error) * ``` */ update: function update() { const data = this.toPlainObject(); return makeRequest({ entityType: 'AppDefinition', action: 'update', params: getParams(data), headers: {}, payload: data, }).then((data) => wrapAppDefinition(makeRequest, data)); }, /** * Deletes this object on the server. * @returns 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.delete()) * .then(() => console.log(`App Definition deleted.`)) * .catch(console.error) * ``` */ delete: function del() { const data = this.toPlainObject(); return makeRequest({ entityType: 'AppDefinition', action: 'delete', params: getParams(data), }); }, /** * Gets an app bundle * @param id - AppBundle ID * @returns Promise for an AppBundle * @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.getAppBundle('<app_upload_id>')) * .then((appBundle) => console.log(appBundle)) * .catch(console.error) * ``` */ getAppBundle(id) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'AppBundle', action: 'get', params: { appBundleId: id, appDefinitionId: raw.sys.id, organizationId: raw.sys.organization.sys.id, }, }).then((data) => wrapAppBundle(makeRequest, data)); }, /** * Gets a collection of AppBundles * @returns Promise for a collection of AppBundles * @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.getAppBundles()) * .then((response) => console.log(response.items)) * .catch(console.error) * ``` */ getAppBundles(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'AppBundle', action: 'getMany', params: { organizationId: raw.sys.organization.sys.id, appDefinitionId: raw.sys.id, query }, }).then((data) => wrapAppBundleCollection(makeRequest, data)); }, /** * Creates an app bundle * @param Object representation of the App Bundle to be created * @returns Promise for the newly created AppBundle * @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.createAppBundle('<app_upload_id>')) * .then((appBundle) => console.log(appBundle)) * .catch(console.error) * ``` */ createAppBundle(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'AppBundle', action: 'create', params: { appDefinitionId: raw.sys.id, organizationId: raw.sys.organization.sys.id, }, payload: data, }).then((data) => wrapAppBundle(makeRequest, data)); }, /** * Gets a list of App Installations across an org for given organization and App Definition * If a spaceId is provided in the query object, it will return the App Installations for that specific space. * @returns Promise for the newly created AppBundle * @example ```javascript * const contentful = require('contentful-management') * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * client.getAppDefinition('<organization_id>', '<app_definition_id>') * .then((appDefinition) => appDefinition.getInstallationsForOrg( * { spaceId: '<space_id>' } // optional * )) * .then((appInstallationsForOrg) => console.log(appInstallationsForOrg.items)) * .catch(console.error) * ``` */ getInstallationsForOrg(query = {}) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'AppDefinition', action: 'getInstallationsForOrg', params: { appDefinitionId: raw.sys.id, organizationId: raw.sys.organization.sys.id, query, }, }); }, /** * Creates or updates a resource provider * @param data representation of the ResourceProvider * @returns Promise for the newly created or updated ResourceProvider * @example ```javascript * const contentful = require('contentful-management') * const client = contentful.createClient({ * accessToken: '<content_management_api_key>' * }) * * // You need a valid AppDefinition with an activated AppBundle that has a contentful function configured * client.getOrganization('<org_id>') * .then((org) => org.getAppDefinition('<app_def_id>')) * .then((appDefinition) => appDefinition.upsertResourceProvider({ * sys: { * id: '<resource_provider_id>' * }, * type: 'function', * function: { * sys: { * id: '<contentful_function_id>', * type: 'Link' * linkType: 'Function' * } * } * })) * .then((resourceProvider) => console.log(resourceProvider)) * .catch(console.error) * ``` */ upsertResourceProvider(data) { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ResourceProvider', action: 'upsert', params: { appDefinitionId: raw.sys.id, organizationId: raw.sys.organization.sys.id, }, payload: data, }).then((payload) => wrapResourceProvider(makeRequest, payload)); }, /** * Gets a Resource Provider * @returns Promise for a Resource Provider * @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) => console.log(resourceProvider)) * .catch(console.error) * ``` */ getResourceProvider() { const raw = this.toPlainObject(); return makeRequest({ entityType: 'ResourceProvider', action: 'get', params: { appDefinitionId: raw.sys.id, organizationId: raw.sys.organization.sys.id, }, }).then((payload) => wrapResourceProvider(makeRequest, payload)); }, }; } export { createAppDefinitionApi as default }; //# sourceMappingURL=create-app-definition-api.js.map