import {z} from 'zod' export type GetApiOrdersArgs = {page?: number | undefined, types?: Array | undefined, branch_ids?: Array | undefined, ids?: Array | undefined, numbers?: Array | undefined, statuses?: Array | undefined, managers?: Array | undefined, client_ids?: Array | undefined, client_names?: Array | undefined, client_phones?: Array | undefined, asset_uid?: Array | undefined, created_at?: Array | undefined, modified_at?: Array | undefined, closed_at?: Array | undefined, scheduled_for?: Array | undefined, due_date?: Array | undefined, sort?: ('id' | '-id' | 'modified_at' | '-modified_at' | 'scheduled_for' | '-scheduled_for' | 'due_date' | '-due_date' | 'closed_at' | '-closed_at') | undefined}; export type GetApiOrdersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrdersResponse = z.object({}); 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 Orders * * GET /orders */ export const getApiOrders = async (args: GetApiOrdersArgs, options?: GetApiOrdersOptions) => { const {baseUrl = "https://api.roapp.io/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) for (const item of args.types ?? []) query.append("types", String(item)) for (const item of args.branch_ids ?? []) query.append("branch_ids", String(item)) for (const item of args.ids ?? []) query.append("ids", String(item)) for (const item of args.numbers ?? []) query.append("numbers", String(item)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.managers ?? []) query.append("managers", String(item)) for (const item of args.client_ids ?? []) query.append("client_ids", String(item)) for (const item of args.client_names ?? []) query.append("client_names", String(item)) for (const item of args.client_phones ?? []) query.append("client_phones", String(item)) for (const item of args.asset_uid ?? []) query.append("asset_uid", String(item)) for (const item of args.created_at ?? []) query.append("created_at", String(item)) for (const item of args.modified_at ?? []) query.append("modified_at", String(item)) for (const item of args.closed_at ?? []) query.append("closed_at", String(item)) for (const item of args.scheduled_for ?? []) query.append("scheduled_for", String(item)) for (const item of args.due_date ?? []) query.append("due_date", String(item)) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders${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(getApiOrdersResponse, await res.json()) };