import {z} from 'zod' export type FieldFormat = {format?: ('number' | 'currency' | 'percent' | 'label') | undefined, type?: ('date' | 'dob') | undefined, decimals?: number | undefined, label?: string | undefined, labelAt?: ('left' | 'right') | undefined}; export type FieldOption = {id?: number | undefined, name?: string | undefined, color?: string | undefined}; export type FieldRequestUpdateRequest = {name?: string | undefined, type?: ('number' | 'text' | 'select' | 'date') | undefined, format?: FieldFormat | undefined, icon?: string | undefined, options?: Array | undefined}; export type UpdateApiFieldsFieldIdArgs = {field_id: number, body: FieldRequestUpdateRequest}; export type UpdateApiFieldsFieldIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fieldFormat = z.object({format: z.enum(["number", "currency", "percent", "label"]).optional(), type: z.enum(["date", "dob"]).optional(), decimals: z.number().optional(), label: z.string().optional(), labelAt: z.enum(["left", "right"]).optional()}); export const fieldOption = z.object({id: z.number().optional(), name: z.string().optional(), color: z.string().optional()}); export const field = z.object({id: z.number().optional(), name: z.string().optional(), type: z.string().optional(), format: fieldFormat.optional(), options: z.array(fieldOption).optional(), position: z.number().optional(), icon: 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 }; /** * Update Field * * PUT /fields/{field_id} */ export const updateApiFieldsFieldId = async (args: UpdateApiFieldsFieldIdArgs, options?: UpdateApiFieldsFieldIdOptions) => { const {baseUrl = "https://api.everhour.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/fields/${encodeURIComponent(String(args.field_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(field, await res.json()) };