|
| 1 | +import type { BasicMetaSysProps, DefaultElements, MakeRequest, SysLink } from '../common-types' |
| 2 | +import { toPlainObject, freezeSys } from 'contentful-sdk-core' |
| 3 | +import copy from 'fast-copy' |
| 4 | +import enhanceWithMethods from '../enhance-with-methods' |
| 5 | + |
| 6 | +export type ResourceProviderProps = { |
| 7 | + /** |
| 8 | + * System metadata |
| 9 | + */ |
| 10 | + sys: Omit<BasicMetaSysProps, 'version'> & { |
| 11 | + organization: SysLink |
| 12 | + appDefinition: SysLink |
| 13 | + } |
| 14 | + /** |
| 15 | + * Resource Provider type, value is 'function' |
| 16 | + */ |
| 17 | + type: 'function' |
| 18 | + /** |
| 19 | + * Link to a Contentful function |
| 20 | + */ |
| 21 | + function: SysLink |
| 22 | +} |
| 23 | + |
| 24 | +export type UpsertResourceProviderProps = Omit<ResourceProviderProps, 'sys'> & { |
| 25 | + sys: { id: string } |
| 26 | +} |
| 27 | + |
| 28 | +export interface ResourceProvider |
| 29 | + extends ResourceProviderProps, |
| 30 | + DefaultElements<ResourceProviderProps> { |
| 31 | + upsert(): Promise<ResourceProvider> |
| 32 | + delete(): Promise<void> |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @private |
| 37 | + */ |
| 38 | +function createResourceProviderApi(makeRequest: MakeRequest) { |
| 39 | + return { |
| 40 | + /** |
| 41 | + * Sends an update to the server with any changes made to the object's properties |
| 42 | + * @return Object returned from the server with updated changes. |
| 43 | + * @example ```javascript |
| 44 | + * const contentful = require('contentful-management') |
| 45 | + * |
| 46 | + * const client = contentful.createClient({ |
| 47 | + * accessToken: '<content_management_api_key>' |
| 48 | + * }) |
| 49 | + * |
| 50 | + * client.getOrganization('<org_id>') |
| 51 | + * .then((org) => org.getAppDefinition('<app_def_id>')) |
| 52 | + * .then((appDefinition) => appDefinition.getResourceProvider()) |
| 53 | + * .then((resourceProvider) => { |
| 54 | + * resourceProvider.function.sys.id = '<new_contentful_function_id>' |
| 55 | + * return resourceProvider.upsert() |
| 56 | + * }) |
| 57 | + * .catch(console.error) |
| 58 | + * ``` |
| 59 | + */ |
| 60 | + upsert: function upsert() { |
| 61 | + const data = this.toPlainObject() as ResourceProviderProps |
| 62 | + return makeRequest({ |
| 63 | + entityType: 'ResourceProvider', |
| 64 | + action: 'upsert', |
| 65 | + params: getParams(data), |
| 66 | + headers: {}, |
| 67 | + payload: getUpsertParams(data), |
| 68 | + }).then((data) => wrapResourceProvider(makeRequest, data)) |
| 69 | + }, |
| 70 | + /** |
| 71 | + * Deletes this object on the server. |
| 72 | + * @return Promise for the deletion. It contains no data, but the Promise error case should be handled. |
| 73 | + * @example ```javascript |
| 74 | + * const contentful = require('contentful-management') |
| 75 | + * |
| 76 | + * const client = contentful.createClient({ |
| 77 | + * accessToken: '<content_management_api_key>' |
| 78 | + * }) |
| 79 | + * |
| 80 | + * client.getOrganization('<org_id>') |
| 81 | + * .then((org) => org.getAppDefinition('<app_def_id>')) |
| 82 | + * .then((appDefinition) => appDefinition.getResourceProvider()) |
| 83 | + * .then((resourceProvider) => resourceProvider.delete()) |
| 84 | + * .catch(console.error) |
| 85 | + * ``` |
| 86 | + */ |
| 87 | + delete: function del() { |
| 88 | + const data = this.toPlainObject() as ResourceProviderProps |
| 89 | + return makeRequest({ |
| 90 | + entityType: 'ResourceProvider', |
| 91 | + action: 'delete', |
| 92 | + params: getParams(data), |
| 93 | + }) |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | +/** |
| 98 | + * @private |
| 99 | + * @param data - raw ResourceProvider Object |
| 100 | + * @return Object containing the http params for the ResourceProvider request: organizationId and appDefinitionId |
| 101 | + */ |
| 102 | +const getParams = (data: ResourceProviderProps) => ({ |
| 103 | + organizationId: data.sys.organization.sys.id, |
| 104 | + appDefinitionId: data.sys.appDefinition.sys.id, |
| 105 | +}) |
| 106 | +/** |
| 107 | + * @private |
| 108 | + * @param data - raw ResourceProvider Object |
| 109 | + * @return UpsertResourceProviderProps |
| 110 | + */ |
| 111 | +const getUpsertParams = (data: ResourceProviderProps): UpsertResourceProviderProps => ({ |
| 112 | + sys: { id: data.sys.id }, |
| 113 | + type: data.type, |
| 114 | + function: data.function, |
| 115 | +}) |
| 116 | + |
| 117 | +/** |
| 118 | + * @private |
| 119 | + * @param makeRequest - function to make requests via an adapter |
| 120 | + * @param data - Raw Resource Provider data |
| 121 | + * @return Wrapped Resource Provider data |
| 122 | + */ |
| 123 | +export function wrapResourceProvider( |
| 124 | + makeRequest: MakeRequest, |
| 125 | + data: ResourceProviderProps |
| 126 | +): ResourceProvider { |
| 127 | + const resourceProvider = toPlainObject(copy(data)) |
| 128 | + const ResourceProviderWithMethods = enhanceWithMethods( |
| 129 | + resourceProvider, |
| 130 | + createResourceProviderApi(makeRequest) |
| 131 | + ) |
| 132 | + return freezeSys(ResourceProviderWithMethods) |
| 133 | +} |
0 commit comments