Skip to content

Commit

Permalink
feat: allow custom schema to be provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jjolton-contentful committed Sep 11, 2024
1 parent 79f6a05 commit 7723a08
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/requests/typings/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,21 @@ export type AppEventTransformationResponse = {

export type AppEventHandlerResponse = void

export type AppActionRequest<S extends AppActionSchema = 'Custom'> = {
/**
* The app action request body will contain different properties depending on the schema of the app action
*
* Specify your app action schema as the generic type `Schema` to get the correct body type,
* e.g. `const { body: { message, recipient }} = event as AppActionRequest<'Notification.v1.0'>`
*
* If you are using a Custom schema, you can specify its shape as the second generic type `CustomSchemaBody`,
* e.g. `const { body: { myNumber }} = event as AppActionRequest<'Custom', { myNumber: number }>`
*/
export type AppActionRequest<
Schema extends AppActionSchema = 'Custom',
CustomSchemaBody = AppActionSchemaBodyMap['Custom'],
> = {
headers: Record<string, string | number>
body: AppActionSchemaBodyMap[S]
body: Schema extends 'Custom' ? CustomSchemaBody : AppActionSchemaBodyMap[Schema]
type: typeof APP_ACTION_CALL
}

Expand All @@ -187,7 +199,7 @@ type FunctionEventHandlers = {
response: GraphQLQueryResponse
}
[APP_ACTION_CALL]: {
event: AppActionRequest<keyof AppActionSchemaBodyMap>
event: AppActionRequest<AppActionSchema>
response: AppActionResponse
}
[APP_EVENT_FILTER]: {
Expand Down Expand Up @@ -226,7 +238,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 7723a08

Please sign in to comment.