asked.io
Published in

asked.io

A Laravel(ish) Resource/Transformer in javascript.

My basic attempt at javascript data transformation, resources.js

check out the npm package, resources.js
class UserResource extends Resource {
toArray() {
return {
id: Number(this.id),
name: this.name,
email: this.email,
posts: PostResource.collection(this.posts),
created_at: this.created_at,
updated_at: this.updated_at,
}
}
}
// Collections are static
const collection = UserResource.collection(data);
// Single items are not
const single = new UserResource(data[0]).exec();

Update:

new UserResource({...}, false).exec()
UserResource.collection(data, false)

A full example

Conclusion

--

--

A day-to-day log of the questions we’ve asked & the answers we’ve found.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store