import {z} from 'zod' export type CustomFieldOptionObject = {id?: number | undefined, name: string, position?: number | undefined, raw_name?: string | undefined, url?: string | undefined, value: string}; export type CustomObjectField = {active?: boolean | undefined, created_at?: string | undefined, custom_field_options?: Array | undefined, description?: string | undefined, id?: number | undefined, key: string, position?: number | undefined, raw_description?: string | undefined, raw_title?: string | undefined, regexp_for_validation?: (string | null) | undefined, relationship_filter?: Record | undefined, relationship_target_type?: string | undefined, system?: boolean | undefined, tag?: string | undefined, title: string, type: string, updated_at?: string | undefined, url?: string | undefined}; export type CustomObjectFieldsCreateRequest = {custom_object_field?: CustomObjectField | undefined}; export type CreateApiApiV2CustomObjectsCustomObjectKeyFieldsArgs = {custom_object_key: string, body: CustomObjectFieldsCreateRequest}; export type CreateApiApiV2CustomObjectsCustomObjectKeyFieldsOptions = {baseUrl?: string | undefined, apiTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const customFieldOptionObject = z.object({id: z.number().int().optional(), name: z.string(), position: z.number().int().optional(), raw_name: z.string().optional(), url: z.string().optional(), value: z.string()}); export const customObjectField = z.object({active: z.boolean().optional(), created_at: z.string().optional(), custom_field_options: z.array(customFieldOptionObject).optional(), description: z.string().optional(), id: z.number().int().optional(), key: z.string(), position: z.number().int().optional(), raw_description: z.string().optional(), raw_title: z.string().optional(), regexp_for_validation: z.string().nullable().optional(), relationship_filter: z.object({}).optional(), relationship_target_type: z.string().optional(), system: z.boolean().optional(), tag: z.string().optional(), title: z.string(), type: z.string(), updated_at: z.string().optional(), url: z.string().optional()}); export const customObjectFieldResponse = z.object({custom_object_field: customObjectField.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 }; /** * Create Custom Object Field * * POST /api/v2/custom_objects/{custom_object_key}/fields */ export const createApiApiV2CustomObjectsCustomObjectKeyFields = async (args: CreateApiApiV2CustomObjectsCustomObjectKeyFieldsArgs, options?: CreateApiApiV2CustomObjectsCustomObjectKeyFieldsOptions) => { const {baseUrl = "https://example.zendesk.com", fetch: fetchFn = fetch, apiTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiTokenAuth.username}:${apiTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/custom_objects/${encodeURIComponent(String(args.custom_object_key))}/fields${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(customObjectFieldResponse, await res.json()) };