import {z} from 'zod' export type UpdateApiApiIntegrationsIdArgs = {id: number, body: {deactivated_datetime?: (string | null) | undefined, description?: (string | null) | undefined, http?: ({form: unknown, headers: unknown, hmac_secret?: string | undefined, method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD' | 'PATCH') | undefined, request_content_type?: ('application/json' | 'application/x-www-form-urlencoded') | undefined, response_content_type?: 'application/json' | undefined, triggers?: ({'ticket-created'?: boolean | undefined, 'ticket-updated'?: boolean | undefined, 'ticket-message-created'?: boolean | undefined, 'ticket-self-unsnoozed'?: boolean | undefined, 'ticket-message-failed'?: boolean | undefined, 'ticket-assignment-updated'?: boolean | undefined, 'ticket-status-updated'?: boolean | undefined, 'ticket-handed-over'?: boolean | undefined}) | undefined, url: string}) | undefined, name: string}}; export type UpdateApiApiIntegrationsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const integration = z.object({id: z.number().int().optional(), created_datetime: z.string().nullable().optional(), deactivated_datetime: z.string().nullable().optional(), description: z.string().nullable().optional(), http: z.object({form: z.unknown(), method: z.enum(["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"]).optional(), request_content_type: z.enum(["application/json", "application/x-www-form-urlencoded"]).optional(), response_content_type: z.literal("application/json").optional(), triggers: z.object({"ticket-created": z.boolean().optional(), "ticket-updated": z.boolean().optional(), "ticket-message-created": z.boolean().optional(), "ticket-self-unsnoozed": z.boolean().optional(), "ticket-message-failed": z.boolean().optional(), "ticket-assignment-updated": z.boolean().optional(), "ticket-status-updated": z.boolean().optional(), "ticket-handed-over": z.boolean().optional()}).optional(), url: z.string(), oauth2: z.unknown()}).optional(), name: z.string(), type: z.literal("http").optional(), updated_datetime: z.string().nullable().optional(), user: z.object({id: z.number().int().optional()}).optional(), uri: z.string().optional(), application_id: z.string().nullable().optional(), managed: z.boolean().optional(), business_hours_id: z.number().int().nullable().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 an integration * * PUT /api/integrations/{id} */ export const updateApiApiIntegrationsId = async (args: UpdateApiApiIntegrationsIdArgs, options?: UpdateApiApiIntegrationsIdOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/integrations/${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(integration, await res.json()) };