@tndhuy/create-app 1.2.8 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/mongo/src/common/decorators/standard-response.decorator.ts +9 -0
- package/templates/mongo/src/common/index.ts +1 -0
- package/templates/mongo/src/common/interceptors/transform.interceptor.ts +12 -1
- package/templates/mongo/src/modules/example/presenter/item.controller.ts +2 -2
- package/templates/postgres/src/common/decorators/standard-response.decorator.ts +9 -0
- package/templates/postgres/src/common/index.ts +1 -0
- package/templates/postgres/src/common/interceptors/transform.interceptor.ts +12 -1
- package/templates/postgres/src/modules/example/presenter/item.controller.ts +2 -2
- package/templates/mongo/src/common/decorators/public-api.decorator.ts +0 -9
- package/templates/postgres/src/common/decorators/public-api.decorator.ts +0 -9
package/package.json
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
export const STANDARD_RESPONSE_KEY = 'standard_response';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Decorator to wrap the response in a standardized envelope:
|
|
7
|
+
* { success: true, data: T, meta?: PaginationMeta }
|
|
8
|
+
*/
|
|
9
|
+
export const StandardResponse = () => SetMetadata(STANDARD_RESPONSE_KEY, true);
|
|
@@ -2,4 +2,5 @@ export * from './filters/http-exception.filter';
|
|
|
2
2
|
export * from './interceptors/transform.interceptor';
|
|
3
3
|
export * from './interceptors/timeout.interceptor';
|
|
4
4
|
export * from './decorators/raw-response.decorator';
|
|
5
|
+
export * from './decorators/standard-response.decorator';
|
|
5
6
|
export * from './middleware/correlation-id.middleware';
|
|
@@ -2,6 +2,7 @@ import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nes
|
|
|
2
2
|
import { Reflector } from '@nestjs/core';
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
4
|
import { map } from 'rxjs/operators';
|
|
5
|
+
import { STANDARD_RESPONSE_KEY } from '../decorators/standard-response.decorator';
|
|
5
6
|
|
|
6
7
|
export const RAW_RESPONSE_KEY = 'raw_response';
|
|
7
8
|
|
|
@@ -19,7 +20,17 @@ export class TransformInterceptor<T> implements NestInterceptor<T, unknown> {
|
|
|
19
20
|
return next.handle();
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
const useStandard = this.reflector.getAllAndOverride<boolean>(STANDARD_RESPONSE_KEY, [
|
|
24
|
+
context.getHandler(),
|
|
25
|
+
context.getClass(),
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
// Internal first: If not explicitly marked for Standard Response, return RAW data
|
|
29
|
+
if (!useStandard) {
|
|
30
|
+
return next.handle();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Standard Response: Wrap response in a standardized success object
|
|
23
34
|
return next.handle().pipe(
|
|
24
35
|
map((data) => {
|
|
25
36
|
// Handle paginated results: { items: [], meta: {} }
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
|
|
2
2
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
|
3
3
|
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
|
4
|
-
import {
|
|
4
|
+
import { StandardResponse } from '../../../common/decorators/standard-response.decorator';
|
|
5
5
|
import { CreateItemDto } from '../application/dtos/create-item.dto';
|
|
6
6
|
import { ItemResponseDto } from '../application/dtos/item.response.dto';
|
|
7
7
|
import { CreateItemCommand } from '../application/commands/create-item.command';
|
|
@@ -11,7 +11,7 @@ import { ListItemsQuery } from '../application/queries/list-items.query';
|
|
|
11
11
|
import { PaginationDto } from '../../../shared';
|
|
12
12
|
|
|
13
13
|
@ApiTags('example')
|
|
14
|
-
@
|
|
14
|
+
@StandardResponse()
|
|
15
15
|
@Controller('items')
|
|
16
16
|
export class ItemController {
|
|
17
17
|
constructor(
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
export const STANDARD_RESPONSE_KEY = 'standard_response';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Decorator to wrap the response in a standardized envelope:
|
|
7
|
+
* { success: true, data: T, meta?: PaginationMeta }
|
|
8
|
+
*/
|
|
9
|
+
export const StandardResponse = () => SetMetadata(STANDARD_RESPONSE_KEY, true);
|
|
@@ -2,4 +2,5 @@ export * from './filters/http-exception.filter';
|
|
|
2
2
|
export * from './interceptors/transform.interceptor';
|
|
3
3
|
export * from './interceptors/timeout.interceptor';
|
|
4
4
|
export * from './decorators/raw-response.decorator';
|
|
5
|
+
export * from './decorators/standard-response.decorator';
|
|
5
6
|
export * from './middleware/correlation-id.middleware';
|
|
@@ -2,6 +2,7 @@ import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nes
|
|
|
2
2
|
import { Reflector } from '@nestjs/core';
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
4
|
import { map } from 'rxjs/operators';
|
|
5
|
+
import { STANDARD_RESPONSE_KEY } from '../decorators/standard-response.decorator';
|
|
5
6
|
|
|
6
7
|
export const RAW_RESPONSE_KEY = 'raw_response';
|
|
7
8
|
|
|
@@ -19,7 +20,17 @@ export class TransformInterceptor<T> implements NestInterceptor<T, unknown> {
|
|
|
19
20
|
return next.handle();
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
const useStandard = this.reflector.getAllAndOverride<boolean>(STANDARD_RESPONSE_KEY, [
|
|
24
|
+
context.getHandler(),
|
|
25
|
+
context.getClass(),
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
// Internal first: If not explicitly marked for Standard Response, return RAW data
|
|
29
|
+
if (!useStandard) {
|
|
30
|
+
return next.handle();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Standard Response: Wrap response in a standardized success object
|
|
23
34
|
return next.handle().pipe(
|
|
24
35
|
map((data) => {
|
|
25
36
|
// Handle paginated results: { items: [], meta: {} }
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
|
|
2
2
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
|
3
3
|
import { CommandBus, QueryBus } from '@nestjs/cqrs';
|
|
4
|
-
import {
|
|
4
|
+
import { StandardResponse } from '../../../common/decorators/standard-response.decorator';
|
|
5
5
|
import { CreateItemDto } from '../application/dtos/create-item.dto';
|
|
6
6
|
import { ItemResponseDto } from '../application/dtos/item.response.dto';
|
|
7
7
|
import { CreateItemCommand } from '../application/commands/create-item.command';
|
|
@@ -11,7 +11,7 @@ import { ListItemsQuery } from '../application/queries/list-items.query';
|
|
|
11
11
|
import { PaginationDto } from '../../../shared';
|
|
12
12
|
|
|
13
13
|
@ApiTags('example')
|
|
14
|
-
@
|
|
14
|
+
@StandardResponse()
|
|
15
15
|
@Controller('items')
|
|
16
16
|
export class ItemController {
|
|
17
17
|
constructor(
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { SetMetadata } from '@nestjs/common';
|
|
2
|
-
|
|
3
|
-
export const PUBLIC_API_KEY = 'public_api';
|
|
4
|
-
/**
|
|
5
|
-
* Decorator to mark an endpoint as Public API.
|
|
6
|
-
* This will trigger the TransformInterceptor to wrap the response in { success: true, data: T }.
|
|
7
|
-
* Without this decorator, the response remains raw.
|
|
8
|
-
*/
|
|
9
|
-
export const PublicApi = () => SetMetadata(PUBLIC_API_KEY, true);
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { SetMetadata } from '@nestjs/common';
|
|
2
|
-
|
|
3
|
-
export const PUBLIC_API_KEY = 'public_api';
|
|
4
|
-
/**
|
|
5
|
-
* Decorator to mark an endpoint as Public API.
|
|
6
|
-
* This will trigger the TransformInterceptor to wrap the response in { success: true, data: T }.
|
|
7
|
-
* Without this decorator, the response remains raw.
|
|
8
|
-
*/
|
|
9
|
-
export const PublicApi = () => SetMetadata(PUBLIC_API_KEY, true);
|