import {z} from 'zod' export type GetApiMerchantWorkflowsWorkflowCodeExecutionsArgs = {'filter[merchantReference]'?: string | undefined, 'filter[holderReference]'?: string | undefined, 'filter[workspaceId]'?: string | undefined, 'filter[workflowCode]'?: string | undefined, 'filter[workflowVersion]'?: string | undefined, 'filter[lastStatus]'?: string | undefined, 'page[after]'?: string | undefined, 'page[before]'?: string | undefined, 'page[size]'?: number | undefined, workflowCode: string}; export type GetApiMerchantWorkflowsWorkflowCodeExecutionsOptions = {baseUrl?: string | undefined, BearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMerchantWorkflowsWorkflowCodeExecutionsResponse = 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(), createdAt: z.string(), updatedAt: z.string(), status: z.array(z.object({code: z.string(), time: z.string(), errors: z.array(z.object({id: z.string(), code: z.string(), detail: z.string(), docUrl: z.string().optional(), reason: z.record(z.string(), z.unknown()).optional()})).optional()})), merchantReference: z.string(), holderReference: z.string(), workflow: z.object({code: z.string(), version: z.number().int()})}))}); 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 executions * * GET /merchant/workflows/{workflowCode}/executions */ export const getApiMerchantWorkflowsWorkflowCodeExecutions = async (args: GetApiMerchantWorkflowsWorkflowCodeExecutionsArgs, options?: GetApiMerchantWorkflowsWorkflowCodeExecutionsOptions) => { 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[merchantReference]'] != null) query.append("filter[merchantReference]", String(args['filter[merchantReference]'])) if (args['filter[holderReference]'] != null) query.append("filter[holderReference]", String(args['filter[holderReference]'])) if (args['filter[workspaceId]'] != null) query.append("filter[workspaceId]", String(args['filter[workspaceId]'])) if (args['filter[workflowCode]'] != null) query.append("filter[workflowCode]", String(args['filter[workflowCode]'])) if (args['filter[workflowVersion]'] != null) query.append("filter[workflowVersion]", String(args['filter[workflowVersion]'])) if (args['filter[lastStatus]'] != null) query.append("filter[lastStatus]", String(args['filter[lastStatus]'])) 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/workflows/${encodeURIComponent(String(args.workflowCode))}/executions${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(getApiMerchantWorkflowsWorkflowCodeExecutionsResponse, await res.json()) };