import {z} from 'zod' export type ZoneRecordType = 'A' | 'AAAA' | 'ALIAS' | 'CAA' | 'CNAME' | 'DNSKEY' | 'DS' | 'HINFO' | 'MX' | 'NAPTR' | 'NS' | 'POOL' | 'PTR' | 'SOA' | 'SPF' | 'SRV' | 'SSHFP' | 'TXT' | 'URL'; export type TTL = number; export type ZoneRecordRegion = 'global' | 'SV1' | 'ORD' | 'IAD' | 'AMS' | 'TKO' | 'SYD' | 'CDG' | 'FRA'; export type CreateApiAccountZonesZoneBatchArgs = {account: number, zone: string, body: {creates?: (Array<{name: string, type: ZoneRecordType, content: string, ttl?: TTL | undefined, priority?: number | undefined, regions?: Array | undefined}>) | undefined, updates?: (Array<{id: number, name?: string | undefined, content?: string | undefined, ttl?: TTL | undefined, priority?: number | undefined, regions?: Array | undefined}>) | undefined, deletes?: Array<{id: number}> | undefined}}; export type CreateApiAccountZonesZoneBatchOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const tTL = z.number().int().gte(0); export const zoneRecordType = z.enum(["A", "AAAA", "ALIAS", "CAA", "CNAME", "DNSKEY", "DS", "HINFO", "MX", "NAPTR", "NS", "POOL", "PTR", "SOA", "SPF", "SRV", "SSHFP", "TXT", "URL"]); export const zoneRecordRegion = z.enum(["global", "SV1", "ORD", "IAD", "AMS", "TKO", "SYD", "CDG", "FRA"]); export const dateTimeCreatedAt = z.string(); export const dateTimeUpdatedAt = z.string(); export const zoneRecord = z.object({id: z.number().int(), zone_id: z.string(), parent_id: z.number().int().nullable(), name: z.string(), content: z.string(), ttl: tTL, priority: z.number().int().nullable(), type: zoneRecordType, regions: z.array(zoneRecordRegion), system_record: z.boolean(), created_at: dateTimeCreatedAt, updated_at: dateTimeUpdatedAt}); export const batchChangeZoneRecordsResponse = z.object({creates: z.array(zoneRecord), updates: z.array(zoneRecord), deletes: z.array(z.object({id: z.number().int()}))}); export const createApiAccountZonesZoneBatchResponse = z.object({data: batchChangeZoneRecordsResponse}); 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 }; /** * Batch create, update, and/or delete records in a zone * * POST /{account}/zones/{zone}/batch * * Also accepts auth: bearerAuth */ export const createApiAccountZonesZoneBatch = async (args: CreateApiAccountZonesZoneBatchArgs, options?: CreateApiAccountZonesZoneBatchOptions) => { const {baseUrl = "https://api.dnsimple.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.account))}/zones/${encodeURIComponent(String(args.zone))}/batch${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(createApiAccountZonesZoneBatchResponse, await res.json()) };