import {z} from 'zod' export type NameserverByFqdn = {fqdn: string}; export type NameserverById = {id: number, vanityNameserverFQDN?: string | undefined}; export type NameserverSetRequest = {name: string, isDefault?: boolean | undefined, nameservers: Array, resourceGroupsIds: Array}; export type CreateApiNameserversetsArgs = {body: NameserverSetRequest}; export type CreateApiNameserversetsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const nameserverDetail = z.object({id: z.number().int().optional(), isSystem: z.boolean().optional(), isSecondary: z.boolean().nullable().optional(), fqdn: z.string().optional(), ipv4: z.string().nullable().optional(), ipv6: z.string().nullable().optional(), vanityNameserverFQDN: z.string().nullable().optional(), createdTs: z.string().optional(), modifiedTs: z.string().optional()}); export const resourceGroupDetail = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const nameserverSetResponse = z.object({id: z.number().int().optional(), name: z.string().optional(), isSystem: z.boolean().optional(), isDefault: z.boolean().optional(), nameservers: z.array(nameserverDetail).optional(), domains: z.array(z.object({})).optional(), resourceGroups: z.array(resourceGroupDetail).optional(), createdTs: z.string().optional(), modifiedTs: 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 }; /** * Creates a new nameserver set. * * POST /nameserversets */ export const createApiNameserversets = async (args: CreateApiNameserversetsArgs, options?: CreateApiNameserversetsOptions) => { const {baseUrl = "https://manager.digicertdns.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/nameserversets${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(nameserverSetResponse, await res.json()) };