Skip to content

Commit 0eaee1f

Browse files
committed
Better write path for BaseService create method
Uses `getApiMapping` function on model to determine the attributes and relationships to use for the payload
1 parent 98b411b commit 0eaee1f

File tree

13 files changed

+188
-10
lines changed

13 files changed

+188
-10
lines changed

dist/index.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,44 @@ class BaseService extends RequestBuilder {
285285
data: hydratedData
286286
};
287287
}
288-
async create(payload) {
288+
async create(model) {
289+
let modelType = model.type;
290+
let ModelClass = this.modelRegistry.models[modelType];
291+
let payload;
292+
if ("getApiMapping" in ModelClass.prototype) {
293+
const mapping = ModelClass.prototype.getApiMapping();
294+
payload = {
295+
data: {
296+
type: model.type,
297+
attributes: {}
298+
}
299+
};
300+
mapping.attributes.forEach((attr) => {
301+
payload.data.attributes[attr] = model[attr];
302+
});
303+
if (mapping.relationships) {
304+
payload.data.relationships = {};
305+
Object.entries(mapping.relationships).forEach(([key, type]) => {
306+
const relationshipValue = model[key];
307+
if (relationshipValue) {
308+
payload.data.relationships[key] = {
309+
data: {
310+
type,
311+
id: relationshipValue
312+
}
313+
};
314+
}
315+
});
316+
}
317+
} else {
318+
let { type, id, ...rest } = model;
319+
payload = {
320+
data: {
321+
type: model.type,
322+
attributes: rest
323+
}
324+
};
325+
}
289326
return await this.client.makePostRequest(this.endpoint, payload);
290327
}
291328
}
@@ -314,6 +351,7 @@ class PermissionsService extends BaseService {
314351
// src/models/BaseModel.ts
315352
class BaseModel {
316353
id = "";
354+
type = "";
317355
meta;
318356
links;
319357
included;
@@ -556,6 +594,7 @@ class VehicleSpecification extends BaseModel {
556594
transmission = "";
557595
year = 0;
558596
documentation = [];
597+
model;
559598
static relationships = [
560599
{
561600
name: "model",
@@ -571,6 +610,7 @@ class VehicleSpecification extends BaseModel {
571610
this.transmission = data?.attributes?.transmission ?? "";
572611
this.year = data?.attributes?.year ?? 0;
573612
this.documentation = data?.attributes?.documentation ?? [];
613+
this.model = new VehicleModel;
574614
}
575615
static hydrate(data) {
576616
return new VehicleSpecification(data);
@@ -1029,7 +1069,15 @@ class Vehicle extends BaseModel {
10291069
vin = "";
10301070
description = "";
10311071
colour = "";
1032-
specification;
1072+
getApiMapping() {
1073+
return {
1074+
attributes: ["registration", "vin", "description", "colour"],
1075+
relationships: {
1076+
specification: "vehicle-specifications"
1077+
}
1078+
};
1079+
}
1080+
specification = "";
10331081
static relationships = [
10341082
{
10351083
name: "specification",
@@ -1043,6 +1091,7 @@ class Vehicle extends BaseModel {
10431091
this.vin = data?.attributes?.vin ?? "";
10441092
this.description = data?.attributes?.description ?? "";
10451093
this.colour = data?.attributes?.colour ?? "";
1094+
this.specification = "";
10461095
}
10471096
static hydrate(data) {
10481097
return new Vehicle(data);

dist/models/BaseModel.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Model } from '../types/Model';
22
import type { RelationshipDefinition } from '../types/RelationshipDefinition';
33
export declare abstract class BaseModel implements Model {
44
id: string;
5-
abstract type: string;
5+
type: string;
66
meta?: Record<string, any>;
77
links?: Record<string, any>;
88
included?: Record<string, any>;

dist/models/BaseModel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export class BaseModel {
22
id = '';
3+
type = '';
34
meta;
45
links;
56
included;

dist/models/Vehicle.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ export declare class Vehicle extends BaseModel {
77
vin: string;
88
description: string;
99
colour: string;
10-
specification?: VehicleSpecification;
10+
getApiMapping(): {
11+
attributes: string[];
12+
relationships: {
13+
specification: string;
14+
};
15+
};
16+
specification?: VehicleSpecification | string;
1117
static relationships: RelationshipDefinition[];
1218
constructor(data?: any);
1319
static hydrate(data: any): Vehicle;

dist/models/Vehicle.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ export class Vehicle extends BaseModel {
88
vin = '';
99
description = '';
1010
colour = '';
11-
specification;
11+
getApiMapping() {
12+
return {
13+
attributes: ['registration', 'vin', 'description', 'colour'],
14+
relationships: {
15+
specification: 'vehicle-specifications'
16+
}
17+
};
18+
}
19+
specification = '';
1220
static relationships = [
1321
{
1422
name: 'specification',
@@ -22,6 +30,7 @@ export class Vehicle extends BaseModel {
2230
this.vin = data?.attributes?.vin ?? '';
2331
this.description = data?.attributes?.description ?? '';
2432
this.colour = data?.attributes?.colour ?? '';
33+
this.specification = '';
2534
}
2635
static hydrate(data) {
2736
return new Vehicle(data);

dist/models/VehicleSpecification.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RelationshipDefinition } from '../types/RelationshipDefinition';
22
import { BaseModel } from '@models/BaseModel';
3+
import { VehicleModel } from '@models/VehicleModel';
34
type VehicleModelDocumentation = {
45
name: string;
56
description: string;
@@ -13,6 +14,7 @@ export declare class VehicleSpecification extends BaseModel {
1314
transmission: string;
1415
year: number;
1516
documentation: VehicleModelDocumentation[];
17+
model: VehicleModel;
1618
static relationships: RelationshipDefinition[];
1719
constructor(data?: any);
1820
static hydrate(data: any): VehicleSpecification;

dist/models/VehicleSpecification.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RegisterModel } from '../utils/ModelRegistry';
22
import { BaseModel } from '@models/BaseModel';
3+
import { VehicleModel } from '@models/VehicleModel';
34
@RegisterModel
45
export class VehicleSpecification extends BaseModel {
56
type = 'vehicle-specifications';
@@ -9,6 +10,7 @@ export class VehicleSpecification extends BaseModel {
910
transmission = '';
1011
year = 0;
1112
documentation = [];
13+
model;
1214
static relationships = [
1315
{
1416
name: 'model',
@@ -24,6 +26,7 @@ export class VehicleSpecification extends BaseModel {
2426
this.transmission = data?.attributes?.transmission ?? '';
2527
this.year = data?.attributes?.year ?? 0;
2628
this.documentation = data?.attributes?.documentation ?? [];
29+
this.model = new VehicleModel();
2730
}
2831
static hydrate(data) {
2932
return new VehicleSpecification(data);

dist/services/BaseService.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export declare class BaseService<T extends Model> extends RequestBuilder {
1515
get(param: string): Promise<InternalResponse<T>>;
1616
get(param: string, options?: RequestOptionsType): Promise<InternalResponse<T>>;
1717
get(param: RequestOptionsType): Promise<InternalResponse<T[]>>;
18-
create(payload: any): Promise<InternalResponse<T>>;
18+
create(model: Model): Promise<InternalResponse<T>>;
1919
}

dist/services/BaseService.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,45 @@ export class BaseService extends RequestBuilder {
2323
data: hydratedData
2424
};
2525
}
26-
async create(payload) {
26+
async create(model) {
27+
let modelType = model.type;
28+
let ModelClass = this.modelRegistry.models[modelType];
29+
let payload;
30+
if ('getApiMapping' in ModelClass.prototype) {
31+
const mapping = ModelClass.prototype.getApiMapping();
32+
payload = {
33+
data: {
34+
type: model.type,
35+
attributes: {}
36+
}
37+
};
38+
mapping.attributes.forEach((attr) => {
39+
payload.data.attributes[attr] = model[attr];
40+
});
41+
if (mapping.relationships) {
42+
payload.data.relationships = {};
43+
Object.entries(mapping.relationships).forEach(([key, type]) => {
44+
const relationshipValue = model[key];
45+
if (relationshipValue) {
46+
payload.data.relationships[key] = {
47+
data: {
48+
type,
49+
id: relationshipValue
50+
}
51+
};
52+
}
53+
});
54+
}
55+
}
56+
else {
57+
let { type, id, ...rest } = model;
58+
payload = {
59+
data: {
60+
type: model.type,
61+
attributes: rest
62+
}
63+
};
64+
}
2765
return await this.client.makePostRequest(this.endpoint, payload);
2866
}
2967
}

src/models/BaseModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { RelationshipDefinition } from '../types/RelationshipDefinition';
33

44
export abstract class BaseModel implements Model {
55
public id: string = '';
6-
public abstract type: string;
6+
public type: string = '';
77
public meta?: Record<string, any>;
88
public links?: Record<string, any>;
99
public included?: Record<string, any>;

0 commit comments

Comments
 (0)