import {z} from 'zod' export type Expiration = {year?: number | undefined, month?: number | undefined}; export type Segment = 'SEGMENT_A' | 'SEGMENT_B' | 'SEGMENT_C' | 'SEGMENT_D' | 'SEGMENT_E' | 'SEGMENT_F'; export type PinStatus = 'D' | 'W' | 'S'; export type DigitalLayoutCode = string; export type ScheduledClosingType = 'TIMEBASED'; export type ScheduledClosingTime = string; export type ScheduledClosing = {type?: ScheduledClosingType | undefined, time?: ScheduledClosingTime | undefined}; export type KeyValuePair = {key: string, value: string}; export type AdditionalValues = Array; export type CreditLimit = number; export type CreditPaymentTerms = number; export type EInvoice = {address?: string | undefined, operator?: string | undefined, paymentInstruction?: string | undefined}; export type InvoiceDayOfMonth = number; export type InvoiceDeliveryMethod = 'REGULAR_MAIL' | 'EMAIL' | 'E_INVOICE' | 'CUSTOM_1' | 'CUSTOM_2' | 'CUSTOM_3' | 'CUSTOM_4' | 'CUSTOM_5' | 'BETALINGSSERVICE'; export type MinimumToPay = {amount?: number | undefined, percentage?: number | undefined}; export type ProductCodeInRequest = string; export type InvoicePaymentMethod = 'DIRECT_DEBIT' | 'PLUS_GIROT' | 'BANK_GIROT' | 'BANK_TRANSFER' | 'IBAN'; export type CreateCreditResourceBody = {expiration?: Expiration | undefined, segment?: Segment | undefined, pinStatus?: PinStatus | undefined, digitalLayoutCode?: DigitalLayoutCode | undefined, scheduledClosing?: ScheduledClosing | undefined, additionalValues?: AdditionalValues | undefined, encryptedData?: string | undefined, parentId?: unknown | undefined, creditLimit: CreditLimit, paymentTerms?: CreditPaymentTerms | undefined, eInvoice?: EInvoice | undefined, invoiceDayOfMonth?: InvoiceDayOfMonth | undefined, invoiceDeliveryMethod?: InvoiceDeliveryMethod | undefined, minimumToPay?: MinimumToPay | undefined, productCode?: ProductCodeInRequest | undefined, invoicePaymentMethod?: InvoicePaymentMethod | undefined}; export type CreateApiV4CREDITCustomerCustomerIdArgs = {auditUser: string, customerId: unknown, body: CreateCreditResourceBody}; export type CreateApiV4CREDITCustomerCustomerIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const resourceResponse = z.object({description: z.string().optional(), id: 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 }; /** * Create credit account * * POST /v4/CREDIT/customer/{customerId} */ export const createApiV4CREDITCustomerCustomerId = async (args: CreateApiV4CREDITCustomerCustomerIdArgs, options: CreateApiV4CREDITCustomerCustomerIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/CREDIT/customer/${encodeURIComponent(String(args.customerId))}${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(resourceResponse, await res.json()) };