@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.d.mts CHANGED
@@ -31,8 +31,14 @@ declare class SerializerService {
31
31
  config: SerializerConfig;
32
32
  globalEntityNames: string[];
33
33
  constructor(config: SerializerConfig);
34
- transform(response: any, scopes?: string[]): Promise<any>;
35
- transformEntity(entity: any, scopes?: string[]): Promise<any>;
34
+ transform(response: any, config: {
35
+ scopes?: string[];
36
+ fields?: string[];
37
+ }): Promise<any>;
38
+ transformEntity(entity: any, config: {
39
+ scopes?: string[];
40
+ fields?: string[];
41
+ }): Promise<any>;
36
42
  getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
37
43
  makeRelations(relationField: string, relationRelations: string[]): string[];
38
44
  }
package/dist/index.d.ts CHANGED
@@ -31,8 +31,14 @@ declare class SerializerService {
31
31
  config: SerializerConfig;
32
32
  globalEntityNames: string[];
33
33
  constructor(config: SerializerConfig);
34
- transform(response: any, scopes?: string[]): Promise<any>;
35
- transformEntity(entity: any, scopes?: string[]): Promise<any>;
34
+ transform(response: any, config: {
35
+ scopes?: string[];
36
+ fields?: string[];
37
+ }): Promise<any>;
38
+ transformEntity(entity: any, config: {
39
+ scopes?: string[];
40
+ fields?: string[];
41
+ }): Promise<any>;
36
42
  getRelations(scopes: string[], relationMap: Record<string, string[]>): string[];
37
43
  makeRelations(relationField: string, relationRelations: string[]): string[];
38
44
  }
package/dist/index.js CHANGED
@@ -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 (!_lodash.isNil.call(void 0, response.pagination) && !_lodash.isNil.call(void 0, 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 (_lodash.isArray.call(void 0, 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 _lodash.pick.call(void 0, 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 (!_lodash.isNil.call(void 0, fieldConfig.relationScopes)) {
9108
9115
  if (_lodash.isArray.call(void 0, 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 (_lodash.isFunction.call(void 0, 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 (_lodash.isFunction.call(void 0, 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
  }
@@ -9195,7 +9208,10 @@ var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
9195
9208
  return _lodash.pick.call(void 0, 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
  }));