UNPKG

@yoroi/resolver

Version:
86 lines (82 loc) 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unstoppableSupportedTlds = exports.unstoppableApiGetCryptoAddress = exports.unstoppableApiConfig = exports.isUnstoppableDomain = exports.getUnstoppableApiError = void 0; var _types = require("@yoroi/types"); var _common = require("@yoroi/common"); var _zod = require("zod"); const initialDeps = { request: _common.fetchData }; const unstoppableApiGetCryptoAddress = function (_ref) { let { apiKey } = _ref; let { request } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : initialDeps; return async (resolve, fetcherConfig) => { if (!resolve.includes('.')) throw new _types.Resolver.Errors.InvalidDomain(); if (!isUnstoppableDomain(resolve)) throw new _types.Resolver.Errors.UnsupportedTld(); const config = { url: `${unstoppableApiConfig.mainnet.getCryptoAddress}${resolve}`, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${apiKey}` } }; try { const response = await request(config, fetcherConfig); if ((0, _common.isLeft)(response)) { const error = response.error; if (error.responseData?.message?.includes('Unsupported TLD')) throw new _types.Resolver.Errors.UnsupportedTld(); throw (0, _common.getApiError)(error); } else { // parsing const safeParsedAdaResponse = UnstoppableApiAdaResponseSchema.safeParse(response.value.data); const safeParsedGeneralResponse = UnstoppableApiGeneralResponseSchema.safeParse(response.value.data); // checking const hasCardanoAddress = safeParsedAdaResponse.success; const hasOtherBlockchainAddress = safeParsedGeneralResponse.success; if (hasCardanoAddress) return response.value.data.records['crypto.ADA.address']; if (hasOtherBlockchainAddress) throw new _types.Resolver.Errors.WrongBlockchain(); throw new _types.Resolver.Errors.InvalidResponse(); } } catch (error) { throw getUnstoppableApiError(error); } }; }; // https://docs.unstoppabledomains.com/openapi/resolution/#operation/DomainsController.getDomain exports.unstoppableApiGetCryptoAddress = unstoppableApiGetCryptoAddress; const UnstoppableApiAdaResponseSchema = _zod.z.object({ records: _zod.z.object({ 'crypto.ADA.address': _zod.z.string() }) }); const UnstoppableApiGeneralResponseSchema = _zod.z.object({ meta: _zod.z.object({ blockchain: _zod.z.string() }), records: _zod.z.record(_zod.z.string(), _zod.z.string()) }); // curl https://api.unstoppabledomains.com/resolve/supported_tlds const unstoppableSupportedTlds = ['.x', '.polygon', '.nft', '.crypto', '.blockchain', '.bitcoin', '.dao', '.888', '.wallet', '.binanceus', '.hi', '.klever', '.kresus', '.anime', '.manga', '.go', '.altimist', '.pudgy', '.unstoppable', '.austin', '.bitget', '.pog', '.clay', '.zil', '.eth']; exports.unstoppableSupportedTlds = unstoppableSupportedTlds; const isUnstoppableDomain = value => { return unstoppableSupportedTlds.some(tld => value.endsWith(tld)); }; exports.isUnstoppableDomain = isUnstoppableDomain; const unstoppableApiConfig = { mainnet: { getCryptoAddress: 'https://api.unstoppabledomains.com/resolve/domains/' } }; exports.unstoppableApiConfig = unstoppableApiConfig; const getUnstoppableApiError = error => { if (error instanceof _types.Api.Errors.NotFound) return new _types.Resolver.Errors.NotFound(); return error; }; exports.getUnstoppableApiError = getUnstoppableApiError; //# sourceMappingURL=api.js.map