import {z} from 'zod' export type BasicString = string; export type BooleanOptInFlag = boolean; export type IdentifierString = string; export type ResponseFormat = 'html' | 'json' | 'xml'; export type GetApiV2DomainSearchArgs = {query: BasicString, exclude_query?: BasicString | undefined, max_length?: number | undefined, min_length?: number | undefined, has_hyphen?: BooleanOptInFlag | undefined, has_number?: BooleanOptInFlag | undefined, active_only?: BooleanOptInFlag | undefined, deleted_only?: BooleanOptInFlag | undefined, anchor_left?: BooleanOptInFlag | undefined, anchor_right?: BooleanOptInFlag | undefined, page?: number | undefined, app_partner?: IdentifierString | undefined, app_name?: IdentifierString | undefined, app_version?: IdentifierString | undefined, format?: ResponseFormat | undefined}; export type GetApiV2DomainSearchOptions = {baseUrl?: string | undefined, headerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const domainSearch = z.object({response: z.object({query_info: z.object({active_only: z.boolean().optional(), min_length: z.number().int().optional(), total_results: z.number().int().optional(), exclude_query: z.string().optional(), has_hyphen: z.boolean().optional(), max_length: z.number().int().optional(), anchor_right: z.boolean().optional(), anchor_left: z.boolean().optional(), page: z.number().int().optional(), limit: z.number().int().optional(), has_number: z.boolean().optional(), deleted_only: z.boolean().optional()}).optional(), results: z.array(z.object({sld: z.string().optional(), tlds: z.array(z.string()).optional(), hashad_tlds: z.array(z.string()).optional(), char_count: z.number().int().optional(), has_hyphen: z.number().int().optional(), has_deleted: z.number().int().optional(), has_number: z.number().int().optional(), tlds_count: z.number().int().optional(), has_active: z.number().int().optional()})).optional()}).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 }; /** * Domain Search * * GET /v2/domain-search/ * * Also accepts auth: open_key_auth, hmac_auth */ export const getApiV2DomainSearch = async (args: GetApiV2DomainSearchArgs, options?: GetApiV2DomainSearchOptions) => { const {baseUrl = "https://api.domaintools.com", fetch: fetchFn = fetch, headerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.exclude_query != null) query.append("exclude_query", String(args.exclude_query)) if (args.max_length != null) query.append("max_length", String(args.max_length)) if (args.min_length != null) query.append("min_length", String(args.min_length)) if (args.has_hyphen != null) query.append("has_hyphen", String(args.has_hyphen)) if (args.has_number != null) query.append("has_number", String(args.has_number)) if (args.active_only != null) query.append("active_only", String(args.active_only)) if (args.deleted_only != null) query.append("deleted_only", String(args.deleted_only)) if (args.anchor_left != null) query.append("anchor_left", String(args.anchor_left)) if (args.anchor_right != null) query.append("anchor_right", String(args.anchor_right)) if (args.page != null) query.append("page", String(args.page)) if (args.app_partner != null) query.append("app_partner", String(args.app_partner)) if (args.app_name != null) query.append("app_name", String(args.app_name)) if (args.app_version != null) query.append("app_version", String(args.app_version)) if (args.format != null) query.append("format", String(args.format)) if (headerAuth !== undefined) headers.set("X-Api-Key", headerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/domain-search/${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(domainSearch, await res.json()) };