source: frontend/node_modules/axios/lib/adapters/adapters.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.4 KB
Line 
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.js
11 * - `xhr` for browsers
12 * - `fetch` for fetch API-based requests
13 *
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 identification
25utils.forEach(knownAdapters, (fn, value) => {
26 if (fn) {
27 try {
28 // Null-proto descriptors so a polluted Object.prototype.get cannot turn
29 // these data descriptors into accessor descriptors on the way in.
30 Object.defineProperty(fn, 'name', { __proto__: null, value });
31 } catch (e) {
32 // eslint-disable-next-line no-empty
33 }
34 Object.defineProperty(fn, 'adapterName', { __proto__: null, value });
35 }
36});
37
38/**
39 * Render a rejection reason string for unknown or unsupported adapters
40 *
41 * @param {string} reason
42 * @returns {string}
43 */
44const renderReason = (reason) => `- ${reason}`;
45
46/**
47 * Check if the adapter is resolved (function, null, or false)
48 *
49 * @param {Function|null|false} adapter
50 * @returns {boolean}
51 */
52const isResolvedHandle = (adapter) =>
53 utils.isFunction(adapter) || adapter === null || adapter === false;
54
55/**
56 * Get the first suitable adapter from the provided list.
57 * Tries each adapter in order until a supported one is found.
58 * Throws an AxiosError if no adapter is suitable.
59 *
60 * @param {Array<string|Function>|string|Function} adapters - Adapter(s) by name or function.
61 * @param {Object} config - Axios request configuration
62 * @throws {AxiosError} If no suitable adapter is available
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 'ERR_NOT_SUPPORT'
111 );
112 }
113
114 return adapter;
115}
116
117/**
118 * Exports Axios adapters and utility to resolve an adapter
119 */
120export default {
121 /**
122 * Resolve an adapter from a list of adapter names or functions.
123 * @type {Function}
124 */
125 getAdapter,
126
127 /**
128 * Exposes all known adapters
129 * @type {Object<string, Function|Object>}
130 */
131 adapters: knownAdapters,
132};
Note: See TracBrowser for help on using the repository browser.