@iamnnort/nestjs-serializer 1.1.5 → 1.1.7

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/dist/index.mjs CHANGED
@@ -9026,7 +9026,7 @@ var import_operators = __toESM(require_operators());
9026
9026
  import { Injectable as Injectable2 } from "@nestjs/common";
9027
9027
 
9028
9028
  // src/service.ts
9029
- import { flattenDeep, isArray, isFunction, isNil, isPlainObject, merge, set, uniq } from "lodash";
9029
+ import { flattenDeep, isArray, isFunction, isNil, isPlainObject, merge, pick, set, uniq } from "lodash";
9030
9030
  import { Inject, Injectable } from "@nestjs/common";
9031
9031
 
9032
9032
  // src/module-definition.ts
@@ -9061,30 +9061,36 @@ var SerializerService = class {
9061
9061
  this.config = config;
9062
9062
  this.globalEntityNames = config.globalEntityNames || [];
9063
9063
  }
9064
- async transform(response, scopes) {
9065
- if (!scopes) {
9064
+ async transform(response, config) {
9065
+ if (!config.scopes && !config.fields) {
9066
9066
  return response;
9067
9067
  }
9068
9068
  if (!isNil(response.pagination) && !isNil(response.data)) {
9069
9069
  const transformedData = await Promise.all(response.data.map((entity) => {
9070
- return this.transformEntity(entity, scopes);
9070
+ return this.transformEntity(entity, config);
9071
9071
  }));
9072
9072
  response.data = transformedData;
9073
9073
  return response;
9074
9074
  }
9075
9075
  if (isArray(response)) {
9076
9076
  const transformedEntities = await Promise.all(response.map((entity) => {
9077
- return this.transformEntity(entity, scopes);
9077
+ return this.transformEntity(entity, config);
9078
9078
  }));
9079
9079
  return transformedEntities;
9080
9080
  }
9081
- const transformedEntity = await this.transformEntity(response, scopes);
9081
+ const transformedEntity = await this.transformEntity(response, config);
9082
9082
  return transformedEntity;
9083
9083
  }
9084
- async transformEntity(entity, scopes = []) {
9084
+ async transformEntity(entity, config) {
9085
9085
  if (!entity) {
9086
9086
  return entity;
9087
9087
  }
9088
+ if (!config.scopes && !config.fields) {
9089
+ return entity;
9090
+ }
9091
+ if (config.fields) {
9092
+ return pick(entity, config.fields);
9093
+ }
9088
9094
  const serializerFieldConfigs = global.serializerFieldConfigs.filter((fieldConfig) => {
9089
9095
  const isTarget = fieldConfig.target === entity.constructor;
9090
9096
  if (!isTarget) {
@@ -9094,7 +9100,10 @@ var SerializerService = class {
9094
9100
  }
9095
9101
  }
9096
9102
  const isScope = fieldConfig.scopes.some((scope) => {
9097
- return scopes.includes(scope);
9103
+ if (!config.scopes) {
9104
+ return false;
9105
+ }
9106
+ return config.scopes.includes(scope);
9098
9107
  });
9099
9108
  return isScope;
9100
9109
  });
@@ -9105,14 +9114,18 @@ var SerializerService = class {
9105
9114
  if (!isNil(fieldConfig.relationScopes)) {
9106
9115
  if (isArray(fieldValue)) {
9107
9116
  fieldValue = await Promise.all(fieldValue.map(async (relationEntity) => {
9108
- let relationEntityFieldValue = await this.transformEntity(relationEntity, fieldConfig.relationScopes);
9117
+ let relationEntityFieldValue = await this.transformEntity(relationEntity, {
9118
+ scopes: fieldConfig.relationScopes
9119
+ });
9109
9120
  if (isFunction(fieldConfig.fieldTransform)) {
9110
9121
  relationEntityFieldValue = await fieldConfig.fieldTransform(relationEntityFieldValue);
9111
9122
  }
9112
9123
  return relationEntityFieldValue;
9113
9124
  }));
9114
9125
  } else {
9115
- fieldValue = await this.transformEntity(fieldValue, fieldConfig.relationScopes);
9126
+ fieldValue = await this.transformEntity(fieldValue, {
9127
+ scopes: fieldConfig.relationScopes
9128
+ });
9116
9129
  if (isFunction(fieldConfig.fieldTransform)) {
9117
9130
  fieldValue = await fieldConfig.fieldTransform(fieldValue);
9118
9131
  }
@@ -9130,7 +9143,9 @@ var SerializerService = class {
9130
9143
  const transformedRelatedScope = {};
9131
9144
  await Promise.all(Object.keys(entity.relatedScope).map(async (relaitedEntityKey) => {
9132
9145
  const relaitedEntity = entity.relatedScope[relaitedEntityKey];
9133
- transformedRelatedScope[relaitedEntityKey] = await this.transformEntity(relaitedEntity, scopes);
9146
+ transformedRelatedScope[relaitedEntityKey] = await this.transformEntity(relaitedEntity, {
9147
+ scopes: config.scopes
9148
+ });
9134
9149
  }));
9135
9150
  transformedEntity.relatedScope = transformedRelatedScope;
9136
9151
  }
@@ -9164,7 +9179,6 @@ SerializerService = _ts_decorate([
9164
9179
  ], SerializerService);
9165
9180
 
9166
9181
  // src/interceptor.ts
9167
- import { pick } from "lodash";
9168
9182
  function _ts_decorate2(decorators, target, key, desc) {
9169
9183
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9170
9184
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -9189,13 +9203,13 @@ var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
9189
9203
  const scopes = this.getScopes(ctx);
9190
9204
  return next.handle().pipe((0, import_operators.map)(async (responsePromise) => {
9191
9205
  const response = await responsePromise;
9192
- if (config.fields) {
9193
- return pick(response, config.fields);
9206
+ if (!scopes && !config.fields) {
9207
+ return response;
9194
9208
  }
9195
- if (scopes) {
9196
- return this.serializerService.transform(response, scopes);
9197
- }
9198
- return response;
9209
+ return this.serializerService.transform(response, {
9210
+ scopes,
9211
+ fields: config.fields
9212
+ });
9199
9213
  }));
9200
9214
  }
9201
9215
  getScopes(ctx) {