style: run format

Run format
This commit is contained in:
Conicaw 2023-07-05 17:37:57 -05:00
parent 1850dd8948
commit db053cfb8e
No known key found for this signature in database
GPG key ID: CE0B9DEFED1E6375
5 changed files with 53 additions and 71 deletions

View file

@ -1,3 +1,3 @@
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN';
export const HTTP_MODULE_ID = 'HTTP_MODULE_ID';
export const HTTP_MODULE_OPTIONS = 'HTTP_MODULE_OPTIONS';
export const HTTP_MODULE_OPTIONS = 'HTTP_MODULE_OPTIONS';

View file

@ -1,28 +1,20 @@
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import Axios from 'axios';
import {
AXIOS_INSTANCE_TOKEN,
HTTP_MODULE_ID,
HTTP_MODULE_OPTIONS,
} from './http.constants';
import { AXIOS_INSTANCE_TOKEN, HTTP_MODULE_ID, HTTP_MODULE_OPTIONS } from './http.constants';
import { HttpService } from './http.service';
import {
HttpModuleAsyncOptions,
HttpModuleOptions,
HttpModuleOptionsFactory,
} from './interfaces';
import { HttpModuleAsyncOptions, HttpModuleOptions, HttpModuleOptionsFactory } from './interfaces';
import axiosRetry from 'axios-retry';
import axiosBetterStacktrace from 'axios-better-stacktrace';
const createAxiosInstance = (config?: HttpModuleOptions) => {
const axiosInstance = Axios.create(config);
axiosRetry(axiosInstance, config);
if(config?.isBetterStackTraceEnabled === undefined || config?.isBetterStackTraceEnabled) {
if (config?.isBetterStackTraceEnabled === undefined || config?.isBetterStackTraceEnabled) {
axiosBetterStacktrace(axiosInstance);
}
return axiosInstance;
}
};
@Module({
providers: [
@ -71,29 +63,23 @@ export class HttpModule {
};
}
private static createAsyncProviders(
options: HttpModuleAsyncOptions,
): Provider[] {
private static createAsyncProviders(options: HttpModuleAsyncOptions): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
const providers = [
this.createAsyncOptionsProvider(options)
];
const providers = [this.createAsyncOptionsProvider(options)];
if(options.useClass)
if (options.useClass)
providers.push({
provide: options.useClass,
useClass: options.useClass,
})
});
return providers;
}
private static createAsyncOptionsProvider(
options: HttpModuleAsyncOptions,
): Provider {
private static createAsyncOptionsProvider(options: HttpModuleAsyncOptions): Provider {
if (options.useFactory) {
return {
provide: HTTP_MODULE_OPTIONS,
@ -103,16 +89,14 @@ export class HttpModule {
}
let inject;
if (options.useExisting)
inject = [options.useExisting];
else if (options.useClass)
inject = [options.useClass];
if (options.useExisting) inject = [options.useExisting];
else if (options.useClass) inject = [options.useClass];
return {
provide: HTTP_MODULE_OPTIONS,
useFactory: async (optionsFactory: HttpModuleOptionsFactory) =>
optionsFactory.createHttpOptions(),
optionsFactory.createHttpOptions(),
inject,
};
}
}
}

View file

@ -1,33 +1,33 @@
import { Injectable } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import Axios ,{ AxiosInstance } from 'axios';
import { AXIOS_INSTANCE_TOKEN } from "./http.constants";
import Axios, { AxiosInstance } from 'axios';
import { AXIOS_INSTANCE_TOKEN } from './http.constants';
@Injectable()
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;
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(
@Inject(AXIOS_INSTANCE_TOKEN)
private readonly instance: AxiosInstance = Axios,
) {
this.put = this.instance.put;
this.post = this.instance.post;
this.patch = this.instance.patch;
this.head = this.instance.head;
this.head = this.instance.head;
this.delete = this.instance.delete;
this.get = this.instance.get;
this.request = this.instance.request;
}
constructor(
@Inject(AXIOS_INSTANCE_TOKEN)
private readonly instance: AxiosInstance = Axios,
) {
this.put = this.instance.put;
this.post = this.instance.post;
this.patch = this.instance.patch;
this.head = this.instance.head;
this.head = this.instance.head;
this.delete = this.instance.delete;
this.get = this.instance.get;
this.request = this.instance.request;
}
get axiosRef(): AxiosInstance {
return this.instance
}
get axiosRef(): AxiosInstance {
return this.instance;
}
}

View file

@ -1,3 +1,3 @@
export { HttpService } from './http.service'
export { HttpModule } from './http.module'
export * from './interfaces'
export { HttpService } from './http.service';
export { HttpModule } from './http.module';
export * from './interfaces';

View file

@ -1,20 +1,18 @@
import { ModuleMetadata, Provider, Type } from '@nestjs/common';
import { AxiosRequestConfig } from 'axios';
import { IAxiosRetryConfig } from 'axios-retry'
import { IAxiosRetryConfig } from 'axios-retry';
export type HttpModuleOptions = (AxiosRequestConfig & IAxiosRetryConfig & { isBetterStackTraceEnabled?: boolean });
export type HttpModuleOptions = AxiosRequestConfig &
IAxiosRetryConfig & { isBetterStackTraceEnabled?: boolean };
export interface HttpModuleOptionsFactory {
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
}
export interface HttpModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<HttpModuleOptionsFactory>;
useClass?: Type<HttpModuleOptionsFactory>;
useFactory?: (
...args: any[]
) => Promise<HttpModuleOptions> | HttpModuleOptions;
inject?: any[];
extraProviders?: Provider[];
}
export interface HttpModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<HttpModuleOptionsFactory>;
useClass?: Type<HttpModuleOptionsFactory>;
useFactory?: (...args: any[]) => Promise<HttpModuleOptions> | HttpModuleOptions;
inject?: any[];
extraProviders?: Provider[];
}