import {z} from 'zod' export type GetApiV3CreditMemosIdArgs = {id: string}; export type GetApiV3CreditMemosIdOptions = {baseUrl?: string | undefined, customHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const creditMemoCustomerDto = z.object({id: z.string(), name: z.string(), nameWithPrefix: z.string().optional()}); export const creditMemoItemDto = z.object({id: z.string(), name: z.string(), externalId: z.string().optional()}); export const creditMemoErpClassDto = z.object({id: z.string(), name: z.string()}); export const creditMemoLineItemDto = z.object({id: z.string(), name: z.string(), note: z.string().optional(), quantity: z.number(), amount: z.number(), salesTaxAmount: z.number(), item: creditMemoItemDto.optional(), erpClass: creditMemoErpClassDto.optional(), contraRevenueScheduleId: z.string()}); export const creditMemoApplicationDto = z.object({id: z.string(), creditMemoId: z.string(), invoiceId: z.string(), amountApplied: z.number(), invoiceNumber: z.string(), invoicePrefix: z.string(), appliedAt: z.string()}); export const creditMemoExternalIdDto = z.object({externalId: z.string(), sourceType: z.string()}); export const integratorsApiError = z.object({code: z.number(), message: z.string(), details: z.object({}).optional()}); export const getApiV3CreditMemosIdResponse = z.object({payload: z.object({creditMemoId: z.string(), creditMemoNumber: z.string(), creditMemoPrefix: z.string(), customerId: z.string(), manufacturerId: z.string(), issueDate: z.string(), total: z.number(), memo: z.string().optional(), reason: z.string().optional(), status: z.string(), customer: creditMemoCustomerDto, items: z.array(creditMemoLineItemDto), applications: z.array(creditMemoApplicationDto), externalIds: z.array(creditMemoExternalIdDto), createdAt: z.string(), lastUpdatedAt: z.string(), syncStatus: z.string()}), success: z.boolean(), message: z.string(), error: integratorsApiError}); 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 credit memo by ID * * GET /v3/credit-memos/{id} */ export const getApiV3CreditMemosId = async (args: GetApiV3CreditMemosIdArgs, options?: GetApiV3CreditMemosIdOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/credit-memos/${encodeURIComponent(String(args.id))}${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(getApiV3CreditMemosIdResponse, await res.json()) };