Skip to content

Commit

Permalink
feat: enhance comment endpoint to support workflows as parent entity …
Browse files Browse the repository at this point in the history
…[HOMER-1455] (#1544)

* feat: enhance comment entity to support workflows as parent entity

feat: enhance comment entity to support workflows as parent entity

* feat: export new TS definitions and use new interface already in entry api

* fix: use new types in plain comment types as well

* fix: readd space and env id for integration test

* fix: remove test isolation and fix lint warning regarding ts-ignore

* feat: make parent version optional for fetching workflow comments

* fix: use versions instead of version

Co-authored-by: Felix Boenke <FLoppix@users.noreply.github.com>
Co-authored-by: Felix Boenke <felixboenke@googlemail.com>
  • Loading branch information
3 people authored Dec 7, 2022
1 parent 9f7488c commit bc0c6ae
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 28 deletions.
52 changes: 38 additions & 14 deletions lib/adapters/REST/endpoints/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CollectionProp,
GetEntryParams,
GetCommentParams,
GetSpaceEnvironmentParams,
QueryParams,
} from '../../../common-types'
import {
Expand All @@ -13,40 +14,57 @@ import {
DeleteCommentParams,
CommentProps,
UpdateCommentProps,
GetManyCommentsParams,
} from '../../../entities/comment'
import { RestEndpoint } from '../types'
import * as raw from './raw'
import { normalizeSelect } from './utils'

const getBaseUrl = (params: GetEntryParams) =>
`/spaces/${params.spaceId}/environments/${params.environmentId}/entries/${params.entryId}/comments`
const getCommentUrl = (params: GetCommentParams) => `${getBaseUrl(params)}/${params.commentId}`
const getSpaceEnvBaseUrl = (params: GetSpaceEnvironmentParams) =>
`/spaces/${params.spaceId}/environments/${params.environmentId}`
const getEntryBaseUrl = (params: GetEntryParams) =>
`${getSpaceEnvBaseUrl(params)}/entries/${params.entryId}/comments`
const getEntryCommentUrl = (params: GetCommentParams) =>
`${getEntryBaseUrl(params)}/${params.commentId}`

/**
* Comments can be added to either an entry or a workflow. The latter one requires a version
* to be set as part of the URL path. Workflow comments only support `create` (with
* versionized URL) and `getMany` (without version). The API might support more methods
* in the future with new use cases being discovered.
*/
const getEntityBaseUrl = (params: GetEntryParams | GetManyCommentsParams) => {
if ('entryId' in params) {
return getEntryBaseUrl(params)
}
const { parentEntityId, parentEntityType } = params
// No need for mapping or switch-case as long as there are only two supported cases
const parentPlural = parentEntityType === 'Workflow' ? 'workflows' : 'entries'
const versionPath =
'parentEntityVersion' in params ? `/versions/${params.parentEntityVersion}` : ''
return `${getSpaceEnvBaseUrl(params)}/${parentPlural}/${parentEntityId}${versionPath}/comments`
}

export const get: RestEndpoint<'Comment', 'get'> = (
http: AxiosInstance,
params: GetCommentParams
) => raw.get<CommentProps>(http, getCommentUrl(params))
) => raw.get<CommentProps>(http, getEntryCommentUrl(params))

export const getMany: RestEndpoint<'Comment', 'getMany'> = (
http: AxiosInstance,
params: GetEntryParams & QueryParams
params: GetManyCommentsParams & QueryParams
) =>
raw.get<CollectionProp<CommentProps>>(http, getBaseUrl(params), {
raw.get<CollectionProp<CommentProps>>(http, getEntityBaseUrl(params), {
params: normalizeSelect(params.query),
})

/**
* @deprecated use `getMany` instead. `getAll` may never be removed for app compatibility reasons.
*/
export const getAll: RestEndpoint<'Comment', 'getAll'> = getMany

export const create: RestEndpoint<'Comment', 'create'> = (
http: AxiosInstance,
params: CreateCommentParams,
rawData: CreateCommentProps
) => {
const data = copy(rawData)
return raw.post<CommentProps>(http, getBaseUrl(params), data)
return raw.post<CommentProps>(http, getEntityBaseUrl(params), data)
}

export const update: RestEndpoint<'Comment', 'update'> = (
Expand All @@ -58,7 +76,7 @@ export const update: RestEndpoint<'Comment', 'update'> = (
const data: SetOptional<typeof rawData, 'sys'> = copy(rawData)
delete data.sys

return raw.put<CommentProps>(http, getCommentUrl(params), data, {
return raw.put<CommentProps>(http, getEntryCommentUrl(params), data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
...headers,
Expand All @@ -70,5 +88,11 @@ export const del: RestEndpoint<'Comment', 'delete'> = (
http: AxiosInstance,
{ version, ...params }: DeleteCommentParams
) => {
return raw.del(http, getCommentUrl(params), { headers: { 'X-Contentful-Version': version } })
return raw.del(http, getEntryCommentUrl(params), { headers: { 'X-Contentful-Version': version } })
}

// Add a deprecation notive. But `getAll` may never be removed for app compatibility reasons.
/**
* @deprecated use `getMany` instead.
*/
export const getAll: RestEndpoint<'Comment', 'getAll'> = getMany
5 changes: 3 additions & 2 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CommentProps,
UpdateCommentParams,
UpdateCommentProps,
GetManyCommentsParams,
} from './entities/comment'
import { EditorInterfaceProps } from './entities/editor-interface'
import { CreateEntryProps, EntryProps, EntryReferenceProps } from './entities/entry'
Expand Down Expand Up @@ -896,11 +897,11 @@ export type MRActions = {
Comment: {
get: { params: GetCommentParams; return: CommentProps }
getMany: {
params: GetEntryParams & QueryParams
params: GetManyCommentsParams & QueryParams
return: CollectionProp<CommentProps>
}
getAll: {
params: GetEntryParams & QueryParams
params: GetManyCommentsParams & QueryParams
return: CollectionProp<CommentProps>
}
create: { params: CreateCommentParams; payload: CreateCommentProps; return: CommentProps }
Expand Down
9 changes: 7 additions & 2 deletions lib/create-entry-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OpPatch } from 'json-patch'
import { MakeRequest } from './common-types'
import { CreateCommentProps } from './entities/comment'
import { CreateCommentParams, CreateCommentProps } from './entities/comment'
import { Entry, EntryProps, EntryReferenceOptionsProps } from './entities/entry'
import { CreateTaskProps } from './entities/task'
import * as checks from './plain/checks'
Expand Down Expand Up @@ -323,7 +323,12 @@ export default function createEntryApi(makeRequest: MakeRequest) {
return makeRequest({
entityType: 'Comment',
action: 'create',
params,
params: {
spaceId: params.spaceId,
environmentId: params.environmentId,
parentEntityId: params.entryId,
parentEntityType: 'Entry',
} as CreateCommentParams,
payload: data,
}).then((data) => wrapComment(makeRequest, data))
},
Expand Down
30 changes: 28 additions & 2 deletions lib/entities/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ import {
DefaultElements,
GetCommentParams,
GetEntryParams,
GetSpaceEnvironmentParams,
Link,
MakeRequest,
PaginationQueryOptions,
SysLink,
} from '../common-types'
import { wrapCollection } from '../common-utils'
import enhanceWithMethods from '../enhance-with-methods'

export interface LinkWithRef<T extends string> {
sys: {
type: 'Link'
linkType: T
id: string
// Selected `ref` over explicit `version` for the future (e.g. `fields.de-DE.foo`)
ref?: `versions.${number}`
}
}

export type CommentSysProps = Pick<
BasicMetaSysProps,
'id' | 'version' | 'createdAt' | 'createdBy' | 'updatedAt' | 'updatedBy'
> & {
type: 'Comment'
space: SysLink
environment: SysLink
parentEntity: Link<'Entry'>
parentEntity: Link<'Entry'> | LinkWithRef<'Workflow'>
}

export type CommentProps = {
Expand All @@ -32,7 +44,21 @@ export type UpdateCommentProps = Omit<CommentProps, 'sys'> & {
sys: Pick<CommentSysProps, 'version'>
}

export type CreateCommentParams = GetEntryParams
// We keep this type as explicit as possible until we open up the comments entity further
export type GetCommentParentEntityParams = GetSpaceEnvironmentParams &
(
| {
parentEntityType: 'Entry'
parentEntityId: string
}
| {
parentEntityType: 'Workflow'
parentEntityId: string
parentEntityVersion?: number
}
)
export type GetManyCommentsParams = GetEntryParams | GetCommentParentEntityParams
export type CreateCommentParams = GetEntryParams | GetCommentParentEntityParams
export type UpdateCommentParams = GetCommentParams
export type DeleteCommentParams = GetCommentParams & { version: number }

Expand Down
4 changes: 4 additions & 0 deletions lib/export-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export type {
CommentProps,
CreateCommentProps,
UpdateCommentProps,
DeleteCommentParams,
GetCommentParentEntityParams,
GetManyCommentsParams,
LinkWithRef,
} from './entities/comment'
export type {
ContentType,
Expand Down
3 changes: 2 additions & 1 deletion lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
CommentProps,
UpdateCommentParams,
UpdateCommentProps,
GetManyCommentsParams,
} from '../entities/comment'
import { ContentTypeProps, CreateContentTypeProps } from '../entities/content-type'
import { EditorInterfaceProps } from '../entities/editor-interface'
Expand Down Expand Up @@ -385,7 +386,7 @@ export type PlainClientAPI = {
comment: {
get(params: OptionalDefaults<GetCommentParams>): Promise<CommentProps>
getMany(
params: OptionalDefaults<GetEntryParams & QueryParams>
params: OptionalDefaults<GetManyCommentsParams & QueryParams>
): Promise<CollectionProp<CommentProps>>
create(
params: OptionalDefaults<CreateCommentParams>,
Expand Down
8 changes: 3 additions & 5 deletions test/unit/adapters/REST/endpoints/workflow-definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
WorkflowDefinitionProps,
wrapWorkflowDefinition,
} from '../../../../../lib/entities/workflow-definition'
import { MakeRequest } from '../../../../../lib/export-types'
import { MakeRequest, MakeRequestOptions } from '../../../../../lib/export-types'

function setup(params = {}) {
const entityMock: WorkflowDefinitionProps = cloneMock('workflowDefinition')
Expand All @@ -22,8 +22,7 @@ describe('Rest Workflow Definition', () => {
const { httpMock, adapterMock, entityMock } = setup()
entityMock.sys.version = 2
const entity = wrapWorkflowDefinition(
// @ts-ignore
((...args) => adapterMock.makeRequest(...args)) as MakeRequest,
((...args: [MakeRequestOptions]) => adapterMock.makeRequest(...args)) as MakeRequest,
entityMock
)
entity.description = 'new description'
Expand All @@ -49,8 +48,7 @@ describe('Rest Workflow Definition', () => {
const { httpMock, adapterMock, entityMock } = setup()
entityMock.sys.version = 2
const entity = wrapWorkflowDefinition(
// @ts-ignore
((...args) => adapterMock.makeRequest(...args)) as MakeRequest,
((...args: [MakeRequestOptions]) => adapterMock.makeRequest(...args)) as MakeRequest,
entityMock
)
return entity.delete().then(() => {
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions test/unit/mocks/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,10 @@ const commentMock = {
},
parentEntity: {
sys: {
id: 'entry-id',
id: 'workflow-id',
type: 'Link',
linkType: 'Entry',
linkType: 'Workflow',
ref: 'versions.3',
},
},
},
Expand Down

0 comments on commit bc0c6ae

Please sign in to comment.