import {z} from 'zod' export type CreateOverrideRequest = {covering_for?: (string | null) | undefined, end_at: number, reason?: (string | null) | undefined, rotation_id?: (string | null) | undefined, start_at: number, user_email: string}; export type CreateApiApiOrgIdOncallTeamsTeamIdOverridesArgs = {org_id: string, team_id: string, body: CreateOverrideRequest}; export type CreateApiApiOrgIdOncallTeamsTeamIdOverridesOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleOverride = z.object({covering_for: z.string().nullable().optional(), created_at: z.number().int(), created_by: z.string(), end_at: z.number().int(), id: z.string(), org_id: z.string(), reason: z.string().nullable().optional(), rotation_id: z.string(), start_at: z.number().int(), team_id: z.string(), user_email: z.string()}); 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 }; /** * Arrange cover for a bounded window * * POST /api/{org_id}/oncall/teams/{team_id}/overrides */ export const createApiApiOrgIdOncallTeamsTeamIdOverrides = async (args: CreateApiApiOrgIdOncallTeamsTeamIdOverridesArgs, options: CreateApiApiOrgIdOncallTeamsTeamIdOverridesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/oncall/teams/${encodeURIComponent(String(args.team_id))}/overrides${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(scheduleOverride, await res.json()) };