Skip to content

Commit 6994edc

Browse files
committed
Refactor JsonSerializer (reduce repetition between buildCreatePayload and buildUpdatePayload)
1 parent 4bf0756 commit 6994edc

File tree

1 file changed

+22
-71
lines changed

1 file changed

+22
-71
lines changed

src/utils/JsonSerializer.ts

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,75 +32,19 @@ export class JsonApiSerializer {
3232
}
3333

3434
buildCreatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload {
35-
const ModelClass = this.modelMap[model.type];
36-
37-
if (!ModelClass) {
38-
console.warn(`No model class found for type: ${model.type}`);
39-
return this.buildDefaultPayload(model);
40-
}
41-
42-
const prototype = ModelClass.prototype;
43-
44-
if (typeof prototype.jsonApiMapping === 'function') {
45-
const mapping = prototype.jsonApiMapping.call(model);
46-
47-
const payload: JsonApiPayload = {
48-
data: {
49-
type: model.type,
50-
attributes: {},
51-
relationships: {},
52-
},
53-
};
54-
55-
prototype.constructor.relationships.forEach((relationship: {
56-
name: string;
57-
type: string;
58-
modelType: string;
59-
}) => {
60-
if (relationship.type === 'array') {
61-
const value = (model as any)[relationship.name];
62-
if (value) {
63-
payload.data.relationships![relationship.name] = {
64-
data: value.map((item: any) => ({
65-
type: relationship.modelType,
66-
id: item.id,
67-
})),
68-
};
69-
}
70-
} else {
71-
const value = (model as any)[relationship.name];
72-
if (value) {
73-
payload.data.relationships![relationship.name] = {
74-
data: {
75-
type: relationship.modelType,
76-
id: value.id ?? value,
77-
},
78-
};
79-
}
80-
}
81-
});
82-
83-
if (mapping.attributes) {
84-
mapping.attributes.forEach((attr: string) => {
85-
const value = (model as any)[attr];
86-
if (value !== undefined && value !== '') {
87-
payload.data.attributes[attr] = value;
88-
}
89-
});
90-
}
91-
92-
return payload;
93-
}
94-
95-
return this.buildDefaultPayload(model);
35+
return this.buildPayload(model, false);
9636
}
9737

9838
buildUpdatePayload(model: Model & Partial<JsonApiMapping>): JsonApiPayload {
39+
return this.buildPayload(model, true);
40+
}
41+
42+
private buildPayload(model: Model & Partial<JsonApiMapping>, isUpdate: boolean): JsonApiPayload {
9943
const ModelClass = this.modelMap[model.type];
10044

10145
if (!ModelClass) {
10246
console.warn(`No model class found for type: ${model.type}`);
103-
return this.buildDefaultPayload(model);
47+
return this.buildDefaultPayload(model, isUpdate);
10448
}
10549

10650
const prototype = ModelClass.prototype;
@@ -110,13 +54,16 @@ export class JsonApiSerializer {
11054

11155
const payload: JsonApiPayload = {
11256
data: {
113-
id: model.id,
11457
type: model.type,
11558
attributes: {},
11659
relationships: {},
11760
},
11861
};
11962

63+
if (isUpdate && model.id) {
64+
payload.data.id = model.id;
65+
}
66+
12067
prototype.constructor.relationships.forEach((relationship: {
12168
name: string;
12269
type: string;
@@ -138,7 +85,7 @@ export class JsonApiSerializer {
13885
payload.data.relationships![relationship.name] = {
13986
data: {
14087
type: relationship.modelType,
141-
id: value.id ?? value,
88+
id: typeof value === 'string' ? value : value.id,
14289
},
14390
};
14491
}
@@ -157,7 +104,7 @@ export class JsonApiSerializer {
157104
return payload;
158105
}
159106

160-
return this.buildDefaultPayload(model);
107+
return this.buildDefaultPayload(model, isUpdate);
161108
}
162109

163110
buildRelationshipPayload(model: Model, relationships: Array<Model>): JsonApiRelationshipsPayload {
@@ -174,20 +121,24 @@ export class JsonApiSerializer {
174121
type: model.type,
175122
id: relationship.id!,
176123
}));
177-
const payload: JsonApiRelationshipsPayload = {
178-
data: data,
179-
};
180124

181-
return payload;
125+
return { data };
182126
}
183127

184-
private buildDefaultPayload(model: Model): JsonApiPayload {
128+
private buildDefaultPayload(model: Model, includeId: boolean): JsonApiPayload {
185129
const { type, id, meta, links, included, _relationships, ...attributes } = model;
186-
return {
130+
131+
const payload: JsonApiPayload = {
187132
data: {
188133
type: model.type,
189134
attributes,
190135
},
191136
};
137+
138+
if (includeId && id) {
139+
payload.data.id = id;
140+
}
141+
142+
return payload;
192143
}
193144
}

0 commit comments

Comments
 (0)