import {z} from 'zod' export type DomainName = string; export type GetApiDomainsNameArgs = {name: DomainName, $expand?: (Array<'records' | 'registration'>) | undefined}; export type GetApiDomainsNameOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const domainRecords = z.record(z.string(), z.string()); export const domainName = z.string(); export const ownerType = z.enum(["NONE", "UD", "SELF", "EXTERNAL"]); export const ownerAddress = z.string(); export const domainOwnerResponse = z.object({type: ownerType, address: ownerAddress.optional()}); export const domainAvailabilityStatus = z.enum(["AVAILABLE", "AVAILABLE_FROM_ISSUER", "REGISTERED", "PROTECTED", "COMING_SOON", "DISALLOWED", "RESERVED", "EXCEEDS_PRICE_LIMIT"]); export const domainPriceType = z.enum(["STANDARD", "PREMIUM", "DEFERRED"]); export const domainPriceItemResponse = z.object({usdCents: z.number()}); export const domainPriceResponse = z.object({type: domainPriceType, listPrice: domainPriceItemResponse, subTotal: domainPriceItemResponse}); export const domainAvailabilityResponse = z.object({status: domainAvailabilityStatus, price: domainPriceResponse.optional(), issuer: domainOwnerResponse.optional()}); export const domainRegistrationResponse = z.object({tokenId: z.string(), tokenType: z.enum(["ERC721", "ERC1155"]).optional()}); export const blockchainType = z.enum(["MATIC", "ETH", "BASE"]); export const domainSearchResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.Domain"), records: domainRecords.nullable().optional(), name: domainName, owner: domainOwnerResponse, availability: domainAvailabilityResponse, registration: domainRegistrationResponse.nullable().optional(), blockchain: blockchainType.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 }; /** * Get domain details and availability * * GET /domains/{name} */ export const getApiDomainsName = async (args: GetApiDomainsNameArgs, options?: GetApiDomainsNameOptions) => { const {baseUrl = "https://api.unstoppabledomains.com/partner/v3", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.$expand ?? []) query.append("$expand", String(item)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/domains/${encodeURIComponent(String(args.name))}${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(domainSearchResponse, await res.json()) };