import {z} from 'zod' export type CreateApiMaintenanceScheduleArgs = {'x-api-id': string, 'x-api-key': string, body: {statuspage_id: string, all_infrastructure_affected: string, infrastructure_affected: Array, maintenance_name: string, maintenance_details: string, date_planned_start: string, time_planned_start: string, date_planned_end: string, time_planned_end: string, maintenance_notify_now: string, maintenance_notify_72_hr: string, maintenance_notify_24_hr: string, maintenance_notify_1_hr: string, message_subject?: string | undefined, automation: string, automation_start_text?: string | undefined, automation_stop_text?: string | undefined, automation_notify_start_email?: string | undefined, automation_notify_start_sms?: string | undefined, automation_notify_start_webhook?: string | undefined, automation_notify_start_twitter?: string | undefined, automation_notify_start_irc?: string | undefined, automation_notify_start_msteams?: string | undefined, automation_notify_start_slack?: string | undefined, automation_notify_stop_email?: string | undefined, automation_notify_stop_sms?: string | undefined, automation_notify_stop_webhook?: string | undefined, automation_notify_stop_twitter?: string | undefined, automation_notify_stop_irc?: string | undefined, automation_notify_stop_msteams?: string | undefined, automation_notify_stop_slack?: string | undefined}}; export type CreateApiMaintenanceScheduleOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMaintenanceScheduleResponse = z.object({result: z.string().optional(), status: z.object({error: z.string().optional(), message: z.string().optional()}).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 }; /** * Schedule Maintenance * * POST /maintenance/schedule */ export const createApiMaintenanceSchedule = async (args: CreateApiMaintenanceScheduleArgs, options?: CreateApiMaintenanceScheduleOptions) => { const {baseUrl = "https://api.status.io/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-id'] != null) headers.set("x-api-id", String(args['x-api-id'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/maintenance/schedule${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(createApiMaintenanceScheduleResponse, await res.json()) };