opusdev/vector-similarity-api
1
1'use strict';2 3import transformData from './transformData.js';4import isCancel from '../cancel/isCancel.js';5import defaults from '../defaults/index.js';6import CanceledError from '../cancel/CanceledError.js';7import AxiosHeaders from '../core/AxiosHeaders.js';8import adapters from "../adapters/adapters.js";9 10/**11 * Throws a `CanceledError` if cancellation has been requested.12 *13 * @param {Object} config The config that is to be used for the request14 *15 * @returns {void}16 */17function throwIfCancellationRequested(config) {18 if (config.cancelToken) {19 config.cancelToken.throwIfRequested();20 }21 22 if (config.signal && config.signal.aborted) {23 throw new CanceledError(null, config);24 }25}26 27/**28 * Dispatch a request to the server using the configured adapter.29 *30 * @param {object} config The config that is to be used for the request31 *32 * @returns {Promise} The Promise to be fulfilled33 */34export default function dispatchRequest(config) {35 throwIfCancellationRequested(config);36 37 config.headers = AxiosHeaders.from(config.headers);38 39 // Transform request data40 config.data = transformData.call(41 config,42 config.transformRequest43 );44 45 if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {46 config.headers.setContentType('application/x-www-form-urlencoded', false);47 }48 49 const adapter = adapters.getAdapter(config.adapter || defaults.adapter, config);50 51 return adapter(config).then(function onAdapterResolution(response) {52 throwIfCancellationRequested(config);53 54 // Transform response data55 response.data = transformData.call(56 config,57 config.transformResponse,58 response59 );60 61 response.headers = AxiosHeaders.from(response.headers);62 63 return response;64 }, function onAdapterRejection(reason) {65 if (!isCancel(reason)) {66 throwIfCancellationRequested(config);67 68 // Transform response data69 if (reason && reason.response) {70 reason.response.data = transformData.call(71 config,72 config.transformResponse,73 reason.response74 );75 reason.response.headers = AxiosHeaders.from(reason.response.headers);76 }77 }78 79 return Promise.reject(reason);80 });81}82 