import {z} from 'zod' export type SerializedDomainProfileImageData = {base64?: string | undefined, type?: string | undefined}; export type SerializedDomainProfilePost = {displayName?: string | undefined, displayNamePublic?: boolean | undefined, description?: string | undefined, descriptionPublic?: boolean | undefined, location?: string | undefined, locationPublic?: boolean | undefined, imagePath?: string | undefined, imagePathPublic?: boolean | undefined, coverPath?: string | undefined, coverPathPublic?: boolean | undefined, web2Url?: string | undefined, publicDomainSellerEmail?: string | undefined, web2UrlPublic?: boolean | undefined, showDomainSuggestion?: boolean | undefined, showFeaturedCommunity?: boolean | undefined, showFeaturedPartner?: boolean | undefined, phoneNumber?: string | undefined, phoneNumberPublic?: boolean | undefined, privateEmail?: string | undefined, messagingDisabled?: boolean | undefined, thirdPartyMessagingEnabled?: boolean | undefined, thirdPartyMessagingConfigType?: string | undefined, data?: ({image?: SerializedDomainProfileImageData | undefined, cover?: SerializedDomainProfileImageData | undefined}) | undefined, socialAccounts?: ({discord?: string | undefined, reddit?: string | undefined, twitter?: string | undefined, telegram?: string | undefined, youtube?: string | undefined}) | undefined}; export type SerializedBulkDomainProfilePost = {domains?: Array | undefined, profile?: SerializedDomainProfilePost | undefined}; export type CreateApiUserDomainsArgs = {'x-auth-domain': string, 'x-auth-expires': string, 'x-auth-signature': string, body: SerializedBulkDomainProfilePost}; export type CreateApiUserDomainsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const serializedBulkDomainResponse = z.object({success: z.boolean().optional(), domains: z.array(z.string()).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 }; /** * Edit domain profile data in bulk * * POST /user/domains */ export const createApiUserDomains = async (args: CreateApiUserDomainsArgs, options?: CreateApiUserDomainsOptions) => { 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/domains${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(serializedBulkDomainResponse, await res.json()) };