Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
@data-client/endpoint
Advanced tools
Declarative, strongly typed, reusable network definitions for networking libraries.
export class Todo {
id = 0;
userId = 0;
title = '';
completed = false;
}
export const getTodo = (id: string) =>
fetch(`https://jsonplaceholder.typicode.com/todos/${id}`).then(res => res.json());
export const getTodoList = () =>
fetch('https://jsonplaceholder.typicode.com/todos').then(res => res.json());
export const updateTodo = (id: string, body: Partial<Todo>) =>
fetch(`https://jsonplaceholder.typicode.com/todos/${id}`, {
method: 'PUT',
body: JSON.stringify(body),
}).then(res => res.json());
import { EntityMixin, Endpoint } from '@data-client/endpoint';
import { Todo, getTodoList, updateTodo } from './existing';
export const TodoEntity = EntityMixin(Todo, { key: 'Todo' });
export const TodoResource = {
get: new Endpoint(getTodo, {
schema: TodoEntity,
}),
getList: new Endpoint(getTodoList, {
schema: [TodoEntity],
}),
update: new Endpoint(updateTodo, {
schema: TodoEntity,
sideEffect: true,
}),
};
import { useSuspense, useController } from '@data-client/react';
function TodoEdit() {
const todo = useSuspense(TodoResource.get, '5');
const ctrl = useController();
const updateTodo = (data) => ctrl.fetch(TodoResource.update, id, data);
return <TodoForm todo={todo} onSubmit={updateTodo} />
}
const todo = await TodoResource.get('5')
console.log(todo);
There is a distinction between
Thus, there are many benefits to creating a distinct seperation of concerns between these two concepts.
With TypeScript Standard Endpoints
, we define a standard for declaring in
TypeScript the definition of a networking API.
@data-client/endpoint
defines a standard interface
interface EndpointInterface {
(params?: any, body?: any): Promise<any>;
key(parmas?: any): string;
schema?: Readonly<S>;
sideEffects?: true;
// other optionals like 'optimistic'
}
as well as a helper class
to make construction easier.
class Endpoint<F extends () => Promise<any>> {
constructor(fetchFunction: F, options: EndpointOptions);
key(...args: Parameters<F>): string;
readonly sideEffect?: true;
readonly schema?: Schema;
fetch: F;
extend(options: EndpointOptions): Endpoint;
}
export interface EndpointOptions extends EndpointExtraOptions {
key?: (params: any) => string;
sideEffect?: true | undefined;
schema?: Schema;
}
Serializes the parameters. This is used to build a lookup key in global stores.
Default:
`${this.fetch.name} ${JSON.stringify(params)}`
Disallows usage in hooks like useSuspense()
since they might call fetch
an unpredictable number of times. Use this for APIs with mutation side-effects like update, create, deletes.
Defaults to undefined meaning no side effects.
Declarative definition of where Entities
appear in the fetch response.
Not providing this option means no entities will be extracted.
import { Entity } from '@data-client/normalizr';
import { Endpoint } from '@data-client/endpoint';
class User extends Entity {
id = '';
username = '';
}
const getUser = new Endpoint(
({ id }) ⇒ fetch(`/users/${id}`),
{ schema: User }
);
Can be used to further customize the endpoint definition
const UserDetail = new Endpoint(({ id }) ⇒ fetch(`/users/${id}`));
const UserDetailNormalized = UserDetail.extend({ schema: User });
Networking definition: Endpoints
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 | ✅ |
FAQs
Declarative Network Interface Definitions
The npm package @data-client/endpoint receives a total of 929 weekly downloads. As such, @data-client/endpoint popularity was classified as not popular.
We found that @data-client/endpoint demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.