@iamnnort/nestjs-serializer 1.1.5 → 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 +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +29 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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,
|
|
35
|
-
|
|
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,
|
|
35
|
-
|
|
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,30 +9061,36 @@ var SerializerService = class {
|
|
|
9061
9061
|
this.config = config;
|
|
9062
9062
|
this.globalEntityNames = config.globalEntityNames || [];
|
|
9063
9063
|
}
|
|
9064
|
-
async transform(response,
|
|
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,
|
|
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,
|
|
9077
|
+
return this.transformEntity(entity, config);
|
|
9078
9078
|
}));
|
|
9079
9079
|
return transformedEntities;
|
|
9080
9080
|
}
|
|
9081
|
-
const transformedEntity = await this.transformEntity(response,
|
|
9081
|
+
const transformedEntity = await this.transformEntity(response, config);
|
|
9082
9082
|
return transformedEntity;
|
|
9083
9083
|
}
|
|
9084
|
-
async transformEntity(entity,
|
|
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 _lodash.pick.call(void 0, 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
|
-
|
|
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 (!_lodash.isNil.call(void 0, fieldConfig.relationScopes)) {
|
|
9106
9115
|
if (_lodash.isArray.call(void 0, fieldValue)) {
|
|
9107
9116
|
fieldValue = await Promise.all(fieldValue.map(async (relationEntity) => {
|
|
9108
|
-
let relationEntityFieldValue = await this.transformEntity(relationEntity,
|
|
9117
|
+
let relationEntityFieldValue = await this.transformEntity(relationEntity, {
|
|
9118
|
+
scopes: fieldConfig.relationScopes
|
|
9119
|
+
});
|
|
9109
9120
|
if (_lodash.isFunction.call(void 0, 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,
|
|
9126
|
+
fieldValue = await this.transformEntity(fieldValue, {
|
|
9127
|
+
scopes: fieldConfig.relationScopes
|
|
9128
|
+
});
|
|
9116
9129
|
if (_lodash.isFunction.call(void 0, 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,
|
|
9146
|
+
transformedRelatedScope[relaitedEntityKey] = await this.transformEntity(relaitedEntity, {
|
|
9147
|
+
scopes: config.scopes
|
|
9148
|
+
});
|
|
9134
9149
|
}));
|
|
9135
9150
|
transformedEntity.relatedScope = transformedRelatedScope;
|
|
9136
9151
|
}
|
|
@@ -9193,7 +9208,10 @@ var SerializerInterceptor = /* @__PURE__ */ __name((config) => {
|
|
|
9193
9208
|
return _lodash.pick.call(void 0, response, config.fields);
|
|
9194
9209
|
}
|
|
9195
9210
|
if (scopes) {
|
|
9196
|
-
return this.serializerService.transform(response,
|
|
9211
|
+
return this.serializerService.transform(response, {
|
|
9212
|
+
scopes,
|
|
9213
|
+
fields: config.fields
|
|
9214
|
+
});
|
|
9197
9215
|
}
|
|
9198
9216
|
return response;
|
|
9199
9217
|
}));
|