import {z} from 'zod' export type IncidentPageRoleType = 'incident_user_defined_roles' | 'incident_reserved_roles'; export type IncidentPageRoleReference = {id: string, type: IncidentPageRoleType}; export type IncidentPageTargetType = 'team_handle' | 'team_uuid' | 'user_uuid'; export type IncidentPageTarget = {identifier: string, type: IncidentPageTargetType}; export type IncidentCreatePageFromIncidentDataAttributesRequest = {description?: string | undefined, incident_public_id?: string | undefined, role?: IncidentPageRoleReference | undefined, services?: Array | undefined, tags?: Array | undefined, target?: IncidentPageTarget | undefined, title?: string | undefined}; export type IncidentCreatePageFromIncidentType = 'page'; export type IncidentCreatePageFromIncidentDataRequest = {attributes: IncidentCreatePageFromIncidentDataAttributesRequest, type: IncidentCreatePageFromIncidentType}; export type IncidentCreatePageFromIncidentRequest = {data: IncidentCreatePageFromIncidentDataRequest}; export type CreateApiApiV2IncidentsIncidentIdCasesPageArgs = {incident_id: string, body: IncidentCreatePageFromIncidentRequest}; export type CreateApiApiV2IncidentsIncidentIdCasesPageOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentPageUUIDType = z.literal("page_uuid"); export const incidentPageUUIDDataResponse = z.object({id: z.string(), type: incidentPageUUIDType}); export const incidentPageUUIDResponse = z.object({data: incidentPageUUIDDataResponse}); 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 page from an incident * * POST /api/v2/incidents/{incident_id}/cases/page * * Also accepts auth: AuthZ */ export const createApiApiV2IncidentsIncidentIdCasesPage = async (args: CreateApiApiV2IncidentsIncidentIdCasesPageArgs, options?: CreateApiApiV2IncidentsIncidentIdCasesPageOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/incidents/${encodeURIComponent(String(args.incident_id))}/cases/page${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(incidentPageUUIDResponse, await res.json()) };