import {z} from 'zod' export type GetApiApiV2OnCallSchedulesArgs = {'page[size]'?: number | undefined, 'page[number]'?: number | undefined, 'filter[query]'?: string | undefined, include?: string | undefined}; export type GetApiApiV2OnCallSchedulesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleDataAttributes = z.object({max_responders: z.number().int().gte(1).lte(10).optional(), name: z.string().optional(), tags: z.array(z.string()).optional(), time_zone: z.string().optional()}); export const dataRelationshipsTeamsDataItemsType = z.literal("teams"); export const dataRelationshipsTeamsDataItems = z.object({id: z.string(), type: dataRelationshipsTeamsDataItemsType}); export const dataRelationshipsTeams = z.object({data: z.array(dataRelationshipsTeamsDataItems).optional()}); export const scheduleListItemRelationships = z.object({teams: dataRelationshipsTeams.optional()}); export const scheduleDataType = z.literal("schedules"); export const scheduleListItem = z.object({attributes: scheduleDataAttributes.optional(), id: z.string(), relationships: scheduleListItemRelationships.optional(), type: scheduleDataType}); 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 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 schedules = z.object({data: z.array(scheduleListItem).optional(), included: z.array(teamReference).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 schedules * * GET /api/v2/on-call/schedules * * Also accepts auth: AuthZ */ export const getApiApiV2OnCallSchedules = async (args: GetApiApiV2OnCallSchedulesArgs, options?: GetApiApiV2OnCallSchedulesOptions) => { 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.include != null) query.append("include", String(args.include)) 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/schedules${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(schedules, await res.json()) };