import {z} from 'zod' export type UUID = string; export type TinyText = string; export type AmountDTO = {gross: number, net: number, tax: number}; export type LineItemsRequest = {external_id?: (string | null) | undefined, title?: TinyText | undefined, description?: (string | null) | undefined, quantity?: number | undefined, category?: (string | null) | undefined, brand?: (string | null) | undefined, gtin?: (string | null) | undefined, mpn?: (string | null) | undefined, amount_gross?: number | undefined, amount_net?: (number | null) | undefined, amount_tax?: (number | null) | undefined, tax_rate?: (number | null) | undefined, unit_price?: number | undefined, total_discount_amount?: (number | null) | undefined, product_url?: (string | null) | undefined, image_url?: (string | null) | undefined, quantity_unit?: (string | null) | undefined, type?: (string | null) | undefined}; export type CreateCreditNoteRequest = {external_code: TinyText, amount: AmountDTO, comment?: TinyText | undefined, line_items?: Array | undefined}; export type CreateApiInvoicesCaptureIdCreditNotesArgs = {captureId: UUID, body: CreateCreditNoteRequest}; export type CreateApiInvoicesCaptureIdCreditNotesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(36).max(36); export const createApiInvoicesCaptureIdCreditNotesResponse = z.object({uuid: uUID}); 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 }; /** * Create Credit Note * * POST /invoices/{captureId}/credit-notes */ export const createApiInvoicesCaptureIdCreditNotes = async (args: CreateApiInvoicesCaptureIdCreditNotesArgs, options?: CreateApiInvoicesCaptureIdCreditNotesOptions) => { const {baseUrl = "https://paella-sandbox.billie.io/api/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoices/${encodeURIComponent(String(args.captureId))}/credit-notes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiInvoicesCaptureIdCreditNotesResponse, await res.json()) };