import {z} from 'zod' export type CreateIntegrationPayload = {config?: (Record | null) | undefined, destinations?: Array | undefined, name: string, source_type?: (string | null) | undefined}; export type CreateApiApiV2OrgIdIncidentsIntegrationsArgs = {org_id: string, body: CreateIntegrationPayload}; export type CreateApiApiV2OrgIdIncidentsIntegrationsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const integrationResponse = z.object({config: z.object({}), created_at: z.number().int(), created_by: z.string(), destinations: z.array(z.string()), enabled: z.boolean(), id: z.string(), name: z.string(), org_id: z.string(), source_type: z.string(), token: z.string().nullable().optional(), updated_at: z.number().int(), url: z.string().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 }; /** * Create an incident integration * * POST /api/v2/{org_id}/incidents/integrations */ export const createApiApiV2OrgIdIncidentsIntegrations = async (args: CreateApiApiV2OrgIdIncidentsIntegrationsArgs, options: CreateApiApiV2OrgIdIncidentsIntegrationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/incidents/integrations${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(integrationResponse, await res.json()) };