import {z} from 'zod' export type GetApiV1DirectdebitsDirectDebitUuidArgs = {directDebitUuid: string}; export type GetApiV1DirectdebitsDirectDebitUuidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.object({code: z.string().optional(), description: z.string().optional()}); export const directDebit = z.object({directDebitUuid: z.string().optional(), currency: currency.optional(), status: z.enum(["RECEIVED", "REJECT_REQUESTED", "REJECT_READY_FOR_PROCESSING", "REJECT_RECORD_IN_PROGRESS", "REJECT_RECORDED", "REJECT_FILE_CREATED", "REJECT_FILE_SENT", "COLLECTED", "REFUND_REQUESTED", "REFUND_RECORD_IN_PROGRESS", "REFUND_RECORDED", "REFUND_FILE_CREATED", "REFUND_FILE_SENT"]).optional(), type: z.enum(["FIRST_COLLECTION", "ONGOING_COLLECTION", "REPRESENTED_COLLECTION", "FINAL_COLLECTION"]).optional(), mandateUUid: z.string().optional(), originatorReference: z.string().optional(), originatorName: z.string().optional(), originatorAlias: z.string().optional(), directDebitReference: z.string().optional(), targetIcan: z.number().int().optional(), targetPayeeId: z.number().int().optional(), isDDIC: z.boolean().optional(), amount: z.number().int().optional(), schemeRejectReason: z.string().optional(), schemeRejectReasonCode: z.enum(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B"]).optional(), lastUpdated: z.string().optional(), dateCreated: 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 }; /** * Get the details of a direct debit * * GET /v1/directdebits/{directDebitUuid} */ export const getApiV1DirectdebitsDirectDebitUuid = async (args: GetApiV1DirectdebitsDirectDebitUuidArgs, options?: GetApiV1DirectdebitsDirectDebitUuidOptions) => { const {baseUrl = "https://api.fire.com/business", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/directdebits/${encodeURIComponent(String(args.directDebitUuid))}${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(directDebit, await res.json()) };