import {z} from 'zod' export type ZoneName = string; export type Identifier = string; export type ZoneAuthority = {name: Identifier, mname?: (string | null) | undefined, rname?: (string | null) | undefined, nameservers?: (Array | null) | undefined, extend?: (boolean | null) | undefined}; export type CreateApiV1DnsZonesZoneNameAuthorityArgs = {zone_name: ZoneName, body: ZoneAuthority}; export type CreateApiV1DnsZonesZoneNameAuthorityOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const warning = z.object({code: z.string(), title: z.string(), detail: z.string().nullable().optional()}); export const commandSuccess = z.object({warnings: z.array(warning).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 }; /** * Set authority for zone * * POST /v1/dns/zones/{zone_name}/authority * * Also accepts auth: basic */ export const createApiV1DnsZonesZoneNameAuthority = async (args: CreateApiV1DnsZonesZoneNameAuthorityArgs, options?: CreateApiV1DnsZonesZoneNameAuthorityOptions) => { const {baseUrl = "https://api.noip.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dns/zones/${encodeURIComponent(String(args.zone_name))}/authority${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(commandSuccess, await res.json()) };