import {z} from 'zod' export type UpdateDomainRecordRequest = {address?: string | undefined, primaryDomain?: boolean | undefined, transferToAddress?: string | undefined, clearRecords?: string | undefined, records?: ({key?: string | undefined}) | undefined}; export type CreateApiUserDomainRecordsManageArgs = {domain: string, 'x-auth-domain': string, 'x-auth-expires': string, 'x-auth-signature': string, body: UpdateDomainRecordRequest}; export type CreateApiUserDomainRecordsManageOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dependency = z.object({id: z.string().optional(), transaction: z.object({hash: z.string().optional(), messageToSign: z.string().optional()}).optional()}); export const operation = z.object({id: z.string().optional(), dependencies: z.array(dependency).optional()}); export const operationResponse = z.object({type: z.string().optional(), operation: operation.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 }; /** * Request to update a domain's on-chain records * * POST /user/{domain}/records/manage */ export const createApiUserDomainRecordsManage = async (args: CreateApiUserDomainRecordsManageArgs, options?: CreateApiUserDomainRecordsManageOptions) => { const {baseUrl = "https://api.unstoppabledomains.com/profile", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-auth-domain'] != null) headers.set("x-auth-domain", String(args['x-auth-domain'])) if (args['x-auth-expires'] != null) headers.set("x-auth-expires", String(args['x-auth-expires'])) if (args['x-auth-signature'] != null) headers.set("x-auth-signature", String(args['x-auth-signature'])) const url = `${baseUrl.replace(/\/+$/, '')}/user/${encodeURIComponent(String(args.domain))}/records/manage${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(operationResponse, await res.json()) };