import {z} from 'zod' export type GetApiDomainArgs = {filters?: string | undefined}; export type GetApiDomainOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const headerConstraint = z.object({name: z.string(), value: z.string().optional(), case_sensitive: z.boolean().optional(), invert: z.boolean().optional()}); export const redirect = z.object({name: z.string(), from: z.string(), to: z.string(), redirect_type: z.enum(["permanent", "temporary"]), header_constraints: z.array(headerConstraint).optional()}); export const certKeyPathPair = z.object({certificate_path: z.string(), key_path: z.string()}); export const sSLConfig = z.object({cert_key_pairs: z.array(certKeyPathPair), cipher_filter: z.string().optional(), protocols: z.array(z.string()).optional()}); export const cORSConfig = z.object({allowed_origins: z.array(z.string()), allow_credentials: z.boolean().optional(), exposed_headers: z.array(z.string()).optional(), max_age: z.number().int().optional(), allowed_methods: z.array(z.string()), allowed_headers: z.array(z.string()).optional()}); export const domain = z.object({domain_key: z.string(), zone_key: z.string(), name: z.string(), port: z.number().int(), checksum: z.string(), redirects: z.array(redirect).optional(), gzip_enabled: z.boolean().optional(), ssl_config: sSLConfig.optional(), cors_config: cORSConfig.optional(), aliases: z.array(z.string()).optional(), force_https: z.boolean().optional()}); export const multiDomainResult = z.object({result: z.array(domain).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 domains * * GET /domain */ export const getApiDomain = async (args: GetApiDomainArgs, options?: GetApiDomainOptions) => { const {baseUrl = "https://api.turbinelabs.io/v1.0", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filters != null) query.append("filters", String(args.filters)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/domain${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(multiDomainResult, await res.json()) };