import {z} from 'zod' export type MonitorMetricType = 'cpu_load' | 'disk' | 'free_memory' | 'used_memory'; export type MonitorOperator = 'gte' | 'lte'; export type CreateMonitorRequest = {type: MonitorMetricType, operator: MonitorOperator, threshold: number, minutes?: number | undefined, notify: string}; export type CreateApiOrgsOrganizationServersServerMonitorsArgs = {organization: string, server: number, body: CreateMonitorRequest}; export type CreateApiOrgsOrganizationServersServerMonitorsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const monitorMetricType = z.enum(["cpu_load", "disk", "free_memory", "used_memory"]); export const monitorOperator = z.enum(["gte", "lte"]); export const resourceState = z.enum(["installing", "installed", "removing", "restarting", "stopping", "stopped", "starting", "syncing", "updating", "disabling", "disabled", "enabling", "running", "restoring", "deleting", "failed", "success", "failed-unknown", "failed-runner", "renewing"]); export const monitorState = z.enum(["OK", "ALERT", "UNKNOWN"]); export const link = z.object({href: z.string(), rel: z.string().optional(), describedby: z.string().optional(), title: z.string().optional(), type: z.string().optional(), hreflang: z.union([z.string(), z.array(z.string())]).optional(), meta: z.object({}).optional()}); export const monitorResource = z.object({id: z.string(), type: z.literal("monitors"), attributes: z.object({type: monitorMetricType, operator: monitorOperator, threshold: z.number(), minutes: z.number().int().nullable(), notify: z.string(), status: resourceState, state: monitorState, state_changed_at: z.string().nullable(), created_at: z.string(), updated_at: z.string()}).optional(), links: z.object({self: link})}); export const createApiOrgsOrganizationServersServerMonitorsResponse = z.object({data: monitorResource}); 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 server monitor * * POST /orgs/{organization}/servers/{server}/monitors */ export const createApiOrgsOrganizationServersServerMonitors = async (args: CreateApiOrgsOrganizationServersServerMonitorsArgs, options?: CreateApiOrgsOrganizationServersServerMonitorsOptions) => { const {baseUrl = "https://forge.laravel.com/api", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/${encodeURIComponent(String(args.organization))}/servers/${encodeURIComponent(String(args.server))}/monitors${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(createApiOrgsOrganizationServersServerMonitorsResponse, await res.json()) };