@vroskus/library-api 1.0.25 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +9 -19
- package/dist/index.js +94 -68
- package/dist/types.d.ts +6 -0
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AxiosInstance
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
2
|
import AxiosMockAdapter from 'axios-mock-adapter';
|
|
3
3
|
import type { $Config, $RequestContext, $ResponseContext } from './types';
|
|
4
4
|
export * from './types';
|
|
@@ -6,24 +6,14 @@ type $RequestContextListener = (arg0: $RequestContext) => void;
|
|
|
6
6
|
type $ResponseContextListener = (arg0: $ResponseContext) => void;
|
|
7
7
|
type $UnauthenticatedHandler = () => unknown;
|
|
8
8
|
declare class ApiService<C extends $Config> {
|
|
9
|
+
#private;
|
|
9
10
|
connection: AxiosInstance;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
handler: $UnauthenticatedHandler;
|
|
18
|
-
}): void;
|
|
19
|
-
setRequestContextListener({ listener, }: {
|
|
20
|
-
listener: $RequestContextListener;
|
|
21
|
-
}): void;
|
|
22
|
-
setResponseContextListener({ listener, }: {
|
|
23
|
-
listener: $ResponseContextListener;
|
|
24
|
-
}): void;
|
|
25
|
-
pushRequestContext(config: AxiosRequestConfig<unknown>, requestId: string): void;
|
|
26
|
-
pushResponseContext(response: AxiosResponse<unknown>): void;
|
|
27
|
-
initMock(mockSetup: (mock: AxiosMockAdapter) => void): void;
|
|
11
|
+
mockAdapter: AxiosMockAdapter | null;
|
|
12
|
+
expressRouteToMockRoute: (arg0: string) => RegExp | string;
|
|
13
|
+
constructor({ apiUrl, headers, httpsAgent, interceptors, timeout, }: C);
|
|
14
|
+
setUnauthenticatedHandler(handler: $UnauthenticatedHandler): void;
|
|
15
|
+
setRequestContextListener(listener: $RequestContextListener): void;
|
|
16
|
+
setResponseContextListener(listener: $ResponseContextListener): void;
|
|
17
|
+
initMock(mockSetup: (mockAdapter: AxiosMockAdapter) => void, delay?: number): void;
|
|
28
18
|
}
|
|
29
19
|
export default ApiService;
|
package/dist/index.js
CHANGED
|
@@ -35,9 +35,21 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
37
|
};
|
|
38
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
39
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
40
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
41
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
42
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
43
|
+
};
|
|
44
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
45
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
46
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
47
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
48
|
+
};
|
|
38
49
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
50
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
51
|
};
|
|
52
|
+
var _ApiService_instances, _ApiService_unauthenticatedHandler, _ApiService_requestContextListener, _ApiService_responseContextListener, _ApiService_initInterceptors, _ApiService_pushRequestContext, _ApiService_pushResponseContext;
|
|
41
53
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
54
|
const axios = __importStar(require("axios"));
|
|
43
55
|
const axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter"));
|
|
@@ -49,7 +61,11 @@ const defaultRetryQuantity = 3;
|
|
|
49
61
|
const defaultRetryIncrementor = 1;
|
|
50
62
|
const zeroValue = 0;
|
|
51
63
|
class ApiService {
|
|
52
|
-
constructor({ apiUrl, headers, httpsAgent, timeout, }) {
|
|
64
|
+
constructor({ apiUrl, headers, httpsAgent, interceptors, timeout, }) {
|
|
65
|
+
_ApiService_instances.add(this);
|
|
66
|
+
_ApiService_unauthenticatedHandler.set(this, void 0);
|
|
67
|
+
_ApiService_requestContextListener.set(this, void 0);
|
|
68
|
+
_ApiService_responseContextListener.set(this, void 0);
|
|
53
69
|
const connectionConfig = {
|
|
54
70
|
baseURL: apiUrl,
|
|
55
71
|
headers: Object.assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, (headers || {})),
|
|
@@ -58,26 +74,66 @@ class ApiService {
|
|
|
58
74
|
withCredentials: true,
|
|
59
75
|
};
|
|
60
76
|
this.connection = axios.default.create(connectionConfig);
|
|
61
|
-
this
|
|
62
|
-
this
|
|
63
|
-
this
|
|
77
|
+
__classPrivateFieldSet(this, _ApiService_unauthenticatedHandler, () => { }, "f");
|
|
78
|
+
__classPrivateFieldSet(this, _ApiService_requestContextListener, () => { }, "f");
|
|
79
|
+
__classPrivateFieldSet(this, _ApiService_responseContextListener, () => { }, "f");
|
|
80
|
+
this.mockAdapter = null;
|
|
81
|
+
this.expressRouteToMockRoute = (v) => {
|
|
82
|
+
if (v.includes(':')) {
|
|
83
|
+
return new RegExp(v.replace(/:\w+/g, '[^/]+'));
|
|
84
|
+
}
|
|
85
|
+
return v;
|
|
86
|
+
};
|
|
87
|
+
const interceptorsConfig = Object.assign({ context: true, requestReplay: true, unauth: true }, interceptors || {});
|
|
88
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_initInterceptors).call(this, interceptorsConfig);
|
|
89
|
+
}
|
|
90
|
+
setUnauthenticatedHandler(handler) {
|
|
91
|
+
__classPrivateFieldSet(this, _ApiService_unauthenticatedHandler, handler, "f");
|
|
92
|
+
}
|
|
93
|
+
setRequestContextListener(listener) {
|
|
94
|
+
__classPrivateFieldSet(this, _ApiService_requestContextListener, listener, "f");
|
|
95
|
+
}
|
|
96
|
+
setResponseContextListener(listener) {
|
|
97
|
+
__classPrivateFieldSet(this, _ApiService_responseContextListener, listener, "f");
|
|
98
|
+
}
|
|
99
|
+
initMock(mockSetup, delay) {
|
|
100
|
+
this.mockAdapter = new axios_mock_adapter_1.default(this.connection, {
|
|
101
|
+
delayResponse: delay,
|
|
102
|
+
});
|
|
103
|
+
mockSetup(this.mockAdapter);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
_ApiService_unauthenticatedHandler = new WeakMap(), _ApiService_requestContextListener = new WeakMap(), _ApiService_responseContextListener = new WeakMap(), _ApiService_instances = new WeakSet(), _ApiService_initInterceptors = function _ApiService_initInterceptors(interceptorsConfig) {
|
|
107
|
+
if (interceptorsConfig.unauth !== false) {
|
|
64
108
|
this.connection.interceptors.response.use((response) => response, (error) => {
|
|
65
109
|
const status = lodash_1.default.get(error, 'response.status');
|
|
66
110
|
if (status === unauthenticatedStatus) {
|
|
67
|
-
this.
|
|
111
|
+
__classPrivateFieldGet(this, _ApiService_unauthenticatedHandler, "f").call(this);
|
|
68
112
|
}
|
|
69
113
|
return Promise.reject(error);
|
|
70
114
|
});
|
|
115
|
+
}
|
|
116
|
+
if (interceptorsConfig.context !== false) {
|
|
117
|
+
this.connection.interceptors.request.use((config) => {
|
|
118
|
+
const requestId = crypto.randomUUID();
|
|
119
|
+
lodash_1.default.set(config, 'headers.X-Request-Id', requestId);
|
|
120
|
+
const startTimestamp = performance.now();
|
|
121
|
+
lodash_1.default.set(config, 'startTimestamp', startTimestamp);
|
|
122
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushRequestContext).call(this, config, requestId);
|
|
123
|
+
return config;
|
|
124
|
+
});
|
|
71
125
|
this.connection.interceptors.response.use((response) => {
|
|
72
|
-
this.
|
|
126
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushResponseContext).call(this, response);
|
|
73
127
|
return response;
|
|
74
128
|
}, (error) => {
|
|
75
129
|
const response = lodash_1.default.get(error, 'response');
|
|
76
130
|
if (response) {
|
|
77
|
-
this.
|
|
131
|
+
__classPrivateFieldGet(this, _ApiService_instances, "m", _ApiService_pushResponseContext).call(this, response);
|
|
78
132
|
}
|
|
79
133
|
return Promise.reject(error);
|
|
80
134
|
});
|
|
135
|
+
}
|
|
136
|
+
if (interceptorsConfig.requestReplay !== false) {
|
|
81
137
|
this.connection.interceptors.response.use(undefined, (error) => {
|
|
82
138
|
const message = lodash_1.default.get(error, 'message');
|
|
83
139
|
const config = lodash_1.default.get(error, 'config');
|
|
@@ -96,66 +152,36 @@ class ApiService {
|
|
|
96
152
|
}
|
|
97
153
|
return Promise.reject(error);
|
|
98
154
|
});
|
|
99
|
-
this.connection.interceptors.request.use((config) => {
|
|
100
|
-
const requestId = crypto.randomUUID();
|
|
101
|
-
lodash_1.default.set(config, 'headers.X-Request-Id', requestId);
|
|
102
|
-
const startTimestamp = performance.now();
|
|
103
|
-
lodash_1.default.set(config, 'startTimestamp', startTimestamp);
|
|
104
|
-
this.pushRequestContext(config, requestId);
|
|
105
|
-
return config;
|
|
106
|
-
});
|
|
107
|
-
this.mock = null;
|
|
108
|
-
this.path = (v) => {
|
|
109
|
-
if (v.includes(':')) {
|
|
110
|
-
return new RegExp(v.replace(/:\w+/g, '[^/]+'));
|
|
111
|
-
}
|
|
112
|
-
return v;
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
setUnauthenticatedHandler({ handler, }) {
|
|
116
|
-
this.unauthenticatedHandler = handler;
|
|
117
155
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
ResponseHeaders: headers,
|
|
150
|
-
Route: config.url || 'Unknown',
|
|
151
|
-
Status: status,
|
|
152
|
-
};
|
|
153
|
-
const cleanResponseContext = lodash_1.default.omitBy(responseContext, lodash_1.default.isUndefined);
|
|
154
|
-
this.responseContextListener(cleanResponseContext);
|
|
155
|
-
}
|
|
156
|
-
initMock(mockSetup) {
|
|
157
|
-
this.mock = new axios_mock_adapter_1.default(this.connection);
|
|
158
|
-
mockSetup(this.mock);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
156
|
+
}, _ApiService_pushRequestContext = function _ApiService_pushRequestContext(config, requestId) {
|
|
157
|
+
const requestContext = {
|
|
158
|
+
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
159
|
+
RequestData: config.data,
|
|
160
|
+
RequestHeaders: config.headers,
|
|
161
|
+
RequestId: requestId,
|
|
162
|
+
RequestParams: config.params,
|
|
163
|
+
Route: config.url || 'Unknown',
|
|
164
|
+
};
|
|
165
|
+
const cleanRequestContext = lodash_1.default.omitBy(requestContext, lodash_1.default.isUndefined);
|
|
166
|
+
__classPrivateFieldGet(this, _ApiService_requestContextListener, "f").call(this, cleanRequestContext);
|
|
167
|
+
}, _ApiService_pushResponseContext = function _ApiService_pushResponseContext(response) {
|
|
168
|
+
const requestId = lodash_1.default.get(response.config, 'headers.X-Request-Id', '');
|
|
169
|
+
const { config, data, headers, status, } = response;
|
|
170
|
+
const endTimestamp = performance.now();
|
|
171
|
+
const startTimestamp = lodash_1.default.get(config, 'startTimestamp', endTimestamp);
|
|
172
|
+
const responseContext = {
|
|
173
|
+
Duration: endTimestamp - startTimestamp,
|
|
174
|
+
Method: `${(config.method || 'Unknown').toUpperCase()}`,
|
|
175
|
+
RequestData: config.data ? JSON.parse(config.data) : undefined,
|
|
176
|
+
RequestHeaders: config.headers,
|
|
177
|
+
RequestId: requestId,
|
|
178
|
+
RequestParams: config.params,
|
|
179
|
+
ResponseData: data,
|
|
180
|
+
ResponseHeaders: headers,
|
|
181
|
+
Route: config.url || 'Unknown',
|
|
182
|
+
Status: status,
|
|
183
|
+
};
|
|
184
|
+
const cleanResponseContext = lodash_1.default.omitBy(responseContext, lodash_1.default.isUndefined);
|
|
185
|
+
__classPrivateFieldGet(this, _ApiService_responseContextListener, "f").call(this, cleanResponseContext);
|
|
186
|
+
};
|
|
161
187
|
exports.default = ApiService;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { Agent } from 'https';
|
|
2
|
+
export type $ConfigInterceptors = {
|
|
3
|
+
context?: boolean;
|
|
4
|
+
requestReplay?: boolean;
|
|
5
|
+
unauth?: boolean;
|
|
6
|
+
};
|
|
2
7
|
export type $Config = {
|
|
3
8
|
apiUrl: string;
|
|
4
9
|
headers?: Record<string, string>;
|
|
5
10
|
httpsAgent?: Agent;
|
|
11
|
+
interceptors?: $ConfigInterceptors;
|
|
6
12
|
timeout: number;
|
|
7
13
|
};
|
|
8
14
|
export type $RequestContext = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vroskus/library-api",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Api",
|
|
5
5
|
"author": "Vilius Roškus <vilius@regattas.eu>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
],
|
|
53
53
|
"coverageThreshold": {
|
|
54
54
|
"global": {
|
|
55
|
-
"branches":
|
|
56
|
-
"functions":
|
|
57
|
-
"lines":
|
|
58
|
-
"statements":
|
|
55
|
+
"branches": 59.67,
|
|
56
|
+
"functions": 74.28,
|
|
57
|
+
"lines": 87.71,
|
|
58
|
+
"statements": 79.85
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
}
|