import {z} from 'zod' export type GetApiV1FieldsFieldIdValuesArgs = {field_id: string, 'brex_id[]'?: (Array | null) | undefined, 'value_id[]'?: (Array | null) | undefined, 'remote_id[]'?: (Array | null) | undefined, cursor?: (string | null) | undefined, limit?: (number | null) | undefined, value?: (string | null) | undefined}; export type GetApiV1FieldsFieldIdValuesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fieldValue = z.object({brex_id: z.string(), remote_id: z.string().nullable().optional(), value_id: z.string().nullable().optional(), value: z.string(), field_id: z.string(), updated_at: z.string(), is_disabled: z.boolean()}); export const pageFieldValue = z.object({next_cursor: z.string().nullable().optional(), items: z.array(fieldValue)}); 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 field values * * GET /v1/fields/{field_id}/values */ export const getApiV1FieldsFieldIdValues = async (args: GetApiV1FieldsFieldIdValuesArgs, options?: GetApiV1FieldsFieldIdValuesOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['brex_id[]'] ?? []) query.append("brex_id[]", String(item)) for (const item of args['value_id[]'] ?? []) query.append("value_id[]", String(item)) for (const item of args['remote_id[]'] ?? []) query.append("remote_id[]", String(item)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.value != null) query.append("value", String(args.value)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fields/${encodeURIComponent(String(args.field_id))}/values${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(pageFieldValue, await res.json()) };