import {z} from 'zod' export type PageLimit = number; export type PageCursor = string; export type GetApiCustomerFamiliesArgs = {limit?: PageLimit | undefined, cursor?: PageCursor | undefined, search?: string | undefined, 'updated.gt'?: string | undefined, sort_by?: ('created' | 'updated') | undefined}; export type GetApiCustomerFamiliesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customerFamily = z.object({id: z.string(), name: z.string(), root_parent_customer_id: z.string(), member_count: z.number().int()}); export const pageCursor = z.string(); export const pagination = z.object({next_cursor: pageCursor.optional()}); export const getApiCustomerFamiliesResponse = z.object({customer_families: z.array(customerFamily), pagination: pagination.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 }; /** * Lists all customer families * * GET /customer-families */ export const getApiCustomerFamilies = async (args: GetApiCustomerFamiliesArgs, options?: GetApiCustomerFamiliesOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.search != null) query.append("search", String(args.search)) if (args['updated.gt'] != null) query.append("updated.gt", String(args['updated.gt'])) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customer-families${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(getApiCustomerFamiliesResponse, await res.json()) };