import {z} from 'zod' export type GetApiProjectsProjectIdEnvironmentsEnvironmentIdRoutesArgs = {projectId: string, environmentId: string}; export type GetApiProjectsProjectIdEnvironmentsEnvironmentIdRoutesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const proxyRoute = z.object({id: z.string(), primary: z.boolean().nullable(), production_url: z.string().nullable(), attributes: z.record(z.string(), z.string()), type: z.enum(["proxy", "redirect", "upstream"]), tls: z.object({strict_transport_security: z.object({enabled: z.boolean().nullable(), include_subdomains: z.boolean().nullable(), preload: z.boolean().nullable()}), min_version: z.enum(["TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3"]).nullable(), client_authentication: z.enum(["request", "require"]).nullable(), client_certificate_authorities: z.array(z.string())}), to: z.string()}); export const redirectRoute = z.object({id: z.string(), primary: z.boolean().nullable(), production_url: z.string().nullable(), attributes: z.record(z.string(), z.string()), type: z.enum(["proxy", "redirect", "upstream"]), tls: z.object({strict_transport_security: z.object({enabled: z.boolean().nullable(), include_subdomains: z.boolean().nullable(), preload: z.boolean().nullable()}), min_version: z.enum(["TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3"]).nullable(), client_authentication: z.enum(["request", "require"]).nullable(), client_certificate_authorities: z.array(z.string())}), to: z.string(), redirects: z.object({expires: z.string(), paths: z.record(z.string(), z.object({regexp: z.boolean(), to: z.string(), prefix: z.boolean().nullable(), append_suffix: z.boolean().nullable(), code: z.union([z.literal(301), z.literal(302), z.literal(307), z.literal(308)]), expires: z.string().nullable()}))})}); export const upstreamRoute = z.object({id: z.string(), primary: z.boolean().nullable(), production_url: z.string().nullable(), attributes: z.record(z.string(), z.string()), type: z.enum(["proxy", "redirect", "upstream"]), tls: z.object({strict_transport_security: z.object({enabled: z.boolean().nullable(), include_subdomains: z.boolean().nullable(), preload: z.boolean().nullable()}), min_version: z.enum(["TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3"]).nullable(), client_authentication: z.enum(["request", "require"]).nullable(), client_certificate_authorities: z.array(z.string())}), cache: z.object({enabled: z.boolean(), default_ttl: z.number().int(), cookies: z.array(z.string()), headers: z.array(z.string())}), ssi: z.object({enabled: z.boolean()}), upstream: z.string(), redirects: z.object({expires: z.string(), paths: z.record(z.string(), z.object({regexp: z.boolean(), to: z.string(), prefix: z.boolean().nullable(), append_suffix: z.boolean().nullable(), code: z.union([z.literal(301), z.literal(302), z.literal(307), z.literal(308)]), expires: z.string().nullable()}))}), sticky: z.object({enabled: z.boolean()})}); export const route = z.union([proxyRoute, redirectRoute, upstreamRoute]); export const routeCollection = z.array(route); 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 list of routes * * GET /projects/{projectId}/environments/{environmentId}/routes */ export const getApiProjectsProjectIdEnvironmentsEnvironmentIdRoutes = async (args: GetApiProjectsProjectIdEnvironmentsEnvironmentIdRoutesArgs, options?: GetApiProjectsProjectIdEnvironmentsEnvironmentIdRoutesOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/environments/${encodeURIComponent(String(args.environmentId))}/routes${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(routeCollection, await res.json()) };