import {z} from 'zod' export type GtdRegion = {gtdLocationId?: number | undefined, regionName?: string | undefined, regionLabel?: string | undefined}; export type DateTime = string; export type MXRecordWithoutIdValues = {id?: number | undefined, server?: string | undefined, enabled?: boolean | undefined, priority?: number | undefined, sortOrder?: number | undefined}; export type MXRecordWithoutId = {type?: string | undefined, name?: string | undefined, ttl?: number | undefined, region?: GtdRegion | undefined, createdTs?: DateTime | undefined, modifiedTs?: DateTime | undefined, systemFlag?: boolean | undefined, values?: Array | undefined}; export type CreateApiDomainsPrimaryDomainIdRecordsMXArgs = {domainId: number, body: MXRecordWithoutId}; export type CreateApiDomainsPrimaryDomainIdRecordsMXOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gtdRegion = z.object({gtdLocationId: z.number().int().optional(), regionName: z.string().optional(), regionLabel: z.string().optional()}); export const dateTime = z.string(); export const mXRecordWithoutIdValues = z.object({id: z.number().int().optional(), server: z.string().optional(), enabled: z.boolean().optional(), priority: z.number().int().optional(), sortOrder: z.number().int().optional()}); export const mXRecord = z.object({id: z.number().int().optional(), type: z.string().optional(), name: z.string().optional(), ttl: z.number().int().optional(), region: gtdRegion.optional(), createdTs: dateTime.optional(), modifiedTs: dateTime.optional(), systemFlag: z.boolean().optional(), values: z.array(mXRecordWithoutIdValues).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 }; /** * Adds an MX record to a specific primary domain. * * POST /domains/primary/{domainId}/records/MX */ export const createApiDomainsPrimaryDomainIdRecordsMX = async (args: CreateApiDomainsPrimaryDomainIdRecordsMXArgs, options?: CreateApiDomainsPrimaryDomainIdRecordsMXOptions) => { 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(/\/+$/, '')}/domains/primary/${encodeURIComponent(String(args.domainId))}/records/MX${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(mXRecord, await res.json()) };