mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
docs: add jsdoc to adapters (#7134)
* add contributors and documentation * docs(adapters): add JSDoc for adapters module --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -4,34 +4,65 @@ import xhrAdapter from './xhr.js';
|
|||||||
import * as fetchAdapter from './fetch.js';
|
import * as fetchAdapter from './fetch.js';
|
||||||
import AxiosError from "../core/AxiosError.js";
|
import AxiosError from "../core/AxiosError.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Known adapters mapping.
|
||||||
|
* Provides environment-specific adapters for Axios:
|
||||||
|
* - `http` for Node.js
|
||||||
|
* - `xhr` for browsers
|
||||||
|
* - `fetch` for fetch API-based requests
|
||||||
|
*
|
||||||
|
* @type {Object<string, Function|Object>}
|
||||||
|
*/
|
||||||
const knownAdapters = {
|
const knownAdapters = {
|
||||||
http: httpAdapter,
|
http: httpAdapter,
|
||||||
xhr: xhrAdapter,
|
xhr: xhrAdapter,
|
||||||
fetch: {
|
fetch: {
|
||||||
get: fetchAdapter.getFetch,
|
get: fetchAdapter.getFetch,
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// Assign adapter names for easier debugging and identification
|
||||||
utils.forEach(knownAdapters, (fn, value) => {
|
utils.forEach(knownAdapters, (fn, value) => {
|
||||||
if (fn) {
|
if (fn) {
|
||||||
try {
|
try {
|
||||||
Object.defineProperty(fn, 'name', {value});
|
Object.defineProperty(fn, 'name', { value });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// eslint-disable-next-line no-empty
|
// eslint-disable-next-line no-empty
|
||||||
}
|
}
|
||||||
Object.defineProperty(fn, 'adapterName', {value});
|
Object.defineProperty(fn, 'adapterName', { value });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a rejection reason string for unknown or unsupported adapters
|
||||||
|
*
|
||||||
|
* @param {string} reason
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
const renderReason = (reason) => `- ${reason}`;
|
const renderReason = (reason) => `- ${reason}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the adapter is resolved (function, null, or false)
|
||||||
|
*
|
||||||
|
* @param {Function|null|false} adapter
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
const isResolvedHandle = (adapter) => utils.isFunction(adapter) || adapter === null || adapter === false;
|
const isResolvedHandle = (adapter) => utils.isFunction(adapter) || adapter === null || adapter === false;
|
||||||
|
|
||||||
export default {
|
/**
|
||||||
getAdapter: (adapters, config) => {
|
* 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 - Axios request configuration
|
||||||
|
* @throws {AxiosError} If no suitable adapter is available
|
||||||
|
* @returns {Function} The resolved adapter function
|
||||||
|
*/
|
||||||
|
function getAdapter(adapters, config) {
|
||||||
adapters = utils.isArray(adapters) ? adapters : [adapters];
|
adapters = utils.isArray(adapters) ? adapters : [adapters];
|
||||||
|
|
||||||
const {length} = adapters;
|
const { length } = adapters;
|
||||||
let nameOrAdapter;
|
let nameOrAdapter;
|
||||||
let adapter;
|
let adapter;
|
||||||
|
|
||||||
@@ -59,7 +90,6 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!adapter) {
|
if (!adapter) {
|
||||||
|
|
||||||
const reasons = Object.entries(rejectedReasons)
|
const reasons = Object.entries(rejectedReasons)
|
||||||
.map(([id, state]) => `adapter ${id} ` +
|
.map(([id, state]) => `adapter ${id} ` +
|
||||||
(state === false ? 'is not supported by the environment' : 'is not available in the build')
|
(state === false ? 'is not supported by the environment' : 'is not available in the build')
|
||||||
@@ -76,6 +106,21 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return adapter;
|
return adapter;
|
||||||
},
|
|
||||||
adapters: knownAdapters
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exports Axios adapters and utility to resolve an adapter
|
||||||
|
*/
|
||||||
|
export default {
|
||||||
|
/**
|
||||||
|
* Resolve an adapter from a list of adapter names or functions.
|
||||||
|
* @type {Function}
|
||||||
|
*/
|
||||||
|
getAdapter,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exposes all known adapters
|
||||||
|
* @type {Object<string, Function|Object>}
|
||||||
|
*/
|
||||||
|
adapters: knownAdapters
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user