import {z} from 'zod' export type GetApiV1BusinessesClientIdCustomFieldsArgs = {client_id: number, offset?: number | undefined, limit?: number | undefined}; export type GetApiV1BusinessesClientIdCustomFieldsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const customField = z.object({id: z.string().optional(), name: z.string().optional(), field_type: z.enum(["LIST", "FREE_TEXT"]).optional(), budget_setting: z.enum(["NONE", "SOME", "ALL_CURRENT_AND_NEW"]).optional(), is_manager_only: z.boolean().optional(), created_by: z.enum(["MANUAL", "PUBLIC_API", "XERO", "MYOB", "QUICKBOOKS", "NETSUITE", "BUSINESS_CENTRAL"]).optional(), created: z.string().optional(), updated: z.string().optional()}); export const customFieldListResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(customField).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 custom fields * * GET /v1/businesses/{client_id}/custom-fields */ export const getApiV1BusinessesClientIdCustomFields = async (args: GetApiV1BusinessesClientIdCustomFieldsArgs, options?: GetApiV1BusinessesClientIdCustomFieldsOptions) => { const {baseUrl = "https://public.letsweel.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses/${encodeURIComponent(String(args.client_id))}/custom-fields${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(customFieldListResponse, await res.json()) };