opusdev/vector-similarity-api
1
1import utils from '../utils.js';2import httpAdapter from './http.js';3import xhrAdapter from './xhr.js';4import * as fetchAdapter from './fetch.js';5import AxiosError from "../core/AxiosError.js";6 7/**8 * Known adapters mapping.9 * Provides environment-specific adapters for Axios:10 * - `http` for Node.js11 * - `xhr` for browsers12 * - `fetch` for fetch API-based requests13 * 14 * @type {Object<string, Function|Object>}15 */16const knownAdapters = {17 http: httpAdapter,18 xhr: xhrAdapter,19 fetch: {20 get: fetchAdapter.getFetch,21 }22};23 24// Assign adapter names for easier debugging and identification25utils.forEach(knownAdapters, (fn, value) => {26 if (fn) {27 try {28 Object.defineProperty(fn, 'name', { value });29 } catch (e) {30 // eslint-disable-next-line no-empty31 }32 Object.defineProperty(fn, 'adapterName', { value });33 }34});35 36/**37 * Render a rejection reason string for unknown or unsupported adapters38 * 39 * @param {string} reason40 * @returns {string}41 */42const renderReason = (reason) => `- ${reason}`;43 44/**45 * Check if the adapter is resolved (function, null, or false)46 * 47 * @param {Function|null|false} adapter48 * @returns {boolean}49 */50const isResolvedHandle = (adapter) => utils.isFunction(adapter) || adapter === null || adapter === false;51 52/**53 * Get the first suitable adapter from the provided list.54 * Tries each adapter in order until a supported one is found.55 * Throws an AxiosError if no adapter is suitable.56 * 57 * @param {Array<string|Function>|string|Function} adapters - Adapter(s) by name or function.58 * @param {Object} config - Axios request configuration59 * @throws {AxiosError} If no suitable adapter is available60 * @returns {Function} The resolved adapter function61 */62function getAdapter(adapters, config) {63 adapters = utils.isArray(adapters) ? adapters : [adapters];64 65 const { length } = adapters;66 let nameOrAdapter;67 let adapter;68 69 const rejectedReasons = {};70 71 for (let i = 0; i < length; i++) {72 nameOrAdapter = adapters[i];73 let id;74 75 adapter = nameOrAdapter;76 77 if (!isResolvedHandle(nameOrAdapter)) {78 adapter = knownAdapters[(id = String(nameOrAdapter)).toLowerCase()];79 80 if (adapter === undefined) {81 throw new AxiosError(`Unknown adapter '${id}'`);82 }83 }84 85 if (adapter && (utils.isFunction(adapter) || (adapter = adapter.get(config)))) {86 break;87 }88 89 rejectedReasons[id || '#' + i] = adapter;90 }91 92 if (!adapter) {93 const reasons = Object.entries(rejectedReasons)94 .map(([id, state]) => `adapter ${id} ` +95 (state === false ? 'is not supported by the environment' : 'is not available in the build')96 );97 98 let s = length ?99 (reasons.length > 1 ? 'since :\n' + reasons.map(renderReason).join('\n') : ' ' + renderReason(reasons[0])) :100 'as no adapter specified';101 102 throw new AxiosError(103 `There is no suitable adapter to dispatch the request ` + s,104 'ERR_NOT_SUPPORT'105 );106 }107 108 return adapter;109}110 111/**112 * Exports Axios adapters and utility to resolve an adapter113 */114export default {115 /**116 * Resolve an adapter from a list of adapter names or functions.117 * @type {Function}118 */119 getAdapter,120 121 /**122 * Exposes all known adapters123 * @type {Object<string, Function|Object>}124 */125 adapters: knownAdapters126};127 