import {z} from 'zod' export type GetApiV2BillingAgreementsArgs = {clientId?: number | undefined, name?: string | undefined, isDefault?: boolean | undefined, productId?: number | undefined}; export type GetApiV2BillingAgreementsOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2BillingAgreementsResponse = z.array(z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().nullable().optional(), invoiceGenerationType: z.enum(["AUTOMATIC", "MANUAL", "ONE_OFF"]).optional(), lastBillDate: z.number().nullable().optional(), nextBillDate: z.string().nullable().optional(), nextBillingPeriodStartDate: z.string().nullable().optional(), lastPeriodStartDate: z.string().nullable().optional(), lastPeriodEndDate: z.string().nullable().optional(), content: z.object({interval: z.string().nullable().optional(), billDate: z.string().nullable().optional(), startDate: z.string().optional(), endDate: z.string().nullable().optional(), businessHours: z.object({twentyFourSeven: z.boolean().optional(), days: z.array(z.object({day: z.enum(["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]).optional(), startHour: z.number().int().gte(0).lte(86399).optional(), endHour: z.number().int().gte(0).lte(86399).optional(), allDayClosed: z.boolean().optional()})).optional()}).optional()}).optional(), client: z.object({id: z.number().int().optional(), name: z.string().optional(), deleted: z.boolean()}).optional(), createTime: z.number().optional(), updateTime: z.number().optional(), timeDefault: z.boolean().optional(), businessHoursSource: z.enum(["CUSTOM", "TICKETING"]).optional(), taxId: z.number().int().optional(), archived: z.boolean().optional(), active: z.boolean().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 }; /** * Retrieves an agreement list. * * GET /v2/billing/agreements * * Also accepts auth: sessionKey */ export const getApiV2BillingAgreements = async (args: GetApiV2BillingAgreementsArgs, options: GetApiV2BillingAgreementsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.clientId != null) query.append("clientId", String(args.clientId)) if (args.name != null) query.append("name", String(args.name)) if (args.isDefault != null) query.append("isDefault", String(args.isDefault)) if (args.productId != null) query.append("productId", String(args.productId)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/billing/agreements${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(getApiV2BillingAgreementsResponse, await res.json()) };