import {z} from 'zod' export type GetApiV1MandatesOptions = {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 mandate = z.object({mandateUuid: z.string().optional(), currency: currency.optional(), status: z.enum(["CREATED", "LIVE", "REJECT_REQUESTED", "REJECT_RECORD_IN_PROGRESS", "REJECT_RECORDED", "REJECT_FILE_CREATED", "REJECT_FILE_SENT", "CANCEL_REQUESTED", "CANCEL_RECORD_IN_PROGRESS", "CANCEL_RECORDED", "CANCEL_FILE_CREATED", "CANCEL_FILE_SENT", "COMPLETE", "DORMANT"]).optional(), originatorReference: z.string().optional(), originatorName: z.string().optional(), originatorAlias: z.string().optional(), originatorLogoUrlSmall: z.string().optional(), originatorLogoUrlLarge: z.string().optional(), mandateReference: z.string().optional(), alias: z.string().optional(), targetIcan: z.number().int().optional(), numberOfDirectDebitCollected: z.number().int().optional(), valueOfDirectDebitCollected: z.number().int().optional(), latestDirectDebitAmount: z.number().int().optional(), latestDirectDebitDate: z.string().optional(), fireRejectionReason: z.enum(["ACCOUNT_DOES_NOT_ACCEPT_DIRECT_DEBITS", "DDIC", "ACCOUNT_NOT_FOUND", "ACCOUNT_NOT_LIVE", "CUSTOMER_NOT_FOUND", "BUSINESS_NOT_LIVE", "BUSINESS_NOT_FULL", "PERSONAL_USER_NOT_LIVE", "PERSONAL_USER_NOT_FULL", "MANDATE_ALREADY_EXISTS", "MANDATE_WITH_DIFERENT_ACCOUNT", "NULL_MANDATE_REFERENCE", "INVALID_ACCOUNT_CURRENCY", "INVALID_MANDATE_REFERENCE", "REQUESTED_BY_CUSTOMER_VIA_SUPPORT", "CUSTOMER_ACCOUNT_CLOSED", "CUSTOMER_DECEASED", "ACCOUNT_TRANSFERRED", "MANDATE_NOT_FOUND", "ACCOUNT_TRANSFERRED_TO_DIFFERENT_ACCOUNT", "INVALID_ACCOUNT_TYPE", "MANDATE_EXPIRED", "MANDATE_CANCELLED", "REQUESTED_BY_CUSTOMER"]).optional(), schemeCancelReason: z.string().optional(), schemeCancelReasonCode: z.string().optional(), lastUpdated: z.string().optional(), dateCreated: z.string().optional(), dateCompleted: z.string().optional(), dateCancelled: z.string().optional()}); export const getApiV1MandatesResponse = z.object({total: z.number().int().optional(), mandates: z.array(mandate).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 }; /** * List all direct debit mandates * * GET /v1/mandates */ export const getApiV1Mandates = async (options?: GetApiV1MandatesOptions) => { 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/mandates${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(getApiV1MandatesResponse, await res.json()) };