
Security News
Socket Releases Free Certified Patches for Critical vm2 Sandbox Escape
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.
@data-client/rest
Advanced tools
๐Read The Docs ย |ย ๐คAgent Skills ย |ย ๐ฎTodo Demo ย |ย ๐ฎGithub Demo
Simplify TypeScript fetch functions with RestEndpoint
const getTodo = new RestEndpoint({
urlPrefix: 'https://jsonplaceholder.typicode.com',
path: '/todos/:id',
});
RestEndpoint infers path-to-regexp argument types, enabling enforcement of function calls
// signature requires id!
const todo = await getTodo({ id: 5 });
It automatically handles REST concepts like JSON serialization, consolidated error handling and more.
Simplify related CRUD endpoints with Resources
Resources are a collection of methods for a given data model.
Entities and Schemas declaratively define the data model. RestEndpoints are the methods on that data.
class Todo extends Entity {
id = 0;
userId = 0;
title = '';
completed = false;
}
const TodoResource = resource({
urlPrefix: 'https://jsonplaceholder.typicode.com',
path: '/todos/:id',
searchParams: {} as { userId?: string | number },
schema: Todo,
paginationField: 'page',
});
One Resource defines seven endpoints:
// GET https://jsonplaceholder.typicode.com/todos/5
let todo5 = await TodoResource.get({ id: 5 });
// GET https://jsonplaceholder.typicode.com/todos
const todoList = await TodoResource.getList();
// GET https://jsonplaceholder.typicode.com/todos?userId=1
const userOneTodos = await TodoResource.getList({ userId: 1 });
// POST https://jsonplaceholder.typicode.com/todos
const newTodo = await TodoResource.getList.push({ title: 'my todo' });
// POST https://jsonplaceholder.typicode.com/todos?userId=1
const newUserOneTodo = await TodoResource.getList.push(
{ userId: 1 },
{ title: 'my todo' },
);
// GET https://jsonplaceholder.typicode.com/todos?userId=1&page=2
const nextPageOfTodos = await TodoResource.getList.getPage({
userId: 1,
page: 2,
});
// PUT https://jsonplaceholder.typicode.com/todos/5
todo5 = await TodoResource.update({ id: 5 }, { title: 'my todo' });
// PATCH https://jsonplaceholder.typicode.com/todos/5
todo5 = await TodoResource.partialUpdate({ id: 5 }, { title: 'my todo' });
// DELETE https://jsonplaceholder.typicode.com/todos/5
await TodoResource.delete({ id: 5 });
No need for any custom hooks. All endpoints are 100% compatible with Reactive Data Client
const todo = useSuspense(TodoResource.get, { id: 5 });
const todoList = useSuspense(TodoResource.getList);
const ctrl = useController();
const updateTodo = data => ctrl.fetch(TodoResource.update, { id }, data);
const partialUpdateTodo = data =>
ctrl.fetch(TodoResource.partialUpdate, { id }, data);
const addTodoToEnd = data => ctrl.fetch(TodoResource.getList.push, data);
const addTodoToBeginning = data =>
ctrl.fetch(TodoResource.getList.unshift, data);
const deleteTodo = data => ctrl.fetch(TodoResource.delete, { id });
const queryRemainingTodos = new Query(
TodoResource.getList.schema,
entries => entries.filter(todo => !todo.completed).length,
);
const allRemainingTodos = useQuery(queryRemainingTodos);
const firstUserRemainingTodos = useQuery(queryRemainingTodos, { userId: 1 });
const groupTodoByUser = new Query(
TodoResource.getList.schema,
todos => Object.groupBy(todos, todo => todo.userId),
);
const todosByUser = useQuery(groupTodoByUser);
TypeScript is optional, but will only work with 4.0 or above. 4.1 is needed for stronger types as it supports inferring argument types from the path templates.
| Data Type | Mutable | Schema | Description | Queryable |
|---|---|---|---|---|
| Object | โ | Entity, EntityMixin | single unique object | โ |
| โ | Union(Entity) | polymorphic objects (A | B) | โ | |
| ๐ | Object | statically known keys | ๐ | |
| Invalidate(Entity) | delete an entity | ๐ | ||
| List | โ | Collection(Array) | growable lists | โ |
| ๐ | Array | immutable lists | ๐ | |
| All | list of all entities of a kind | โ | ||
| Map | โ | Collection(Values) | growable maps | โ |
| ๐ | Values | immutable maps | ๐ | |
| any | Query(Queryable) | memoized custom transforms | โ | |
| Lazy(Schema) | deferred denormalization | โ |
FAQs
Quickly define typed REST resources and endpoints
The npm package @data-client/rest receives a total of 1,438 weekly downloads. As such, @data-client/rest popularity was classified as popular.
We found that @data-client/rest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.ย It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A critical vm2 sandbox escape can allow untrusted JavaScript to break isolation and execute commands on the host Node.js process.

Research
Five malicious NuGet packages impersonate Chinese .NET libraries to deploy a stealer targeting browser credentials, crypto wallets, SSH keys, and local files.

Security News
pnpm 11 turns on a 1-day Minimum Release Age and blocks exotic subdeps by default, adding safeguards against fast-moving supply chain attacks.