Define Async Methods (Endpoints)
Endpoints are the methods of your data. Entities are a type of Schema, which define the data model. Resources are
a collection of endpoints
around one schema
.
- Rest
- GraphQL
- Promise
npm install --save @rest-hooks/rest
▶api/Todo
import { createResource, Entity } from '@rest-hooks/rest';export class Todo extends Entity {id = 0;userId = 0;title = '';completed = false;pk() {return `${this.id}`;}static key = 'Todo';}export const TodoResource = createResource({urlPrefix: 'https://jsonplaceholder.typicode.com',path: '/todos/:id',schema: Todo,});
▶Methods
import { TodoResource } from './api/Todo';// GET https://jsonplaceholder.typicode.com/todos/5TodoResource.get({ id: 5 });// GET https://jsonplaceholder.typicode.com/todosTodoResource.getList();// POST https://jsonplaceholder.typicode.com/todosTodoResource.create({ title: 'my todo' });// PUT https://jsonplaceholder.typicode.com/todos/5TodoResource.update({ id: 5 }, { title: 'my todo' });// PATCH https://jsonplaceholder.typicode.com/todos/5TodoResource.partialUpdate({ id: 5 }, { title: 'my todo' });// DELETE https://jsonplaceholder.typicode.com/todos/5TodoResource.delete({ id: 5 });
npm install --save @rest-hooks/graphql
api/Todo
import { GQLEndpoint, GQLEntity } from '@rest-hooks/graphql';const gql = new GQLEndpoint('/');export class Todo extends GQLEntity {title = '';completed = false;static key = 'Todo';}export const TodoResource = {getList: gql.query(`query GetTodos {todo {idtitlecompleted}}`,{ todos: [Todo] },),update: gql.mutation(`mutation UpdateTodo($todo: Todo!) {updateTodo(todo: $todo) {idtitlecompleted}}`,{ updateTodo: Todo },),};
npm install --save @rest-hooks/endpoint
▶existing/Todo
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());
▶api/Todo
import { schema, Endpoint } from '@rest-hooks/endpoint';import { Todo, getTodo, getTodoList, updateTodo } from '../existing/Todo';export const TodoEntity = schema.Entity(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,}),};
It's highly encouraged to design APIs with consistent patterns. Because of this, you can extend our protocol specific helpers. After choosing your protocol, you can read up on the full docs for reach protocol REST, GraphQL, Image/binary, Websockets+SSE
To use your own protocol or existing helpers, use the lower-level primitives from
@rest-hooks/endpoint like Endpoint and schema.Entity.
[See Promise
tab above]