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