import {z} from 'zod' export type GetApiApiV2OnCallSchedulesScheduleIdOverridesArgs = {schedule_id: string, 'filter[start]': string, 'filter[end]': string, include?: string | undefined, 'page[size]'?: number | undefined, 'page[number]'?: number | undefined}; export type GetApiApiV2OnCallSchedulesScheduleIdOverridesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const overrideAttributes = z.object({end: z.string().optional(), inactive: z.boolean().optional(), start: z.string().optional()}); export const overrideRelationshipsUserDataType = z.literal("users"); export const overrideRelationshipsUserData = z.object({id: z.string(), type: overrideRelationshipsUserDataType}); export const overrideRelationshipsUser = z.object({data: overrideRelationshipsUserData}); export const overrideRelationshipsScheduleDataType = z.literal("schedules"); export const overrideRelationshipsScheduleData = z.object({id: z.string(), type: overrideRelationshipsScheduleDataType}); export const overrideRelationshipsSchedule = z.object({data: overrideRelationshipsScheduleData}); export const overrideRelationships = z.object({overridden_user: overrideRelationshipsUser.optional(), schedule: overrideRelationshipsSchedule.optional(), user: overrideRelationshipsUser.optional()}); export const overrideDataType = z.literal("overrides"); export const overrideData = z.object({attributes: overrideAttributes.optional(), id: z.string(), relationships: overrideRelationships.optional(), type: overrideDataType}); 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 scheduleUserType = z.literal("users"); export const overrideIncluded = z.object({attributes: scheduleUserAttributes.optional(), id: z.string(), type: scheduleUserType}); export const overrides = z.object({data: z.array(overrideData), included: z.array(overrideIncluded).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 schedule overrides * * GET /api/v2/on-call/schedules/{schedule_id}/overrides * * Also accepts auth: AuthZ */ export const getApiApiV2OnCallSchedulesScheduleIdOverrides = async (args: GetApiApiV2OnCallSchedulesScheduleIdOverridesArgs, options?: GetApiApiV2OnCallSchedulesScheduleIdOverridesOptions) => { 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['filter[start]'] != null) query.append("filter[start]", String(args['filter[start]'])) if (args['filter[end]'] != null) query.append("filter[end]", String(args['filter[end]'])) if (args.include != null) query.append("include", String(args.include)) 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 (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/${encodeURIComponent(String(args.schedule_id))}/overrides${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(overrides, await res.json()) };