Skip to content

Commit

Permalink
feat: add types for resource events (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
msieroslawska authored Jul 31, 2024
1 parent 916665c commit b8fed5b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/requests/typings/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import {
ScheduledActionProps,
TaskProps,
} from 'contentful-management'
import {
RESOURCES_SEARCH_EVENT,
RESOURCES_LOOKUP_EVENT,
type ResourcesLookupRequest,
type ResourcesLookupResponse,
type ResourcesSearchRequest,
type ResourcesSearchResponse,
} from './resources'

const GRAPHQL_FIELD_MAPPING_EVENT = 'graphql.field.mapping'
const GRAPHQL_QUERY_EVENT = 'graphql.query'
Expand Down Expand Up @@ -180,9 +188,22 @@ type FunctionEventHandlers = {
event: AppEventRequest
response: AppEventTransformationResponse
}
[RESOURCES_SEARCH_EVENT]: {
event: ResourcesSearchRequest
response: ResourcesSearchResponse
}
[RESOURCES_LOOKUP_EVENT]: {
event: ResourcesLookupRequest
response: ResourcesLookupResponse
}
}

export type FunctionEvent = GraphQLFieldTypeMappingRequest | GraphQLQueryRequest | AppEventRequest
export type FunctionEvent =
| GraphQLFieldTypeMappingRequest
| GraphQLQueryRequest
| AppEventRequest
| ResourcesSearchRequest
| ResourcesLookupRequest
export type FunctionEventType = keyof FunctionEventHandlers

/**
Expand Down
39 changes: 39 additions & 0 deletions src/requests/typings/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const RESOURCES_SEARCH_EVENT = 'resources.search'
export const RESOURCES_LOOKUP_EVENT = 'resources.lookup'

export type ResourcesSearchRequest = {
type: 'resources.search'
resourceType: string
query: string
limit?: number
pages?: {
nextCursor: string
}
}

export type ResourcesSearchResponse<S extends Record<string, unknown> = Record<string, unknown>> = {
items: S[]
pages: {
nextCursor?: string
}
}

type Scalar = string | number | boolean

export type ResourcesLookupRequest<L extends Record<string, Scalar[]> = Record<string, Scalar[]>> =
{
type: 'resources.lookup'
lookupBy: L
resourceType: string
limit?: number
pages?: {
nextCursor: string
}
}

export type ResourcesLookupResponse<L extends Record<string, unknown> = Record<string, unknown>> = {
items: L[]
pages: {
nextCursor?: string
}
}

0 comments on commit b8fed5b

Please sign in to comment.