import {z} from 'zod' export type OnCallPagesSort = 'created_at' | '-created_at' | 'modified_at' | '-modified_at' | 'status' | '-status' | 'priority' | '-priority'; export type GetApiApiV2OnCallPagesArgs = {'page[size]'?: number | undefined, 'page[number]'?: number | undefined, 'filter[query]'?: string | undefined, sort?: OnCallPagesSort | undefined}; export type GetApiApiV2OnCallPagesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const onCallPageLiveCallOutcome = z.enum(["success_call_answered", "failure_no_responder", "failure_caller_abandoned", "failure_outside_support_hours", "failure_internal_error"]); export const onCallPageLiveCallOver = z.object({outcome: onCallPageLiveCallOutcome.optional(), support_hours_start_at: z.string().optional()}); export const onCallPageLiveCall = z.object({call_over: onCallPageLiveCallOver.optional()}); export const onCallPageReleaseOrigin = z.enum(["ack_timeout", "team_rules_active_hours", "manual_snooze"]); export const onCallPageStatus = z.enum(["triggered", "acknowledged", "resolved"]); export const pageUrgency = z.enum(["low", "high"]); export const onCallPageDataAttributes = z.object({acknowledged_at: z.string().optional(), closed_at: z.string().optional(), comment_count: z.number().int().optional(), created_at: z.string(), description: z.string(), escalation_policy_uuid: z.string().optional(), key: z.string(), live_call: onCallPageLiveCall.optional(), modified_at: z.string().optional(), release_at: z.string().optional(), release_origin: onCallPageReleaseOrigin.optional(), responders_added_at: z.string().optional(), services: z.array(z.string()).optional(), status: onCallPageStatus, tags: z.array(z.string()), title: z.string(), urgency: pageUrgency}); export const onCallPageAuthorRelationshipData = z.object({id: z.string(), type: z.string()}).nullable(); export const onCallPageAuthorRelationship = z.object({data: onCallPageAuthorRelationshipData.optional()}); export const onCallPageJiraIssueType = z.literal("jira_issues"); export const onCallPageJiraIssueRelationshipData = z.object({id: z.string(), type: onCallPageJiraIssueType}); export const onCallPageJiraIssueRelationship = z.object({data: onCallPageJiraIssueRelationshipData.optional()}); export const onCallPageLinkType = z.literal("links"); export const onCallPageLinkRelationshipData = z.object({id: z.string(), type: onCallPageLinkType}); export const onCallPageLinksRelationship = z.object({data: z.array(onCallPageLinkRelationshipData).optional()}); export const scheduleUserType = z.literal("users"); export const onCallPageUserRelationshipData = z.object({id: z.string(), type: scheduleUserType}); export const onCallPageRespondersRelationship = z.object({data: z.array(onCallPageUserRelationshipData).optional()}); export const onCallPageScheduledActionType = z.literal("scheduled_actions"); export const onCallPageScheduledActionRelationshipData = z.object({id: z.string(), type: onCallPageScheduledActionType}).nullable(); export const onCallPageScheduledActionRelationship = z.object({data: onCallPageScheduledActionRelationshipData.optional()}); export const onCallPageServiceNowIncidentType = z.literal("service_now_incidents"); export const onCallPageServiceNowIncidentRelationshipData = z.object({id: z.string(), type: onCallPageServiceNowIncidentType}); export const onCallPageServiceNowIncidentRelationship = z.object({data: onCallPageServiceNowIncidentRelationshipData.optional()}); export const onCallPageTargetRelationshipDataType = z.enum(["teams", "users", "cross_org_handles"]); export const onCallPageTargetRelationshipData = z.object({id: z.string(), type: onCallPageTargetRelationshipDataType}).nullable(); export const onCallPageTargetRelationship = z.object({data: onCallPageTargetRelationshipData.optional()}); export const teamRoutingRulesDataRelationshipsRulesDataItemsType = z.literal("team_routing_rules"); export const onCallPageTeamRoutingRuleRelationshipData = z.object({id: z.string(), type: teamRoutingRulesDataRelationshipsRulesDataItemsType}); export const onCallPageTeamRoutingRuleRelationship = z.object({data: onCallPageTeamRoutingRuleRelationshipData.optional()}); export const onCallPageDataRelationships = z.object({author: onCallPageAuthorRelationship.optional(), closed_by: onCallPageAuthorRelationship.optional(), jira_issue: onCallPageJiraIssueRelationship.optional(), links: onCallPageLinksRelationship.optional(), responders: onCallPageRespondersRelationship.optional(), scheduled_action: onCallPageScheduledActionRelationship.optional(), service_now_incident: onCallPageServiceNowIncidentRelationship.optional(), target: onCallPageTargetRelationship.optional(), team_routing_rule: onCallPageTeamRoutingRuleRelationship.optional()}); export const onCallPageDataType = z.literal("pages"); export const onCallPageData = z.object({attributes: onCallPageDataAttributes.optional(), id: z.string(), relationships: onCallPageDataRelationships.optional(), type: onCallPageDataType}); export const userAttributesStatus = z.enum(["active", "deactivated", "pending"]); export const scheduleUserAttributes = z.object({color: z.string().optional(), email: z.string().optional(), name: z.string().optional(), status: userAttributesStatus.optional()}); export const scheduleUser = z.object({attributes: scheduleUserAttributes.optional(), id: z.string(), type: scheduleUserType}); export const teamReferenceAttributes = z.object({avatar: z.string().optional(), description: z.string().optional(), handle: z.string().optional(), name: z.string().optional()}); export const teamReferenceType = z.literal("teams"); export const teamReference = z.object({attributes: teamReferenceAttributes.optional(), id: z.string().optional(), type: teamReferenceType}); export const onCallPageScheduledActionAttributesType = z.enum(["escalate", "resolve", "release", "retrigger"]); export const onCallPageScheduledActionAttributes = z.object({at: z.string().optional(), type: onCallPageScheduledActionAttributesType.optional()}); export const scheduleDataType = z.literal("schedules"); export const onCallPageScheduledActionRelationshipsSchedulesData = z.object({id: z.string(), type: scheduleDataType}); export const onCallPageScheduledActionRelationshipsSchedules = z.object({data: z.array(onCallPageScheduledActionRelationshipsSchedulesData).optional()}); export const onCallPageScheduledActionRelationshipsUsers = z.object({data: z.array(onCallPageUserRelationshipData).optional()}); export const onCallPageScheduledActionRelationships = z.object({schedules: onCallPageScheduledActionRelationshipsSchedules.optional(), users: onCallPageScheduledActionRelationshipsUsers.optional()}); export const onCallPageScheduledAction = z.object({attributes: onCallPageScheduledActionAttributes.optional(), id: z.string(), relationships: onCallPageScheduledActionRelationships.optional(), type: onCallPageScheduledActionType}); export const onCallPageLinkEntityType = z.enum(["case", "incident", "page", "agent_conversation"]); export const onCallPageLinkRelationshipType = z.enum(["relates_to", "causes", "blocks", "duplicates", "parent_of", "successor_of", "escalates_to", "pages"]); export const onCallPageLinkAttributes = z.object({child_entity_id: z.string(), child_entity_type: onCallPageLinkEntityType, parent_entity_id: z.string(), parent_entity_type: onCallPageLinkEntityType, relationship: onCallPageLinkRelationshipType}); export const onCallPageLink = z.object({attributes: onCallPageLinkAttributes.optional(), id: z.string(), type: onCallPageLinkType}); export const onCallPagesIncluded = z.union([scheduleUser, teamReference, onCallPageScheduledAction, onCallPageLink]); export const schedulesResponseMetaPage = z.object({first_number: z.number().int().optional(), last_number: z.number().int().optional(), next_number: z.number().int().nullable().optional(), number: z.number().int().optional(), prev_number: z.number().int().nullable().optional(), size: z.number().int().optional(), total: z.number().int().optional(), type: z.string().optional()}); export const schedulesResponseMeta = z.object({page: schedulesResponseMetaPage.optional()}); export const onCallPagesResponse = z.object({data: z.array(onCallPageData), included: z.array(onCallPagesIncluded).optional(), meta: schedulesResponseMeta.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 }; /** * List On-Call Pages * * GET /api/v2/on-call/pages * * Also accepts auth: AuthZ */ export const getApiApiV2OnCallPages = async (args: GetApiApiV2OnCallPagesArgs, options?: GetApiApiV2OnCallPagesOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['filter[query]'] != null) query.append("filter[query]", String(args['filter[query]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/on-call/pages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(onCallPagesResponse, await res.json()) };