@iamnnort/nestjs-serializer 1.0.4 → 1.1.1
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 +23 -4
- package/dist/index.d.mts +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +31 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -74,8 +74,8 @@ export class User extends Model {
|
|
|
74
74
|
|
|
75
75
|
// controller.ts
|
|
76
76
|
|
|
77
|
-
import { Controller, Get, UseInterceptors } from '@nestjs/common';
|
|
78
|
-
import { SerializerInterceptor } from '@iamnnort/nestjs-serializer';
|
|
77
|
+
import { Controller, Get, Post, UseInterceptors } from '@nestjs/common';
|
|
78
|
+
import { SerializerIdInterceptor, SerializerInterceptor } from '@iamnnort/nestjs-serializer';
|
|
79
79
|
import { User } from './user';
|
|
80
80
|
import { Scopes } from './types';
|
|
81
81
|
import { City } from './city';
|
|
@@ -83,7 +83,7 @@ import { City } from './city';
|
|
|
83
83
|
@Controller()
|
|
84
84
|
export class AppController {
|
|
85
85
|
@Get()
|
|
86
|
-
@UseInterceptors(SerializerInterceptor([Scopes.BASE]))
|
|
86
|
+
@UseInterceptors(SerializerInterceptor({ scopes: [Scopes.BASE] }))
|
|
87
87
|
search() {
|
|
88
88
|
const city = new City({
|
|
89
89
|
id: 1,
|
|
@@ -100,7 +100,7 @@ export class AppController {
|
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
@Get(':id')
|
|
103
|
-
@UseInterceptors(SerializerInterceptor([Scopes.BASE, Scopes.FULL]))
|
|
103
|
+
@UseInterceptors(SerializerInterceptor({ scopes: [Scopes.BASE, Scopes.FULL] }))
|
|
104
104
|
get() {
|
|
105
105
|
const city = new City({
|
|
106
106
|
id: 1,
|
|
@@ -115,6 +115,23 @@ export class AppController {
|
|
|
115
115
|
|
|
116
116
|
return user;
|
|
117
117
|
}
|
|
118
|
+
|
|
119
|
+
@Post()
|
|
120
|
+
@UseInterceptors(SerializerIdInterceptor)
|
|
121
|
+
create() {
|
|
122
|
+
const city = new City({
|
|
123
|
+
id: 1,
|
|
124
|
+
name: 'London',
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const user = new User({
|
|
128
|
+
id: 1,
|
|
129
|
+
name: 'John',
|
|
130
|
+
city,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return user;
|
|
134
|
+
}
|
|
118
135
|
}
|
|
119
136
|
|
|
120
137
|
// module.ts
|
|
@@ -164,6 +181,8 @@ bootstrap();
|
|
|
164
181
|
[System] [Response] GET / 200 OK [{ id: 1 }]
|
|
165
182
|
[System] [Request] GET /1
|
|
166
183
|
[System] [Response] GET /1 200 OK [{ id: 1, name: "John", city: { id: 1, name: "London" } }]
|
|
184
|
+
[System] [Request] POST /1
|
|
185
|
+
[System] [Response] POST /1 201 Created { id: 1 }
|
|
167
186
|
```
|
|
168
187
|
|
|
169
188
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -14,7 +14,7 @@ declare const SerializeRelation: (configs: {
|
|
|
14
14
|
}[]) => (target: object, name: string) => void;
|
|
15
15
|
|
|
16
16
|
declare global {
|
|
17
|
-
var
|
|
17
|
+
var serializerFieldConfigs: {
|
|
18
18
|
scopes: string[];
|
|
19
19
|
relationScopes?: string[];
|
|
20
20
|
target: Function;
|
|
@@ -31,23 +31,36 @@ declare class SerializerService {
|
|
|
31
31
|
config: SerializerConfig;
|
|
32
32
|
globalEntityNames: string[];
|
|
33
33
|
constructor(config: SerializerConfig);
|
|
34
|
-
transform(response: any, scopes
|
|
34
|
+
transform(response: any, scopes?: string[]): Promise<any>;
|
|
35
35
|
transformEntity(entity: any, scopes?: string[]): Promise<any>;
|
|
36
36
|
getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
|
|
37
37
|
makeRelations(relationField: string, relationRelations: string[]): string[];
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
declare const SerializerInterceptor: (
|
|
40
|
+
declare const SerializerInterceptor: (config: {
|
|
41
|
+
scopes?: string[];
|
|
42
|
+
extendedScopes?: string[];
|
|
43
|
+
fields?: string[];
|
|
44
|
+
}) => {
|
|
41
45
|
new (serializerService: SerializerService): {
|
|
42
46
|
serializerService: SerializerService;
|
|
43
47
|
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
44
|
-
getScopes(ctx: ExecutionContext): string[];
|
|
48
|
+
getScopes(ctx: ExecutionContext): string[] | undefined;
|
|
45
49
|
};
|
|
46
50
|
};
|
|
51
|
+
declare const SerializerIdInterceptor_base: {
|
|
52
|
+
new (serializerService: SerializerService): {
|
|
53
|
+
serializerService: SerializerService;
|
|
54
|
+
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
55
|
+
getScopes(ctx: ExecutionContext): string[] | undefined;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
declare class SerializerIdInterceptor extends SerializerIdInterceptor_base {
|
|
59
|
+
}
|
|
47
60
|
|
|
48
61
|
declare const ConfigurableModuleClass: _nestjs_common.ConfigurableModuleCls<SerializerConfig, "register", "create", {}>;
|
|
49
62
|
|
|
50
63
|
declare class SerializerModule extends ConfigurableModuleClass {
|
|
51
64
|
}
|
|
52
65
|
|
|
53
|
-
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerInterceptor, SerializerModule, SerializerService };
|
|
66
|
+
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerIdInterceptor, SerializerInterceptor, SerializerModule, SerializerService };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ declare const SerializeRelation: (configs: {
|
|
|
14
14
|
}[]) => (target: object, name: string) => void;
|
|
15
15
|
|
|
16
16
|
declare global {
|
|
17
|
-
var
|
|
17
|
+
var serializerFieldConfigs: {
|
|
18
18
|
scopes: string[];
|
|
19
19
|
relationScopes?: string[];
|
|
20
20
|
target: Function;
|
|
@@ -31,23 +31,36 @@ declare class SerializerService {
|
|
|
31
31
|
config: SerializerConfig;
|
|
32
32
|
globalEntityNames: string[];
|
|
33
33
|
constructor(config: SerializerConfig);
|
|
34
|
-
transform(response: any, scopes
|
|
34
|
+
transform(response: any, scopes?: string[]): Promise<any>;
|
|
35
35
|
transformEntity(entity: any, scopes?: string[]): Promise<any>;
|
|
36
36
|
getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
|
|
37
37
|
makeRelations(relationField: string, relationRelations: string[]): string[];
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
declare const SerializerInterceptor: (
|
|
40
|
+
declare const SerializerInterceptor: (config: {
|
|
41
|
+
scopes?: string[];
|
|
42
|
+
extendedScopes?: string[];
|
|
43
|
+
fields?: string[];
|
|
44
|
+
}) => {
|
|
41
45
|
new (serializerService: SerializerService): {
|
|
42
46
|
serializerService: SerializerService;
|
|
43
47
|
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
44
|
-
getScopes(ctx: ExecutionContext): string[];
|
|
48
|
+
getScopes(ctx: ExecutionContext): string[] | undefined;
|
|
45
49
|
};
|
|
46
50
|
};
|
|
51
|
+
declare const SerializerIdInterceptor_base: {
|
|
52
|
+
new (serializerService: SerializerService): {
|
|
53
|
+
serializerService: SerializerService;
|
|
54
|
+
intercept(ctx: ExecutionContext, next: CallHandler<Promise<any>>): rxjs.Observable<Promise<any>>;
|
|
55
|
+
getScopes(ctx: ExecutionContext): string[] | undefined;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
declare class SerializerIdInterceptor extends SerializerIdInterceptor_base {
|
|
59
|
+
}
|
|
47
60
|
|
|
48
61
|
declare const ConfigurableModuleClass: _nestjs_common.ConfigurableModuleCls<SerializerConfig, "register", "create", {}>;
|
|
49
62
|
|
|
50
63
|
declare class SerializerModule extends ConfigurableModuleClass {
|
|
51
64
|
}
|
|
52
65
|
|
|
53
|
-
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerInterceptor, SerializerModule, SerializerService };
|
|
66
|
+
export { SerializeField, SerializeRelation, type SerializerConfig, SerializerIdInterceptor, SerializerInterceptor, SerializerModule, SerializerService };
|
package/dist/index.js
CHANGED
|
@@ -8993,11 +8993,11 @@ var require_operators = __commonJS({
|
|
|
8993
8993
|
});
|
|
8994
8994
|
|
|
8995
8995
|
// src/decorator.ts
|
|
8996
|
-
global.
|
|
8996
|
+
global.serializerFieldConfigs = [];
|
|
8997
8997
|
var SerializeField = /* @__PURE__ */ __name((configs) => {
|
|
8998
8998
|
return (target, name) => {
|
|
8999
8999
|
configs.forEach((config) => {
|
|
9000
|
-
global.
|
|
9000
|
+
global.serializerFieldConfigs.push({
|
|
9001
9001
|
scopes: config.scopes,
|
|
9002
9002
|
target: target.constructor,
|
|
9003
9003
|
name,
|
|
@@ -9009,7 +9009,7 @@ var SerializeField = /* @__PURE__ */ __name((configs) => {
|
|
|
9009
9009
|
var SerializeRelation = /* @__PURE__ */ __name((configs) => {
|
|
9010
9010
|
return (target, name) => {
|
|
9011
9011
|
configs.forEach((config) => {
|
|
9012
|
-
global.
|
|
9012
|
+
global.serializerFieldConfigs.push({
|
|
9013
9013
|
scopes: config.scopes,
|
|
9014
9014
|
relationScopes: config.relationScopes,
|
|
9015
9015
|
target: target.constructor,
|
|
@@ -9085,7 +9085,7 @@ var SerializerService = class {
|
|
|
9085
9085
|
if (!entity) {
|
|
9086
9086
|
return entity;
|
|
9087
9087
|
}
|
|
9088
|
-
const
|
|
9088
|
+
const serializerFieldConfigs = global.serializerFieldConfigs.filter((fieldConfig) => {
|
|
9089
9089
|
const isTarget = fieldConfig.target === entity.constructor;
|
|
9090
9090
|
if (!isTarget) {
|
|
9091
9091
|
const isGlobalTarget = this.globalEntityNames.includes(fieldConfig.target.name);
|
|
@@ -9099,7 +9099,7 @@ var SerializerService = class {
|
|
|
9099
9099
|
return isScope;
|
|
9100
9100
|
});
|
|
9101
9101
|
let transformedEntity = {};
|
|
9102
|
-
await Promise.all(
|
|
9102
|
+
await Promise.all(serializerFieldConfigs.map(async (fieldConfig) => {
|
|
9103
9103
|
const fieldName = fieldConfig.fieldName || fieldConfig.name;
|
|
9104
9104
|
let fieldValue = await entity[fieldConfig.name];
|
|
9105
9105
|
if (!_lodash.isNil.call(void 0, fieldConfig.relationScopes)) {
|
|
@@ -9164,6 +9164,7 @@ SerializerService = exports.SerializerService = _ts_decorate([
|
|
|
9164
9164
|
], SerializerService);
|
|
9165
9165
|
|
|
9166
9166
|
// src/interceptor.ts
|
|
9167
|
+
|
|
9167
9168
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
9168
9169
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
9169
9170
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -9175,7 +9176,7 @@ function _ts_metadata2(k, v) {
|
|
|
9175
9176
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9176
9177
|
}
|
|
9177
9178
|
__name(_ts_metadata2, "_ts_metadata");
|
|
9178
|
-
var SerializerInterceptor = /* @__PURE__ */ __name((
|
|
9179
|
+
var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
|
|
9179
9180
|
let SerializerInterceptor2 = class SerializerInterceptor {
|
|
9180
9181
|
static {
|
|
9181
9182
|
__name(this, "SerializerInterceptor");
|
|
@@ -9185,21 +9186,24 @@ var SerializerInterceptor = /* @__PURE__ */ __name((scopes, extendedScopes) => {
|
|
|
9185
9186
|
this.serializerService = serializerService;
|
|
9186
9187
|
}
|
|
9187
9188
|
intercept(ctx, next) {
|
|
9188
|
-
const
|
|
9189
|
+
const scopes = this.getScopes(ctx);
|
|
9189
9190
|
return next.handle().pipe((0, import_operators.map)(async (responsePromise) => {
|
|
9190
|
-
if (!actualScopes) {
|
|
9191
|
-
return responsePromise;
|
|
9192
|
-
}
|
|
9193
9191
|
const response = await responsePromise;
|
|
9194
|
-
|
|
9192
|
+
if (config.fields) {
|
|
9193
|
+
return _lodash.pick.call(void 0, response, config.fields);
|
|
9194
|
+
}
|
|
9195
|
+
if (scopes) {
|
|
9196
|
+
return this.serializerService.transform(response, scopes);
|
|
9197
|
+
}
|
|
9198
|
+
return response;
|
|
9195
9199
|
}));
|
|
9196
9200
|
}
|
|
9197
9201
|
getScopes(ctx) {
|
|
9198
9202
|
const request = ctx.switchToHttp().getRequest();
|
|
9199
9203
|
if (request.query.extended) {
|
|
9200
|
-
return extendedScopes || scopes;
|
|
9204
|
+
return config.extendedScopes || config.scopes;
|
|
9201
9205
|
}
|
|
9202
|
-
return scopes;
|
|
9206
|
+
return config.scopes;
|
|
9203
9207
|
}
|
|
9204
9208
|
};
|
|
9205
9209
|
SerializerInterceptor2 = _ts_decorate2([
|
|
@@ -9211,6 +9215,18 @@ var SerializerInterceptor = /* @__PURE__ */ __name((scopes, extendedScopes) => {
|
|
|
9211
9215
|
], SerializerInterceptor2);
|
|
9212
9216
|
return SerializerInterceptor2;
|
|
9213
9217
|
}, "SerializerInterceptor");
|
|
9218
|
+
var SerializerIdInterceptor = class extends SerializerInterceptor({
|
|
9219
|
+
fields: [
|
|
9220
|
+
"id"
|
|
9221
|
+
]
|
|
9222
|
+
}) {
|
|
9223
|
+
static {
|
|
9224
|
+
__name(this, "SerializerIdInterceptor");
|
|
9225
|
+
}
|
|
9226
|
+
};
|
|
9227
|
+
SerializerIdInterceptor = exports.SerializerIdInterceptor = _ts_decorate2([
|
|
9228
|
+
_common.Injectable.call(void 0, )
|
|
9229
|
+
], SerializerIdInterceptor);
|
|
9214
9230
|
|
|
9215
9231
|
// src/module.ts
|
|
9216
9232
|
|
|
@@ -9242,5 +9258,6 @@ SerializerModule = exports.SerializerModule = _ts_decorate3([
|
|
|
9242
9258
|
|
|
9243
9259
|
|
|
9244
9260
|
|
|
9245
|
-
|
|
9261
|
+
|
|
9262
|
+
exports.SerializeField = SerializeField; exports.SerializeRelation = SerializeRelation; exports.SerializerIdInterceptor = SerializerIdInterceptor; exports.SerializerInterceptor = SerializerInterceptor; exports.SerializerModule = SerializerModule; exports.SerializerService = SerializerService;
|
|
9246
9263
|
//# sourceMappingURL=index.js.map
|