Skip to content

Commit c36b55c

Browse files
API Base angular 2: ModelBase parse method fixed[g log
1 parent dc36679 commit c36b55c

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ApiBase/model-base.model.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,36 @@ export abstract class ModelBase {
4444
if (!params) {
4545
return;
4646
}
47-
47+
if (typeof params?.json === 'function') {
48+
params = params.json();
49+
}
4850
for (const paramKey in params) {
4951
if (!params[paramKey]) {
5052
continue;
5153
}
5254
const paramValue = params[paramKey];
5355
if (this.PARAMS_MAPPER[paramKey]) {
54-
this[paramKey] = this.parseParam(paramKey, paramValue);
56+
// Parser will be executed by the setter
57+
this[paramKey] = paramValue;
5558
}
5659
}
5760
}
5861

5962
parseParam(key: string, value: any): any {
6063
const mapper = this.PARAMS_MAPPER[key];
61-
if (
62-
mapper &&
63-
typeof mapper?.type === 'function' &&
64-
typeof value !== 'function' &&
65-
value?.__proto__?.constructor?.name !== 'Array'
66-
) {
64+
if (!mapper) {
65+
return value;
66+
}
67+
if (value?.__proto__?.constructor?.name === 'Array') {
68+
if (value[0]?.constructor.name !== mapper.type?.name) {
69+
return recursiveInstance(mapper.type, value);
70+
}
71+
return value;
72+
}
73+
if (typeof mapper?.type === 'function' && typeof value !== 'function') {
6774
return recursiveInstance(mapper.type, value);
6875
}
76+
6977
return value;
7078
}
7179

0 commit comments

Comments
 (0)