import {z} from 'zod' export type GetApiAccountZonesZoneRecordsArgs = {account: number, zone: string, name_like?: string | undefined, name?: string | undefined, type?: string | undefined, sort?: ('id:asc' | 'id:desc' | 'name:asc' | 'name:desc' | 'content:asc' | 'content:desc' | 'type:asc' | 'type:desc') | undefined}; export type GetApiAccountZonesZoneRecordsOptions = {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 pagination = z.object({current_page: z.number().int(), per_page: z.number().int(), total_entries: z.number().int(), total_pages: z.number().int()}); export const getApiAccountZonesZoneRecordsResponse = z.object({data: z.array(zoneRecord), pagination: pagination}); 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 }; /** * List zone records * * GET /{account}/zones/{zone}/records * * Also accepts auth: bearerAuth */ export const getApiAccountZonesZoneRecords = async (args: GetApiAccountZonesZoneRecordsArgs, options?: GetApiAccountZonesZoneRecordsOptions) => { const {baseUrl = "https://api.dnsimple.com/v2", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name_like != null) query.append("name_like", String(args.name_like)) if (args.name != null) query.append("name", String(args.name)) if (args.type != null) query.append("type", String(args.type)) if (args.sort != null) query.append("sort", String(args.sort)) 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))}/records${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(getApiAccountZonesZoneRecordsResponse, await res.json()) };