[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
| 4 | exports.__esModule = true;
|
---|
| 5 | exports.default = void 0;
|
---|
| 6 | var _stampit = _interopRequireDefault(require("stampit"));
|
---|
| 7 | var _ramda = require("ramda");
|
---|
| 8 | var _ramdaAdjunct = require("ramda-adjunct");
|
---|
| 9 | var _axios = _interopRequireDefault(require("axios"));
|
---|
| 10 | var _ResolverError = _interopRequireDefault(require("../../../errors/ResolverError.cjs"));
|
---|
| 11 | var _HttpResolver = _interopRequireDefault(require("../HttpResolver.cjs"));
|
---|
| 12 | const HttpResolverAxios = (0, _stampit.default)(_HttpResolver.default).init(function HttpResolverAxios() {
|
---|
| 13 | /**
|
---|
| 14 | * Private Api.
|
---|
| 15 | */
|
---|
| 16 | let axiosInstance;
|
---|
| 17 | let oldAxiosConfig;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Public Api.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | this.name = 'http-axios';
|
---|
| 24 | this.axiosConfig = {};
|
---|
| 25 | this.getHttpClient = function getHttpClient() {
|
---|
| 26 | if (typeof axiosInstance === 'undefined' || oldAxiosConfig !== this.axiosConfig) {
|
---|
| 27 | const config = (0, _ramda.omit)(['interceptors'], this.axiosConfig);
|
---|
| 28 | const interceptors = (0, _ramda.pathOr)({
|
---|
| 29 | request: [],
|
---|
| 30 | response: []
|
---|
| 31 | }, ['axiosConfig', 'interceptors'], this);
|
---|
| 32 | axiosInstance = _axios.default.create({
|
---|
| 33 | timeout: this.timeout,
|
---|
| 34 | maxRedirects: this.redirects,
|
---|
| 35 | withCredentials: this.withCredentials,
|
---|
| 36 | responseType: 'arraybuffer',
|
---|
| 37 | ...config
|
---|
| 38 | });
|
---|
| 39 |
|
---|
| 40 | // settings up request interceptors
|
---|
| 41 | if (Array.isArray(interceptors == null ? void 0 : interceptors.request)) {
|
---|
| 42 | interceptors.request.forEach(requestInterceptor => {
|
---|
| 43 | axiosInstance.interceptors.request.use(...(0, _ramdaAdjunct.ensureArray)(requestInterceptor));
|
---|
| 44 | });
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | // settings up response interceptors
|
---|
| 48 | if (Array.isArray(interceptors == null ? void 0 : interceptors.response)) {
|
---|
| 49 | interceptors.response.forEach(responseInterceptor => {
|
---|
| 50 | axiosInstance.interceptors.response.use(...(0, _ramdaAdjunct.ensureArray)(responseInterceptor));
|
---|
| 51 | });
|
---|
| 52 | }
|
---|
| 53 | oldAxiosConfig = this.axiosConfig;
|
---|
| 54 | }
|
---|
| 55 | return axiosInstance;
|
---|
| 56 | };
|
---|
| 57 | this.read = async function read(file) {
|
---|
| 58 | const client = this.getHttpClient();
|
---|
| 59 | try {
|
---|
| 60 | const response = await client.get(file.uri);
|
---|
| 61 | return response.data;
|
---|
| 62 | } catch (error) {
|
---|
| 63 | throw new _ResolverError.default(`Error downloading "${file.uri}"`, {
|
---|
| 64 | cause: error
|
---|
| 65 | });
|
---|
| 66 | }
|
---|
| 67 | };
|
---|
| 68 | });
|
---|
| 69 | var _default = exports.default = HttpResolverAxios; |
---|