import {z} from 'zod' export type GetApiV2AppsAppIdNetworkErrorsArgs = {appId: string, start: string, end: string, include_4xx?: ('true' | 'false') | undefined}; export type GetApiV2AppsAppIdNetworkErrorsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const networkErrorsTimeBucket = z.object({date: z.string(), buckets: z.record(z.string(), z.number().int()), total: z.number().int()}); export const networkErrorsTopPath = z.object({path: z.string(), method: z.string().nullable(), total: z.number().int(), by_status: z.record(z.string(), z.number().int())}); export const networkErrorsByMethod = z.object({method: z.string().nullable(), total: z.number().int(), by_status: z.record(z.string(), z.number().int())}); export const networkErrors = z.object({summary: z.object({total: z.number().int(), by_class: z.object({"4xx": z.number().int(), "5xx": z.number().int()})}), by_status: z.array(z.object({status: z.number().int(), requests: z.number().int()})), timeseries: z.array(networkErrorsTimeBucket), top_paths: z.array(networkErrorsTopPath), by_method: z.array(networkErrorsByMethod)}); export const getApiV2AppsAppIdNetworkErrorsResponse = z.object({status: z.literal("success"), response: z.union([networkErrors, z.object({})])}); 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 edge error breakdown for the application's domains * * GET /v2/apps/{appId}/network/errors */ export const getApiV2AppsAppIdNetworkErrors = async (args: GetApiV2AppsAppIdNetworkErrorsArgs, options?: GetApiV2AppsAppIdNetworkErrorsOptions) => { const {baseUrl = "https://api.squarecloud.app", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.include_4xx != null) query.append("include_4xx", String(args.include_4xx)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/apps/${encodeURIComponent(String(args.appId))}/network/errors${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(getApiV2AppsAppIdNetworkErrorsResponse, await res.json()) };