import {z} from 'zod' export type GetApiOwnersAddressDomainsArgs = {address: string, $expand?: (Array<'records' | 'registration' | 'owner' | 'availability'>) | undefined, $cursor?: string | undefined}; export type GetApiOwnersAddressDomainsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 domainRecords = z.record(z.string(), z.string()); export const minimalDomainSearchResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.DomainMinimal"), name: z.string(), blockchain: z.enum(["MATIC", "ETH", "ZIL"]).optional(), owner: domainOwnerResponse.nullable().optional(), availability: domainAvailabilityResponse.nullable().optional(), registration: domainRegistrationResponse.nullable().optional(), records: domainRecords.nullable().optional()}); export const cursorListResponseNext = z.object({$cursor: z.string().nullable()}); export const domainSearchListResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.CursorList"), items: z.array(minimalDomainSearchResponse), next: cursorListResponseNext.nullable()}); 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 }; /** * Search for domains owned by an address * * GET /owners/{address}/domains */ export const getApiOwnersAddressDomains = async (args: GetApiOwnersAddressDomainsArgs, options?: GetApiOwnersAddressDomainsOptions) => { 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 (args.$cursor != null) query.append("$cursor", String(args.$cursor)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/owners/${encodeURIComponent(String(args.address))}/domains${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(domainSearchListResponse, await res.json()) };