-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Resource Type to contentful-management [DANTE-1832] (#2429)
* feat: add Resource Type to contentful-management [DANTE-1832] * feat: add tests * feat: add getMany and orgId to ResourceType APIs * fix: unit test * fix: remove resource type id in test * feat: update comments, add more tests * fix: wait 1s after deletion * fix: remove type id from getMany * feat: clean up urls * fix: types * fix: remove empty headers --------- Co-authored-by: Marouen Ben Salem <[email protected]>
- Loading branch information
1 parent
0b42925
commit 8fa9399
Showing
16 changed files
with
916 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { RawAxiosRequestHeaders } from 'axios' | ||
import type { AxiosInstance } from 'contentful-sdk-core' | ||
import * as raw from './raw' | ||
import copy from 'fast-copy' | ||
import type { CollectionProp } from '../../../common-types' | ||
import { type GetResourceTypeParams } from '../../../common-types' | ||
import type { RestEndpoint } from '../types' | ||
import type { ResourceTypeProps, UpsertResourceTypeProps } from '../../../entities/resource-type' | ||
|
||
const getBaseUrl = ( | ||
params: GetResourceTypeParams | Omit<GetResourceTypeParams, 'resourceTypeId'> | ||
) => | ||
`/organizations/${params.organizationId}/app_definitions/${params.appDefinitionId}/resource_provider/resource_types` | ||
|
||
const getEntityUrl = (params: GetResourceTypeParams) => | ||
`${getBaseUrl(params)}/${params.resourceTypeId}` | ||
|
||
export const get: RestEndpoint<'ResourceType', 'get'> = ( | ||
http: AxiosInstance, | ||
params: GetResourceTypeParams | ||
) => { | ||
return raw.get<ResourceTypeProps>(http, getEntityUrl(params)) | ||
} | ||
|
||
export const upsert: RestEndpoint<'ResourceType', 'upsert'> = ( | ||
http: AxiosInstance, | ||
params: GetResourceTypeParams, | ||
rawData: UpsertResourceTypeProps, | ||
headers?: RawAxiosRequestHeaders | ||
) => { | ||
const data = copy(rawData) | ||
|
||
return raw.put<ResourceTypeProps>(http, getEntityUrl(params), data, { headers }) | ||
} | ||
|
||
export const del: RestEndpoint<'ResourceType', 'delete'> = ( | ||
http: AxiosInstance, | ||
params: GetResourceTypeParams | ||
) => { | ||
return raw.del(http, getEntityUrl(params)) | ||
} | ||
|
||
export const getMany: RestEndpoint<'ResourceType', 'getMany'> = ( | ||
http: AxiosInstance, | ||
params: Omit<GetResourceTypeParams, 'resourceTypeId'> | ||
) => { | ||
return raw.get<CollectionProp<ResourceTypeProps>>(http, getBaseUrl(params)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import type { | ||
BasicMetaSysProps, | ||
DefaultElements, | ||
GetResourceTypeParams, | ||
MakeRequest, | ||
SysLink, | ||
} from '../common-types' | ||
import { toPlainObject, freezeSys } from 'contentful-sdk-core' | ||
import copy from 'fast-copy' | ||
import enhanceWithMethods from '../enhance-with-methods' | ||
|
||
export type ResourceTypeProps = { | ||
/** | ||
* System metadata | ||
*/ | ||
sys: Omit<BasicMetaSysProps, 'version'> & { | ||
appDefinition: SysLink | ||
organization: SysLink | ||
resourceProvider: SysLink | ||
} | ||
/** | ||
* Resource Type name | ||
*/ | ||
name: string | ||
/** | ||
* Resource Type defaultFieldMapping | ||
*/ | ||
defaultFieldMapping: { | ||
title: string | ||
subtitle?: string | ||
description?: string | ||
externalUrl?: string | ||
image?: { | ||
url: string | ||
altText?: string | ||
} | ||
badge?: { | ||
label: string | ||
variant: string | ||
} | ||
} | ||
} | ||
|
||
export type UpsertResourceTypeProps = Omit<ResourceTypeProps, 'sys'> | ||
|
||
export interface ResourceType extends ResourceTypeProps, DefaultElements<ResourceTypeProps> { | ||
upsert(): Promise<ResourceType> | ||
delete(): Promise<void> | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
function createResourceTypeApi(makeRequest: MakeRequest) { | ||
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.getResourceType()) | ||
* .then((resourceType) => { | ||
* resourceType.name = '<new_name>' | ||
* return resourceType.upsert() | ||
* }) | ||
* .catch(console.error) | ||
* ``` | ||
*/ | ||
upsert: function upsert() { | ||
const data = this.toPlainObject() as ResourceTypeProps | ||
|
||
return makeRequest({ | ||
entityType: 'ResourceType', | ||
action: 'upsert', | ||
params: getParams(data), | ||
headers: {}, | ||
payload: getUpsertParams(data), | ||
}).then((data) => wrapResourceType(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.getResourceType()) | ||
* .then((resourceType) => resourceType.delete()) | ||
* .catch(console.error) | ||
* ``` | ||
*/ | ||
delete: function del() { | ||
const data = this.toPlainObject() as ResourceTypeProps | ||
|
||
return makeRequest({ | ||
entityType: 'ResourceType', | ||
action: 'delete', | ||
params: getParams(data), | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
const getParams = (data: ResourceTypeProps): GetResourceTypeParams => ({ | ||
organizationId: data.sys.organization.sys.id, | ||
appDefinitionId: data.sys.appDefinition.sys.id, | ||
resourceTypeId: data.sys.id, | ||
}) | ||
|
||
const getUpsertParams = (data: ResourceTypeProps): UpsertResourceTypeProps => ({ | ||
name: data.name, | ||
defaultFieldMapping: data.defaultFieldMapping, | ||
}) | ||
|
||
/** | ||
* @private | ||
* @param makeRequest - function to make requests via an adapter | ||
* @param data - Raw Resource Type data | ||
* @return Wrapped Resource Type data | ||
*/ | ||
export function wrapResourceType(makeRequest: MakeRequest, data: ResourceTypeProps): ResourceType { | ||
const resourceType = toPlainObject(copy(data)) | ||
const ResourceTypeWithMethods = enhanceWithMethods( | ||
resourceType, | ||
createResourceTypeApi(makeRequest) | ||
) | ||
return freezeSys(ResourceTypeWithMethods) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.