@iamnnort/nestjs-serializer 2.0.1 → 2.1.0

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.d.mts CHANGED
@@ -26,11 +26,11 @@ type SerializerFieldConfig = {
26
26
  };
27
27
  type SerializerConfig = {
28
28
  globalEntityNames?: string[];
29
+ globalLimitedScopes?: string[];
29
30
  };
30
31
 
31
32
  declare class SerializerService {
32
33
  config: SerializerConfig;
33
- globalEntityNames: string[];
34
34
  constructor(config: SerializerConfig);
35
35
  transform(response: any, config: {
36
36
  scopes?: string[];
package/dist/index.d.ts CHANGED
@@ -26,11 +26,11 @@ type SerializerFieldConfig = {
26
26
  };
27
27
  type SerializerConfig = {
28
28
  globalEntityNames?: string[];
29
+ globalLimitedScopes?: string[];
29
30
  };
30
31
 
31
32
  declare class SerializerService {
32
33
  config: SerializerConfig;
33
- globalEntityNames: string[];
34
34
  constructor(config: SerializerConfig);
35
35
  transform(response: any, config: {
36
36
  scopes?: string[];
package/dist/index.js CHANGED
@@ -9054,10 +9054,8 @@ var SerializerService = class {
9054
9054
  __name(this, "SerializerService");
9055
9055
  }
9056
9056
 
9057
-
9058
9057
  constructor(config) {
9059
9058
  this.config = config;
9060
- this.globalEntityNames = config.globalEntityNames || [];
9061
9059
  }
9062
9060
  async transform(response, config) {
9063
9061
  if (!response) {
@@ -9108,7 +9106,7 @@ var SerializerService = class {
9108
9106
  const serializerFieldConfigs = global.serializerFieldConfigs.filter((fieldConfig) => {
9109
9107
  const isTarget = fieldConfig.target === entity.constructor;
9110
9108
  if (!isTarget) {
9111
- const isGlobalTarget = this.globalEntityNames.includes(fieldConfig.target.name);
9109
+ const isGlobalTarget = (this.config.globalEntityNames || []).includes(fieldConfig.target.name);
9112
9110
  if (!isGlobalTarget) {
9113
9111
  return false;
9114
9112
  }
@@ -9136,6 +9134,25 @@ var SerializerService = class {
9136
9134
  ...transformedEntity
9137
9135
  }, fieldName, fieldValue), transformedEntity);
9138
9136
  }));
9137
+ const fieldCountMap = serializerFieldConfigs.reduce((fieldCountMap2, fieldConfig) => {
9138
+ const fieldName = fieldConfig.fieldName || fieldConfig.name;
9139
+ const rootFieldName = fieldName.split(".")[0];
9140
+ return {
9141
+ ...fieldCountMap2,
9142
+ [rootFieldName]: _lodash.isNil.call(void 0, fieldCountMap2[rootFieldName]) ? 1 : fieldCountMap2[rootFieldName] + 1
9143
+ };
9144
+ }, {});
9145
+ Object.entries(transformedEntity).forEach(([rootFieldName, rootFieldValue]) => {
9146
+ if (!_lodash.isPlainObject.call(void 0, rootFieldValue)) {
9147
+ return;
9148
+ }
9149
+ const rootFieldCount = fieldCountMap[rootFieldName];
9150
+ const rootFieldChildrenCount = Object.keys(rootFieldValue).length;
9151
+ if (rootFieldCount < rootFieldChildrenCount) {
9152
+ return;
9153
+ }
9154
+ transformedEntity[rootFieldName] = void 0;
9155
+ });
9139
9156
  if (_lodash.isPlainObject.call(void 0, entity.relatedScope)) {
9140
9157
  const transformedRelatedScope = {};
9141
9158
  await Promise.all(Object.keys(entity.relatedScope).map(async (relaitedEntityKey) => {
@@ -9215,7 +9232,7 @@ var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
9215
9232
  return config.extendedScopes || config.scopes;
9216
9233
  }
9217
9234
  if (request.query.limited) {
9218
- return config.limitedScopes || config.scopes;
9235
+ return config.limitedScopes || this.serializerService.config.globalLimitedScopes || config.scopes;
9219
9236
  }
9220
9237
  return config.scopes;
9221
9238
  }