A Laravel(ish) Resource/Transformer in javascript.

My basic attempt at javascript data transformation, resources.js

Will Bowman
Apr 1, 2018 · 2 min read
check out the npm package, resources.js

We‘ve gone serverless at work and we’re using my favorite, nodejs! That means a lot of javascript.

I’ve been using callbacks to transform my results but I’ve become accustomed to Fractal and now Laravels own Eloquent Resources. I like having a “view layer” for my responses, it makes things a lot easier to modify months later.

I don’t need all the features, just basic transformation, so the script is pretty slim.

Resources are defined nearly the same as Laravel.

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,

You even execute it nearly the same.

// Collections are static
const collection = UserResource.collection(data);
// Single items are not
const single = new UserResource(data[0]).exec();

Is it the same? Not at all, Laravel and Fractal actually do a bit more. I’m in the market for something similar but simpler, if you are too maybe this will be of use.

It would be nice to have more features, use a process method and the filter for even more options. We’ll see where it goes.


By default your results will be wrapped in a data key. You can disable this by passing the second parameter as false.

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

You can now pass a paginated object to collection, the data field is required for parsing. The rest of your fields will be merged with the results.

A full example

I’ve made this a package instead, check out the npmjs page for its readme.


I love writing javascript. It’s fun. I hope sharing will help provide some feed back so please let me know your opinions.


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

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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