@tndhuy/create-app 1.2.6 → 1.2.8
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/interceptors/transform.interceptor.ts +14 -12
- package/templates/mongo/src/modules/example/application/queries/list-items.handler.ts +13 -2
- package/templates/postgres/src/common/interceptors/transform.interceptor.ts +14 -12
- package/templates/postgres/src/modules/example/application/queries/list-items.handler.ts +13 -2
package/package.json
CHANGED
|
@@ -2,7 +2,6 @@ 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 { PUBLIC_API_KEY } from '../decorators/public-api.decorator';
|
|
6
5
|
|
|
7
6
|
export const RAW_RESPONSE_KEY = 'raw_response';
|
|
8
7
|
|
|
@@ -20,17 +19,20 @@ export class TransformInterceptor<T> implements NestInterceptor<T, unknown> {
|
|
|
20
19
|
return next.handle();
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
// Wrap all responses in a standardized success object
|
|
23
|
+
return next.handle().pipe(
|
|
24
|
+
map((data) => {
|
|
25
|
+
// Handle paginated results: { items: [], meta: {} }
|
|
26
|
+
if (data && typeof data === 'object' && 'items' in data && 'meta' in data) {
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
data: data.items,
|
|
30
|
+
meta: data.meta,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
return { success: true, data };
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -3,6 +3,7 @@ import { Inject } from '@nestjs/common';
|
|
|
3
3
|
import { ListItemsQuery } from './list-items.query';
|
|
4
4
|
import { ITEM_REPOSITORY, IItemRepository } from '../../domain/item.repository.interface';
|
|
5
5
|
import { Item } from '../../domain/item.entity';
|
|
6
|
+
import { PaginationMeta } from '../../../shared/dto/pagination.dto';
|
|
6
7
|
|
|
7
8
|
@QueryHandler(ListItemsQuery)
|
|
8
9
|
export class ListItemsHandler implements IQueryHandler<ListItemsQuery> {
|
|
@@ -10,7 +11,17 @@ export class ListItemsHandler implements IQueryHandler<ListItemsQuery> {
|
|
|
10
11
|
@Inject(ITEM_REPOSITORY) private readonly itemRepository: IItemRepository,
|
|
11
12
|
) {}
|
|
12
13
|
|
|
13
|
-
async execute(_query: ListItemsQuery): Promise<Item[]> {
|
|
14
|
-
|
|
14
|
+
async execute(_query: ListItemsQuery): Promise<{ items: Item[]; meta: PaginationMeta }> {
|
|
15
|
+
const items = await this.itemRepository.findAll();
|
|
16
|
+
|
|
17
|
+
// Example metadata — in a real app, these values would come from the database query
|
|
18
|
+
const meta: PaginationMeta = {
|
|
19
|
+
total: items.length,
|
|
20
|
+
page: 1,
|
|
21
|
+
limit: 20,
|
|
22
|
+
totalPages: Math.ceil(items.length / 20) || 1,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return { items, meta };
|
|
15
26
|
}
|
|
16
27
|
}
|
|
@@ -2,7 +2,6 @@ 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 { PUBLIC_API_KEY } from '../decorators/public-api.decorator';
|
|
6
5
|
|
|
7
6
|
export const RAW_RESPONSE_KEY = 'raw_response';
|
|
8
7
|
|
|
@@ -20,17 +19,20 @@ export class TransformInterceptor<T> implements NestInterceptor<T, unknown> {
|
|
|
20
19
|
return next.handle();
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
// Wrap all responses in a standardized success object
|
|
23
|
+
return next.handle().pipe(
|
|
24
|
+
map((data) => {
|
|
25
|
+
// Handle paginated results: { items: [], meta: {} }
|
|
26
|
+
if (data && typeof data === 'object' && 'items' in data && 'meta' in data) {
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
data: data.items,
|
|
30
|
+
meta: data.meta,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
return { success: true, data };
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -3,6 +3,7 @@ import { Inject } from '@nestjs/common';
|
|
|
3
3
|
import { ListItemsQuery } from './list-items.query';
|
|
4
4
|
import { ITEM_REPOSITORY, IItemRepository } from '../../domain/item.repository.interface';
|
|
5
5
|
import { Item } from '../../domain/item.entity';
|
|
6
|
+
import { PaginationMeta } from '../../../shared/dto/pagination.dto';
|
|
6
7
|
|
|
7
8
|
@QueryHandler(ListItemsQuery)
|
|
8
9
|
export class ListItemsHandler implements IQueryHandler<ListItemsQuery> {
|
|
@@ -10,7 +11,17 @@ export class ListItemsHandler implements IQueryHandler<ListItemsQuery> {
|
|
|
10
11
|
@Inject(ITEM_REPOSITORY) private readonly itemRepository: IItemRepository,
|
|
11
12
|
) {}
|
|
12
13
|
|
|
13
|
-
async execute(_query: ListItemsQuery): Promise<Item[]> {
|
|
14
|
-
|
|
14
|
+
async execute(_query: ListItemsQuery): Promise<{ items: Item[]; meta: PaginationMeta }> {
|
|
15
|
+
const items = await this.itemRepository.findAll();
|
|
16
|
+
|
|
17
|
+
// Example metadata — in a real app, these values would come from the database query
|
|
18
|
+
const meta: PaginationMeta = {
|
|
19
|
+
total: items.length,
|
|
20
|
+
page: 1,
|
|
21
|
+
limit: 20,
|
|
22
|
+
totalPages: Math.ceil(items.length / 20) || 1,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return { items, meta };
|
|
15
26
|
}
|
|
16
27
|
}
|