import {z} from 'zod' export type CreateApiMaintenanceHistoryCreateArgs = {'x-api-id': string, 'x-api-key': string, body: {statuspage_id: string, all_infrastructure_affected: string, infrastructure_affected: Array, maintenance_name: string, maintenance_details_schedule: string, maintenance_details_start: string, maintenance_details_end: string, maintenance_start_date: string, maintenance_start_time: string, maintenance_end_date: string, maintenance_end_time: string}}; export type CreateApiMaintenanceHistoryCreateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMaintenanceHistoryCreateResponse = 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 }; /** * Create Historic Maintenance * * POST /maintenance/history/create */ export const createApiMaintenanceHistoryCreate = async (args: CreateApiMaintenanceHistoryCreateArgs, options?: CreateApiMaintenanceHistoryCreateOptions) => { 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/history/create${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(createApiMaintenanceHistoryCreateResponse, await res.json()) };