import {z} from 'zod' export type GetApiAccountsPublicArgs = {search?: string | undefined, page?: string | undefined, erp_code?: string | undefined, null_erp_code?: boolean | undefined}; export type GetApiAccountsPublicOptions = {baseUrl?: string | undefined, appId?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountsPublicResponse = z.array(z.object({name: z.string().optional(), erp_code: z.string().optional(), id: z.number().int().optional(), phone: z.string().optional(), phone_ext: z.string().optional(), type: z.string().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 }; /** * List accounts * * GET /accounts/public */ export const getApiAccountsPublic = async (args: GetApiAccountsPublicArgs, options?: GetApiAccountsPublicOptions) => { const {baseUrl = "https://api.paperlessparts.com/v1", fetch: fetchFn = fetch, appId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.page != null) query.append("page", String(args.page)) if (args.erp_code != null) query.append("erp_code", String(args.erp_code)) if (args.null_erp_code != null) query.append("null_erp_code", String(args.null_erp_code)) if (appId !== undefined) headers.set("Authorization", appId) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/public${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(getApiAccountsPublicResponse, await res.json()) };