Commit af1fccaa authored by nanahira's avatar nanahira

move to nesties

parent 2eeb7974
import 'reflect-metadata'; import 'reflect-metadata';
export * from 'nesties';
export * from './src/crud-base'; export * from './src/crud-base';
export * from './src/bases'; export * from './src/bases';
export * from './src/decorators'; export * from './src/decorators';
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"nesties": "^1.0.1",
"typed-reflector": "^1.0.11" "typed-reflector": "^1.0.11"
}, },
"devDependencies": { "devDependencies": {
...@@ -35,8 +36,8 @@ ...@@ -35,8 +36,8 @@
"typescript": "^4.7.4" "typescript": "^4.7.4"
}, },
"peerDependencies": { "peerDependencies": {
"@nestjs/common": "^9.4.0", "@nestjs/common": "^9.4.0 || ^10.0.0",
"@nestjs/swagger": "^7.1.8", "@nestjs/swagger": "^7.1.8 || ^6.3.0",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"typeorm": "^0.3.16" "typeorm": "^0.3.16"
...@@ -1123,6 +1124,21 @@ ...@@ -1123,6 +1124,21 @@
"integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==", "integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==",
"peer": true "peer": true
}, },
"node_modules/@nestjs/config": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-3.3.0.tgz",
"integrity": "sha512-pdGTp8m9d0ZCrjTpjkUbZx6gyf2IKf+7zlkrPNMsJzYZ4bFRRTpXrnj+556/5uiI6AfL5mMrJc2u7dB6bvM+VA==",
"peer": true,
"dependencies": {
"dotenv": "16.4.5",
"dotenv-expand": "10.0.0",
"lodash": "4.17.21"
},
"peerDependencies": {
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
"rxjs": "^7.1.0"
}
},
"node_modules/@nestjs/core": { "node_modules/@nestjs/core": {
"version": "9.4.0", "version": "9.4.0",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.0.tgz", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.0.tgz",
...@@ -2656,9 +2672,21 @@ ...@@ -2656,9 +2672,21 @@
} }
}, },
"node_modules/dotenv": { "node_modules/dotenv": {
"version": "16.0.3", "version": "16.4.5",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==",
"peer": true,
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dotenv-expand": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz",
"integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==",
"peer": true, "peer": true,
"engines": { "engines": {
"node": ">=12" "node": ">=12"
...@@ -4821,6 +4849,16 @@ ...@@ -4821,6 +4849,16 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/nesties": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/nesties/-/nesties-1.0.1.tgz",
"integrity": "sha512-WDc+/sEDGMnHmMD2lZSSxMqMsml0SxyxoDkQhfo5xd78xjQUcO1o4fOUfkPV4pSsTPdhqLO97E0OwPbKXuc7Gw==",
"peerDependencies": {
"@nestjs/common": "^9.4.0 || ^10.0.0",
"@nestjs/config": "^3.0.0",
"@nestjs/swagger": "^7.1.8 || ^6.3.0"
}
},
"node_modules/node-fetch": { "node_modules/node-fetch": {
"version": "2.6.7", "version": "2.6.7",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
...@@ -7583,6 +7621,17 @@ ...@@ -7583,6 +7621,17 @@
} }
} }
}, },
"@nestjs/config": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/@nestjs/config/-/config-3.3.0.tgz",
"integrity": "sha512-pdGTp8m9d0ZCrjTpjkUbZx6gyf2IKf+7zlkrPNMsJzYZ4bFRRTpXrnj+556/5uiI6AfL5mMrJc2u7dB6bvM+VA==",
"peer": true,
"requires": {
"dotenv": "16.4.5",
"dotenv-expand": "10.0.0",
"lodash": "4.17.21"
}
},
"@nestjs/core": { "@nestjs/core": {
"version": "9.4.0", "version": "9.4.0",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.0.tgz", "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.0.tgz",
...@@ -8692,9 +8741,15 @@ ...@@ -8692,9 +8741,15 @@
} }
}, },
"dotenv": { "dotenv": {
"version": "16.0.3", "version": "16.4.5",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==",
"peer": true
},
"dotenv-expand": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz",
"integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==",
"peer": true "peer": true
}, },
"ee-first": { "ee-first": {
...@@ -10330,6 +10385,12 @@ ...@@ -10330,6 +10385,12 @@
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==", "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==",
"devOptional": true "devOptional": true
}, },
"nesties": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/nesties/-/nesties-1.0.1.tgz",
"integrity": "sha512-WDc+/sEDGMnHmMD2lZSSxMqMsml0SxyxoDkQhfo5xd78xjQUcO1o4fOUfkPV4pSsTPdhqLO97E0OwPbKXuc7Gw==",
"requires": {}
},
"node-fetch": { "node-fetch": {
"version": "2.6.7", "version": "2.6.7",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
......
...@@ -69,6 +69,7 @@ ...@@ -69,6 +69,7 @@
}, },
"dependencies": { "dependencies": {
"lodash": "^4.17.21", "lodash": "^4.17.21",
"nesties": "^1.0.1",
"typed-reflector": "^1.0.11" "typed-reflector": "^1.0.11"
} }
} }
...@@ -3,12 +3,12 @@ import { Generated, SelectQueryBuilder } from 'typeorm'; ...@@ -3,12 +3,12 @@ import { Generated, SelectQueryBuilder } from 'typeorm';
import { applyQueryProperty } from '../utility'; import { applyQueryProperty } from '../utility';
import { import {
IntColumn, IntColumn,
MergePropertyDecorators,
NotChangeable, NotChangeable,
NotWritable, NotWritable,
StringColumn, StringColumn,
} from '../decorators'; } from '../decorators';
import { IsNotEmpty } from 'class-validator'; import { IsNotEmpty } from 'class-validator';
import { MergePropertyDecorators } from 'nesties';
export interface IdOptions { export interface IdOptions {
description?: string; description?: string;
......
...@@ -3,11 +3,7 @@ import { SelectQueryBuilder } from 'typeorm'; ...@@ -3,11 +3,7 @@ import { SelectQueryBuilder } from 'typeorm';
import { IsInt, IsPositive } from 'class-validator'; import { IsInt, IsPositive } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { reflector } from '../utility/metadata'; import { reflector } from '../utility/metadata';
import { PageSettingsWise } from 'nesties';
export interface PageSettingsWise {
pageCount: number;
recordsPerPage: number;
}
export interface PageSettingsFactory { export interface PageSettingsFactory {
getActualPageSettings(): PageSettingsWise; getActualPageSettings(): PageSettingsWise;
......
import { import { ImportEntryDto } from './dto';
BlankReturnMessageDto,
ImportEntryDto,
PaginatedReturnMessageDto,
ReturnMessageDto,
} from './dto';
import { import {
DeepPartial, DeepPartial,
DeleteResult, DeleteResult,
...@@ -23,6 +18,11 @@ import { ConsoleLogger } from '@nestjs/common'; ...@@ -23,6 +18,11 @@ import { ConsoleLogger } from '@nestjs/common';
import { camelCase } from 'typeorm/util/StringUtils'; import { camelCase } from 'typeorm/util/StringUtils';
import _ from 'lodash'; import _ from 'lodash';
import { ClassType } from './utility/insert-field'; import { ClassType } from './utility/insert-field';
import {
BlankReturnMessageDto,
PaginatedReturnMessageDto,
ReturnMessageDto,
} from 'nesties';
export type EntityId<T extends { id: any }> = T['id']; export type EntityId<T extends { id: any }> = T['id'];
export interface RelationDef { export interface RelationDef {
......
import { Expose } from 'class-transformer'; import { Expose } from 'class-transformer';
import { IsOptional } from 'class-validator'; import { IsOptional } from 'class-validator';
import { Metadata } from '../utility/metadata'; import { Metadata } from '../utility/metadata';
import { MergePropertyDecorators } from './merge'; import { MergePropertyDecorators } from 'nesties';
export const NotWritable = () => export const NotWritable = () =>
MergePropertyDecorators([ MergePropertyDecorators([
......
export * from './access'; export * from './access';
export * from './merge';
export * from './property'; export * from './property';
export * from './restful'; export * from './restful';
export * from './pipes'; export * from './pipes';
......
export function MergePropertyDecorators(
decs: PropertyDecorator[],
): PropertyDecorator {
return (obj, key) => {
for (const dec of decs) {
dec(obj, key);
}
};
}
export function MergeMethodDecorators(
decs: MethodDecorator[],
): MethodDecorator {
return (obj, key, descriptor) => {
for (const dec of decs) {
dec(obj, key, descriptor);
}
};
}
export function MergeClassDecorators(decs: ClassDecorator[]): ClassDecorator {
return (obj) => {
for (const dec of decs) {
dec(obj);
}
};
}
export function MergeParameterDecorators(
decs: ParameterDecorator[],
): ParameterDecorator {
return (obj, key, index) => {
for (const dec of decs) {
dec(obj, key, index);
}
};
}
...@@ -23,9 +23,3 @@ export const UpdatePipe = () => ...@@ -23,9 +23,3 @@ export const UpdatePipe = () =>
skipNullProperties: true, skipNullProperties: true,
skipUndefinedProperties: true, skipUndefinedProperties: true,
}); });
export const DataPipe = () =>
new ValidationPipe({
transform: true,
transformOptions: { enableImplicitConversion: true },
});
import { ColumnCommonOptions } from 'typeorm/decorator/options/ColumnCommonOptions'; import { ColumnCommonOptions } from 'typeorm/decorator/options/ColumnCommonOptions';
import { ApiProperty, ApiPropertyOptions } from '@nestjs/swagger'; import { ApiProperty, ApiPropertyOptions } from '@nestjs/swagger';
import { ColumnWithLengthOptions } from 'typeorm/decorator/options/ColumnWithLengthOptions'; import { ColumnWithLengthOptions } from 'typeorm/decorator/options/ColumnWithLengthOptions';
import { MergePropertyDecorators } from './merge'; import { MergePropertyDecorators } from 'nesties';
import { Column, Index } from 'typeorm'; import { Column, Index } from 'typeorm';
import { import {
IsDate, IsDate,
......
...@@ -9,14 +9,13 @@ import { ...@@ -9,14 +9,13 @@ import {
Post, Post,
Query, Query,
} from '@nestjs/common'; } from '@nestjs/common';
import { ImportDataDto, ImportEntryDto } from '../dto';
import { import {
BlankReturnMessageDto, BlankReturnMessageDto,
ImportDataDto, MergeMethodDecorators,
ImportEntryDto,
PaginatedReturnMessageDto, PaginatedReturnMessageDto,
ReturnMessageDto, ReturnMessageDto,
} from '../dto'; } from 'nesties';
import { MergeMethodDecorators } from './merge';
import { ClassType } from '../utility/insert-field'; import { ClassType } from '../utility/insert-field';
import { import {
ApiBadRequestResponse, ApiBadRequestResponse,
......
export * from './return-message';
export * from './import-entry'; export * from './import-entry';
import { ApiProperty } from '@nestjs/swagger';
import { HttpException } from '@nestjs/common';
import { PageSettingsWise } from '../bases/page-settings';
import {
AnyClass,
ClassOrArray,
getClassFromClassOrArray,
InsertField,
ParseType,
} from '../utility/insert-field';
export interface BlankReturnMessage {
statusCode: number;
message: string;
success: boolean;
}
export interface ReturnMessage<T> extends BlankReturnMessage {
data?: T;
}
export class BlankReturnMessageDto implements BlankReturnMessage {
@ApiProperty({ description: 'Return code', type: Number })
statusCode: number;
@ApiProperty({ description: 'Return message', type: String })
message: string;
@ApiProperty({ description: 'Whether success.', type: Boolean })
success: boolean;
constructor(statusCode: number, message?: string) {
this.statusCode = statusCode;
this.message = message || 'success';
this.success = statusCode < 400;
}
toException() {
return new HttpException(this, this.statusCode);
}
}
export class BlankPaginatedReturnMessageDto
extends BlankReturnMessageDto
implements PageSettingsWise
{
@ApiProperty({ description: 'Total record count.', type: Number })
total: number;
@ApiProperty({ description: 'Total page count.', type: Number })
totalPages: number;
@ApiProperty({ description: 'Current page.', type: Number })
pageCount: number;
@ApiProperty({ description: 'Records per page.', type: Number })
recordsPerPage: number;
constructor(
statusCode: number,
message: string,
total: number,
pageSettings: PageSettingsWise,
) {
super(statusCode, message);
this.total = total;
this.pageCount = pageSettings.pageCount;
this.recordsPerPage = pageSettings.recordsPerPage;
this.totalPages = Math.ceil(total / pageSettings.recordsPerPage);
}
}
export class GenericReturnMessageDto<T>
extends BlankReturnMessageDto
implements ReturnMessage<T>
{
data?: T;
constructor(statusCode: number, message?: string, data?: T) {
super(statusCode, message);
this.data = data;
}
}
export function ReturnMessageDto<T extends ClassOrArray>(
type: T,
): new (
statusCode: number,
message: string,
data: ParseType<T>,
) => GenericReturnMessageDto<ParseType<T>> {
return InsertField(
GenericReturnMessageDto,
{
data: {
type,
options: {
required: false,
description: 'Return data.',
},
},
},
`${getClassFromClassOrArray(type).name}ReturnMessageDto`,
);
}
export class GenericPaginatedReturnMessageDto<T>
extends BlankPaginatedReturnMessageDto
implements PageSettingsWise, ReturnMessage<T[]>
{
data: T[];
constructor(
statusCode: number,
message: string,
data: T[],
total: number,
pageSettings: PageSettingsWise,
) {
super(statusCode, message, total, pageSettings);
this.data = data;
}
}
export function PaginatedReturnMessageDto<T extends AnyClass>(
type: T,
): new (
statusCode: number,
message: string,
data: InstanceType<T>[],
total: number,
pageSettings: PageSettingsWise,
) => GenericPaginatedReturnMessageDto<InstanceType<T>> {
return InsertField(
GenericPaginatedReturnMessageDto,
{
data: {
type: [type],
options: {
required: false,
description: 'Return data.',
},
},
},
`${getClassFromClassOrArray(type).name}PaginatedReturnMessageDto`,
);
}
export class StringReturnMessageDto extends GenericReturnMessageDto<string> {
@ApiProperty({ description: 'Return data.', type: String, required: false })
data: string;
}
import { BlankReturnMessageDto } from '../dto';
import { ApiResponse } from '@nestjs/swagger';
export const ApiError = (status: number, description: string) =>
ApiResponse({ status, type: BlankReturnMessageDto, description });
export * from './query'; export * from './query';
export * from './api-error';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment