@@ -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
315352class 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 ) ;
0 commit comments