import {z} from 'zod' export type ActionsActionRequestParamObjectType = 'businesses' | 'watchlist_result'; export type ActionsActionRequestParamType = 'add_sources' | 'add_attributes' | 'verify_tin' | 'decision' | 'dismissal'; export type CreateApiV1ActionsArgs = {body: {object_type: ActionsActionRequestParamObjectType, object_id: string, type: ActionsActionRequestParamType, note?: string | undefined, thread_id?: string | undefined, run_id?: string | undefined, payload: {[key: string]: unknown}}}; export type CreateApiV1ActionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const actionType = z.enum(["add_sources", "add_attributes", "update_address_risk", "verify_tin", "decision", "dismissal"]); export const actionObjectType = z.enum(["Business", "Watchlist::Result"]); export const actionEffectOperation = z.enum(["created", "updated", "linked"]); export const actionEffectTarget = z.object({object: z.string(), id: z.string()}); export const actionEffect = z.object({operation: actionEffectOperation, diff: z.record(z.string(), z.unknown()).nullable(), target: actionEffectTarget.nullable()}); export const actionActorActorType = z.enum(["account", "user", "run"]); export const actionActor = z.object({actor_type: actionActorActorType, data: z.record(z.string(), z.unknown()).optional()}); export const action = z.object({id: z.string(), type: actionType, object_type: actionObjectType, object_id: z.string(), created_at: z.string(), note: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), effects: z.array(actionEffect), actors: z.array(actionActor)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create an action on an object * * POST /v1/actions */ export const createApiV1Actions = async (args: CreateApiV1ActionsArgs, options?: CreateApiV1ActionsOptions) => { const {baseUrl = "https://api.middesk.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/actions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(action, await res.json()) };