@iamnnort/nestjs-serializer 1.0.2 → 1.0.4
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/README.md +115 -21
- package/dist/index.d.mts +43 -7
- package/dist/index.d.ts +43 -7
- package/dist/index.js +9177 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9176 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -11,32 +11,125 @@ yarn install @iamnnort/nestjs-serializer
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```javascript
|
|
14
|
-
// app.controller.ts
|
|
15
|
-
import { Controller, Get } from '@nestjs/common';
|
|
16
|
-
import { RequestService } from '@iamnnort/nestjs-serializer';
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
// types.ts
|
|
16
|
+
|
|
17
|
+
export enum Scopes {
|
|
18
|
+
BASE = 'BASE',
|
|
19
|
+
FULL = 'FULL',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// model.ts
|
|
23
|
+
|
|
24
|
+
import { SerializeField } from '@iamnnort/nestjs-serializer';
|
|
25
|
+
import { Scopes } from './types';
|
|
26
|
+
|
|
27
|
+
export class Model {
|
|
28
|
+
@SerializeField([{ scopes: [Scopes.BASE] }])
|
|
29
|
+
id: number;
|
|
30
|
+
|
|
31
|
+
constructor(dto: { id: number }) {
|
|
32
|
+
this.id = dto.id;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// city.ts
|
|
37
|
+
|
|
38
|
+
import { SerializeField } from '@iamnnort/nestjs-serializer';
|
|
39
|
+
import { Model } from './model';
|
|
40
|
+
import { Scopes } from './types';
|
|
41
|
+
|
|
42
|
+
export class City extends Model {
|
|
43
|
+
@SerializeField([{ scopes: [Scopes.FULL] }])
|
|
44
|
+
name: string;
|
|
45
|
+
|
|
46
|
+
constructor(dto: { id: number; name: string }) {
|
|
47
|
+
super(dto);
|
|
48
|
+
|
|
49
|
+
this.name = dto.name;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
21
52
|
|
|
53
|
+
// user.ts
|
|
54
|
+
|
|
55
|
+
import { SerializeField, SerializeRelation } from '@iamnnort/nestjs-serializer';
|
|
56
|
+
import { City } from './city';
|
|
57
|
+
import { Model } from './model';
|
|
58
|
+
import { Scopes } from './types';
|
|
59
|
+
|
|
60
|
+
export class User extends Model {
|
|
61
|
+
@SerializeField([{ scopes: [Scopes.FULL] }])
|
|
62
|
+
name: string;
|
|
63
|
+
|
|
64
|
+
@SerializeRelation([{ scopes: [Scopes.FULL], relationScopes: [Scopes.BASE, Scopes.FULL] }])
|
|
65
|
+
city: City;
|
|
66
|
+
|
|
67
|
+
constructor(dto: { id: number; name: string; city: City }) {
|
|
68
|
+
super(dto);
|
|
69
|
+
|
|
70
|
+
this.name = dto.name;
|
|
71
|
+
this.city = dto.city;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// controller.ts
|
|
76
|
+
|
|
77
|
+
import { Controller, Get, UseInterceptors } from '@nestjs/common';
|
|
78
|
+
import { SerializerInterceptor } from '@iamnnort/nestjs-serializer';
|
|
79
|
+
import { User } from './user';
|
|
80
|
+
import { Scopes } from './types';
|
|
81
|
+
import { City } from './city';
|
|
82
|
+
|
|
83
|
+
@Controller()
|
|
84
|
+
export class AppController {
|
|
22
85
|
@Get()
|
|
23
|
-
|
|
24
|
-
|
|
86
|
+
@UseInterceptors(SerializerInterceptor([Scopes.BASE]))
|
|
87
|
+
search() {
|
|
88
|
+
const city = new City({
|
|
89
|
+
id: 1,
|
|
90
|
+
name: 'London',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const user = new User({
|
|
94
|
+
id: 1,
|
|
95
|
+
name: 'John',
|
|
96
|
+
city,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return [user];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Get(':id')
|
|
103
|
+
@UseInterceptors(SerializerInterceptor([Scopes.BASE, Scopes.FULL]))
|
|
104
|
+
get() {
|
|
105
|
+
const city = new City({
|
|
106
|
+
id: 1,
|
|
107
|
+
name: 'London',
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const user = new User({
|
|
111
|
+
id: 1,
|
|
112
|
+
name: 'John',
|
|
113
|
+
city,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return user;
|
|
25
117
|
}
|
|
26
118
|
}
|
|
27
119
|
|
|
28
|
-
//
|
|
120
|
+
// module.ts
|
|
121
|
+
|
|
29
122
|
import { Module } from '@nestjs/common';
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
123
|
+
import { LoggerModule } from '@iamnnort/nestjs-logger';
|
|
124
|
+
import { SerializerModule } from '@iamnnort/nestjs-serializer';
|
|
125
|
+
import { Model } from './model';
|
|
126
|
+
import { AppController } from './controller';
|
|
32
127
|
|
|
33
128
|
@Module({
|
|
34
129
|
imports: [
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
url: '/todos',
|
|
39
|
-
logger: true,
|
|
130
|
+
LoggerModule,
|
|
131
|
+
SerializerModule.register({
|
|
132
|
+
globalEntityNames: [Model.name],
|
|
40
133
|
}),
|
|
41
134
|
],
|
|
42
135
|
controllers: [AppController],
|
|
@@ -44,9 +137,10 @@ import { AppController } from './app.controller';
|
|
|
44
137
|
export class AppModule {}
|
|
45
138
|
|
|
46
139
|
// index.ts
|
|
140
|
+
|
|
47
141
|
import { NestFactory } from '@nestjs/core';
|
|
48
142
|
import { LoggerService } from '@iamnnort/nestjs-logger';
|
|
49
|
-
import { AppModule } from './
|
|
143
|
+
import { AppModule } from './module';
|
|
50
144
|
|
|
51
145
|
async function bootstrap() {
|
|
52
146
|
const app = await NestFactory.create(AppModule, {
|
|
@@ -66,10 +160,10 @@ bootstrap();
|
|
|
66
160
|
```bash
|
|
67
161
|
[System] Application is starting...
|
|
68
162
|
[System] Application started.
|
|
69
|
-
[System] [Request] GET /
|
|
70
|
-
[
|
|
71
|
-
[
|
|
72
|
-
[System] [Response] GET /
|
|
163
|
+
[System] [Request] GET /
|
|
164
|
+
[System] [Response] GET / 200 OK [{ id: 1 }]
|
|
165
|
+
[System] [Request] GET /1
|
|
166
|
+
[System] [Response] GET /1 200 OK [{ id: 1, name: "John", city: { id: 1, name: "London" } }]
|
|
73
167
|
```
|
|
74
168
|
|
|
75
169
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -1,17 +1,53 @@
|
|
|
1
|
+
import * as rxjs from 'rxjs';
|
|
1
2
|
import * as _nestjs_common from '@nestjs/common';
|
|
3
|
+
import { ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare const
|
|
5
|
+
declare const SerializeField: (configs: {
|
|
6
|
+
scopes: string[];
|
|
7
|
+
fieldName?: string;
|
|
8
|
+
}[]) => (target: object, name: string) => void;
|
|
9
|
+
declare const SerializeRelation: (configs: {
|
|
10
|
+
scopes: string[];
|
|
11
|
+
relationScopes: string[];
|
|
12
|
+
fieldName?: string;
|
|
13
|
+
fieldTransform?: (entity: any) => Promise<any>;
|
|
14
|
+
}[]) => (target: object, name: string) => void;
|
|
8
15
|
|
|
9
|
-
declare
|
|
16
|
+
declare global {
|
|
17
|
+
var transformFieldConfigs: {
|
|
18
|
+
scopes: string[];
|
|
19
|
+
relationScopes?: string[];
|
|
20
|
+
target: Function;
|
|
21
|
+
name: string;
|
|
22
|
+
fieldName?: string;
|
|
23
|
+
fieldTransform?: (entity: any) => Promise<any>;
|
|
24
|
+
}[];
|
|
10
25
|
}
|
|
26
|
+
type SerializerConfig = {
|
|
27
|
+
globalEntityNames?: string[];
|
|
28
|
+
};
|
|
11
29
|
|
|
12
30
|
declare class SerializerService {
|
|
13
31
|
config: SerializerConfig;
|
|
32
|
+
globalEntityNames: string[];
|
|
14
33
|
constructor(config: SerializerConfig);
|
|
34
|
+
transform(response: any, scopes: string[]): Promise<any>;
|
|
35
|
+
transformEntity(entity: any, scopes?: string[]): Promise<any>;
|
|
36
|
+
getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
|
|
37
|
+
makeRelations(relationField: string, relationRelations: string[]): string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare const SerializerInterceptor: (scopes: string[], extendedScopes?: string[]) => {
|
|
41
|
+
new (serializerService: SerializerService): {
|
|
42
|
+
serializerService: SerializerService;
|
|
43
|
+
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
44
|
+
getScopes(ctx: ExecutionContext): string[];
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
declare const ConfigurableModuleClass: _nestjs_common.ConfigurableModuleCls<SerializerConfig, "register", "create", {}>;
|
|
49
|
+
|
|
50
|
+
declare class SerializerModule extends ConfigurableModuleClass {
|
|
15
51
|
}
|
|
16
52
|
|
|
17
|
-
export { type SerializerConfig, SerializerModule, SerializerService };
|
|
53
|
+
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerInterceptor, SerializerModule, SerializerService };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,53 @@
|
|
|
1
|
+
import * as rxjs from 'rxjs';
|
|
1
2
|
import * as _nestjs_common from '@nestjs/common';
|
|
3
|
+
import { ExecutionContext, CallHandler } from '@nestjs/common';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
declare const
|
|
5
|
+
declare const SerializeField: (configs: {
|
|
6
|
+
scopes: string[];
|
|
7
|
+
fieldName?: string;
|
|
8
|
+
}[]) => (target: object, name: string) => void;
|
|
9
|
+
declare const SerializeRelation: (configs: {
|
|
10
|
+
scopes: string[];
|
|
11
|
+
relationScopes: string[];
|
|
12
|
+
fieldName?: string;
|
|
13
|
+
fieldTransform?: (entity: any) => Promise<any>;
|
|
14
|
+
}[]) => (target: object, name: string) => void;
|
|
8
15
|
|
|
9
|
-
declare
|
|
16
|
+
declare global {
|
|
17
|
+
var transformFieldConfigs: {
|
|
18
|
+
scopes: string[];
|
|
19
|
+
relationScopes?: string[];
|
|
20
|
+
target: Function;
|
|
21
|
+
name: string;
|
|
22
|
+
fieldName?: string;
|
|
23
|
+
fieldTransform?: (entity: any) => Promise<any>;
|
|
24
|
+
}[];
|
|
10
25
|
}
|
|
26
|
+
type SerializerConfig = {
|
|
27
|
+
globalEntityNames?: string[];
|
|
28
|
+
};
|
|
11
29
|
|
|
12
30
|
declare class SerializerService {
|
|
13
31
|
config: SerializerConfig;
|
|
32
|
+
globalEntityNames: string[];
|
|
14
33
|
constructor(config: SerializerConfig);
|
|
34
|
+
transform(response: any, scopes: string[]): Promise<any>;
|
|
35
|
+
transformEntity(entity: any, scopes?: string[]): Promise<any>;
|
|
36
|
+
getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
|
|
37
|
+
makeRelations(relationField: string, relationRelations: string[]): string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare const SerializerInterceptor: (scopes: string[], extendedScopes?: string[]) => {
|
|
41
|
+
new (serializerService: SerializerService): {
|
|
42
|
+
serializerService: SerializerService;
|
|
43
|
+
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
44
|
+
getScopes(ctx: ExecutionContext): string[];
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
declare const ConfigurableModuleClass: _nestjs_common.ConfigurableModuleCls<SerializerConfig, "register", "create", {}>;
|
|
49
|
+
|
|
50
|
+
declare class SerializerModule extends ConfigurableModuleClass {
|
|
15
51
|
}
|
|
16
52
|
|
|
17
|
-
export { type SerializerConfig, SerializerModule, SerializerService };
|
|
53
|
+
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerInterceptor, SerializerModule, SerializerService };
|