import {z} from 'zod' export type GetApiMerchantActionsArgs = {'filter[workflowExecutionId]'?: string | undefined, 'filter[workflowCode]'?: string | undefined, 'filter[workspaceId]'?: string | undefined, 'filter[type]'?: ('authorize' | 'capture' | 'cancel' | 'confirm' | 'crateInstrument' | 'fraudScore' | 'fraudUpdate' | 'lookup' | 'payout' | 'refund' | 'startPaymentSession') | undefined, 'filter[status]'?: ('created' | 'failed' | 'success') | undefined, 'filter[errorCode]'?: string | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined}; export type GetApiMerchantActionsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMerchantActionsResponse = z.object({links: z.object({self: z.string(), prev: z.string().optional(), next: z.string().optional(), first: z.string().optional(), last: z.string().optional()}), results: z.array(z.object({id: z.string().optional(), workflowExecutionId: z.string(), workflowCode: z.string(), workspaceId: z.string(), type: z.enum(["authorize", "capture", "cancel", "confirm", "crateInstrument", "fraudScore", "fraudUpdate", "lookup", "payout", "refund", "startPaymentSession"]), status: z.enum(["created", "failed", "success"]), createdAt: z.string().optional(), updatedAt: z.string().optional(), errorCode: z.string().optional()}))}); 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 }; /** * Search & list actions * * GET /merchant/actions */ export const getApiMerchantActions = async (args: GetApiMerchantActionsArgs, options?: GetApiMerchantActionsOptions) => { const {baseUrl = "https://api.staging.payrails.io", fetch: fetchFn = fetch, BearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[workflowExecutionId]'] != null) query.append("filter[workflowExecutionId]", String(args['filter[workflowExecutionId]'])) if (args['filter[workflowCode]'] != null) query.append("filter[workflowCode]", String(args['filter[workflowCode]'])) if (args['filter[workspaceId]'] != null) query.append("filter[workspaceId]", String(args['filter[workspaceId]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[errorCode]'] != null) query.append("filter[errorCode]", String(args['filter[errorCode]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (BearerToken !== undefined) headers.set('authorization', `Bearer ${BearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/merchant/actions${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(getApiMerchantActionsResponse, await res.json()) };