Skip to content

Commit

Permalink
refactor: reduce implicit types
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 24, 2018
1 parent 1cb3ed7 commit 0b23422
Show file tree
Hide file tree
Showing 54 changed files with 217 additions and 177 deletions.
6 changes: 4 additions & 2 deletions packages/common/cache/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class CacheInterceptor implements NestInterceptor {
@Inject(HTTP_SERVER_REF)
protected readonly httpServer: HttpServer,
@Inject(CACHE_MANAGER) protected readonly cacheManager: any,
@Inject(REFLECTOR) protected readonly reflector,
@Inject(REFLECTOR) protected readonly reflector: any,
) {
this.isHttpApp = httpServer && !!httpServer.getRequestMethod;
}
Expand All @@ -41,7 +41,9 @@ export class CacheInterceptor implements NestInterceptor {
if (value) {
return of(value);
}
return call$.pipe(tap(response => this.cacheManager.set(key, response)));
return call$.pipe(
tap((response: any) => this.cacheManager.set(key, response)),
);
} catch {
return call$;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/common/decorators/core/bind.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
* Useful when the language doesn't provide a 'Parameter Decorators' feature (vanilla JavaScript)
* @param {} ...decorators
*/
export function Bind(...decorators: any[]) {
return (target: object, key, descriptor) => {
export function Bind(...decorators: any[]): MethodDecorator {
return <T>(
target: object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<T>,
) => {
decorators.forEach((fn, index) => fn(target, key, index));
return descriptor;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function FileFieldsInterceptor(
this.upload.fields(uploadFields)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/files/interceptors/file.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function FileInterceptor(
this.upload.single(fieldName)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/files/interceptors/files.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function FilesInterceptor(
this.upload.array(fieldName, maxCount)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Transform<T = any> = (value: T, metadata: ArgumentMetadata) => any;

export interface ArgumentMetadata {
readonly type: Paramtype;
readonly metatype?: new (...args) => any | undefined;
readonly metatype?: new (...args: any[]) => any | undefined;
readonly data?: string | undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/common/interfaces/nest-application.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface INestApplication extends INestApplicationContext {
*
* @returns {void}
*/
use(...args): this;
use(...args: any[]): this;

/**
* Enables CORS (Cross-Origin Resource Sharing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Observable } from 'rxjs';

export interface WebSocketAdapter<T = any> {
create(port: number, options?: T);
bindClientConnect(server: any, callback: (...args) => void);
bindClientDisconnect?(client: any, callback: (...args) => void);
bindClientConnect(server: any, callback: (...args: any[]) => void);
bindClientDisconnect?(client: any, callback: (...args: any[]) => void);
bindMessageHandlers(
client: any,
handlers: Array<{
Expand Down
18 changes: 9 additions & 9 deletions packages/common/utils/shared.utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const isUndefined = (obj): obj is undefined =>
export const isUndefined = (obj: any): obj is undefined =>
typeof obj === 'undefined';
export const isFunction = (fn): boolean => typeof fn === 'function';
export const isObject = (fn): fn is object => typeof fn === 'object';
export const isString = (fn): fn is string => typeof fn === 'string';
export const isConstructor = (fn): boolean => fn === 'constructor';
export const validatePath = (path): string =>
export const isFunction = (fn: any): boolean => typeof fn === 'function';
export const isObject = (fn: any): fn is object => typeof fn === 'object';
export const isString = (fn: any): fn is string => typeof fn === 'string';
export const isConstructor = (fn: any): boolean => fn === 'constructor';
export const validatePath = (path: string): string =>
path.charAt(0) !== '/' ? '/' + path : path;
export const isNil = (obj): boolean => isUndefined(obj) || obj === null;
export const isEmpty = (array): boolean => !(array && array.length > 0);
export const isSymbol = (fn): boolean => typeof fn === 'symbol';
export const isNil = (obj: any): boolean => isUndefined(obj) || obj === null;
export const isEmpty = (array: any): boolean => !(array && array.length > 0);
export const isSymbol = (fn: any): boolean => typeof fn === 'symbol';
8 changes: 4 additions & 4 deletions packages/core/guards/guards-consumer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isEmpty } from '@nestjs/common/utils/shared.utils';
import { Controller } from '@nestjs/common/interfaces';
import { CanActivate } from '@nestjs/common';
import { Controller } from '@nestjs/common/interfaces';
import { isEmpty } from '@nestjs/common/utils/shared.utils';
import { Observable } from 'rxjs';
import { ExecutionContextHost } from '../helpers/execution-context.host';

Expand All @@ -9,7 +9,7 @@ export class GuardsConsumer {
guards: CanActivate[],
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): Promise<boolean> {
if (!guards || isEmpty(guards)) {
return true;
Expand All @@ -28,7 +28,7 @@ export class GuardsConsumer {
public createContext(
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): ExecutionContextHost {
return new ExecutionContextHost(
args,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/guards/guards-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { CanActivate } from '@nestjs/common';
import { GUARDS_METADATA } from '@nestjs/common/constants';
import { Controller } from '@nestjs/common/interfaces';
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ContextCreator } from '../helpers/context-creator';
import { NestContainer } from '../injector/container';
Expand All @@ -19,7 +23,7 @@ export class GuardsContextCreator extends ContextCreator {

public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): CanActivate[] {
this.moduleContext = module;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/helpers/context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export abstract class ContextCreator {

public createContext<T extends any[], R extends any[]>(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
metadataKey: string,
): R {
const globalMetadata =
Expand All @@ -28,7 +28,7 @@ export abstract class ContextCreator {
}

public reflectMethodMetadata<T>(
callback: (...args) => any,
callback: (...args: any[]) => any,
metadataKey: string,
): T {
return Reflect.getMetadata(metadataKey, callback);
Expand Down
11 changes: 7 additions & 4 deletions packages/core/helpers/external-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class ExternalContextCreator {

public create<T extends ParamsMetadata = ParamsMetadata>(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
methodName: string,
metadataKey?: string,
paramsFactory?: ParamsFactory,
Expand Down Expand Up @@ -95,7 +95,7 @@ export class ExternalContextCreator {
return callback.apply(instance, args);
};

return async (...args) => {
return async (...args: any[]) => {
const initialArgs = this.contextUtils.createNullArray(argsLength);
const canActivate = await this.guardsConsumer.tryActivate(
guards,
Expand Down Expand Up @@ -165,9 +165,12 @@ export class ExternalContextCreator {
});
}

public getCustomFactory(factory: (...args) => void, data): (...args) => any {
public getCustomFactory(
factory: (...args: any[]) => void,
data,
): (...args: any[]) => any {
return !isUndefined(factory) && isFunction(factory)
? (...args) => factory(data, args)
? (...args: any[]) => factory(data, args)
: () => null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface CustomProvider {
export type OpaqueToken = string | symbol | object | Type<any>;
export type CustomClass = CustomProvider & { useClass: Type<any> };
export type CustomFactory = CustomProvider & {
useFactory: (...args) => any;
useFactory: (...args: any[]) => any;
inject?: OpaqueToken[];
};
export type CustomValue = CustomProvider & { useValue: any };
Expand Down
7 changes: 4 additions & 3 deletions packages/core/interceptors/interceptors-consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class InterceptorsConsumer {
interceptors: NestInterceptor[],
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
next: () => Promise<any>,
): Promise<any> {
if (isEmpty(interceptors)) {
Expand All @@ -27,7 +27,8 @@ export class InterceptorsConsumer {
};
*/
const result$ = await interceptors.reduce(
async (stream$, interceptor) => interceptor.intercept(context, await stream$),
async (stream$, interceptor) =>
interceptor.intercept(context, await stream$),
Promise.resolve(start$),
);
return result$.toPromise();
Expand All @@ -36,7 +37,7 @@ export class InterceptorsConsumer {
public createContext(
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): ExecutionContextHost {
return new ExecutionContextHost(
args,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/interceptors/interceptors-context-creator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { INTERCEPTORS_METADATA } from '@nestjs/common/constants';
import { Controller, NestInterceptor } from '@nestjs/common/interfaces';
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ContextCreator } from '../helpers/context-creator';
import { NestContainer } from '../injector/container';
Expand All @@ -18,7 +22,7 @@ export class InterceptorsContextCreator extends ContextCreator {

public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): NestInterceptor[] {
this.moduleContext = module;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/middleware/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const mapToClass = middleware => {
}
return assignToken(
class {
resolve = (...args) => (...params) => middleware(...params);
resolve = (...args: any[]) => (...params) => middleware(...params);
},
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/nest-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class NestFactoryStatic {
if (!(prop in receiver)) return;

if (isFunction(receiver[prop])) {
return (...args) => {
return (...args: any[]) => {
let result;
ExceptionsZone.run(() => {
result = receiver[prop](...args);
Expand Down
21 changes: 10 additions & 11 deletions packages/core/pipes/pipes-consumer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { Transform } from '@nestjs/common/interfaces';
import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum';
import { ArgumentMetadata, Transform } from '@nestjs/common/interfaces';
import { ParamsTokenFactory } from './params-token-factory';

export class PipesConsumer {
private readonly paramsTokenFactory = new ParamsTokenFactory();

public async apply(
value,
{ metatype, type, data },
public async apply<TInput = any>(
value: TInput,
{ metatype, type, data }: ArgumentMetadata,
transforms: Transform<any>[],
) {
const token = this.paramsTokenFactory.exchangeEnumForString(type);
return this.applyPipes(
value,
{ metatype, type: token, data },
transforms,
const token = this.paramsTokenFactory.exchangeEnumForString(
(type as any) as RouteParamtypes,
);
return this.applyPipes(value, { metatype, type: token, data }, transforms);
}

public async applyPipes(
value,
public async applyPipes<TInput = any>(
value: TInput,
{ metatype, type, data }: { metatype; type?; data? },
transforms: Transform<any>[],
) {
Expand Down
14 changes: 11 additions & 3 deletions packages/core/pipes/pipes-context-creator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { PIPES_METADATA } from '@nestjs/common/constants';
import { Controller, PipeTransform, Transform } from '@nestjs/common/interfaces';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
Controller,
PipeTransform,
Transform,
} from '@nestjs/common/interfaces';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ApplicationConfig } from '../application-config';
import { ContextCreator } from '../helpers/context-creator';
Expand All @@ -18,7 +26,7 @@ export class PipesContextCreator extends ContextCreator {

public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): Transform<any>[] {
this.moduleContext = module;
Expand Down
11 changes: 7 additions & 4 deletions packages/core/router/router-execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class RouterExecutionContext {

public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
methodName: string,
module: string,
requestMethod: RequestMethod,
Expand Down Expand Up @@ -127,7 +127,7 @@ export class RouterExecutionContext {
};
}

public reflectHttpStatusCode(callback: (...args) => any): number {
public reflectHttpStatusCode(callback: (...args: any[]) => any): number {
return Reflect.getMetadata(HTTP_CODE_METADATA, callback);
}

Expand Down Expand Up @@ -168,7 +168,10 @@ export class RouterExecutionContext {
});
}

public getCustomFactory(factory: (...args) => void, data): (...args) => any {
public getCustomFactory(
factory: (...args: any[]) => void,
data,
): (...args: any[]) => any {
return !isUndefined(factory) && isFunction(factory)
? (req, res, next) => factory(data, req)
: () => null;
Expand Down Expand Up @@ -197,7 +200,7 @@ export class RouterExecutionContext {
public createGuardsFn(
guards: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): Function | null {
const canActivateFn = async (args: any[]) => {
const canActivate = await this.guardsConsumer.tryActivate(
Expand Down
7 changes: 5 additions & 2 deletions packages/core/services/reflector.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export class Reflector {
public get<T>(metadataKey, target): T {
return Reflect.getMetadata(metadataKey, target) as T;
public get<TResult = any, TKey = any, TInput = any>(
metadataKey: TKey,
target: TInput,
): TResult {
return Reflect.getMetadata(metadataKey, target) as TResult;
}
}
Loading

0 comments on commit 0b23422

Please sign in to comment.