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