import {z} from 'zod' export type ApiV1CreditNoteLineItem = {account: string, cost_center?: string | undefined, created_ts: string, custom_fields?: {[key: string]: unknown} | undefined, description: string, discount?: (number | null) | undefined, id: string, item?: string | undefined, item_unit_of_measure?: (string | null) | undefined, legacy_id: string, line_amount: number, modified_ts: string, order?: number | undefined, quantity: number, tax_amount: number, tax_rate?: string | undefined, unit_amount: number}; export type CreateApiCreditNotesCreditNoteIdLineItemsArgs = {'X-Wafeq-Idempotency-Key'?: string | undefined, credit_note_id: string, expand?: string | undefined, body: ApiV1CreditNoteLineItem}; export type CreateApiCreditNotesCreditNoteIdLineItemsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiV1CreditNoteLineItem = z.object({account: z.string(), cost_center: z.string().optional(), created_ts: z.string(), custom_fields: z.record(z.string(), z.unknown()).optional(), description: z.string(), discount: z.number().gte(0).lt(10000000000000000).nullable().optional(), id: z.string(), item: z.string().optional(), item_unit_of_measure: z.string().nullable().optional(), legacy_id: z.string(), line_amount: z.number().gt(-10000000000000000).lt(10000000000000000), modified_ts: z.string(), order: z.number().int().optional(), quantity: z.number().gt(-10000000000000000).lt(10000000000000000), tax_amount: z.number().gt(-10000000000000000).lt(10000000000000000), tax_rate: z.string().optional(), unit_amount: z.number().gt(-10000000000000000).lt(10000000000000000)}); 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 line item * * POST /credit-notes/{credit_note_id}/line-items/ */ export const createApiCreditNotesCreditNoteIdLineItems = async (args: CreateApiCreditNotesCreditNoteIdLineItemsArgs, options?: CreateApiCreditNotesCreditNoteIdLineItemsOptions) => { const {baseUrl = "https://api.wafeq.com/v1", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Wafeq-Idempotency-Key'] != null) headers.set("X-Wafeq-Idempotency-Key", String(args['X-Wafeq-Idempotency-Key'])) if (args.expand != null) query.append("expand", String(args.expand)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/credit-notes/${encodeURIComponent(String(args.credit_note_id))}/line-items/${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(apiV1CreditNoteLineItem, await res.json()) };