import {z} from 'zod' export type OperationStatus = 'QUEUED' | 'SIGNATURE_REQUIRED' | 'PROCESSING' | 'COMPLETED' | 'FAILED' | 'CANCELLED'; export type OperationType = 'DOMAIN_CLAIM' | 'DOMAIN_UPDATE' | 'DOMAIN_RETURN' | 'WALLET_CREATE' | 'WALLET_UPDATE' | 'WALLET_VERIFY' | 'ACCOUNT_UPDATE'; export type GetApiOperationsArgs = {domain?: Array | undefined, status?: Array | undefined, type?: Array | undefined, $cursor?: string | undefined}; export type GetApiOperationsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const operationStatus = z.enum(["QUEUED", "SIGNATURE_REQUIRED", "PROCESSING", "COMPLETED", "FAILED", "CANCELLED"]); export const operationType = z.enum(["DOMAIN_CLAIM", "DOMAIN_UPDATE", "DOMAIN_RETURN", "WALLET_CREATE", "WALLET_UPDATE", "WALLET_VERIFY", "ACCOUNT_UPDATE"]); export const domainName = z.string(); export const operationMinimalResponse = z.object({id: z.string(), "@type": z.literal("unstoppabledomains.com/partner.v3.OperationMinimal"), status: operationStatus, type: operationType, domain: domainName.optional(), lastUpdatedTimestamp: z.number()}); export const cursorListResponseNext = z.object({$cursor: z.string().nullable()}); export const operationListResponse = z.object({"@type": z.literal("unstoppabledomains.com/partner.v3.CursorList"), items: z.array(operationMinimalResponse), 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 }; /** * Get list of operations * * GET /operations */ export const getApiOperations = async (args: GetApiOperationsArgs, options?: GetApiOperationsOptions) => { 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.domain ?? []) query.append("domain", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.$cursor != null) query.append("$cursor", String(args.$cursor)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/operations${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(operationListResponse, await res.json()) };