import {z} from 'zod' export type GetApiIncidentMessageStatuspageIdMessageIdArgs = {statuspage_id: string, message_id: string, 'x-api-id': string, 'x-api-key': string}; export type GetApiIncidentMessageStatuspageIdMessageIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiIncidentMessageStatuspageIdMessageIdResponse = z.object({result: z.object({components: z.array(z.string()).optional(), v: z.number().optional(), id: z.string().optional(), containers: z.array(z.string()).optional(), datetime: z.string().optional(), details: z.string().optional(), incident: z.string().optional(), ip: z.string().optional(), social: z.string().optional(), source: z.string().optional(), state: z.number().optional(), status: z.number().optional(), statuspage: z.string().optional(), user_email: z.string().optional(), user_full_name: z.string().optional()}).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 }; /** * Get Incident Message * * GET /incident/message/{statuspage_id}/{message_id} */ export const getApiIncidentMessageStatuspageIdMessageId = async (args: GetApiIncidentMessageStatuspageIdMessageIdArgs, options?: GetApiIncidentMessageStatuspageIdMessageIdOptions) => { const {baseUrl = "https://api.status.io/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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(/\/+$/, '')}/incident/message/${encodeURIComponent(String(args.statuspage_id))}/${encodeURIComponent(String(args.message_id))}${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(getApiIncidentMessageStatuspageIdMessageIdResponse, await res.json()) };