Skip to content

Commit

Permalink
style: add more eslint rules
Browse files Browse the repository at this point in the history
Add more eslint rules
  • Loading branch information
ttshivers committed Nov 14, 2023
1 parent 55eff6e commit 876b6ec
Show file tree
Hide file tree
Showing 8 changed files with 694 additions and 147 deletions.
101 changes: 74 additions & 27 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,76 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: true,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
parser: '@typescript-eslint/parser',
parserOptions: {
project: true,
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: [
'@typescript-eslint/eslint-plugin',
'simple-import-sort',
'no-type-assertion',
'import',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/strict-type-checked',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:@typescript-eslint/strict',
'plugin:prettier/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'simple-import-sort/imports': 'error',
'simple-import-sort/exports': 'error',
eqeqeq: 'error',
'no-lonely-if': 'error',
'no-multi-assign': 'error',
'@typescript-eslint/no-shadow': 'error',
'no-useless-return': 'error',
'no-useless-rename': 'error',
'one-var-declaration-per-line': 'error',
'prefer-object-spread': 'error',
'no-unreachable-loop': 'error',
'no-template-curly-in-string': 'error',
'default-case-last': 'error',
'no-array-constructor': 'error',
'no-else-return': 'error',
'no-negated-condition': 'error',
'array-callback-return': 'error',
'@typescript-eslint/consistent-type-definitions': 'error',
'no-type-assertion/no-type-assertion': 'error',
'@typescript-eslint/ban-ts-comment': 'error',
'@typescript-eslint/no-unnecessary-condition': 'error',
'@typescript-eslint/no-extraneous-class': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'error',
'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/consistent-type-exports': 'error',
'import/consistent-type-specifier-style': 'error',
'import/no-duplicates': 'error',
'import/no-unresolved': 'error',
curly: 'error',
'no-console': [
'warn',
{
allow: ['warn', 'error'],
},
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',

'@typescript-eslint/no-import-type-side-effects': 'error',
'@typescript-eslint/consistent-type-imports': 'error',
'@typescript-eslint/consistent-type-exports': 'error',
},
};
quotes: [
'error',
'single',
{
avoidEscape: true,
},
],
'prefer-template': 'error',
'@typescript-eslint/return-await': ['error', 'always'],
},
};
31 changes: 20 additions & 11 deletions lib/http.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import type { DynamicModule, Provider } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import Axios from 'axios';
import axios from 'axios';
import axiosRetry from 'axios-retry';

import { AXIOS_INSTANCE_TOKEN, HTTP_MODULE_ID, HTTP_MODULE_OPTIONS } from './http.constants';
import { HttpService } from './http.service';
import type { HttpModuleAsyncOptions, HttpModuleOptions, HttpModuleOptionsFactory } from './interfaces';
import axiosRetry from 'axios-retry';
import type {
HttpModuleAsyncOptions,
HttpModuleOptions,
HttpModuleOptionsFactory,
} from './interfaces';

const createAxiosInstance = (config?: HttpModuleOptions) => {
const axiosInstance = Axios.create(config);
const axiosInstance = axios.create(config);
axiosRetry(axiosInstance, config);
return axiosInstance;
};
Expand Down Expand Up @@ -55,23 +60,24 @@ export class HttpModule {
provide: HTTP_MODULE_ID,
useValue: randomStringGenerator(),
},
...(options.extraProviders || []),
...(options.extraProviders ?? []),
],
};
}

private static createAsyncProviders(options: HttpModuleAsyncOptions): Provider[] {
if (options.useExisting || options.useFactory) {
if (!!options.useExisting || !!options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}

const providers = [this.createAsyncOptionsProvider(options)];

if (options.useClass)
if (options.useClass) {
providers.push({
provide: options.useClass,
useClass: options.useClass,
});
}

return providers;
}
Expand All @@ -81,18 +87,21 @@ export class HttpModule {
return {
provide: HTTP_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
inject: options.inject ?? [],
};
}

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(),
await optionsFactory.createHttpOptions(),
...(inject && { inject }),
};
}
Expand Down
27 changes: 15 additions & 12 deletions lib/http.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { Injectable } from '@nestjs/common';
import { Inject } from '@nestjs/common';
import Axios, { AxiosInstance } from 'axios';
/* eslint-disable @typescript-eslint/unbound-method */

import { Inject, Injectable } from '@nestjs/common';
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,
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 as typeof Axios.patch;
// eslint-disable-next-line no-type-assertion/no-type-assertion
this.head = this.instance.head as typeof axios.patch;
this.delete = this.instance.delete;
this.get = this.instance.get;
this.request = this.instance.request;
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { HttpService } from './http.service';
export { HttpModule } from './http.module';
export { HttpService } from './http.service';
export * from './interfaces';
5 changes: 3 additions & 2 deletions lib/interfaces/http-module.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { ModuleMetadata, Provider, Type } from '@nestjs/common';
import type { AxiosRequestConfig } from 'axios';
import type IAxiosRetry from 'axios-retry';


export type HttpModuleOptions = AxiosRequestConfig &
IAxiosRetry.IAxiosRetryConfig & { isBetterStackTraceEnabled?: boolean };
IAxiosRetry.IAxiosRetryConfig & { isBetterStackTraceEnabled?: boolean };

export interface HttpModuleOptionsFactory {
createHttpOptions(): Promise<HttpModuleOptions> | HttpModuleOptions;
Expand All @@ -13,7 +12,9 @@ export interface HttpModuleOptionsFactory {
export interface HttpModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<HttpModuleOptionsFactory>;
useClass?: Type<HttpModuleOptionsFactory>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
useFactory?: (...args: any[]) => Promise<HttpModuleOptions> | HttpModuleOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inject?: any[];
extraProviders?: Provider[];
}
Loading

0 comments on commit 876b6ec

Please sign in to comment.