@adatechnology/http-client 0.0.1
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/CHANGELOG.md +7 -0
- package/README.md +596 -0
- package/dist/http.interface.js +27 -0
- package/dist/http.module.js +37 -0
- package/dist/http.provider.js +137 -0
- package/dist/http.token.js +6 -0
- package/dist/implementations/axios/axios.http.module.js +41 -0
- package/dist/implementations/axios/axios.http.provider.js +424 -0
- package/dist/implementations/axios/axios.http.token.js +4 -0
- package/dist/implementations/http.implementation.module.js +20 -0
- package/dist/index.js +11 -0
- package/dist/types/http.interface.d.ts +175 -0
- package/dist/types/http.module.d.ts +9 -0
- package/dist/types/http.provider.d.ts +43 -0
- package/dist/types/http.token.d.ts +3 -0
- package/dist/types/implementations/axios/axios.http.module.d.ts +5 -0
- package/dist/types/implementations/axios/axios.http.provider.d.ts +226 -0
- package/dist/types/implementations/axios/axios.http.token.d.ts +1 -0
- package/dist/types/implementations/http.implementation.module.d.ts +2 -0
- package/dist/types/index.d.ts +4 -0
- package/package.json +19 -0
- package/src/http.interface.ts +259 -0
- package/src/http.module.ts +27 -0
- package/src/http.provider.ts +219 -0
- package/src/http.token.ts +3 -0
- package/src/implementations/axios/axios.http.module.ts +33 -0
- package/src/implementations/axios/axios.http.provider.ts +603 -0
- package/src/implementations/axios/axios.http.token.ts +1 -0
- package/src/implementations/http.implementation.module.ts +9 -0
- package/src/index.ts +8 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.HttpProvider = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const http_token_1 = require("./http.token");
|
|
18
|
+
let HttpProvider = class HttpProvider {
|
|
19
|
+
axiosHttpProvider;
|
|
20
|
+
constructor(axiosHttpProvider) {
|
|
21
|
+
this.axiosHttpProvider = axiosHttpProvider;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Expose underlying axios instance when available from the axios provider implementation.
|
|
25
|
+
* This is intentionally typed as unknown/any to avoid leaking implementation details.
|
|
26
|
+
*/
|
|
27
|
+
getAxiosInstance() {
|
|
28
|
+
try {
|
|
29
|
+
// Some implementations expose `axiosInstance` or `instance`.
|
|
30
|
+
// Use brute-force access guarded in try/catch to avoid runtime errors.
|
|
31
|
+
const provider = this.axiosHttpProvider;
|
|
32
|
+
return provider?.axiosInstance ?? provider?.instance ?? undefined;
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async get(url, config) {
|
|
39
|
+
return this.axiosHttpProvider.get(url, config);
|
|
40
|
+
}
|
|
41
|
+
get$(url, config) {
|
|
42
|
+
return this.axiosHttpProvider.get$(url, config);
|
|
43
|
+
}
|
|
44
|
+
async post(url, data, config) {
|
|
45
|
+
return this.axiosHttpProvider.post(url, data, config);
|
|
46
|
+
}
|
|
47
|
+
post$(url, data, config) {
|
|
48
|
+
return this.axiosHttpProvider.post$(url, data, config);
|
|
49
|
+
}
|
|
50
|
+
async put(url, data, config) {
|
|
51
|
+
return this.axiosHttpProvider.put(url, data, config);
|
|
52
|
+
}
|
|
53
|
+
put$(url, data, config) {
|
|
54
|
+
return this.axiosHttpProvider.put$(url, data, config);
|
|
55
|
+
}
|
|
56
|
+
async patch(url, data, config) {
|
|
57
|
+
return this.axiosHttpProvider.patch(url, data, config);
|
|
58
|
+
}
|
|
59
|
+
patch$(url, data, config) {
|
|
60
|
+
return this.axiosHttpProvider.patch$(url, data, config);
|
|
61
|
+
}
|
|
62
|
+
async delete(url, config) {
|
|
63
|
+
return this.axiosHttpProvider.delete(url, config);
|
|
64
|
+
}
|
|
65
|
+
delete$(url, config) {
|
|
66
|
+
return this.axiosHttpProvider.delete$(url, config);
|
|
67
|
+
}
|
|
68
|
+
async head(url, config) {
|
|
69
|
+
return this.axiosHttpProvider.head(url, config);
|
|
70
|
+
}
|
|
71
|
+
head$(url, config) {
|
|
72
|
+
return this.axiosHttpProvider.head$(url, config);
|
|
73
|
+
}
|
|
74
|
+
async options(url, config) {
|
|
75
|
+
return this.axiosHttpProvider.options(url, config);
|
|
76
|
+
}
|
|
77
|
+
options$(url, config) {
|
|
78
|
+
return this.axiosHttpProvider.options$(url, config);
|
|
79
|
+
}
|
|
80
|
+
async request(config) {
|
|
81
|
+
return this.axiosHttpProvider.request(config);
|
|
82
|
+
}
|
|
83
|
+
request$(config) {
|
|
84
|
+
return this.axiosHttpProvider.request$(config);
|
|
85
|
+
}
|
|
86
|
+
setGlobalHeader(key, value) {
|
|
87
|
+
this.axiosHttpProvider.setGlobalHeader(key, value);
|
|
88
|
+
}
|
|
89
|
+
removeGlobalHeader(key) {
|
|
90
|
+
this.axiosHttpProvider.removeGlobalHeader(key);
|
|
91
|
+
}
|
|
92
|
+
getGlobalHeaders() {
|
|
93
|
+
return this.axiosHttpProvider.getGlobalHeaders();
|
|
94
|
+
}
|
|
95
|
+
setBaseUrl(baseUrl) {
|
|
96
|
+
this.axiosHttpProvider.setBaseUrl(baseUrl);
|
|
97
|
+
}
|
|
98
|
+
getBaseUrl() {
|
|
99
|
+
return this.axiosHttpProvider.getBaseUrl();
|
|
100
|
+
}
|
|
101
|
+
setDefaultTimeout(timeout) {
|
|
102
|
+
this.axiosHttpProvider.setDefaultTimeout(timeout);
|
|
103
|
+
}
|
|
104
|
+
addErrorInterceptor(interceptor) {
|
|
105
|
+
return this.axiosHttpProvider.addErrorInterceptor(interceptor);
|
|
106
|
+
}
|
|
107
|
+
removeErrorInterceptor(id) {
|
|
108
|
+
this.axiosHttpProvider.removeErrorInterceptor(id);
|
|
109
|
+
}
|
|
110
|
+
clearCache(key) {
|
|
111
|
+
this.axiosHttpProvider.clearCache(key);
|
|
112
|
+
}
|
|
113
|
+
setAuthToken(token, type) {
|
|
114
|
+
this.axiosHttpProvider.setAuthToken(token, type);
|
|
115
|
+
}
|
|
116
|
+
clearAuthToken() {
|
|
117
|
+
this.axiosHttpProvider.clearAuthToken();
|
|
118
|
+
}
|
|
119
|
+
addRequestInterceptor(onFulfilled, onRejected) {
|
|
120
|
+
return this.axiosHttpProvider.addRequestInterceptor(onFulfilled, onRejected);
|
|
121
|
+
}
|
|
122
|
+
removeRequestInterceptor(id) {
|
|
123
|
+
this.axiosHttpProvider.removeRequestInterceptor(id);
|
|
124
|
+
}
|
|
125
|
+
addResponseInterceptor(onFulfilled, onRejected) {
|
|
126
|
+
return this.axiosHttpProvider.addResponseInterceptor(onFulfilled, onRejected);
|
|
127
|
+
}
|
|
128
|
+
removeResponseInterceptor(id) {
|
|
129
|
+
this.axiosHttpProvider.removeResponseInterceptor(id);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
exports.HttpProvider = HttpProvider;
|
|
133
|
+
exports.HttpProvider = HttpProvider = __decorate([
|
|
134
|
+
(0, common_1.Injectable)(),
|
|
135
|
+
__param(0, (0, common_1.Inject)(http_token_1.HTTP_AXIOS_PROVIDER)),
|
|
136
|
+
__metadata("design:paramtypes", [Object])
|
|
137
|
+
], HttpProvider);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTP_PROVIDER = exports.HTTP_AXIOS_PROVIDER = exports.HTTP_AXIOS_CONNECTION = void 0;
|
|
4
|
+
exports.HTTP_AXIOS_CONNECTION = 'HTTP_AXIOS_CONNECTION';
|
|
5
|
+
exports.HTTP_AXIOS_PROVIDER = 'HTTP_AXIOS_PROVIDER';
|
|
6
|
+
exports.HTTP_PROVIDER = 'HTTP_PROVIDER';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var HttpImplementationAxiosModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.HttpImplementationAxiosModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const axios_1 = require("axios");
|
|
13
|
+
const http_token_1 = require("../../http.token");
|
|
14
|
+
const axios_http_provider_1 = require("./axios.http.provider");
|
|
15
|
+
let HttpImplementationAxiosModule = HttpImplementationAxiosModule_1 = class HttpImplementationAxiosModule {
|
|
16
|
+
static forRoot(config) {
|
|
17
|
+
const axiosInstance = config && config.request
|
|
18
|
+
? config
|
|
19
|
+
: axios_1.default.create(config);
|
|
20
|
+
const providers = [
|
|
21
|
+
{
|
|
22
|
+
provide: http_token_1.HTTP_AXIOS_CONNECTION,
|
|
23
|
+
useValue: axiosInstance,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
provide: http_token_1.HTTP_AXIOS_PROVIDER,
|
|
27
|
+
useFactory: (conn) => new axios_http_provider_1.AxiosHttpProvider(conn),
|
|
28
|
+
inject: [http_token_1.HTTP_AXIOS_CONNECTION],
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
return {
|
|
32
|
+
module: HttpImplementationAxiosModule_1,
|
|
33
|
+
providers,
|
|
34
|
+
exports: [http_token_1.HTTP_AXIOS_CONNECTION, http_token_1.HTTP_AXIOS_PROVIDER],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.HttpImplementationAxiosModule = HttpImplementationAxiosModule;
|
|
39
|
+
exports.HttpImplementationAxiosModule = HttpImplementationAxiosModule = HttpImplementationAxiosModule_1 = __decorate([
|
|
40
|
+
(0, common_1.Module)({})
|
|
41
|
+
], HttpImplementationAxiosModule);
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AxiosHttpProvider = void 0;
|
|
4
|
+
const axios_1 = require("axios");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
/**
|
|
7
|
+
* Axios-based implementation of the HTTP provider interface.
|
|
8
|
+
* Provides HTTP client functionality with both Promise and Observable APIs,
|
|
9
|
+
* plus basic caching capabilities.
|
|
10
|
+
*/
|
|
11
|
+
class AxiosHttpProvider {
|
|
12
|
+
axiosInstance;
|
|
13
|
+
errorInterceptors = new Map();
|
|
14
|
+
nextErrorInterceptorId = 0;
|
|
15
|
+
requestInterceptorIds = new Set();
|
|
16
|
+
responseInterceptorIds = new Set();
|
|
17
|
+
cache = new Map();
|
|
18
|
+
cacheCleanupInterval;
|
|
19
|
+
constructor(axiosInstance) {
|
|
20
|
+
this.axiosInstance = axiosInstance || axios_1.default.create();
|
|
21
|
+
this.startCacheCleanup();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Starts automatic cache cleanup
|
|
25
|
+
*/
|
|
26
|
+
startCacheCleanup() {
|
|
27
|
+
// Clean expired cache entries every 5 minutes
|
|
28
|
+
this.cacheCleanupInterval = setInterval(() => {
|
|
29
|
+
this.cleanupExpiredCache();
|
|
30
|
+
}, 300000);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Stops automatic cache cleanup
|
|
34
|
+
*/
|
|
35
|
+
stopCacheCleanup() {
|
|
36
|
+
if (this.cacheCleanupInterval) {
|
|
37
|
+
clearInterval(this.cacheCleanupInterval);
|
|
38
|
+
this.cacheCleanupInterval = undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Cleans up expired cache entries
|
|
43
|
+
*/
|
|
44
|
+
cleanupExpiredCache() {
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
const keysToDelete = [];
|
|
47
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
48
|
+
if (now - entry.timestamp > entry.ttl) {
|
|
49
|
+
keysToDelete.push(key);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
keysToDelete.forEach((key) => this.cache.delete(key));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Generates a cache key from URL and config
|
|
56
|
+
*/
|
|
57
|
+
generateCacheKey(url, config) {
|
|
58
|
+
const params = config?.params ? JSON.stringify(config.params) : "";
|
|
59
|
+
return `${url}${params}`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets cached data if valid
|
|
63
|
+
*/
|
|
64
|
+
getCached(key) {
|
|
65
|
+
const entry = this.cache.get(key);
|
|
66
|
+
if (!entry)
|
|
67
|
+
return null;
|
|
68
|
+
const now = Date.now();
|
|
69
|
+
if (now - entry.timestamp > entry.ttl) {
|
|
70
|
+
this.cache.delete(key);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return entry.data;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Sets data in cache
|
|
77
|
+
*/
|
|
78
|
+
setCache(key, data, ttl = 300000) {
|
|
79
|
+
// 5 minutes default
|
|
80
|
+
this.cache.set(key, {
|
|
81
|
+
data,
|
|
82
|
+
timestamp: Date.now(),
|
|
83
|
+
ttl,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Clears cache for a specific key or all cache
|
|
88
|
+
*/
|
|
89
|
+
clearCache(key) {
|
|
90
|
+
if (key) {
|
|
91
|
+
this.cache.delete(key);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
this.cache.clear();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets cache statistics
|
|
99
|
+
*/
|
|
100
|
+
getCacheStats() {
|
|
101
|
+
return {
|
|
102
|
+
size: this.cache.size,
|
|
103
|
+
keys: Array.from(this.cache.keys()),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Sets a global header that will be included in all requests.
|
|
108
|
+
*/
|
|
109
|
+
setGlobalHeader(key, value) {
|
|
110
|
+
this.axiosInstance.defaults.headers.common[key] = value;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Removes a global header.
|
|
114
|
+
*/
|
|
115
|
+
removeGlobalHeader(key) {
|
|
116
|
+
delete this.axiosInstance.defaults.headers.common[key];
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Gets all global headers currently set.
|
|
120
|
+
*/
|
|
121
|
+
getGlobalHeaders() {
|
|
122
|
+
return { ...this.axiosInstance.defaults.headers.common };
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Sets the base URL for all requests.
|
|
126
|
+
*/
|
|
127
|
+
setBaseUrl(baseUrl) {
|
|
128
|
+
this.axiosInstance.defaults.baseURL = baseUrl;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Gets the current base URL.
|
|
132
|
+
*/
|
|
133
|
+
getBaseUrl() {
|
|
134
|
+
return this.axiosInstance.defaults.baseURL || "";
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Sets the default timeout for all requests.
|
|
138
|
+
*/
|
|
139
|
+
setDefaultTimeout(timeout) {
|
|
140
|
+
this.axiosInstance.defaults.timeout = timeout;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Adds an error interceptor for handling errors globally.
|
|
144
|
+
*/
|
|
145
|
+
addErrorInterceptor(interceptor) {
|
|
146
|
+
const id = this.nextErrorInterceptorId++;
|
|
147
|
+
this.errorInterceptors.set(id, interceptor);
|
|
148
|
+
return id;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Adds a request interceptor to the underlying Axios instance.
|
|
152
|
+
*/
|
|
153
|
+
addRequestInterceptor(onFulfilled, onRejected) {
|
|
154
|
+
const id = this.axiosInstance.interceptors.request.use(onFulfilled, onRejected);
|
|
155
|
+
this.requestInterceptorIds.add(id);
|
|
156
|
+
return id;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Removes a request interceptor by id.
|
|
160
|
+
*/
|
|
161
|
+
removeRequestInterceptor(id) {
|
|
162
|
+
this.axiosInstance.interceptors.request.eject(id);
|
|
163
|
+
this.requestInterceptorIds.delete(id);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Adds a response interceptor to the underlying Axios instance.
|
|
167
|
+
*/
|
|
168
|
+
addResponseInterceptor(onFulfilled, onRejected) {
|
|
169
|
+
const id = this.axiosInstance.interceptors.response.use(onFulfilled, onRejected);
|
|
170
|
+
this.responseInterceptorIds.add(id);
|
|
171
|
+
return id;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Removes a response interceptor by id.
|
|
175
|
+
*/
|
|
176
|
+
removeResponseInterceptor(id) {
|
|
177
|
+
this.axiosInstance.interceptors.response.eject(id);
|
|
178
|
+
this.responseInterceptorIds.delete(id);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Removes an error interceptor by its ID.
|
|
182
|
+
*/
|
|
183
|
+
removeErrorInterceptor(id) {
|
|
184
|
+
this.errorInterceptors.delete(id);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Performs a GET request to the specified URL.
|
|
188
|
+
*/
|
|
189
|
+
async get(url, config) {
|
|
190
|
+
const cacheKey = this.generateCacheKey(url, config);
|
|
191
|
+
const cached = this.getCached(cacheKey);
|
|
192
|
+
if (cached) {
|
|
193
|
+
return {
|
|
194
|
+
data: cached,
|
|
195
|
+
status: 200,
|
|
196
|
+
statusText: "OK",
|
|
197
|
+
headers: {},
|
|
198
|
+
config: config || { url },
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return this.wrapWithErrorInterceptors(() => this.performGet(url, config, cacheKey));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Internal method to perform the actual GET request.
|
|
205
|
+
*/
|
|
206
|
+
async performGet(url, config, cacheKey) {
|
|
207
|
+
const response = await this.axiosInstance.get(url, config);
|
|
208
|
+
const transformed = this.transformResponse(response);
|
|
209
|
+
if (cacheKey && config?.cache !== false) {
|
|
210
|
+
this.setCache(cacheKey, transformed.data, config?.cacheTtl);
|
|
211
|
+
}
|
|
212
|
+
return transformed;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Performs a GET request and returns an Observable.
|
|
216
|
+
*/
|
|
217
|
+
get$(url, config) {
|
|
218
|
+
return (0, rxjs_1.from)(this.get(url, config));
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Performs a POST request.
|
|
222
|
+
*/
|
|
223
|
+
async post(url, data, config) {
|
|
224
|
+
return this.wrapWithErrorInterceptors(() => this.performPost(url, data, config));
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Internal method to perform the actual POST request.
|
|
228
|
+
*/
|
|
229
|
+
async performPost(url, data, config) {
|
|
230
|
+
const response = await this.axiosInstance.post(url, data, config);
|
|
231
|
+
return this.transformResponse(response);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Performs a POST request and returns an Observable.
|
|
235
|
+
*/
|
|
236
|
+
post$(url, data, config) {
|
|
237
|
+
return (0, rxjs_1.from)(this.post(url, data, config));
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Performs a PUT request.
|
|
241
|
+
*/
|
|
242
|
+
async put(url, data, config) {
|
|
243
|
+
return this.wrapWithErrorInterceptors(() => this.performPut(url, data, config));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Internal method to perform the actual PUT request.
|
|
247
|
+
*/
|
|
248
|
+
async performPut(url, data, config) {
|
|
249
|
+
const response = await this.axiosInstance.put(url, data, config);
|
|
250
|
+
return this.transformResponse(response);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Performs a PUT request and returns an Observable.
|
|
254
|
+
*/
|
|
255
|
+
put$(url, data, config) {
|
|
256
|
+
return (0, rxjs_1.from)(this.put(url, data, config));
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Performs a PATCH request.
|
|
260
|
+
*/
|
|
261
|
+
async patch(url, data, config) {
|
|
262
|
+
return this.wrapWithErrorInterceptors(() => this.performPatch(url, data, config));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Internal method to perform the actual PATCH request.
|
|
266
|
+
*/
|
|
267
|
+
async performPatch(url, data, config) {
|
|
268
|
+
const response = await this.axiosInstance.patch(url, data, config);
|
|
269
|
+
return this.transformResponse(response);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Performs a PATCH request and returns an Observable.
|
|
273
|
+
*/
|
|
274
|
+
patch$(url, data, config) {
|
|
275
|
+
return (0, rxjs_1.from)(this.patch(url, data, config));
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Performs a DELETE request.
|
|
279
|
+
*/
|
|
280
|
+
async delete(url, config) {
|
|
281
|
+
return this.wrapWithErrorInterceptors(() => this.performDelete(url, config));
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Internal method to perform the actual DELETE request.
|
|
285
|
+
*/
|
|
286
|
+
async performDelete(url, config) {
|
|
287
|
+
const response = await this.axiosInstance.delete(url, config);
|
|
288
|
+
return this.transformResponse(response);
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Performs a DELETE request and returns an Observable.
|
|
292
|
+
*/
|
|
293
|
+
delete$(url, config) {
|
|
294
|
+
return (0, rxjs_1.from)(this.delete(url, config));
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Performs a HEAD request.
|
|
298
|
+
*/
|
|
299
|
+
async head(url, config) {
|
|
300
|
+
return this.wrapWithErrorInterceptors(() => this.performHead(url, config));
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Internal method to perform the actual HEAD request.
|
|
304
|
+
*/
|
|
305
|
+
async performHead(url, config) {
|
|
306
|
+
const response = await this.axiosInstance.head(url, config);
|
|
307
|
+
return this.transformResponse(response);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Performs a HEAD request and returns an Observable.
|
|
311
|
+
*/
|
|
312
|
+
head$(url, config) {
|
|
313
|
+
return (0, rxjs_1.from)(this.head(url, config));
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Performs an OPTIONS request.
|
|
317
|
+
*/
|
|
318
|
+
async options(url, config) {
|
|
319
|
+
return this.wrapWithErrorInterceptors(() => this.performOptions(url, config));
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Internal method to perform the actual OPTIONS request.
|
|
323
|
+
*/
|
|
324
|
+
async performOptions(url, config) {
|
|
325
|
+
const response = await this.axiosInstance.options(url, config);
|
|
326
|
+
return this.transformResponse(response);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Performs an OPTIONS request and returns an Observable.
|
|
330
|
+
*/
|
|
331
|
+
options$(url, config) {
|
|
332
|
+
return (0, rxjs_1.from)(this.options(url, config));
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Performs a custom HTTP request.
|
|
336
|
+
*/
|
|
337
|
+
async request(config) {
|
|
338
|
+
return this.wrapWithErrorInterceptors(() => this.performRequest(config));
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Internal method to perform the actual custom request.
|
|
342
|
+
*/
|
|
343
|
+
async performRequest(config) {
|
|
344
|
+
const response = await this.axiosInstance.request(config);
|
|
345
|
+
return this.transformResponse(response);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Performs a custom HTTP request and returns an Observable.
|
|
349
|
+
*/
|
|
350
|
+
request$(config) {
|
|
351
|
+
return (0, rxjs_1.from)(this.request(config));
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Sets the authorization token for requests.
|
|
355
|
+
*/
|
|
356
|
+
setAuthToken(token, type = "Bearer") {
|
|
357
|
+
this.axiosInstance.defaults.headers.common["Authorization"] =
|
|
358
|
+
`${type} ${token}`;
|
|
359
|
+
// Log only the initial part of the token to avoid exposing the full secret in logs
|
|
360
|
+
try {
|
|
361
|
+
const masked = AxiosHttpProvider.maskToken(token);
|
|
362
|
+
console.debug(`[AxiosHttpProvider] setAuthToken ${type} ${masked}`);
|
|
363
|
+
}
|
|
364
|
+
catch (err) {
|
|
365
|
+
// swallow logging errors
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Clears the authorization token.
|
|
370
|
+
*/
|
|
371
|
+
clearAuthToken() {
|
|
372
|
+
delete this.axiosInstance.defaults.headers.common["Authorization"];
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Processes an error through all registered error interceptors.
|
|
376
|
+
*/
|
|
377
|
+
async processErrorInterceptors(error) {
|
|
378
|
+
let processedError = error;
|
|
379
|
+
for (const interceptor of this.errorInterceptors.values()) {
|
|
380
|
+
try {
|
|
381
|
+
processedError = await interceptor(processedError);
|
|
382
|
+
}
|
|
383
|
+
catch (interceptorError) {
|
|
384
|
+
console.warn("Error interceptor failed:", interceptorError);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return processedError;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Returns a masked version of the token showing only the initial characters.
|
|
391
|
+
*/
|
|
392
|
+
static maskToken(token, visibleChars = 8) {
|
|
393
|
+
if (!token || typeof token !== "string")
|
|
394
|
+
return "";
|
|
395
|
+
return token.length <= visibleChars
|
|
396
|
+
? token
|
|
397
|
+
: `${token.slice(0, visibleChars)}...`;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Wraps a promise-returning HTTP method with error interceptor processing.
|
|
401
|
+
*/
|
|
402
|
+
async wrapWithErrorInterceptors(method) {
|
|
403
|
+
try {
|
|
404
|
+
return await method();
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
const processedError = await this.processErrorInterceptors(error);
|
|
408
|
+
throw processedError;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Transforms an Axios response to the standardized HTTP response format.
|
|
413
|
+
*/
|
|
414
|
+
transformResponse(response) {
|
|
415
|
+
return {
|
|
416
|
+
data: response.data,
|
|
417
|
+
status: response.status,
|
|
418
|
+
statusText: response.statusText,
|
|
419
|
+
headers: response.headers,
|
|
420
|
+
config: response.config,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
exports.AxiosHttpProvider = AxiosHttpProvider;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.HttpImplementationModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const axios_http_module_1 = require("./axios/axios.http.module");
|
|
12
|
+
let HttpImplementationModule = class HttpImplementationModule {
|
|
13
|
+
};
|
|
14
|
+
exports.HttpImplementationModule = HttpImplementationModule;
|
|
15
|
+
exports.HttpImplementationModule = HttpImplementationModule = __decorate([
|
|
16
|
+
(0, common_1.Module)({
|
|
17
|
+
imports: [axios_http_module_1.HttpImplementationAxiosModule],
|
|
18
|
+
exports: [axios_http_module_1.HttpImplementationAxiosModule],
|
|
19
|
+
})
|
|
20
|
+
], HttpImplementationModule);
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTP_AXIOS_CONNECTION = exports.HTTP_AXIOS_PROVIDER = exports.HTTP_PROVIDER = exports.HttpImplementationAxiosModule = exports.HttpModule = void 0;
|
|
4
|
+
var http_module_1 = require("./http.module");
|
|
5
|
+
Object.defineProperty(exports, "HttpModule", { enumerable: true, get: function () { return http_module_1.HttpModule; } });
|
|
6
|
+
var axios_http_module_1 = require("./implementations/axios/axios.http.module");
|
|
7
|
+
Object.defineProperty(exports, "HttpImplementationAxiosModule", { enumerable: true, get: function () { return axios_http_module_1.HttpImplementationAxiosModule; } });
|
|
8
|
+
var http_token_1 = require("./http.token");
|
|
9
|
+
Object.defineProperty(exports, "HTTP_PROVIDER", { enumerable: true, get: function () { return http_token_1.HTTP_PROVIDER; } });
|
|
10
|
+
Object.defineProperty(exports, "HTTP_AXIOS_PROVIDER", { enumerable: true, get: function () { return http_token_1.HTTP_AXIOS_PROVIDER; } });
|
|
11
|
+
Object.defineProperty(exports, "HTTP_AXIOS_CONNECTION", { enumerable: true, get: function () { return http_token_1.HTTP_AXIOS_CONNECTION; } });
|