MCPcopy
hub / github.com/axios/axios / getAdapter

Function getAdapter

lib/adapters/adapters.js:65–115  ·  view source on GitHub ↗

* Get the first suitable adapter from the provided list. * Tries each adapter in order until a supported one is found. * Throws an AxiosError if no adapter is suitable. * * @param {Array<string|Function>|string|Function} adapters - Adapter(s) by name or function. * @param {Object} config - Axio

(adapters, config)

Source from the content-addressed store, hash-verified

63 * @returns {Function} The resolved adapter function
64 */
65function getAdapter(adapters, config) {
66 adapters = utils.isArray(adapters) ? adapters : [adapters];
67
68 const { length } = adapters;
69 let nameOrAdapter;
70 let adapter;
71
72 const rejectedReasons = {};
73
74 for (let i = 0; i < length; i++) {
75 nameOrAdapter = adapters[i];
76 let id;
77
78 adapter = nameOrAdapter;
79
80 if (!isResolvedHandle(nameOrAdapter)) {
81 adapter = knownAdapters[(id = String(nameOrAdapter)).toLowerCase()];
82
83 if (adapter === undefined) {
84 throw new AxiosError(`Unknown adapter '${id}'`);
85 }
86 }
87
88 if (adapter && (utils.isFunction(adapter) || (adapter = adapter.get(config)))) {
89 break;
90 }
91
92 rejectedReasons[id || '#' + i] = adapter;
93 }
94
95 if (!adapter) {
96 const reasons = Object.entries(rejectedReasons).map(
97 ([id, state]) =>
98 `adapter ${id} ` +
99 (state === false ? 'is not supported by the environment' : 'is not available in the build')
100 );
101
102 let s = length
103 ? reasons.length > 1
104 ? 'since :\n' + reasons.map(renderReason).join('\n')
105 : ' ' + renderReason(reasons[0])
106 : 'as no adapter specified';
107
108 throw new AxiosError(
109 `There is no suitable adapter to dispatch the request ` + s,
110 AxiosError.ERR_NOT_SUPPORT
111 );
112 }
113
114 return adapter;
115}
116
117/**
118 * Exports Axios adapters and utility to resolve an adapter

Callers 1

esm-index.tsFile · 0.85

Calls 4

isResolvedHandleFunction · 0.85
renderReasonFunction · 0.85
getMethod · 0.80
entriesMethod · 0.80

Tested by

no test coverage detected