@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/dist/index.mjs
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 (!isNil(fieldConfig.relationScopes)) {
|
|
@@ -9164,6 +9164,7 @@ SerializerService = _ts_decorate([
|
|
|
9164
9164
|
], SerializerService);
|
|
9165
9165
|
|
|
9166
9166
|
// src/interceptor.ts
|
|
9167
|
+
import { pick } from "lodash";
|
|
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 pick(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 = _ts_decorate2([
|
|
9228
|
+
Injectable2()
|
|
9229
|
+
], SerializerIdInterceptor);
|
|
9214
9230
|
|
|
9215
9231
|
// src/module.ts
|
|
9216
9232
|
import { Module } from "@nestjs/common";
|
|
@@ -9239,6 +9255,7 @@ SerializerModule = _ts_decorate3([
|
|
|
9239
9255
|
export {
|
|
9240
9256
|
SerializeField,
|
|
9241
9257
|
SerializeRelation,
|
|
9258
|
+
SerializerIdInterceptor,
|
|
9242
9259
|
SerializerInterceptor,
|
|
9243
9260
|
SerializerModule,
|
|
9244
9261
|
SerializerService
|