Skip to content

Commit

Permalink
Merge pull request #14 from hodfords-solutions/feat/refactor-source
Browse files Browse the repository at this point in the history
feat: refactor eslint and upgrade dependencies
  • Loading branch information
hodfords-phuong-vo-be authored Sep 23, 2024
2 parents 85f14c7 + b5e0c91 commit e520866
Show file tree
Hide file tree
Showing 31 changed files with 1,217 additions and 1,060 deletions.
39 changes: 0 additions & 39 deletions .eslintrc.js

This file was deleted.

24 changes: 13 additions & 11 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ name: Publish Package to npmjs
on:
push:
branches:
- master
- main
jobs:
lint:
uses: hodfords-solutions/actions/.github/workflows/lint.yaml@main
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '22.x'
registry-url: 'https://registry.npmjs.org'
- run: sh scripts/publish.sh
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
uses: hodfords-solutions/actions/.github/workflows/publish.yaml@main
with:
build_path: dist/lib
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
update-docs:
uses: hodfords-solutions/actions/.github/workflows/update-doc.yaml@main
needs: build
secrets:
DOC_SSH_PRIVATE_KEY: ${{ secrets.DOC_SSH_PRIVATE_KEY }}
2 changes: 1 addition & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"libs/**/*.ts": ["prettier --write"]
"lib/**/*.ts": ["prettier --write"]
}
12 changes: 0 additions & 12 deletions .prettierrc

This file was deleted.

1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@hodfords/nestjs-eslint-config');
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResponseClass } from './../types/response-metadata.type';
import { ResponseClass } from '../types/response-metadata.type';
import { applyDecorators, SetMetadata } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, refs } from '@nestjs/swagger';
import { RESPONSE_METADATA_KEYS } from '../constants/metadata.constant';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UseInterceptors } from '@nestjs/common';
import { ResponseInterceptor } from '../interceptors/response.interceptor';

export function UseResponseInterceptor() {
export function UseResponseInterceptor(): MethodDecorator & ClassDecorator {
return UseInterceptors(ResponseInterceptor);
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common';
import { plainToInstance } from 'class-transformer';
import { isBoolean, validateSync } from 'class-validator';
import { NESTJS_RESPONSE_CONFIG_OPTIONS } from 'libs/constants/provider-key.constant';
import { ConfigOption } from 'libs/types/config-option.type';
import { NESTJS_RESPONSE_CONFIG_OPTIONS } from 'lib/constants/provider-key.constant';
import { ConfigOption } from 'lib/types/config-option.type';
import { Observable, map } from 'rxjs';
import { RESPONSE_METADATA_KEY, RESPONSE_METADATA_KEYS } from '../constants/metadata.constant';
import { ResponseValidateException } from '../exceptions/response-validate.exception';
import { NativeValueResponse } from '../responses/native-value.response';
import { HandleResult } from '../types/handle-result.type';
import { ResponseMetadata } from '../types/response-metadata.type';
import { NativeClassResponseNamesConstant } from './../constants/native-class-response-names.constant';
import { NativeClassResponseNamesConstant } from '../constants/native-class-response-names.constant';
import { ModuleRef } from '@nestjs/core';

let Metadata = null;
let grpcMetadataClass = null;

try {
// Check project is using grpc
// eslint-disable-next-line
const grpc = require('@grpc/grpc-js');
Metadata = grpc.Metadata;
} catch (ex) {}
grpcMetadataClass = grpc.Metadata;
} catch (ex) {
console.log(ex?.message);
}

@Injectable()
export class ResponseInterceptor implements NestInterceptor {
Expand All @@ -30,7 +32,7 @@ export class ResponseInterceptor implements NestInterceptor {
this.configOption = this.moduleRef.get(NESTJS_RESPONSE_CONFIG_OPTIONS, { strict: false });
}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
intercept(context: ExecutionContext, next: CallHandler): Observable<object> {
return next.handle().pipe(
map((data) => {
const res = this.handleResponse(context, data);
Expand All @@ -40,7 +42,7 @@ export class ResponseInterceptor implements NestInterceptor {
);
}

handleResponse(context: ExecutionContext, data: any) {
handleResponse(context: ExecutionContext, data: object): object {
const responseMetadata: ResponseMetadata = Reflect.getMetadata(RESPONSE_METADATA_KEY, context.getHandler());
const responseMetadatas: ResponseMetadata[] = Reflect.getMetadata(RESPONSE_METADATA_KEYS, context.getHandler());
if (responseMetadata) {
Expand All @@ -52,7 +54,7 @@ export class ResponseInterceptor implements NestInterceptor {
return data;
}

private excludeByKeys(context: ExecutionContext, data: any, keys: string[]) {
private excludeByKeys(context: ExecutionContext, data: object, keys: string[]): object {
/**
* Check if endpoint has response metadata, if not, then return data
*/
Expand All @@ -70,12 +72,12 @@ export class ResponseInterceptor implements NestInterceptor {
return data;
}

private excludeByKey(data: any, key: string) {
private excludeByKey(data: object | object[], key: string): object {
for (const prop in data) {
if (typeof data[prop] === 'object') {
data[prop] = this.excludeByKey(data[prop], key);
} else if (Array.isArray(data[prop])) {
data[prop] = data[prop].map((item: any) => this.excludeByKey(item, key));
data[prop] = data[prop].map((item: object) => this.excludeByKey(item, key));
} else if (prop === key) {
delete data[prop];
}
Expand All @@ -84,18 +86,26 @@ export class ResponseInterceptor implements NestInterceptor {
return data;
}

private handleOneTypeResponse(context: ExecutionContext, data: any, responseMetadata: ResponseMetadata) {
private handleOneTypeResponse(
context: ExecutionContext,
data: object | object[],
responseMetadata: ResponseMetadata
): object {
if (!isBoolean(data) && !data) {
return this.handleEmptyResponse(responseMetadata, data);
}
if (responseMetadata.isArray) {
return this.handleListResponse(context, responseMetadata, data);
return this.handleListResponse(context, responseMetadata, data as object[]);
}

return this.handleSingleResponse(responseMetadata, data);
}

private handleMultiTypeResponse(context: ExecutionContext, data: any, responseMetadatas: ResponseMetadata[]) {
private handleMultiTypeResponse(
context: ExecutionContext,
data: object,
responseMetadatas: ResponseMetadata[]
): object {
const results: HandleResult[] = [];
const newMetadatas = this.filterResponseMetadatas(responseMetadatas, data);
for (const metadata of newMetadatas) {
Expand All @@ -116,7 +126,7 @@ export class ResponseInterceptor implements NestInterceptor {
throw new ResponseValidateException(errors);
}

private handleSingleResponse(responseMetadata: ResponseMetadata, data: any) {
private handleSingleResponse(responseMetadata: ResponseMetadata, data: object): object {
if (NativeClassResponseNamesConstant.includes(responseMetadata.responseClass.name)) {
return this.handleNativeValueResponse(responseMetadata, data);
}
Expand All @@ -132,19 +142,23 @@ export class ResponseInterceptor implements NestInterceptor {
return plainToInstance(responseMetadata.responseClass, data);
}

private handleListResponse(context: ExecutionContext, responseMetadata: ResponseMetadata, data: any) {
private handleListResponse(
context: ExecutionContext,
responseMetadata: ResponseMetadata,
data: object[]
): object[] | { items: object[]; grpcArray: boolean } {
const newData: object[] = [];
for (const item of data) {
const newItem = this.handleSingleResponse(responseMetadata, item);
newData.push(newItem);
}
if (Metadata && context.switchToRpc().getContext() instanceof Metadata) {
if (grpcMetadataClass && context.switchToRpc().getContext() instanceof grpcMetadataClass) {
return { items: newData, grpcArray: true };
}
return newData;
}

private handleEmptyResponse(responseMetadata: ResponseMetadata, data: any) {
private handleEmptyResponse(responseMetadata: ResponseMetadata, data: object): object {
if (responseMetadata.isAllowEmpty) {
return data;
} else {
Expand All @@ -160,7 +174,7 @@ export class ResponseInterceptor implements NestInterceptor {
}
}

private handleNativeValueResponse(responseMetadata: ResponseMetadata, data: any): any {
private handleNativeValueResponse(responseMetadata: ResponseMetadata, data: object): object {
const responseMap = this.getResponseMap(responseMetadata, data);
const newData = plainToInstance(NativeValueResponse, responseMap);
const errors = validateSync(newData, {
Expand All @@ -173,7 +187,7 @@ export class ResponseInterceptor implements NestInterceptor {
return data;
}

private getResponseMap(responseMetadata: ResponseMetadata, data: any): Record<string, any> {
private getResponseMap(responseMetadata: ResponseMetadata, data: object): Record<string, object> {
switch (responseMetadata.responseClass.name) {
case 'Boolean':
return { ['boolean']: data };
Expand All @@ -186,7 +200,7 @@ export class ResponseInterceptor implements NestInterceptor {
}
}

private filterResponseMetadatas(responseMetadatas: ResponseMetadata[], data: any): ResponseMetadata[] {
private filterResponseMetadatas(responseMetadatas: ResponseMetadata[], data: object): ResponseMetadata[] {
if (Array.isArray(data)) {
return this.getArrayTypeMetadata(responseMetadatas, data);
}
Expand All @@ -196,7 +210,7 @@ export class ResponseInterceptor implements NestInterceptor {
return this.getObjectTypeMetadata(responseMetadatas, data);
}

private getArrayTypeMetadata(responseMetadatas: ResponseMetadata[], data: any) {
private getArrayTypeMetadata(responseMetadatas: ResponseMetadata[], data: object): ResponseMetadata[] {
const arrayTypes = responseMetadatas.filter((metadata) => metadata.isArray);
if (arrayTypes.length == 0) {
throw new ResponseValidateException([
Expand All @@ -212,7 +226,7 @@ export class ResponseInterceptor implements NestInterceptor {
return arrayTypes;
}

private getEmptyTypeMetadata(responseMetadatas: ResponseMetadata[], data: any) {
private getEmptyTypeMetadata(responseMetadatas: ResponseMetadata[], data: object): ResponseMetadata[] {
const emptyType = responseMetadatas.find((metadata) => metadata.isAllowEmpty);
if (emptyType) {
return [emptyType];
Expand All @@ -228,7 +242,7 @@ export class ResponseInterceptor implements NestInterceptor {
]);
}

private getObjectTypeMetadata(responseMetadatas: ResponseMetadata[], data: any) {
private getObjectTypeMetadata(responseMetadatas: ResponseMetadata[], data: object): ResponseMetadata[] {
const objectTypes = responseMetadatas.filter((metadata) => !metadata.isArray);
if (objectTypes.length == 0) {
throw new ResponseValidateException([
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { ValidationError } from 'class-validator';

export type HandleResult = {
error: ValidationError;
data: any;
data: object | object[];
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
import { ClassConstructor } from 'class-transformer';

export type ResponseMetadata = {
Expand Down
8 changes: 4 additions & 4 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"sourceRoot": "samples",
"projects": {
"nestjs-response": {
"type": "library",
"root": "libs",
"root": "lib",
"entryFile": "index",
"sourceRoot": "libs"
"sourceRoot": "lib"
}
},
"compilerOptions": {
"webpack": false,
"assets": [
{
"include": "../libs/public/**",
"include": "../lib/public/**",
"watchAssets": true
}
]
Expand Down
Loading

0 comments on commit e520866

Please sign in to comment.