Skip to content

Commit

Permalink
feat: add UIConfig and UserUIConfig [EXT-3738] (#1415)
Browse files Browse the repository at this point in the history
* feat: add UIConfig and UserUIConfig

* feat: export & move new types

* fix: remove `isFolder` & `folderId`

* fix: remove roles prop from UserUIConfig

* test: add unit tests

* test: unit test REST endpoints

* docs: update js docs

* fix: add page field

* feat: remove page prop

Co-authored-by: Kado <kadodamball@hotmail.com>
  • Loading branch information
andipaetzold and kdamball authored Aug 15, 2022
1 parent 166dd3d commit 0fb8626
Show file tree
Hide file tree
Showing 21 changed files with 675 additions and 65 deletions.
10 changes: 7 additions & 3 deletions lib/adapters/REST/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as AppAction from './app-action'
import * as AppActionCall from './app-action-call'
import * as AppBundle from './app-bundle'
import * as AppDefinition from './app-definition'
import * as AppInstallation from './app-installation'
import * as AppUpload from './app-upload'
import * as AppDetails from './app-details'
import * as AppInstallation from './app-installation'
import * as AppSignedRequest from './app-signed-request'
import * as AppSigningSecret from './app-signing-secret'
import * as AppUpload from './app-upload'
import * as Asset from './asset'
import * as AssetKey from './asset-key'
import * as BulkAction from './bulk-action'
Expand Down Expand Up @@ -38,12 +38,14 @@ import * as Task from './task'
import * as Team from './team'
import * as TeamMembership from './team-membership'
import * as TeamSpaceMembership from './team-space-membership'
import * as UIConfig from './ui-config'
import * as Upload from './upload'
import * as Usage from './usage'
import * as User from './user'
import * as UserUIConfig from './user-ui-config'
import * as Webhook from './webhook'
import * as WorkflowDefinition from './workflow-definition'
import * as Workflow from './workflow'
import * as WorkflowDefinition from './workflow-definition'
import * as WorkflowsChangelog from './workflows-changelog'

export default {
Expand Down Expand Up @@ -87,9 +89,11 @@ export default {
Team,
TeamMembership,
TeamSpaceMembership,
UIConfig,
Upload,
Usage,
User,
UserUIConfig,
Webhook,
WorkflowDefinition,
Workflow,
Expand Down
31 changes: 31 additions & 0 deletions lib/adapters/REST/endpoints/ui-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { AxiosInstance } from 'contentful-sdk-core'
import { SetOptional } from 'type-fest'
import { GetUIConfigParams } from '../../../common-types'
import { UIConfigProps } from '../../../entities/ui-config'
import { RestEndpoint } from '../types'
import * as raw from './raw'
import copy from 'fast-copy'

const getUrl = (params: GetUIConfigParams) =>
`/spaces/${params.spaceId}/environments/${params.environmentId}/ui_config`

export const get: RestEndpoint<'UIConfig', 'get'> = (
http: AxiosInstance,
params: GetUIConfigParams
) => {
return raw.get<UIConfigProps>(http, getUrl(params))
}

export const update: RestEndpoint<'UIConfig', 'update'> = (
http: AxiosInstance,
params: GetUIConfigParams,
rawData: UIConfigProps
) => {
const data: SetOptional<typeof rawData, 'sys'> = copy(rawData)
delete data.sys
return raw.put<UIConfigProps>(http, getUrl(params), data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
},
})
}
31 changes: 31 additions & 0 deletions lib/adapters/REST/endpoints/user-ui-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { AxiosInstance } from 'contentful-sdk-core'
import copy from 'fast-copy'
import { SetOptional } from 'type-fest'
import { GetUserUIConfigParams } from '../../../common-types'
import { UserUIConfigProps } from '../../../entities/user-ui-config'
import { RestEndpoint } from '../types'
import * as raw from './raw'

const getUrl = (params: GetUserUIConfigParams) =>
`/spaces/${params.spaceId}/environments/${params.environmentId}/ui_config/me`

export const get: RestEndpoint<'UserUIConfig', 'get'> = (
http: AxiosInstance,
params: GetUserUIConfigParams
) => {
return raw.get<UserUIConfigProps>(http, getUrl(params))
}

export const update: RestEndpoint<'UserUIConfig', 'update'> = (
http: AxiosInstance,
params: GetUserUIConfigParams,
rawData: UserUIConfigProps
) => {
const data: SetOptional<typeof rawData, 'sys'> = copy(rawData)
delete data.sys
return raw.put<UserUIConfigProps>(http, getUrl(params), data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
},
})
}
18 changes: 18 additions & 0 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ import {
WorkflowsChangelogEntryProps,
WorkflowsChangelogQueryOptions,
} from './entities/workflows-changelog-entry'
import { UIConfigProps } from './entities/ui-config'
import { UserUIConfigProps } from './entities/user-ui-config'

export interface DefaultElements<TPlainObject extends object = object> {
toPlainObject(): TPlainObject
Expand Down Expand Up @@ -534,6 +536,9 @@ type MRInternal<UA extends boolean> = {
(opts: MROpts<'TeamSpaceMembership', 'update', UA>): MRReturn<'TeamSpaceMembership', 'update'>
(opts: MROpts<'TeamSpaceMembership', 'delete', UA>): MRReturn<'TeamSpaceMembership', 'delete'>

(opts: MROpts<'UIConfig', 'get', UA>): MRReturn<'UIConfig', 'get'>
(opts: MROpts<'UIConfig', 'update', UA>): MRReturn<'UIConfig', 'update'>

(opts: MROpts<'Upload', 'get', UA>): MRReturn<'Entry', 'get'>
(opts: MROpts<'Upload', 'create', UA>): MRReturn<'Entry', 'create'>
(opts: MROpts<'Upload', 'delete', UA>): MRReturn<'Entry', 'delete'>
Expand All @@ -547,6 +552,9 @@ type MRInternal<UA extends boolean> = {
(opts: MROpts<'User', 'getForOrganization', UA>): MRReturn<'User', 'getForOrganization'>
(opts: MROpts<'User', 'getManyForOrganization', UA>): MRReturn<'User', 'getManyForOrganization'>

(opts: MROpts<'UserUIConfig', 'get', UA>): MRReturn<'UserUIConfig', 'update'>
(opts: MROpts<'UserUIConfig', 'update', UA>): MRReturn<'UserUIConfig', 'update'>

(opts: MROpts<'Webhook', 'get', UA>): MRReturn<'Webhook', 'get'>
(opts: MROpts<'Webhook', 'getMany', UA>): MRReturn<'Webhook', 'getMany'>
(opts: MROpts<'Webhook', 'getCallDetails', UA>): MRReturn<'Webhook', 'getCallDetails'>
Expand Down Expand Up @@ -1355,6 +1363,10 @@ export type MRActions = {
}
delete: { params: GetTeamSpaceMembershipParams; return: any }
}
UIConfig: {
get: { params: GetUIConfigParams; return: UIConfigProps }
update: { params: GetUIConfigParams; payload: UIConfigProps; return: UIConfigProps }
}
Upload: {
get: { params: GetSpaceParams & { uploadId: string }; return: any }
create: {
Expand Down Expand Up @@ -1384,6 +1396,10 @@ export type MRActions = {
return: CollectionProp<UserProps>
}
}
UserUIConfig: {
get: { params: GetUserUIConfigParams; return: UserUIConfigProps }
update: { params: GetUserUIConfigParams; payload: UserUIConfigProps; return: UserUIConfigProps }
}
Webhook: {
get: { params: GetWebhookParams; return: WebhookProps }
getMany: { params: GetSpaceParams & QueryParams; return: CollectionProp<WebhookProps> }
Expand Down Expand Up @@ -1561,6 +1577,8 @@ export type GetWorkflowDefinitionParams = GetSpaceEnvironmentParams & {
export type GetWorkflowParams = GetSpaceEnvironmentParams & {
workflowId: string
}
export type GetUIConfigParams = GetSpaceEnvironmentParams
export type GetUserUIConfigParams = GetUIConfigParams

export type QueryParams = { query?: QueryOptions }
export type PaginationQueryParams = { query?: PaginationQueryOptions }
30 changes: 30 additions & 0 deletions lib/create-environment-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { EnvironmentProps } from './entities/environment'
import type { CreateExtensionProps } from './entities/extension'
import type { CreateLocaleProps } from './entities/locale'
import { TagVisibility, wrapTag, wrapTagCollection } from './entities/tag'
import { wrapUIConfig } from './entities/ui-config'
import { wrapUserUIConfig } from './entities/user-ui-config'

/**
* @private
Expand Down Expand Up @@ -2104,5 +2106,33 @@ export default function createEnvironmentApi(makeRequest: MakeRequest) {
},
}).then((data) => wrapReleaseActionCollection(makeRequest, data))
},

async getUIConfig() {
const raw: EnvironmentProps = this.toPlainObject()

const data = await makeRequest({
entityType: 'UIConfig',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
})
return wrapUIConfig(makeRequest, data)
},

async getUserUIConfig() {
const raw: EnvironmentProps = this.toPlainObject()

const data = await makeRequest({
entityType: 'UserUIConfig',
action: 'get',
params: {
spaceId: raw.sys.space.sys.id,
environmentId: raw.sys.id,
},
})
return wrapUserUIConfig(makeRequest, data)
},
}
}
62 changes: 62 additions & 0 deletions lib/create-ui-config-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { MakeRequest } from './common-types'
import entities from './entities'
import { UIConfig } from './entities/ui-config'

/**
* @private
*/
export type ContentfulUIConfigApi = ReturnType<typeof createUIConfigApi>

/**
* @private
*/
export default function createUIConfigApi(makeRequest: MakeRequest) {
const { wrapUIConfig } = entities.uiConfig

const getParams = (self: UIConfig) => {
const uiConfig = self.toPlainObject()

return {
params: {
spaceId: uiConfig.sys.space.sys.id,
environmentId: uiConfig.sys.environment.sys.id,
},
raw: uiConfig,
}
}

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.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getUIConfig())
* .then((uiConfig) => {
* uiConfig.entryListViews = [...]
* return uiConfig.update()
* })
* .then((uiConfig) => console.log(`UIConfig updated.`))
* .catch(console.error)
* ```
*/
update: async function update() {
const { raw, params } = getParams(this)

const data = await makeRequest({
entityType: 'UIConfig',
action: 'update',
params,
payload: raw,
})
return wrapUIConfig(makeRequest, data)
},
}
}
62 changes: 62 additions & 0 deletions lib/create-user-ui-config-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { MakeRequest } from './common-types'
import entities from './entities'
import { UserUIConfig } from './entities/user-ui-config'

/**
* @private
*/
export type ContentfulUIConfigApi = ReturnType<typeof createUserUIConfigApi>

/**
* @private
*/
export default function createUserUIConfigApi(makeRequest: MakeRequest) {
const { wrapUserUIConfig } = entities.userUIConfig

const getParams = (self: UserUIConfig) => {
const userUIConfig = self.toPlainObject()

return {
params: {
spaceId: userUIConfig.sys.space.sys.id,
environmentId: userUIConfig.sys.environment.sys.id,
},
raw: userUIConfig,
}
}

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.getSpace('<space_id>')
* .then((space) => space.getEnvironment('<environment_id>'))
* .then((environment) => environment.getUserUIConfig())
* .then((uiConfig) => {
* uiConfig.entryListViews = [...]
* return uiConfig.update()
* })
* .then((uiConfig) => console.log(`UserUIConfig updated.`))
* .catch(console.error)
* ```
*/
update: async function update() {
const { raw, params } = getParams(this)

const data = await makeRequest({
entityType: 'UserUIConfig',
action: 'update',
params,
payload: raw,
})
return wrapUserUIConfig(makeRequest, data)
},
}
}
12 changes: 8 additions & 4 deletions lib/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as appAction from './app-action'
import * as appActionCall from './app-action-call'
import * as appBundle from './app-bundle'
import * as appDefinition from './app-definition'
import * as appInstallation from './app-installation'
import * as appUpload from './app-upload'
import * as appDetails from './app-details'
import * as appInstallation from './app-installation'
import * as appSignedRequest from './app-signed-request'
import * as appSigningSecret from './app-signing-secret'
import * as appUpload from './app-upload'
import * as asset from './asset'
import * as assetKey from './asset-key'
import * as bulkAction from './bulk-action'
Expand All @@ -32,14 +32,16 @@ import * as snapshot from './snapshot'
import * as space from './space'
import * as spaceMember from './space-member'
import * as spaceMembership from './space-membership'
import * as team from './team'
import * as teamMembership from './team-membership'
import * as tag from './tag'
import * as task from './task'
import * as team from './team'
import * as teamMembership from './team-membership'
import * as teamSpaceMembership from './team-space-membership'
import * as uiConfig from './ui-config'
import * as upload from './upload'
import * as usage from './usage'
import * as user from './user'
import * as userUIConfig from './user-ui-config'
import * as webhook from './webhook'
import * as workflowDefinition from './workflow-definition'

Expand Down Expand Up @@ -83,9 +85,11 @@ export default {
team,
teamMembership,
teamSpaceMembership,
uiConfig,
upload,
usage,
user,
userUIConfig,
webhook,
workflowDefinition,
}
Loading

0 comments on commit 0fb8626

Please sign in to comment.