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

View file

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

View file

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

View file

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