-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
json api model adaptation to new usage
- Loading branch information
Showing
5 changed files
with
53 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import Model from "dovima"; | ||
import {Collection} from "dovima"; | ||
|
||
export default function JsonApiModelFormatter(model) { | ||
function convertModel(model) { | ||
if(model instanceof Model) { | ||
let attributes = model.attributes; | ||
let attributes = model.toJSON(); | ||
const id = attributes.id; | ||
delete attributes.id; //so it's just on the root | ||
return { | ||
type: model.constructor.name, | ||
id: model.id, | ||
id: id, | ||
attributes: attributes | ||
}; | ||
} else { | ||
throw new Error(`The object provided to be formatted as json is not a model.`); | ||
throw new Error(`The object provided to be formatted as json is not a dovima Model / Collection.`); | ||
} | ||
} | ||
|
||
export default function JsonApiModelFormatter(models) { | ||
if(Array.isArray(models) || (models instanceof Collection)) { | ||
return models.map(convertModel); | ||
} else { | ||
return convertModel(models); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters