-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Resource Type to contentful-management [DANTE-1832] #2429
Merged
mayakarabula
merged 12 commits into
master
from
feat/DANTE-1832-add-resource-type-to-contentful-management
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3bebb69
feat: add Resource Type to contentful-management [DANTE-1832]
mayakarabula 5c61a58
feat: add tests
mayakarabula 375c31e
feat: add getMany and orgId to ResourceType APIs
mayakarabula e2377c7
fix: unit test
mayakarabula 70a8339
fix: remove resource type id in test
mayakarabula 4e3242c
Merge branch 'master' into feat/DANTE-1832-add-resource-type-to-conte…
Cyberxon 419ee47
feat: update comments, add more tests
mayakarabula 6ef6ab6
fix: wait 1s after deletion
mayakarabula 9955658
fix: remove type id from getMany
mayakarabula cd23b53
feat: clean up urls
mayakarabula 1b1315a
fix: types
mayakarabula 884edba
fix: remove empty headers
mayakarabula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
veu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a common pattern in this SDK to expose sub resources both in parent resource methods and standalone? Also, do we need docs for these methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a separate ticket for updating the docs after we finish this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the other question about the standalone vs parent resource in the legacy client, usually in normal cases the parent resource has create and get methods and the standalone resource (child resource) has the update and delete. Let me give you an example to make it clearer: if you want to create a new
appDefinition
, you do it through theorganization
resource, so you init the client,getOrganisation
, and then through the organisation entity you do thecreateAppDefition
so you end up with the AppDefintion entity that you can update or delete. You cannot add a get on that standalone entity because you can't call that method on the entity itself. however, in the plain client you can do it because basically you call everything related to the standalone entity on the standalone entity.