Skip to content

Commit

Permalink
feat: more descriptive app action function types [EXT-5711] (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjolton-contentful authored Sep 16, 2024
1 parent 664609a commit f16fbb5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/requests/typings/appAction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlainClientAPI } from 'contentful-management'
import type { AppActionCategoryType, PlainClientAPI } from 'contentful-management'

export type AppActionCallContext = {
cma: PlainClientAPI
Expand All @@ -11,3 +11,18 @@ export type AppActionCallContext = {
uploadHost: string
}
}

export type AppActionCustomCategoryBody = Record<string, unknown>
export type AppActionEntriesV1CategoryBody = { entryIds: string }
export type AppActionNotificationsV1CategoryBody = { message: string; recipient: string }

export type AppActionCategoryBodyMap = {
Custom: AppActionCustomCategoryBody
'Entries.v1.0': AppActionEntriesV1CategoryBody
'Notifications.v1.0': AppActionNotificationsV1CategoryBody
}

export type AppActionRequestBody<CategoryType extends AppActionCategoryType> =
CategoryType extends keyof AppActionCategoryBodyMap
? AppActionCategoryBodyMap[CategoryType]
: never
23 changes: 19 additions & 4 deletions src/requests/typings/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/*eslint-disable no-unused-vars*/

import {
AppActionCategoryType,
AppInstallationProps,
AssetProps,
BulkActionProps,
Expand All @@ -24,6 +25,7 @@ import {
type ResourcesSearchRequest,
type ResourcesSearchResponse,
} from './resources'
import { AppActionCategoryBodyMap, AppActionRequestBody } from './appAction'

const GRAPHQL_FIELD_MAPPING_EVENT = 'graphql.field.mapping'
const GRAPHQL_QUERY_EVENT = 'graphql.query'
Expand Down Expand Up @@ -158,9 +160,21 @@ export type AppEventTransformationResponse = {

export type AppEventHandlerResponse = void

export type AppActionRequest = {
/**
* The app action request body will contain different parameters depending on the category of the app action
*
* Specify your app action category as the generic type `Category` to get the correct body type,
* e.g. `const { body: { message, recipient }} = event as AppActionRequest<'Notification.v1.0'>`
*
* If you are using the Custom category, you can specify the parameter shape as the second generic type `CustomCategoryBody`,
* e.g. `const { body: { myNumber }} = event as AppActionRequest<'Custom', { myNumber: number }>`
*/
export type AppActionRequest<
CategoryType extends AppActionCategoryType = 'Custom',
CustomCategoryBody = AppActionCategoryBodyMap['Custom'],
> = {
headers: Record<string, string | number>
body: Record<string, unknown>
body: CategoryType extends 'Custom' ? CustomCategoryBody : AppActionRequestBody<CategoryType>
type: typeof APP_ACTION_CALL
}

Expand All @@ -186,7 +200,7 @@ type FunctionEventHandlers = {
response: GraphQLQueryResponse
}
[APP_ACTION_CALL]: {
event: AppActionRequest
event: AppActionRequest<AppActionCategoryType>
response: AppActionResponse
}
[APP_EVENT_FILTER]: {
Expand Down Expand Up @@ -214,6 +228,7 @@ type FunctionEventHandlers = {
export type FunctionEvent =
| GraphQLFieldTypeMappingRequest
| GraphQLQueryRequest
| AppActionRequest
| AppEventRequest
| ResourcesSearchRequest
| ResourcesLookupRequest
Expand All @@ -224,7 +239,7 @@ export type FunctionEventType = keyof FunctionEventHandlers
* e.g. `const handler: FunctionEventHandler = (event, context) => { ... }`
*
* This type can also be used to construct helper functions for specific events
* e.g. `const queryHandler: FunctionEventHandler<'graphql.query'> = (event, context) => { ... }
* e.g. `const queryHandler: FunctionEventHandler<'graphql.query'> = (event, context) => { ... }`
*/
export type FunctionEventHandler<
K extends FunctionEventType = FunctionEventType,
Expand Down

0 comments on commit f16fbb5

Please sign in to comment.