import {z} from 'zod' export type IntegrationServicenow = {pk?: number | undefined, url?: string | undefined, name: string, module?: string | undefined, contact_groups?: Array | undefined, is_errored?: boolean | undefined, last_error?: string | undefined, instance_url?: string | undefined, username?: string | undefined, password?: string | undefined, urgency?: ('' | '1' | '2' | '3') | undefined, impact?: ('' | '1' | '2' | '3') | undefined, assignment_group?: string | undefined, close_code?: string | undefined, autoresolve?: boolean | undefined, include_payload?: boolean | undefined, custom_fields_json?: string | undefined}; export type CreateApiApiV1IntegrationsAddServicenowArgs = {body: IntegrationServicenow}; export type CreateApiApiV1IntegrationsAddServicenowOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const integrationServicenow = z.object({pk: z.number().int().optional(), url: z.string().optional(), name: z.string().max(256), module: z.string().optional(), contact_groups: z.array(z.string()).optional(), is_errored: z.boolean().optional(), last_error: z.string().optional(), instance_url: z.string().optional(), username: z.string().optional(), password: z.string().optional(), urgency: z.enum(["", "1", "2", "3"]).optional(), impact: z.enum(["", "1", "2", "3"]).optional(), assignment_group: z.string().optional(), close_code: z.string().optional(), autoresolve: z.boolean().optional(), include_payload: z.boolean().optional(), custom_fields_json: 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 }; /** * Create a new ServiceNow integration * * POST /api/v1/integrations/add-servicenow/ */ export const createApiApiV1IntegrationsAddServicenow = async (args: CreateApiApiV1IntegrationsAddServicenowArgs, options: CreateApiApiV1IntegrationsAddServicenowOptions) => { const {baseUrl, 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(/\/+$/, '')}/api/v1/integrations/add-servicenow/${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(integrationServicenow, await res.json()) };