import {z} from 'zod' export type DNSDataProp = {digest_type?: string | undefined, protocol?: string | undefined, content?: string | undefined, proto?: string | undefined, digestType?: string | undefined, type?: string | undefined, name?: string | undefined, priority?: string | undefined, key_tag?: string | undefined, algorithm?: string | undefined, certificate?: string | undefined, target?: string | undefined, flags?: string | undefined, public_key?: string | undefined, digest?: string | undefined, value?: string | undefined, order?: string | undefined, preference?: string | undefined, service?: string | undefined, regex?: string | undefined, replacement?: string | undefined, nameserver?: string | undefined, usage?: string | undefined, selector?: string | undefined, matching_type?: string | undefined, weight?: string | undefined, port?: string | undefined, fingerprint?: string | undefined, lat_degrees?: string | undefined, lat_minutes?: string | undefined, lat_seconds?: string | undefined, lat_direction?: string | undefined, long_degrees?: string | undefined, long_minutes?: string | undefined, long_seconds?: string | undefined, long_direction?: string | undefined, precision_horz?: string | undefined, precision_vert?: string | undefined, altitude?: string | undefined, size?: string | undefined}; export type DNSBody = {domain?: string | undefined, name?: string | undefined, type?: string | undefined, content?: string | undefined, data?: DNSDataProp | undefined, ttl?: number | undefined, priority?: string | undefined, proxied?: boolean | undefined}; export type CreateApiDnsArgs = {body: DNSBody}; export type CreateApiDnsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dNSDataProp = z.object({digest_type: z.string().optional(), protocol: z.string().optional(), content: z.string().optional(), proto: z.string().optional(), digestType: z.string().optional(), type: z.string().optional(), name: z.string().optional(), priority: z.string().optional(), key_tag: z.string().optional(), algorithm: z.string().optional(), certificate: z.string().optional(), target: z.string().optional(), flags: z.string().optional(), public_key: z.string().optional(), digest: z.string().optional(), value: z.string().optional(), order: z.string().optional(), preference: z.string().optional(), service: z.string().optional(), regex: z.string().optional(), replacement: z.string().optional(), nameserver: z.string().optional(), usage: z.string().optional(), selector: z.string().optional(), matching_type: z.string().optional(), weight: z.string().optional(), port: z.string().optional(), fingerprint: z.string().optional(), lat_degrees: z.string().optional(), lat_minutes: z.string().optional(), lat_seconds: z.string().optional(), lat_direction: z.string().optional(), long_degrees: z.string().optional(), long_minutes: z.string().optional(), long_seconds: z.string().optional(), long_direction: z.string().optional(), precision_horz: z.string().optional(), precision_vert: z.string().optional(), altitude: z.string().optional(), size: z.string().optional()}); export const dNSFull = z.object({id: z.string().optional(), name: z.string().optional(), type: z.string().optional(), content: z.string().optional(), data: dNSDataProp.optional(), ttl: z.number().int().optional(), priority: z.number().int().optional(), proxied: z.boolean().optional(), domain: 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 }; /** * Create a DNS * * POST /dns */ export const createApiDns = async (args: CreateApiDnsArgs, options: CreateApiDnsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-ak", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/dns${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dNSFull, await res.json()) };