import {z} from 'zod' export type ActionsGetActionRequestObjectType = 'businesses' | 'watchlist_result'; export type GetApiV1ActionsIdArgs = {id: string, object_type: ActionsGetActionRequestObjectType, object_id: string}; export type GetApiV1ActionsIdOptions = {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 }; /** * Retrieve an action * * GET /v1/actions/{id} */ export const getApiV1ActionsId = async (args: GetApiV1ActionsIdArgs, options?: GetApiV1ActionsIdOptions) => { const {baseUrl = "https://api.middesk.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.object_type != null) query.append("object_type", String(args.object_type)) if (args.object_id != null) query.append("object_id", String(args.object_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/actions/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };