import {z} from 'zod' export type GetApiRecordPtrRefRefDataArgs = {'ref.ref_data': string, ref?: string | undefined, _fields?: string | undefined}; export type GetApiRecordPtrRefRefDataOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v3RecordPTR = z.object({comment: z.string().optional(), creator: z.string().optional(), disable: z.boolean().optional(), ipv4addr: z.string().optional(), ipv6addr: z.string().optional(), name: z.string().optional(), ptrdname: z.string(), ref: z.string().optional(), tags: z.object({}).optional(), ttl: z.number().int().optional(), use_ttl: z.boolean().optional(), view: z.string().optional(), zone: z.string().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 a RecordPTR object. * * GET /record:ptr/{ref.ref_data}/** */ export const getApiRecordPtrRefRefData = async (args: GetApiRecordPtrRefRefDataArgs, options: GetApiRecordPtrRefRefDataOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ref != null) query.append("ref", String(args.ref)) if (args._fields != null) query.append("_fields", String(args._fields)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/record:ptr/${encodeURIComponent(String(args['ref.ref_data']))}/**${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(v3RecordPTR, await res.json()) };