import {z} from 'zod' export type CustomFieldsV1UpdateRequestBody = {description: string, name: string, required?: ('never' | 'before_closure' | 'always') | undefined, required_v2?: ('never' | 'before_resolution' | 'always') | undefined, show_before_closure: boolean, show_before_creation: boolean, show_before_update: boolean, show_in_announcement_post?: boolean | undefined}; export type UpdateApiV1CustomFieldsIdArgs = {id: string, body: CustomFieldsV1UpdateRequestBody}; export type UpdateApiV1CustomFieldsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const customFieldOptionV1 = z.object({custom_field_id: z.string(), id: z.string(), sort_key: z.number().int(), value: z.string()}); export const customFieldV1 = z.object({catalog_type_id: z.string().optional(), created_at: z.string(), description: z.string(), field_type: z.enum(["single_select", "multi_select", "text", "link", "numeric"]), id: z.string(), name: z.string().max(50), options: z.array(customFieldOptionV1), required: z.enum(["never", "before_closure", "always"]).optional(), required_v2: z.enum(["never", "before_resolution", "always"]).optional(), show_before_closure: z.boolean(), show_before_creation: z.boolean(), show_before_update: z.boolean(), show_in_announcement_post: z.boolean().optional(), updated_at: z.string()}); export const customFieldsV1UpdateResponseBody = z.object({custom_field: customFieldV1}); 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 Custom Fields V1 * * PUT /v1/custom_fields/{id} */ export const updateApiV1CustomFieldsId = async (args: UpdateApiV1CustomFieldsIdArgs, options?: UpdateApiV1CustomFieldsIdOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/custom_fields/${encodeURIComponent(String(args.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(customFieldsV1UpdateResponseBody, await res.json()) };