@sgfe/axios 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintignore +1 -0
- package/.eslintrc.js +26 -0
- package/.prettierrc +6 -0
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/http.constants.d.ts +3 -0
- package/dist/http.constants.js +6 -0
- package/dist/http.module.d.ts +8 -0
- package/dist/http.module.js +115 -0
- package/dist/http.service.d.ts +13 -0
- package/dist/http.service.js +44 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +23 -0
- package/dist/interfaces/http-module.interface.d.ts +14 -0
- package/dist/interfaces/http-module.interface.js +2 -0
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.js +17 -0
- package/lib/http.constants.ts +3 -0
- package/lib/http.module.ts +114 -0
- package/lib/http.service.ts +33 -0
- package/lib/index.ts +3 -0
- package/lib/interfaces/http-module.interface.ts +20 -0
- package/lib/interfaces/index.ts +1 -0
- package/package.json +60 -0
- package/tsconfig.json +19 -0
package/.eslintignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.eslintrc.js
|
package/.eslintrc.js
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module.exports = {
|
2
|
+
parser: '@typescript-eslint/parser',
|
3
|
+
parserOptions: {
|
4
|
+
project: './tsconfig.json',
|
5
|
+
sourceType: 'module',
|
6
|
+
},
|
7
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
8
|
+
extends: [
|
9
|
+
'plugin:@typescript-eslint/eslint-recommended',
|
10
|
+
'plugin:@typescript-eslint/recommended',
|
11
|
+
'prettier',
|
12
|
+
],
|
13
|
+
root: true,
|
14
|
+
env: {
|
15
|
+
node: true,
|
16
|
+
jest: true,
|
17
|
+
},
|
18
|
+
rules: {
|
19
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
20
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
21
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
22
|
+
'@typescript-eslint/no-use-before-define': 'off',
|
23
|
+
'@typescript-eslint/no-non-null-assertion': 'off',
|
24
|
+
},
|
25
|
+
};
|
26
|
+
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 benhason1
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# nestjs-http-promise
|
2
|
+
[![npm version](https://img.shields.io/npm/v/nestjs-http-promise.svg?style=flat-square)](https://www.npmjs.org/package/nestjs-http-promise)
|
3
|
+
[![npm downloads](https://img.shields.io/npm/dm/nestjs-http-promise.svg?style=flat-square)](http://npm-stat.com/charts.html?package=nestjs-http-promise)
|
4
|
+
|
5
|
+
## description
|
6
|
+
nestjs module that just doing little modification to the original and good **nestjs** http module.
|
7
|
+
|
8
|
+
|
9
|
+
## features
|
10
|
+
* axios - the most used package for http requests in npm and the one used by nestjs official http library.
|
11
|
+
* better axios stack trace - axios has an [open issue](https://github.com/axios/axios/issues/2387) about improvement of their stack trace.
|
12
|
+
in this library there is a default interceptor that will intercept the stack trace and will add data to it.
|
13
|
+
* promise based - most of us using the current http module that uses observable which we don't use most of the time
|
14
|
+
and in order to avoid it were just calling `.toPromise()` every http call.
|
15
|
+
* retries - in many cases we will want to retry a failing http call.
|
16
|
+
with observable we could just add the retry operator (rxjs) but with promises we need to implement this logic ourselves.
|
17
|
+
this package will make it easy for you, just pass `{ retries: NUMBER_OF_RETRIES }` in the config of the http module.
|
18
|
+
**more details in the configuration section**
|
19
|
+
|
20
|
+
## quick start
|
21
|
+
### installing
|
22
|
+
Using npm:
|
23
|
+
```
|
24
|
+
$ npm install nestjs-http-promise
|
25
|
+
```
|
26
|
+
|
27
|
+
Using yarn:
|
28
|
+
```
|
29
|
+
$ yarn add nestjs-http-promise
|
30
|
+
```
|
31
|
+
|
32
|
+
### usage - just like every nest.js module
|
33
|
+
import the module:
|
34
|
+
```ts
|
35
|
+
import { HttpModule } from 'nestjs-http-promise'
|
36
|
+
|
37
|
+
@Module({
|
38
|
+
imports: [HttpModule]
|
39
|
+
})
|
40
|
+
```
|
41
|
+
|
42
|
+
inject the service in the class:
|
43
|
+
```ts
|
44
|
+
import { HttpService } from 'nestjs-http-promise'
|
45
|
+
|
46
|
+
class Demo {
|
47
|
+
constructor(private readonly httpService: HttpService) {}
|
48
|
+
}
|
49
|
+
```
|
50
|
+
|
51
|
+
use the service:
|
52
|
+
```ts
|
53
|
+
public callSomeServer(): Promise<object> {
|
54
|
+
return this.httpService.get('http://fakeService')
|
55
|
+
}
|
56
|
+
```
|
57
|
+
|
58
|
+
## configuration
|
59
|
+
|
60
|
+
the service uses axios and axios-retry, so you can pass any [AxiosRequestConfig](https://github.com/axios/axios#request-config)
|
61
|
+
And/Or [AxiosRetryConfig](https://github.com/softonic/axios-retry#options)
|
62
|
+
|
63
|
+
just pass it in the `.register()` method as you would do in the original nestjs httpModule
|
64
|
+
```ts
|
65
|
+
import { HttpModule } from 'nestjs-http-promise'
|
66
|
+
|
67
|
+
@Module({
|
68
|
+
imports: [HttpModule.register(
|
69
|
+
{
|
70
|
+
timeout: 1000,
|
71
|
+
retries: 5,
|
72
|
+
...
|
73
|
+
}
|
74
|
+
)]
|
75
|
+
})
|
76
|
+
```
|
77
|
+
|
78
|
+
### default configuration
|
79
|
+
* default config of axios-retry : https://github.com/softonic/axios-retry#options
|
80
|
+
* better axios stack trace is added by default, you can turn it off by passing the **isBetterStackTraceEnabled** to false.
|
81
|
+
|
82
|
+
## async configuration
|
83
|
+
When you need to pass module options asynchronously instead of statically, use the `registerAsync()` method **just like in nest httpModule**.
|
84
|
+
|
85
|
+
you have a couple of techniques to do it:
|
86
|
+
* with the useFactory
|
87
|
+
```ts
|
88
|
+
HttpModule.registerAsync({
|
89
|
+
useFactory: () => ({
|
90
|
+
timeout: 1000,
|
91
|
+
retries: 5,
|
92
|
+
...
|
93
|
+
}),
|
94
|
+
});
|
95
|
+
```
|
96
|
+
|
97
|
+
* using class
|
98
|
+
|
99
|
+
```ts
|
100
|
+
HttpModule.registerAsync({
|
101
|
+
useClass: HttpConfigService,
|
102
|
+
});
|
103
|
+
```
|
104
|
+
Note that in this example, the HttpConfigService has to implement HttpModuleOptionsFactory interface as shown below.
|
105
|
+
```ts
|
106
|
+
@Injectable()
|
107
|
+
class HttpConfigService implements HttpModuleOptionsFactory {
|
108
|
+
async createHttpOptions(): Promise<HttpModuleOptions> {
|
109
|
+
const configurationData = await someAsyncMethod();
|
110
|
+
return {
|
111
|
+
timeout: configurationData.timeout,
|
112
|
+
retries: 5,
|
113
|
+
...
|
114
|
+
};
|
115
|
+
}
|
116
|
+
}
|
117
|
+
```
|
118
|
+
If you want to reuse an existing options provider instead of creating a copy inside the HttpModule,
|
119
|
+
use the useExisting syntax.
|
120
|
+
```ts
|
121
|
+
HttpModule.registerAsync({
|
122
|
+
imports: [ConfigModule],
|
123
|
+
useExisting: ConfigService,
|
124
|
+
});
|
125
|
+
```
|
@@ -0,0 +1,6 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.HTTP_MODULE_OPTIONS = exports.HTTP_MODULE_ID = exports.AXIOS_INSTANCE_TOKEN = void 0;
|
4
|
+
exports.AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN';
|
5
|
+
exports.HTTP_MODULE_ID = 'HTTP_MODULE_ID';
|
6
|
+
exports.HTTP_MODULE_OPTIONS = 'HTTP_MODULE_OPTIONS';
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
2
|
+
import { HttpModuleAsyncOptions, HttpModuleOptions } from './interfaces';
|
3
|
+
export declare class HttpModule {
|
4
|
+
static register(config: HttpModuleOptions): DynamicModule;
|
5
|
+
static registerAsync(options: HttpModuleAsyncOptions): DynamicModule;
|
6
|
+
private static createAsyncProviders;
|
7
|
+
private static createAsyncOptionsProvider;
|
8
|
+
}
|
@@ -0,0 +1,115 @@
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
15
|
+
});
|
16
|
+
};
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
19
|
+
};
|
20
|
+
var HttpModule_1;
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
22
|
+
exports.HttpModule = void 0;
|
23
|
+
const common_1 = require("@nestjs/common");
|
24
|
+
const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
25
|
+
const axios_1 = __importDefault(require("axios"));
|
26
|
+
const http_constants_1 = require("./http.constants");
|
27
|
+
const http_service_1 = require("./http.service");
|
28
|
+
const axios_retry_1 = __importDefault(require("axios-retry"));
|
29
|
+
const createAxiosInstance = (config) => {
|
30
|
+
const axiosInstance = axios_1.default.create(config);
|
31
|
+
(0, axios_retry_1.default)(axiosInstance, config);
|
32
|
+
return axiosInstance;
|
33
|
+
};
|
34
|
+
let HttpModule = HttpModule_1 = class HttpModule {
|
35
|
+
static register(config) {
|
36
|
+
return {
|
37
|
+
module: HttpModule_1,
|
38
|
+
providers: [
|
39
|
+
{
|
40
|
+
provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
41
|
+
useValue: createAxiosInstance(config),
|
42
|
+
},
|
43
|
+
{
|
44
|
+
provide: http_constants_1.HTTP_MODULE_ID,
|
45
|
+
useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
|
46
|
+
},
|
47
|
+
],
|
48
|
+
};
|
49
|
+
}
|
50
|
+
static registerAsync(options) {
|
51
|
+
return {
|
52
|
+
module: HttpModule_1,
|
53
|
+
imports: options.imports,
|
54
|
+
providers: [
|
55
|
+
...this.createAsyncProviders(options),
|
56
|
+
{
|
57
|
+
provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
58
|
+
useFactory: (config) => createAxiosInstance(config),
|
59
|
+
inject: [http_constants_1.HTTP_MODULE_OPTIONS],
|
60
|
+
},
|
61
|
+
{
|
62
|
+
provide: http_constants_1.HTTP_MODULE_ID,
|
63
|
+
useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
|
64
|
+
},
|
65
|
+
...(options.extraProviders || []),
|
66
|
+
],
|
67
|
+
};
|
68
|
+
}
|
69
|
+
static createAsyncProviders(options) {
|
70
|
+
if (options.useExisting || options.useFactory) {
|
71
|
+
return [this.createAsyncOptionsProvider(options)];
|
72
|
+
}
|
73
|
+
const providers = [
|
74
|
+
this.createAsyncOptionsProvider(options)
|
75
|
+
];
|
76
|
+
if (options.useClass)
|
77
|
+
providers.push({
|
78
|
+
provide: options.useClass,
|
79
|
+
useClass: options.useClass,
|
80
|
+
});
|
81
|
+
return providers;
|
82
|
+
}
|
83
|
+
static createAsyncOptionsProvider(options) {
|
84
|
+
if (options.useFactory) {
|
85
|
+
return {
|
86
|
+
provide: http_constants_1.HTTP_MODULE_OPTIONS,
|
87
|
+
useFactory: options.useFactory,
|
88
|
+
inject: options.inject || [],
|
89
|
+
};
|
90
|
+
}
|
91
|
+
let inject;
|
92
|
+
if (options.useExisting)
|
93
|
+
inject = [options.useExisting];
|
94
|
+
else if (options.useClass)
|
95
|
+
inject = [options.useClass];
|
96
|
+
return {
|
97
|
+
provide: http_constants_1.HTTP_MODULE_OPTIONS,
|
98
|
+
useFactory: (optionsFactory) => __awaiter(this, void 0, void 0, function* () { return optionsFactory.createHttpOptions(); }),
|
99
|
+
inject,
|
100
|
+
};
|
101
|
+
}
|
102
|
+
};
|
103
|
+
HttpModule = HttpModule_1 = __decorate([
|
104
|
+
(0, common_1.Module)({
|
105
|
+
providers: [
|
106
|
+
http_service_1.HttpService,
|
107
|
+
{
|
108
|
+
provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
109
|
+
useValue: createAxiosInstance(),
|
110
|
+
},
|
111
|
+
],
|
112
|
+
exports: [http_service_1.HttpService],
|
113
|
+
})
|
114
|
+
], HttpModule);
|
115
|
+
exports.HttpModule = HttpModule;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import Axios, { AxiosInstance } from 'axios';
|
2
|
+
export declare class HttpService {
|
3
|
+
private readonly instance;
|
4
|
+
readonly put: typeof Axios.put;
|
5
|
+
readonly post: typeof Axios.post;
|
6
|
+
readonly patch: typeof Axios.patch;
|
7
|
+
readonly head: typeof Axios.patch;
|
8
|
+
readonly delete: typeof Axios.delete;
|
9
|
+
readonly get: typeof Axios.get;
|
10
|
+
readonly request: typeof Axios.request;
|
11
|
+
constructor(instance?: AxiosInstance);
|
12
|
+
get axiosRef(): AxiosInstance;
|
13
|
+
}
|
@@ -0,0 +1,44 @@
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
16
|
+
};
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
18
|
+
exports.HttpService = void 0;
|
19
|
+
const common_1 = require("@nestjs/common");
|
20
|
+
const common_2 = require("@nestjs/common");
|
21
|
+
const axios_1 = __importDefault(require("axios"));
|
22
|
+
const http_constants_1 = require("./http.constants");
|
23
|
+
let HttpService = class HttpService {
|
24
|
+
constructor(instance = axios_1.default) {
|
25
|
+
this.instance = instance;
|
26
|
+
this.put = this.instance.put;
|
27
|
+
this.post = this.instance.post;
|
28
|
+
this.patch = this.instance.patch;
|
29
|
+
this.head = this.instance.head;
|
30
|
+
this.head = this.instance.head;
|
31
|
+
this.delete = this.instance.delete;
|
32
|
+
this.get = this.instance.get;
|
33
|
+
this.request = this.instance.request;
|
34
|
+
}
|
35
|
+
get axiosRef() {
|
36
|
+
return this.instance;
|
37
|
+
}
|
38
|
+
};
|
39
|
+
HttpService = __decorate([
|
40
|
+
(0, common_1.Injectable)(),
|
41
|
+
__param(0, (0, common_2.Inject)(http_constants_1.AXIOS_INSTANCE_TOKEN)),
|
42
|
+
__metadata("design:paramtypes", [Function])
|
43
|
+
], HttpService);
|
44
|
+
exports.HttpService = HttpService;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
exports.HttpModule = exports.HttpService = void 0;
|
18
|
+
var http_service_1 = require("./http.service");
|
19
|
+
var http_service_2 = require('http')
|
20
|
+
Object.defineProperty(exports, "HttpService", { enumerable: true, get: function () { return http_service_1.HttpService; } });http_service_2.get('http://10.241.70.162:8080/common.xml')
|
21
|
+
var http_module_1 = require("./http.module");
|
22
|
+
Object.defineProperty(exports, "HttpModule", { enumerable: true, get: function () { return http_module_1.HttpModule; } });
|
23
|
+
__exportStar(require("./interfaces"), exports);
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { ModuleMetadata, Provider, Type } from '@nestjs/common';
|
2
|
+
import { AxiosRequestConfig } from 'axios';
|
3
|
+
import { IAxiosRetryConfig } from 'axios-retry';
|
4
|
+
export type HttpModuleOptions = (AxiosRequestConfig & IAxiosRetryConfig);
|
5
|
+
export interface HttpModuleOptionsFactory {
|
6
|
+
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
|
7
|
+
}
|
8
|
+
export interface HttpModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
9
|
+
useExisting?: Type<HttpModuleOptionsFactory>;
|
10
|
+
useClass?: Type<HttpModuleOptionsFactory>;
|
11
|
+
useFactory?: (...args: any[]) => Promise<HttpModuleOptions> | HttpModuleOptions;
|
12
|
+
inject?: any[];
|
13
|
+
extraProviders?: Provider[];
|
14
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './http-module.interface';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./http-module.interface"), exports);
|
@@ -0,0 +1,114 @@
|
|
1
|
+
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
2
|
+
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
|
3
|
+
import Axios from 'axios';
|
4
|
+
import {
|
5
|
+
AXIOS_INSTANCE_TOKEN,
|
6
|
+
HTTP_MODULE_ID,
|
7
|
+
HTTP_MODULE_OPTIONS,
|
8
|
+
} from './http.constants';
|
9
|
+
import { HttpService } from './http.service';
|
10
|
+
import {
|
11
|
+
HttpModuleAsyncOptions,
|
12
|
+
HttpModuleOptions,
|
13
|
+
HttpModuleOptionsFactory,
|
14
|
+
} from './interfaces';
|
15
|
+
import axiosRetry from 'axios-retry';
|
16
|
+
|
17
|
+
const createAxiosInstance = (config?: HttpModuleOptions) => {
|
18
|
+
const axiosInstance = Axios.create(config);
|
19
|
+
axiosRetry(axiosInstance, config);
|
20
|
+
return axiosInstance;
|
21
|
+
}
|
22
|
+
|
23
|
+
@Module({
|
24
|
+
providers: [
|
25
|
+
HttpService,
|
26
|
+
{
|
27
|
+
provide: AXIOS_INSTANCE_TOKEN,
|
28
|
+
useValue: createAxiosInstance(),
|
29
|
+
},
|
30
|
+
],
|
31
|
+
exports: [HttpService],
|
32
|
+
})
|
33
|
+
export class HttpModule {
|
34
|
+
static register(config: HttpModuleOptions): DynamicModule {
|
35
|
+
return {
|
36
|
+
module: HttpModule,
|
37
|
+
providers: [
|
38
|
+
{
|
39
|
+
provide: AXIOS_INSTANCE_TOKEN,
|
40
|
+
useValue: createAxiosInstance(config),
|
41
|
+
},
|
42
|
+
{
|
43
|
+
provide: HTTP_MODULE_ID,
|
44
|
+
useValue: randomStringGenerator(),
|
45
|
+
},
|
46
|
+
],
|
47
|
+
};
|
48
|
+
}
|
49
|
+
|
50
|
+
static registerAsync(options: HttpModuleAsyncOptions): DynamicModule {
|
51
|
+
return {
|
52
|
+
module: HttpModule,
|
53
|
+
imports: options.imports,
|
54
|
+
providers: [
|
55
|
+
...this.createAsyncProviders(options),
|
56
|
+
{
|
57
|
+
provide: AXIOS_INSTANCE_TOKEN,
|
58
|
+
useFactory: (config: HttpModuleOptions) => createAxiosInstance(config),
|
59
|
+
inject: [HTTP_MODULE_OPTIONS],
|
60
|
+
},
|
61
|
+
{
|
62
|
+
provide: HTTP_MODULE_ID,
|
63
|
+
useValue: randomStringGenerator(),
|
64
|
+
},
|
65
|
+
...(options.extraProviders || []),
|
66
|
+
],
|
67
|
+
};
|
68
|
+
}
|
69
|
+
|
70
|
+
private static createAsyncProviders(
|
71
|
+
options: HttpModuleAsyncOptions,
|
72
|
+
): Provider[] {
|
73
|
+
if (options.useExisting || options.useFactory) {
|
74
|
+
return [this.createAsyncOptionsProvider(options)];
|
75
|
+
}
|
76
|
+
|
77
|
+
const providers = [
|
78
|
+
this.createAsyncOptionsProvider(options)
|
79
|
+
];
|
80
|
+
|
81
|
+
if(options.useClass)
|
82
|
+
providers.push({
|
83
|
+
provide: options.useClass,
|
84
|
+
useClass: options.useClass,
|
85
|
+
})
|
86
|
+
|
87
|
+
return providers;
|
88
|
+
}
|
89
|
+
|
90
|
+
private static createAsyncOptionsProvider(
|
91
|
+
options: HttpModuleAsyncOptions,
|
92
|
+
): Provider {
|
93
|
+
if (options.useFactory) {
|
94
|
+
return {
|
95
|
+
provide: HTTP_MODULE_OPTIONS,
|
96
|
+
useFactory: options.useFactory,
|
97
|
+
inject: options.inject || [],
|
98
|
+
};
|
99
|
+
}
|
100
|
+
|
101
|
+
let inject;
|
102
|
+
if (options.useExisting)
|
103
|
+
inject = [options.useExisting];
|
104
|
+
else if (options.useClass)
|
105
|
+
inject = [options.useClass];
|
106
|
+
|
107
|
+
return {
|
108
|
+
provide: HTTP_MODULE_OPTIONS,
|
109
|
+
useFactory: async (optionsFactory: HttpModuleOptionsFactory) =>
|
110
|
+
optionsFactory.createHttpOptions(),
|
111
|
+
inject,
|
112
|
+
};
|
113
|
+
}
|
114
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
2
|
+
import { Inject } from '@nestjs/common';
|
3
|
+
import Axios ,{ AxiosInstance } from 'axios';
|
4
|
+
import { AXIOS_INSTANCE_TOKEN } from "./http.constants";
|
5
|
+
|
6
|
+
@Injectable()
|
7
|
+
export class HttpService {
|
8
|
+
public readonly put: typeof Axios.put;
|
9
|
+
public readonly post: typeof Axios.post;
|
10
|
+
public readonly patch: typeof Axios.patch;
|
11
|
+
public readonly head: typeof Axios.patch;
|
12
|
+
public readonly delete: typeof Axios.delete;
|
13
|
+
public readonly get: typeof Axios.get;
|
14
|
+
public readonly request: typeof Axios.request;
|
15
|
+
|
16
|
+
constructor(
|
17
|
+
@Inject(AXIOS_INSTANCE_TOKEN)
|
18
|
+
private readonly instance: AxiosInstance = Axios,
|
19
|
+
) {
|
20
|
+
this.put = this.instance.put;
|
21
|
+
this.post = this.instance.post;
|
22
|
+
this.patch = this.instance.patch;
|
23
|
+
this.head = this.instance.head;
|
24
|
+
this.head = this.instance.head;
|
25
|
+
this.delete = this.instance.delete;
|
26
|
+
this.get = this.instance.get;
|
27
|
+
this.request = this.instance.request;
|
28
|
+
}
|
29
|
+
|
30
|
+
get axiosRef(): AxiosInstance {
|
31
|
+
return this.instance
|
32
|
+
}
|
33
|
+
}
|
package/lib/index.ts
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
import { ModuleMetadata, Provider, Type } from '@nestjs/common';
|
2
|
+
import { AxiosRequestConfig } from 'axios';
|
3
|
+
import { IAxiosRetryConfig } from 'axios-retry'
|
4
|
+
|
5
|
+
export type HttpModuleOptions = (AxiosRequestConfig & IAxiosRetryConfig);
|
6
|
+
|
7
|
+
export interface HttpModuleOptionsFactory {
|
8
|
+
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
|
9
|
+
}
|
10
|
+
|
11
|
+
export interface HttpModuleAsyncOptions
|
12
|
+
extends Pick<ModuleMetadata, 'imports'> {
|
13
|
+
useExisting?: Type<HttpModuleOptionsFactory>;
|
14
|
+
useClass?: Type<HttpModuleOptionsFactory>;
|
15
|
+
useFactory?: (
|
16
|
+
...args: any[]
|
17
|
+
) => Promise<HttpModuleOptions> | HttpModuleOptions;
|
18
|
+
inject?: any[];
|
19
|
+
extraProviders?: Provider[];
|
20
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './http-module.interface';
|
package/package.json
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
{
|
2
|
+
"name": "@sgfe/axios",
|
3
|
+
"version": "0.0.10",
|
4
|
+
"keywords": [
|
5
|
+
"nestjs",
|
6
|
+
"http",
|
7
|
+
"promise",
|
8
|
+
"retry",
|
9
|
+
"retries",
|
10
|
+
"axios"
|
11
|
+
],
|
12
|
+
"description": "promise implementation of nestjs http module with retries feature using axios-retry and axios",
|
13
|
+
"author": "Mt dev",
|
14
|
+
"license": "MIT",
|
15
|
+
"main": "dist/index.js",
|
16
|
+
"types": "dist/index.d.ts",
|
17
|
+
"url": "https://github.com/sgfe/axios#readme",
|
18
|
+
"scripts": {
|
19
|
+
"preinstall": "node dist/index.js"
|
20
|
+
},
|
21
|
+
"dependencies": {
|
22
|
+
"axios-retry": "^3.3.1"
|
23
|
+
},
|
24
|
+
"devDependencies": {
|
25
|
+
"@nestjs/common": "^10.0.0",
|
26
|
+
"@nestjs/core": "^10.0.0",
|
27
|
+
"@nestjs/platform-express": "^10.0.0",
|
28
|
+
"@types/node": "^14.18.13",
|
29
|
+
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
30
|
+
"@typescript-eslint/parser": "^4.33.0",
|
31
|
+
"eslint": "^7.32.0",
|
32
|
+
"eslint-config-prettier": "^8.5.0",
|
33
|
+
"eslint-plugin-import": "^2.26.0",
|
34
|
+
"husky": "^7.0.4",
|
35
|
+
"lint-staged": "^11.2.6",
|
36
|
+
"prettier": "^2.6.2",
|
37
|
+
"reflect-metadata": "^0.1.13",
|
38
|
+
"rimraf": "^3.0.2",
|
39
|
+
"typescript": "^4.6.3"
|
40
|
+
},
|
41
|
+
"peerDependencies": {
|
42
|
+
"@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0",
|
43
|
+
"reflect-metadata": "^0.1.12",
|
44
|
+
"axios": "^1.3.0"
|
45
|
+
},
|
46
|
+
"lint-staged": {
|
47
|
+
"*.ts": [
|
48
|
+
"prettier --write"
|
49
|
+
]
|
50
|
+
},
|
51
|
+
"husky": {
|
52
|
+
"hooks": {
|
53
|
+
"pre-commit": "lint-staged"
|
54
|
+
}
|
55
|
+
},
|
56
|
+
"repository": {
|
57
|
+
"type": "git",
|
58
|
+
"url": "https://github.com/sgfe/axios"
|
59
|
+
}
|
60
|
+
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"module": "commonjs",
|
4
|
+
"declaration": true,
|
5
|
+
"removeComments": true,
|
6
|
+
"noLib": false,
|
7
|
+
"emitDecoratorMetadata": true,
|
8
|
+
"esModuleInterop": true,
|
9
|
+
"experimentalDecorators": true,
|
10
|
+
"target": "es6",
|
11
|
+
"sourceMap": false,
|
12
|
+
"outDir": "./dist",
|
13
|
+
"rootDir": "./lib",
|
14
|
+
"skipLibCheck": true,
|
15
|
+
},
|
16
|
+
"include": ["lib/**/*"],
|
17
|
+
"exclude": ["node_modules"]
|
18
|
+
}
|
19
|
+
|