import {z} from 'zod' export type CreateApiV3VendorCreditsBulkArchiveArgs = {sessionId?: (string | null) | undefined, devKey?: (string | null) | undefined, ids: string}; export type CreateApiV3VendorCreditsBulkArchiveOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const vendorCreditLineItemResponseDto = z.object({id: z.string().optional(), description: z.string().optional(), amount: z.number().optional(), classifications: z.object({chartOfAccountId: z.string().optional(), accountingClassId: z.string().optional(), departmentId: z.string().optional(), jobId: z.string().optional(), locationId: z.string().optional(), itemId: z.string().optional(), employeeId: z.string().optional(), customerId: z.string().optional()}).optional()}); export const vendorCreditResponseDto = z.object({id: z.string().optional(), vendorId: z.string().optional(), referenceNumber: z.string().optional(), creditDate: z.string().optional(), description: z.string().optional(), archived: z.boolean().optional(), applyToChartOfAccountId: z.string().optional(), applyToBankAccountId: z.string().optional(), amount: z.number().optional(), appliedAmount: z.number().optional(), status: z.enum(["FULLY_APPLIED", "NOT_APPLIED", "PARTIALLY_APPLIED", "IN_PROCESS", "UNDEFINED"]).optional(), createdTime: z.string().optional(), updatedTime: z.string().optional(), vendorCreditLineItems: z.array(vendorCreditLineItemResponseDto).optional()}); export const vendorCreditBulkResponseDto = z.object({vendorCredits: z.array(vendorCreditResponseDto).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 }; /** * Archive multiple vendor credits * * POST /v3/vendor-credits/bulk/archive */ export const createApiV3VendorCreditsBulkArchive = async (args: CreateApiV3VendorCreditsBulkArchiveArgs, options?: CreateApiV3VendorCreditsBulkArchiveOptions) => { const {baseUrl = "https://gateway.stage.bill.com/connect", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sessionId != null) headers.set("sessionId", String(args.sessionId)) if (args.devKey != null) headers.set("devKey", String(args.devKey)) if (args.ids != null) query.append("ids", String(args.ids)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/vendor-credits/bulk/archive${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(vendorCreditBulkResponseDto, await res.json()) };