@iamnnort/nestjs-serializer 1.1.3 → 1.1.6

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,31 +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 = []) {
9085
- console.log("1", entity);
9084
+ async transformEntity(entity, config) {
9086
9085
  if (!entity) {
9087
9086
  return entity;
9088
9087
  }
9088
+ if (!config.scopes && !config.fields) {
9089
+ return entity;
9090
+ }
9091
+ if (config.fields) {
9092
+ return pick(entity, config.fields);
9093
+ }
9089
9094
  const serializerFieldConfigs = global.serializerFieldConfigs.filter((fieldConfig) => {
9090
9095
  const isTarget = fieldConfig.target === entity.constructor;
9091
9096
  if (!isTarget) {
@@ -9095,11 +9100,13 @@ var SerializerService = class {
9095
9100
  }
9096
9101
  }
9097
9102
  const isScope = fieldConfig.scopes.some((scope) => {
9098
- return scopes.includes(scope);
9103
+ if (!config.scopes) {
9104
+ return false;
9105
+ }
9106
+ return config.scopes.includes(scope);
9099
9107
  });
9100
9108
  return isScope;
9101
9109
  });
9102
- console.log("2", serializerFieldConfigs);
9103
9110
  let transformedEntity = {};
9104
9111
  await Promise.all(serializerFieldConfigs.map(async (fieldConfig) => {
9105
9112
  const fieldName = fieldConfig.fieldName || fieldConfig.name;
@@ -9107,14 +9114,18 @@ var SerializerService = class {
9107
9114
  if (!isNil(fieldConfig.relationScopes)) {
9108
9115
  if (isArray(fieldValue)) {
9109
9116
  fieldValue = await Promise.all(fieldValue.map(async (relationEntity) => {
9110
- let relationEntityFieldValue = await this.transformEntity(relationEntity, fieldConfig.relationScopes);
9117
+ let relationEntityFieldValue = await this.transformEntity(relationEntity, {
9118
+ scopes: fieldConfig.relationScopes
9119
+ });
9111
9120
  if (isFunction(fieldConfig.fieldTransform)) {
9112
9121
  relationEntityFieldValue = await fieldConfig.fieldTransform(relationEntityFieldValue);
9113
9122
  }
9114
9123
  return relationEntityFieldValue;
9115
9124
  }));
9116
9125
  } else {
9117
- fieldValue = await this.transformEntity(fieldValue, fieldConfig.relationScopes);
9126
+ fieldValue = await this.transformEntity(fieldValue, {
9127
+ scopes: fieldConfig.relationScopes
9128
+ });
9118
9129
  if (isFunction(fieldConfig.fieldTransform)) {
9119
9130
  fieldValue = await fieldConfig.fieldTransform(fieldValue);
9120
9131
  }
@@ -9132,7 +9143,9 @@ var SerializerService = class {
9132
9143
  const transformedRelatedScope = {};
9133
9144
  await Promise.all(Object.keys(entity.relatedScope).map(async (relaitedEntityKey) => {
9134
9145
  const relaitedEntity = entity.relatedScope[relaitedEntityKey];
9135
- transformedRelatedScope[relaitedEntityKey] = await this.transformEntity(relaitedEntity, scopes);
9146
+ transformedRelatedScope[relaitedEntityKey] = await this.transformEntity(relaitedEntity, {
9147
+ scopes: config.scopes
9148
+ });
9136
9149
  }));
9137
9150
  transformedEntity.relatedScope = transformedRelatedScope;
9138
9151
  }
@@ -9166,7 +9179,7 @@ SerializerService = _ts_decorate([
9166
9179
  ], SerializerService);
9167
9180
 
9168
9181
  // src/interceptor.ts
9169
- import { pick } from "lodash";
9182
+ import { pick as pick2 } from "lodash";
9170
9183
  function _ts_decorate2(decorators, target, key, desc) {
9171
9184
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9172
9185
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -9192,10 +9205,13 @@ var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
9192
9205
  return next.handle().pipe((0, import_operators.map)(async (responsePromise) => {
9193
9206
  const response = await responsePromise;
9194
9207
  if (config.fields) {
9195
- return pick(response, config.fields);
9208
+ return pick2(response, config.fields);
9196
9209
  }
9197
9210
  if (scopes) {
9198
- return this.serializerService.transform(response, scopes);
9211
+ return this.serializerService.transform(response, {
9212
+ scopes,
9213
+ fields: config.fields
9214
+ });
9199
9215
  }
9200
9216
  return response;
9201
9217
  }));