basant307/AI_Governance_Project
048
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) =>51 utils.isFunction(adapter) || adapter === null || adapter === false;52 53/**54 * Get the first suitable adapter from the provided list.55 * Tries each adapter in order until a supported one is found.56 * Throws an AxiosError if no adapter is suitable.57 *58 * @param {Array<string|Function>|string|Function} adapters - Adapter(s) by name or function.59 * @param {Object} config - Axios request configuration60 * @throws {AxiosError} If no suitable adapter is available61 * @returns {Function} The resolved adapter function62 */63function getAdapter(adapters, config) {64 adapters = utils.isArray(adapters) ? adapters : [adapters];65 66 const { length } = adapters;67 let nameOrAdapter;68 let adapter;69 70 const rejectedReasons = {};71 72 for (let i = 0; i < length; i++) {73 nameOrAdapter = adapters[i];74 let id;75 76 adapter = nameOrAdapter;77 78 if (!isResolvedHandle(nameOrAdapter)) {79 adapter = knownAdapters[(id = String(nameOrAdapter)).toLowerCase()];80 81 if (adapter === undefined) {82 throw new AxiosError(`Unknown adapter '${id}'`);83 }84 }85 86 if (adapter && (utils.isFunction(adapter) || (adapter = adapter.get(config)))) {87 break;88 }89 90 rejectedReasons[id || '#' + i] = adapter;91 }92 93 if (!adapter) {94 const reasons = Object.entries(rejectedReasons).map(95 ([id, state]) =>96 `adapter ${id} ` +97 (state === false ? 'is not supported by the environment' : 'is not available in the build')98 );99 100 let s = length101 ? reasons.length > 1102 ? 'since :\n' + reasons.map(renderReason).join('\n')103 : ' ' + renderReason(reasons[0])104 : 'as no adapter specified';105 106 throw new AxiosError(107 `There is no suitable adapter to dispatch the request ` + s,108 'ERR_NOT_SUPPORT'109 );110 }111 112 return adapter;113}114 115/**116 * Exports Axios adapters and utility to resolve an adapter117 */118export default {119 /**120 * Resolve an adapter from a list of adapter names or functions.121 * @type {Function}122 */123 getAdapter,124 125 /**126 * Exposes all known adapters127 * @type {Object<string, Function|Object>}128 */129 adapters: knownAdapters,130};131 