Merge pull request #11 from benhason1/beta

Beta
This commit is contained in:
benhason1 2021-12-24 00:23:56 +02:00 committed by GitHub
commit 98f8509885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 34 deletions

View file

@ -5,8 +5,6 @@ on:
push: push:
branches: branches:
- master - master
tags:
- "!*"
jobs: jobs:
publish-to-npm: publish-to-npm:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View file

@ -6,7 +6,9 @@ nestjs module that just doing little modification to the original and good **nes
## features ## features
* axios - the most used package for http requests in npm. * axios - the most used package for http requests in npm and the one used by nestjs official http library.
* better axios stack trace - axios has an [open issue](https://github.com/axios/axios/issues/2387) about improvement of their stack trace.
in this library there is a default interceptor that will intercept the stack trace and will add data to it.
* promise based - most of us using the current http module that uses observable which we don't use most of the time * promise based - most of us using the current http module that uses observable which we don't use most of the time
and in order to avoid it were just calling `.toPromise()` every http call. and in order to avoid it were just calling `.toPromise()` every http call.
* retries - in many cases we will want to retry a failing http call. * retries - in many cases we will want to retry a failing http call.
@ -73,7 +75,8 @@ import { HttpModule } from 'nestjs-http-promise'
``` ```
### default configuration ### default configuration
just the default config of axios-retry : https://github.com/softonic/axios-retry#options * default config of axios-retry : https://github.com/softonic/axios-retry#options
* better axios stack trace is added by default, you can turn it off by passing the **isBetterStackTraceEnabled** to false.
## async configuration ## async configuration
When you need to pass module options asynchronously instead of statically, use the `registerAsync()` method **just like in nest httpModule**. When you need to pass module options asynchronously instead of statically, use the `registerAsync()` method **just like in nest httpModule**.

View file

@ -13,10 +13,14 @@ import {
HttpModuleOptionsFactory, HttpModuleOptionsFactory,
} from './interfaces'; } from './interfaces';
import axiosRetry from 'axios-retry'; import axiosRetry from 'axios-retry';
import axiosBetterStacktrace from 'axios-better-stacktrace';
const createAxiosRetry = (config?: HttpModuleOptions) => { const createAxiosInstance = (config?: HttpModuleOptions) => {
const axiosInstance = Axios.create(config); const axiosInstance = Axios.create(config);
axiosRetry(axiosInstance, config); axiosRetry(axiosInstance, config);
if(config.isBetterStackTraceEnabled === undefined || config.isBetterStackTraceEnabled) {
axiosBetterStacktrace(axiosInstance);
}
return axiosInstance; return axiosInstance;
} }
@ -25,7 +29,7 @@ const createAxiosRetry = (config?: HttpModuleOptions) => {
HttpService, HttpService,
{ {
provide: AXIOS_INSTANCE_TOKEN, provide: AXIOS_INSTANCE_TOKEN,
useValue: createAxiosRetry(), useValue: createAxiosInstance(),
}, },
], ],
exports: [HttpService], exports: [HttpService],
@ -37,7 +41,7 @@ export class HttpModule {
providers: [ providers: [
{ {
provide: AXIOS_INSTANCE_TOKEN, provide: AXIOS_INSTANCE_TOKEN,
useValue: createAxiosRetry(config), useValue: createAxiosInstance(config),
}, },
{ {
provide: HTTP_MODULE_ID, provide: HTTP_MODULE_ID,
@ -55,7 +59,7 @@ export class HttpModule {
...this.createAsyncProviders(options), ...this.createAsyncProviders(options),
{ {
provide: AXIOS_INSTANCE_TOKEN, provide: AXIOS_INSTANCE_TOKEN,
useFactory: (config: HttpModuleOptions) => createAxiosRetry(config), useFactory: (config: HttpModuleOptions) => createAxiosInstance(config),
inject: [HTTP_MODULE_OPTIONS], inject: [HTTP_MODULE_OPTIONS],
}, },
{ {

View file

@ -1,34 +1,30 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Inject } from '@nestjs/common'; import { Inject } from '@nestjs/common';
import Axios ,{AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; import Axios ,{ AxiosInstance } from 'axios';
import { AXIOS_INSTANCE_TOKEN } from "./http.constants"; import { AXIOS_INSTANCE_TOKEN } from "./http.constants";
@Injectable() @Injectable()
export class HttpService { export class HttpService {
public readonly put: typeof Axios.put;
public readonly post: typeof Axios.post;
public readonly patch: typeof Axios.patch;
public readonly head: typeof Axios.patch;
public readonly delete: typeof Axios.delete;
public readonly get: typeof Axios.get;
public readonly request: typeof Axios.request;
constructor( constructor(
@Inject(AXIOS_INSTANCE_TOKEN) @Inject(AXIOS_INSTANCE_TOKEN)
private readonly instance: AxiosInstance = Axios, private readonly instance: AxiosInstance = Axios,
) {} ) {
request<T = any>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> { this.put = this.instance.put;
return this.instance.request(config) this.post = this.instance.post;
} this.patch = this.instance.patch;
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> { this.head = this.instance.head;
return this.instance.get(url, config); this.head = this.instance.head;
} this.delete = this.instance.delete;
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> { this.get = this.instance.get;
return this.instance.delete(url, config); this.request = this.instance.request;
}
head<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.instance.head(url, config);
}
post<T = any>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.instance.post(url, data, config);
}
put<T = any>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.instance.put(url, data, config);
}
patch<T = any>(url: string, data?: Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
return this.instance.patch(url, data, config);
} }
get axiosRef(): AxiosInstance { get axiosRef(): AxiosInstance {

View file

@ -2,7 +2,7 @@ import { ModuleMetadata, Provider, Type } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios'; import { AxiosRequestConfig } from 'axios';
import { IAxiosRetryConfig } from 'axios-retry' import { IAxiosRetryConfig } from 'axios-retry'
export type HttpModuleOptions = (AxiosRequestConfig & IAxiosRetryConfig); export type HttpModuleOptions = (AxiosRequestConfig & IAxiosRetryConfig & { isBetterStackTraceEnabled?: boolean });
export interface HttpModuleOptionsFactory { export interface HttpModuleOptionsFactory {
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions; createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;

10
package-lock.json generated
View file

@ -1,6 +1,6 @@
{ {
"name": "nestjs-http-promise", "name": "nestjs-http-promise",
"version": "1.0.1", "version": "1.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -486,7 +486,8 @@
"ansi-regex": { "ansi-regex": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
"integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==" "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
"dev": true
}, },
"ansi-styles": { "ansi-styles": {
"version": "4.3.0", "version": "4.3.0",
@ -562,6 +563,11 @@
"follow-redirects": "^1.14.4" "follow-redirects": "^1.14.4"
} }
}, },
"axios-better-stacktrace": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/axios-better-stacktrace/-/axios-better-stacktrace-2.1.2.tgz",
"integrity": "sha512-/ikpK5W7UhCAdl9pQss1vpTfLtOQpbv8edRZ2a9wWSux2dnGmqjVHkFsMKnyllysxm046C016lqGjUgGNECcag=="
},
"axios-retry": { "axios-retry": {
"version": "3.2.4", "version": "3.2.4",
"resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.2.4.tgz", "resolved": "https://registry.npmjs.org/axios-retry/-/axios-retry-3.2.4.tgz",

View file

@ -1,6 +1,6 @@
{ {
"name": "nestjs-http-promise", "name": "nestjs-http-promise",
"version": "1.1.1", "version": "1.2.0",
"keywords": [ "keywords": [
"nestjs", "nestjs",
"http", "http",
@ -26,6 +26,7 @@
}, },
"dependencies": { "dependencies": {
"axios": "~0.24.0", "axios": "~0.24.0",
"axios-better-stacktrace": "^2.1.2",
"axios-retry": "^3.2.4" "axios-retry": "^3.2.4"
}, },
"devDependencies": { "devDependencies": {