@iamnnort/nestjs-serializer 1.1.0 → 1.1.2
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 +17 -4
- package/dist/index.d.ts +17 -4
- package/dist/index.js +27 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -9
- 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
|
@@ -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
|
@@ -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
|
@@ -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
|
|
|
@@ -9227,6 +9243,7 @@ var SerializerModule = class extends ConfigurableModuleClass {
|
|
|
9227
9243
|
}
|
|
9228
9244
|
};
|
|
9229
9245
|
SerializerModule = exports.SerializerModule = _ts_decorate3([
|
|
9246
|
+
_common.Global.call(void 0, ),
|
|
9230
9247
|
_common.Module.call(void 0, {
|
|
9231
9248
|
providers: [
|
|
9232
9249
|
SerializerService
|
|
@@ -9242,5 +9259,6 @@ SerializerModule = exports.SerializerModule = _ts_decorate3([
|
|
|
9242
9259
|
|
|
9243
9260
|
|
|
9244
9261
|
|
|
9245
|
-
|
|
9262
|
+
|
|
9263
|
+
exports.SerializeField = SerializeField; exports.SerializeRelation = SerializeRelation; exports.SerializerIdInterceptor = SerializerIdInterceptor; exports.SerializerInterceptor = SerializerInterceptor; exports.SerializerModule = SerializerModule; exports.SerializerService = SerializerService;
|
|
9246
9264
|
//# sourceMappingURL=index.js.map
|