import {z} from 'zod' export type GetApiV1CustomersMetadataArgs = {'search.customer_id'?: Array | undefined, 'search.account_number'?: Array | undefined, 'search.parent_customer_id'?: Array | undefined, 'search.is_parent'?: boolean | undefined, 'search.bill_profile_id'?: Array | undefined, 'search.status'?: ('ALL' | 'PROSPECT' | 'PENDING' | 'CLOSED' | 'OPEN') | undefined, 'search.name'?: string | undefined, 'search.created_date_start'?: string | undefined, 'search.created_date_end'?: string | undefined, 'search.agent_id'?: Array | undefined, 'search.updated_date_start'?: string | undefined, 'search.updated_date_end'?: string | undefined, 'search.suspended'?: boolean | undefined, 'search.page_size'?: number | undefined, 'search.page'?: number | undefined, 'search.sort'?: string | undefined}; export type GetApiV1CustomersMetadataOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const revAppComponentsLink = z.object({rel: z.string().optional(), href: z.string().optional()}); export const revAppModelsCustomersCustomerMetadata = z.object({customer_ID: z.number().int().optional(), name: z.string().optional(), account_number: z.string().optional(), links: z.array(revAppComponentsLink).optional()}); export const getApiV1CustomersMetadataResponse = z.array(revAppModelsCustomersCustomerMetadata); 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 }; /** * View a customer metadata * * GET /v1/Customers/Metadata */ export const getApiV1CustomersMetadata = async (args: GetApiV1CustomersMetadataArgs, options?: GetApiV1CustomersMetadataOptions) => { const {baseUrl = "https://restapi.rev.io", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['search.customer_id'] ?? []) query.append("search.customer_id", String(item)) for (const item of args['search.account_number'] ?? []) query.append("search.account_number", String(item)) for (const item of args['search.parent_customer_id'] ?? []) query.append("search.parent_customer_id", String(item)) if (args['search.is_parent'] != null) query.append("search.is_parent", String(args['search.is_parent'])) for (const item of args['search.bill_profile_id'] ?? []) query.append("search.bill_profile_id", String(item)) if (args['search.status'] != null) query.append("search.status", String(args['search.status'])) if (args['search.name'] != null) query.append("search.name", String(args['search.name'])) if (args['search.created_date_start'] != null) query.append("search.created_date_start", String(args['search.created_date_start'])) if (args['search.created_date_end'] != null) query.append("search.created_date_end", String(args['search.created_date_end'])) for (const item of args['search.agent_id'] ?? []) query.append("search.agent_id", String(item)) if (args['search.updated_date_start'] != null) query.append("search.updated_date_start", String(args['search.updated_date_start'])) if (args['search.updated_date_end'] != null) query.append("search.updated_date_end", String(args['search.updated_date_end'])) if (args['search.suspended'] != null) query.append("search.suspended", String(args['search.suspended'])) if (args['search.page_size'] != null) query.append("search.page_size", String(args['search.page_size'])) if (args['search.page'] != null) query.append("search.page", String(args['search.page'])) if (args['search.sort'] != null) query.append("search.sort", String(args['search.sort'])) if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Customers/Metadata${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(getApiV1CustomersMetadataResponse, await res.json()) };