import {z} from 'zod' export type GetApiXWebhooksAllOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookPrivateResourceV2 = z.object({id: z.string()}); export const webhookIncidentUserV2 = z.object({actor_user_id: z.string().optional(), incident_id: z.string(), user_id: z.string()}); export const userV1 = z.object({email: z.string().optional(), id: z.string(), name: z.string(), role: z.enum(["viewer", "responder", "administrator", "owner", "unset"]), slack_user_id: z.string().optional()}); export const externalIssueReferenceV1 = z.object({issue_name: z.string().optional(), issue_permalink: z.string().optional(), provider: z.enum(["asana", "click_up", "linear", "jira", "jira_server", "github", "gitlab", "shortcut"]).optional()}); export const actionV1 = z.object({assignee: userV1.optional(), completed_at: z.string().optional(), created_at: z.string(), description: z.string().optional(), external_issue_reference: externalIssueReferenceV1.optional(), follow_up: z.boolean(), id: z.string(), incident_id: z.string(), status: z.enum(["outstanding", "completed", "deleted", "not_doing"]), updated_at: z.string()}); export const aPIKeyV2 = z.object({id: z.string(), name: z.string()}); export const userV2 = z.object({email: z.string().optional(), id: z.string(), name: z.string(), role: z.enum(["viewer", "responder", "administrator", "owner", "unset"]), slack_user_id: z.string().optional()}); export const actorV2 = z.object({api_key: aPIKeyV2.optional(), user: userV2.optional()}); export const customFieldOptionV2 = z.object({custom_field_id: z.string(), id: z.string(), sort_key: z.number().int(), value: z.string()}); export const customFieldTypeInfoV2 = z.object({description: z.string(), field_type: z.enum(["single_select", "multi_select", "text", "link", "numeric"]), id: z.string(), name: z.string().max(50), options: z.array(customFieldOptionV2)}); export const embeddedCatalogEntryV2 = z.object({aliases: z.array(z.string()).optional(), external_id: z.string().optional(), id: z.string(), name: z.string()}); export const customFieldValueV2 = z.object({value_catalog_entry: embeddedCatalogEntryV2.optional(), value_link: z.string().optional(), value_numeric: z.string().optional(), value_option: customFieldOptionV2.optional(), value_text: z.string().optional()}); export const customFieldEntryV2 = z.object({custom_field: customFieldTypeInfoV2, values: z.array(customFieldValueV2)}); export const incidentDurationMetricV2 = z.object({id: z.string(), name: z.string()}); export const incidentDurationMetricWithValueV2 = z.object({duration_metric: incidentDurationMetricV2, value_seconds: z.number().int().optional()}); export const externalIssueReferenceV2 = z.object({issue_name: z.string(), issue_permalink: z.string(), provider: z.enum(["asana", "click_up", "linear", "jira", "jira_server", "github", "gitlab", "shortcut"])}); export const embeddedIncidentRoleV2 = z.object({created_at: z.string(), description: z.string().min(1), id: z.string(), instructions: z.string(), name: z.string().min(1), required: z.boolean().optional(), role_type: z.enum(["lead", "reporter", "custom"]), shortform: z.string(), updated_at: z.string()}); export const incidentRoleAssignmentV2 = z.object({assignee: userV2.optional(), role: embeddedIncidentRoleV2}); export const incidentStatusV2 = z.object({category: z.enum(["triage", "declined", "merged", "canceled", "live", "learning", "closed", "paused"]), created_at: z.string(), description: z.string(), id: z.string(), name: z.string(), rank: z.number().int(), updated_at: z.string()}); export const incidentTimestampV2 = z.object({id: z.string(), name: z.string(), rank: z.number().int()}); export const incidentTimestampValueV2 = z.object({value: z.string().optional()}); export const incidentTimestampWithValueV2 = z.object({incident_timestamp: incidentTimestampV2, value: incidentTimestampValueV2.optional()}); export const incidentTypeV2 = z.object({create_in_triage: z.enum(["always", "optional"]), created_at: z.string(), description: z.string(), id: z.string(), is_default: z.boolean(), name: z.string(), private_incidents_only: z.boolean(), updated_at: z.string()}); export const severityV2 = z.object({created_at: z.string(), description: z.string(), id: z.string(), name: z.string().max(50), rank: z.number().int(), updated_at: z.string()}); export const webhookIncidentV2 = z.object({call_url: z.string().optional(), created_at: z.string(), creator: actorV2, custom_field_entries: z.array(customFieldEntryV2), duration_metrics: z.array(incidentDurationMetricWithValueV2).optional(), external_issue_reference: externalIssueReferenceV2.optional(), has_debrief: z.boolean().optional(), id: z.string(), incident_role_assignments: z.array(incidentRoleAssignmentV2), incident_status: incidentStatusV2, incident_timestamp_values: z.array(incidentTimestampWithValueV2).optional(), incident_type: incidentTypeV2.optional(), mode: z.enum(["standard", "retrospective", "test", "tutorial"]), most_recent_update_message: z.string().optional(), name: z.string(), permalink: z.string().optional(), postmortem_document_url: z.string().optional(), reference: z.string(), related_incidents: z.array(z.string()).optional(), severity: severityV2.optional(), slack_channel_id: z.string(), slack_channel_name: z.string().optional(), slack_team_id: z.string(), summary: z.string().optional(), updated_at: z.string(), visibility: z.enum(["public", "private"]), workload_minutes_late: z.number().optional(), workload_minutes_sleeping: z.number().optional(), workload_minutes_total: z.number().optional(), workload_minutes_working: z.number().optional()}); export const incidentV2 = z.object({call_url: z.string().optional(), created_at: z.string(), creator: actorV2, custom_field_entries: z.array(customFieldEntryV2), duration_metrics: z.array(incidentDurationMetricWithValueV2).optional(), external_issue_reference: externalIssueReferenceV2.optional(), has_debrief: z.boolean().optional(), id: z.string(), incident_role_assignments: z.array(incidentRoleAssignmentV2), incident_status: incidentStatusV2, incident_timestamp_values: z.array(incidentTimestampWithValueV2).optional(), incident_type: incidentTypeV2.optional(), mode: z.enum(["standard", "retrospective", "test", "tutorial"]), name: z.string(), permalink: z.string().optional(), postmortem_document_url: z.string().optional(), reference: z.string(), severity: severityV2.optional(), slack_channel_id: z.string(), slack_channel_name: z.string().optional(), slack_team_id: z.string(), summary: z.string().optional(), updated_at: z.string(), visibility: z.enum(["public", "private"]), workload_minutes_late: z.number().optional(), workload_minutes_sleeping: z.number().optional(), workload_minutes_total: z.number().optional(), workload_minutes_working: z.number().optional()}); export const incidentWithStatusChangeV2 = z.object({incident: incidentV2, message: z.string().optional(), new_status: incidentStatusV2, previous_status: incidentStatusV2}); export const webhooksAllResponseBody = z.object({event_type: z.enum(["public_incident.incident_created_v2", "private_incident.incident_created_v2", "public_incident.incident_updated_v2", "private_incident.incident_updated_v2", "public_incident.incident_status_updated_v2", "public_incident.follow_up_created_v1", "private_incident.follow_up_created_v1", "public_incident.follow_up_updated_v1", "private_incident.follow_up_updated_v1", "public_incident.action_created_v1", "private_incident.action_created_v1", "public_incident.action_updated_v1", "private_incident.action_updated_v1", "private_incident.membership_granted_v1", "private_incident.membership_revoked_v1"]), "private_incident.action_created_v1": webhookPrivateResourceV2.optional(), "private_incident.action_updated_v1": webhookPrivateResourceV2.optional(), "private_incident.follow_up_created_v1": webhookPrivateResourceV2.optional(), "private_incident.follow_up_updated_v1": webhookPrivateResourceV2.optional(), "private_incident.incident_created_v2": webhookPrivateResourceV2.optional(), "private_incident.incident_updated_v2": webhookPrivateResourceV2.optional(), "private_incident.membership_granted_v1": webhookIncidentUserV2.optional(), "private_incident.membership_revoked_v1": webhookIncidentUserV2.optional(), "public_incident.action_created_v1": actionV1.optional(), "public_incident.action_updated_v1": actionV1.optional(), "public_incident.follow_up_created_v1": actionV1.optional(), "public_incident.follow_up_updated_v1": actionV1.optional(), "public_incident.incident_created_v2": webhookIncidentV2.optional(), "public_incident.incident_status_updated_v2": incidentWithStatusChangeV2.optional(), "public_incident.incident_updated_v2": webhookIncidentV2.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 }; /** * All Webhooks * * GET /x-webhooks/__all__ */ export const getApiXWebhooksAll = async (options?: GetApiXWebhooksAllOptions) => { const {baseUrl = "https:////api.incident.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/x-webhooks/__all__${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(webhooksAllResponseBody, await res.json()) };