Skip to content

Commit

Permalink
feat: PlainClient docs - task [EXT-4924] (#2181)
Browse files Browse the repository at this point in the history
* feat: PlainClient docs - task [EXT-4924]

* improving examples

* jareds nits (ty jared)

* cleaning up javascript examples

* refactoring create or update

* fixing build

* fixing build

* attempting to fix codeclimate

* attempting to fix codeclimate

* attempting to fix codeclimate
  • Loading branch information
david-shibley-contentful authored Feb 21, 2024
1 parent 9a651ec commit 9c8603f
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 71 deletions.
29 changes: 2 additions & 27 deletions lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import {
QueryParams,
GetBulkActionParams,
GetReleaseParams,
GetTaskParams,
GetEntryParams,
CursorPaginatedCollectionProp,
GetWorkflowDefinitionParams,
GetEnvironmentTemplateParams,
Expand Down Expand Up @@ -102,14 +100,6 @@ import {
ReleaseValidatePayload,
} from '../entities/release'
import { ReleaseActionProps, ReleaseActionQueryOptions } from '../entities/release-action'
import {
CreateTaskParams,
CreateTaskProps,
DeleteTaskParams,
TaskProps,
UpdateTaskParams,
UpdateTaskProps,
} from '../entities/task'
import {
CreateWorkflowDefinitionParams,
CreateWorkflowDefinitionProps,
Expand Down Expand Up @@ -161,6 +151,7 @@ import { AppEventSubscriptionPlainClientAPI } from './entities/app-event-subscri
import { AppKeyPlainClientAPI } from './entities/app-key'
import { UserPlainClientAPI } from './entities/user'
import { UploadPlainClientAPI } from './entities/upload'
import { TaskPlainClientAPI } from './entities/task'
import { OrganizationPlainClientAPI } from './entities/organization'
import { LocalePlainClientAPI } from './entities/locale'

Expand Down Expand Up @@ -758,23 +749,7 @@ export type PlainClientAPI = {
): Promise<SpaceMembershipProps>
delete(params: OptionalDefaults<GetSpaceMembershipProps>): Promise<any>
}
task: {
get(params: OptionalDefaults<GetTaskParams>): Promise<TaskProps>
getMany(
params: OptionalDefaults<GetEntryParams & QueryParams>
): Promise<CollectionProp<TaskProps>>
create(
params: OptionalDefaults<CreateTaskParams>,
rawData: CreateTaskProps,
headers?: RawAxiosRequestHeaders
): Promise<TaskProps>
update(
params: OptionalDefaults<UpdateTaskParams>,
rawData: UpdateTaskProps,
headers?: RawAxiosRequestHeaders
): Promise<TaskProps>
delete(params: OptionalDefaults<DeleteTaskParams>): Promise<void>
}
task: TaskPlainClientAPI
team: {
get(params: OptionalDefaults<GetTeamParams>): Promise<TeamProps>
getMany(
Expand Down
14 changes: 4 additions & 10 deletions lib/plain/entities/app-definition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RawAxiosRequestHeaders } from 'axios'
import {
CollectionProp,
GetAppDefinitionParams,
Expand All @@ -11,6 +10,8 @@ import {
CreateAppDefinitionProps,
} from '../../entities/app-definition'
import { OptionalDefaults } from '../wrappers/wrap'
import { CreateOrUpdate } from './base'
import { Get } from 'type-fest'

export type AppDefinitionPlainClientAPI = {
/**
Expand Down Expand Up @@ -69,10 +70,7 @@ export type AppDefinitionPlainClientAPI = {
* );
* ```
*/
create(
params: OptionalDefaults<GetOrganizationParams>,
rawData: CreateAppDefinitionProps
): Promise<AppDefinitionProps>
create: CreateOrUpdate<GetOrganizationParams, CreateAppDefinitionProps, AppDefinitionProps>
/**
* Update an App Definition
* @param params entity IDs to identify the App Definition
Expand All @@ -93,11 +91,7 @@ export type AppDefinitionPlainClientAPI = {
* );
* ```
*/
update(
params: OptionalDefaults<GetAppDefinitionParams>,
rawData: AppDefinitionProps,
headers?: RawAxiosRequestHeaders
): Promise<AppDefinitionProps>
update: CreateOrUpdate<GetAppDefinitionParams, AppDefinitionProps, AppDefinitionProps>
/**
* Delete an App Definition
* @param params entity IDs to identify the App Definition
Expand Down
8 changes: 8 additions & 0 deletions lib/plain/entities/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RawAxiosRequestHeaders } from 'axios'
import { OptionalDefaults } from '../wrappers/wrap'

export type CreateOrUpdate<T, Props, ReturnProps> = (
params: OptionalDefaults<T>,
rawData: Props,
headers: RawAxiosRequestHeaders
) => Promise<ReturnProps>
20 changes: 4 additions & 16 deletions lib/plain/entities/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RawAxiosRequestHeaders } from 'axios'
import {
CollectionProp,
GetExtensionParams,
Expand All @@ -7,6 +6,7 @@ import {
} from '../../common-types'
import { CreateExtensionProps, ExtensionProps } from '../../entities/extension'
import { OptionalDefaults } from '../wrappers/wrap'
import { CreateOrUpdate } from './base'

export type ExtensionPlainClientAPI = {
/**
Expand Down Expand Up @@ -69,11 +69,7 @@ export type ExtensionPlainClientAPI = {
* );
* ```
*/
create(
params: OptionalDefaults<GetSpaceEnvironmentParams>,
rawData: CreateExtensionProps,
headers?: RawAxiosRequestHeaders
): Promise<ExtensionProps>
create: CreateOrUpdate<GetSpaceEnvironmentParams, CreateExtensionProps, ExtensionProps>
/**
* Creates a new Extension with a given ID
* @param params entity IDs to identify the Environment in which to create the Extension
Expand Down Expand Up @@ -104,11 +100,7 @@ export type ExtensionPlainClientAPI = {
* );
* ```
*/
createWithId(
params: OptionalDefaults<GetExtensionParams>,
rawData: CreateExtensionProps,
headers?: RawAxiosRequestHeaders
): Promise<ExtensionProps>
createWithId: CreateOrUpdate<GetExtensionParams, CreateExtensionProps, ExtensionProps>
/**
* Updates an Extension
* @param params entity IDs to identify the Extension
Expand Down Expand Up @@ -155,11 +147,7 @@ export type ExtensionPlainClientAPI = {
* );
* ```
*/
update(
params: OptionalDefaults<GetExtensionParams>,
rawData: ExtensionProps,
headers?: RawAxiosRequestHeaders
): Promise<ExtensionProps>
update: CreateOrUpdate<GetExtensionParams, ExtensionProps, ExtensionProps>
/**
* Deletes the Extension
* @param params entity IDs to identity the Extension
Expand Down
129 changes: 129 additions & 0 deletions lib/plain/entities/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
CreateTaskParams,
DeleteTaskParams,
UpdateTaskParams,
UpdateTaskProps,
} from '../../entities/task'
import {
CollectionProp,
CreateTaskProps,
GetEntryParams,
GetTaskParams,
QueryParams,
TaskProps,
} from '../../export-types'
import { OptionalDefaults } from '../wrappers/wrap'
import { CreateOrUpdate } from './base'

export type TaskPlainClientAPI = {
/** Fetches a task
*
* @param params Space ID, Entry ID, Environment ID, and Task ID
* @returns the task
* @throws if the request fails or the task is not found
* @example
* ```javascript
* const task = await client.task.get({
* spaceId: '<space_id>',
* entryId: '<entry_id>',
* environmentId: '<environment_id>',
* taskId: '<task_id>',
* });
* ```
*/
get(params: OptionalDefaults<GetTaskParams>): Promise<TaskProps>
/** Fetches all tasks
*
* @param params Space ID, Entry ID, Environment ID, and query parameters
* @returns a collection of tasks
* @throws if the request fails or the tasks are not found
* @example
* ```javascript
* const tasks = await client.task.getMany({
* spaceId: '<space_id>',
* entryId: '<entry_id>',
* environmentId: '<environment_id>',
* query: {
* limit: 100,
* }
* });
* ```
*/
getMany(
params: OptionalDefaults<GetEntryParams & QueryParams>
): Promise<CollectionProp<TaskProps>>
/** Creates a task
*
* @param params Space ID, Entry ID, Environment ID, and query parameters
* @param rawData the task
* @returns the task
* @throws if the request fails or the task is not found
* @example
* ```javascript
* const task = await client.task.create(
* {
* spaceId: '<space_id>',
* entryId: '<entry_id>',
* environmentId: '<environment_id>',
* },
* {
* body: "Review Translation",
* status: "active",
* assignedTo: {
* sys: {
* type: "Link",
* linkType: "User",
* id: <user_id>
* }
* }
* }
* );
* ```
*/
create: CreateOrUpdate<CreateTaskParams, CreateTaskProps, TaskProps>
/** Updates a task
*
* @param params Space ID, Entry ID, Environment ID, and Task ID
* @param rawData the task
* @returns the task
* @throws if the request fails or the task is not found
* @example
* ```javascript
* const task = await client.task.update(
* {
* spaceId: '<space_id>',
* entryId: '<entry_id>',
* environmentId: '<environment_id>',
* taskId: '<task_id>',
* },
* {
* body: "Review Translation",
* status: "active",
* assignedTo: {
* sys: {
* type: "Link",
* linkType: "User",
* id: <user_id>
* }
* }
* }
* );
* ```
*/
update: CreateOrUpdate<UpdateTaskParams, UpdateTaskProps, TaskProps>
/** Deletes a task
*
* @param params Space ID, Entry ID, Environment ID, and Task ID
* @throws if the request fails or the task is not found
* @example
* ```javascript
* await client.task.delete({
* spaceId: '<space_id>',
* entryId: '<entry_id>',
* environmentId: '<environment_id>',
* taskId: '<task_id>',
* });
* ```
*/
delete(params: OptionalDefaults<DeleteTaskParams>): Promise<void>
}
8 changes: 6 additions & 2 deletions lib/plain/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export type UserPlainClientAPI = {
* ```javascript
* const users = await client.user.getManyForSpace({
* spaceId: '<space_id>',
* limit: 100,
* query: {
* limit: 100,
* }
* });
* ```
*/
Expand Down Expand Up @@ -74,7 +76,9 @@ export type UserPlainClientAPI = {
* ```javascript
* const users = await client.user.getManyForOrganization({
* organizationId: '<organization_id>',
* limit: 100,
* query: {
* limit: 100,
* }
* });
* ```
*/
Expand Down
7 changes: 2 additions & 5 deletions lib/plain/entities/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
WebhookSigningSecretProps,
} from '../../entities/webhook'
import { OptionalDefaults } from '../wrappers/wrap'
import { CreateOrUpdate } from './base'

export type WebhookPlainClientAPI = {
// Webhooks
Expand Down Expand Up @@ -70,11 +71,7 @@ export type WebhookPlainClientAPI = {
* );
* ```
*/
create(
params: OptionalDefaults<GetSpaceParams>,
rawData: CreateWebhooksProps,
headers?: RawAxiosRequestHeaders
): Promise<WebhookProps>
create: CreateOrUpdate<GetSpaceParams, CreateWebhooksProps, WebhookProps>
/**
* Creates the Webhook
* @param params entity IDs to identify the Webhook to update
Expand Down
29 changes: 21 additions & 8 deletions lib/plain/plain-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@ import { CommentNode, RichTextCommentDocument } from '../entities/comment'
chai.should()
chai.use(sinonChai)

type Props = {
commentId: string
entryId: string
bodyFormat: string
spaceId: string
environmentId: string
}

describe('Plain Client', () => {
const stub = Sinon.stub()
const defaultProps: Props = {
commentId: '123',
entryId: '123',
bodyFormat: 'plain-text',
spaceId: '123',
environmentId: '123',
}

beforeEach(() => stub.reset())

Expand All @@ -23,11 +38,10 @@ describe('Plain Client', () => {

describe('Comment', () => {
describe('when body is plain text', () => {
const props: { commentId: string; entryId: string; bodyFormat: 'plain-text' } = {
commentId: '123',
entryId: '123',
const props = {
...defaultProps,
bodyFormat: 'plain-text',
}
} as any
const updateText = 'My new text'

it('should create a get object', async () => {
Expand Down Expand Up @@ -148,11 +162,10 @@ describe('Plain Client', () => {
],
}

const props: { commentId: string; entryId: string; bodyFormat: 'rich-text' } = {
commentId: '123',
entryId: '123',
const props = {
...defaultProps,
bodyFormat: 'rich-text',
}
} as any

it('should create a get object', async () => {
await plainClient.comment.get(props)
Expand Down
6 changes: 3 additions & 3 deletions lib/plain/wrappers/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export type DefaultParams = {
* @private
*/
export type OptionalDefaults<T> = Omit<T, keyof DefaultParams> &
('organizationId' extends keyof T ? { organizationId?: string } : Record<string, unknown>) &
('spaceId' extends keyof T ? { spaceId?: string } : Record<string, unknown>) &
('environmentId' extends keyof T ? { environmentId?: string } : Record<string, unknown>)
('organizationId' extends keyof T ? { organizationId: string } : Record<string, unknown>) &
('spaceId' extends keyof T ? { spaceId: string } : Record<string, unknown>) &
('environmentId' extends keyof T ? { environmentId: string } : Record<string, unknown>)

/**
* @private
Expand Down

0 comments on commit 9c8603f

Please sign in to comment.