import {z} from 'zod' export type GetApiRecordHttpsArgs = {paging?: string | undefined, options?: string | undefined}; export type GetApiRecordHttpsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v3SvcParameter = z.object({mandatory: z.boolean().optional(), svc_key: z.string().optional(), svc_value: z.array(z.string()).optional()}); export const v3RecordHTTPS = z.object({comment: z.string().optional(), creator: z.string().optional(), disable: z.boolean().optional(), name: z.string(), priority: z.number().int().optional(), ref: z.string().optional(), svc_parameters: z.array(v3SvcParameter).optional(), tags: z.object({}).optional(), target_name: z.string(), ttl: z.number().int().optional(), use_ttl: z.boolean().optional(), view: z.string().optional(), zone: z.string().optional()}); export const v3ListRecordHTTPSResponse = z.object({next_page_id: z.string().optional(), result: z.array(v3RecordHTTPS).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 }; /** * Retrieve RecordHTTPS objects. * * GET /record:https */ export const getApiRecordHttps = async (args: GetApiRecordHttpsArgs, options: GetApiRecordHttpsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.paging != null) query.append("paging", String(args.paging)) if (args.options != null) query.append("options", String(args.options)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/record:https${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(v3ListRecordHTTPSResponse, await res.json()) };