TL;DR:
RestShape lets you query, reshape, and transform REST API responses declaratively โ no GraphQL server, no heavy setup.
You just feed your data and a query string, and it returns exactly the structure you want.
๐ญ The Problem
Modern APIs often deliver data thatโs too verbose or inconsistent for what the frontend actually needs.
So we write countless lines of glue code:
const result = {
name: res.user.firstName + ' ' + res.user.lastName,
manager: res.department?.manager?.name,
};
Multiply that by dozens of endpoints โ and you get cluttered transformation logic scattered across your app.
I wanted something as expressive as GraphQL, but as simple as JavaScript.
Thatโs why I built RestShape.
โก What Is RestShape?
RestShape is a lightweight JavaScript utility that lets you:
โ
Pick, transform, and reshape API responses declaratively
โ
Compute fields inline (e.g., fullName: firstName + " " + lastName)
โ
Filter, limit, and skip array results
โ
Conditionally include or skip fields with @include and @skip
โ
Apply transformations using @transform(fn: "...")
โ
Merge multiple data sources into one unified object
โ
Use fragments for reusable field definitions
All in plain JS โ no GraphQL server needed.
๐ฆ Installation
npm install rest-shape
# or
yarn add rest-shape
# or
pnpm add rest-shape
// ES Modules
import { shape } from "rest-shape";
// CommonJS
const { shape } = require("rest-shape");
๐งฑ Basic Example
const data = {
user: { firstName: "John", lastName: "Doe" },
department: {
name: "Engineering",
manager: { name: "Alex Johnson", email: "alex.johnson@example.com" },
},
};
const query = `
departmentName: department.name
manager {
name
email: department.manager.email
}
fullName: user.firstName + " " + user.lastName
`;
const result = shape(data, query);
console.log(result);
Output:
{
"departmentName": "Engineering",
"manager": {
"name": "Alex Johnson",
"email": "alex.johnson@example.com"
},
"fullName": "John Doe"
}
๐ง Why This Matters
Sometimes you donโt need GraphQL โ you just need GraphQL-like data shaping.
RestShape gives you:
Fine-grained control of REST responses
Reusable โquery stringsโ for consistent transformations
Fewer data-mapping utilities in your frontend and backend
Cleaner API adapters and response logic
Itโs perfect for BFFs, frontend normalization, or ETL transformations.
๐ Advanced Features
๐งฉ Combine Multiple Sources
const github = { user: { login: "octocat", followers: 1000 } };
const linkedin = { user: { connections: 500 } };
const data = {
user: { ...github.user, ...linkedin.user },
};
const query = `
user {
username: login
totalFollowers: followers + connections
}
`;
shape(data, query);
Output:
{ "user": { "username": "octocat", "totalFollowers": 1500 } }
๐ Conditional Directives
const data = { user: { isActive: false, name: "John" } };
const query = `
user {
name
email @include(if: "user.isActive")
}
`;
shape(data, query);
Output:
{ "user": { "name": "John", "email": null } }
๐ Array Filters and Limits
const data = {
posts: [
{ title: "Post 1", status: "published" },
{ title: "Post 2", status: "draft" },
{ title: "Post 3", status: "published" },
],
};
const query = `
posts(filter: "status === 'published'", limit: 1) {
title
}
`;
shape(data, query);
Output:
{ "posts": [{ "title": "Post 1" }] }
๐งฉ Fragments and Reusability
const data = {
user: {
department: {
manager: { name: "Alex", email: "alex@example.com" },
},
},
};
const fragments = {
managerFields: { name: "name", email: "email" },
};
const query = `
user {
department {
manager {
...managerFields
}
}
}
`;
shape(data, query, fragments);
๐งญ Why I Built It
Iโve always liked how GraphQL lets you shape responses cleanly, but I often didnโt need the server-side complexity.
So I asked:
โWhat if we could get the same expressive power directly in JavaScript?โ
Thatโs the goal of RestShape โ to bridge the gap between REST simplicity and GraphQL flexibility.
๐ Get Started
๐ฆ NPM: https://www.npmjs.com/package/rest-shape
๐ป GitHub: https://github.com/coxxanthony/rest-shape
If this idea resonates โ try it, star it, or share feedback!
Iโm improving it continuously and would love to hear how youโd use it.
โจ Final Thoughts
RestShape isnโt meant to replace GraphQL.
Itโs for developers who want GraphQL-like control over their REST data โ lightweight, composable, and JavaScript-native.
Shape your data, not your stack.
RestShape โ because REST deserves structure too.
Top comments (0)