import {z} from 'zod' export type GetApiV2InvoicesInvoiceIdArgs = {'idempotency-key'?: string | undefined}; export type GetApiV2InvoicesInvoiceIdOptions = {baseUrl?: string | undefined, advancedAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceToken = z.string(); export const invoiceStatus = z.enum(["open", "paid", "overdue"]); export const amount = z.object({currency: z.string(), value: z.string()}); export const url = z.object({href: z.string(), type: z.string()}); export const entityInvoice = z.object({resource: z.string(), id: invoiceToken, reference: z.string(), vatNumber: z.string().nullable(), status: invoiceStatus, netAmount: z.object({currency: z.string(), value: z.string()}), vatAmount: z.object({currency: z.string(), value: z.string()}), grossAmount: z.object({currency: z.string(), value: z.string()}), lines: z.array(z.object({period: z.string(), description: z.string(), count: z.number().int(), vatPercentage: z.number().int(), amount: amount})), issuedAt: z.string(), paidAt: z.string().nullable().optional(), dueAt: z.string().nullable().optional(), _links: z.object({self: url.optional(), pdf: url.optional(), documentation: url.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 invoice * * GET /v2/invoices/{invoiceId} * * Also accepts auth: oAuth */ export const getApiV2InvoicesInvoiceId = async (args: GetApiV2InvoicesInvoiceIdArgs, options?: GetApiV2InvoicesInvoiceIdOptions) => { const {baseUrl = "https://api.mollie.com", fetch: fetchFn = fetch, advancedAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (advancedAccessToken !== undefined) headers.set('authorization', `Bearer ${advancedAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/invoices/${encodeURIComponent(String(args.invoiceId))}${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(entityInvoice, await res.json()) };