# @rest-hooks/rest

> Endpoints for REST APIs

Latest version **7.4.4** (published 2023-08-26) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @rest-hooks/rest
pnpm add @rest-hooks/rest
yarn add @rest-hooks/rest
bun add @rest-hooks/rest
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 7.4.4 |
| Published | 2023-08-26 |
| First published | 2020-07-26 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^12.17 \|\| ^13.7 \|\| >=14 |
| Dependencies | 1 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2035 |
| Author | Nathaniel Tucker |
| Maintainers | ntucker, ljharb |
| Keywords | REST, CRUD, networking, fetch, data, cache, model, resource, typescript, path-to-regex, path template, data fetching, data cache, api, normalized cache, subject pattern, endpoint, react-native, ios, android, web, json |

## Links

- npm: https://www.npmjs.com/package/@rest-hooks/rest
- Repository: https://github.com/data-client/rest-hooks
- Homepage: https://resthooks.io/rest
- Issues: https://github.com/data-client/rest-hooks/issues
- npm.io page: https://npm.io/package/@rest-hooks/rest

## Dependencies (1)

- [@data-client/rest](https://npm.io/package/@data-client/rest.md) ^0.7.6

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 7.4.4 (latest) — 2023-08-26
- 6.6.0-next.0 (next) — 2023-04-26
- 5.2.0-beta.2 (beta) — 2022-09-19
- 7.4.3 — 2023-08-19
- 7.4.2 — 2023-08-17
- 7.4.1 — 2023-08-13
- 7.4.0 — 2023-08-12
- 7.3.0 — 2023-08-11
- 7.2.0 — 2023-08-09
- 7.1.2 — 2023-08-07
- 7.1.1 — 2023-07-31
- 7.1.0 — 2023-07-25
- 7.0.0 — 2023-07-04
- 6.7.2 — 2023-07-02
- 6.7.1 — 2023-06-18
- … 87 more at https://npm.io/package/@rest-hooks/rest/versions

## README

# Rest Hooks for REST

[![CircleCI](https://circleci.com/gh/data-client/rest-hooks/tree/master.svg?style=shield)](https://circleci.com/gh/data-client/rest-hooks)
[![Coverage Status](https://img.shields.io/codecov/c/gh/data-client/rest-hooks/master.svg?style=flat-square)](https://app.codecov.io/gh/data-client/rest-hooks?branch=master)
[![npm downloads](https://img.shields.io/npm/dm/@rest-hooks/rest.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/rest)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@rest-hooks/rest?style=flat-square)](https://bundlephobia.com/result?p=@rest-hooks/rest)
[![npm version](https://img.shields.io/npm/v/@rest-hooks/rest.svg?style=flat-square)](https://www.npmjs.com/package/@rest-hooks/rest)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![Chat](https://img.shields.io/discord/768254430381735967.svg?style=flat-square&colorB=758ED3)](https://discord.gg/35nb8Mz)

Extensible CRUD patterns for REST APIs.

<div align="center">

**[📖Read The Docs](https://resthooks.io/rest)** &nbsp;|&nbsp; [🎮Github Demo](https://stackblitz.com/github/data-client/rest-hooks/tree/rest-hooks-site/examples/github-app?file=src%2Fresources%2FIssue.tsx)

</div>

### Simple TypeScript definition

```typescript
import { Entity, createResource } from '@rest-hooks/rest';

class Article extends Entity {
  id: number | undefined = undefined;
  title = '';
  body = '';

  pk() {
    return this.id;
  }
}
const ArticleResource = createResource({
  path: '/articles/:id',
  schema: Article,
});
```

[Entity](https://resthooks.io/rest/api/Entity) defines a data model.
[createResource](https://resthooks.io/rest/api/createResource) creates a [collection](https://resthooks.io/rest/api/createResource#members)
of six [RestEndpoints](https://resthooks.io/rest/api/RestEndpoint)

[RestEndpoints](https://resthooks.io/rest/api/RestEndpoint) are functions (and more) that return a Promise.
Both call parameters and return value are [automatically inferred](https://resthooks.io/rest/api/RestEndpoint#typing) from
the options used to construct them.

`path` is a templating language using [path-to-regex compile](https://github.com/pillarjs/path-to-regexp#compile-reverse-path-to-regexp).

### [Standard CRUD Endpoints](https://resthooks.io/rest/api/createResource#members)

#### Reads

```typescript
const article = useSuspense(ArticleResource.get, { id: 5 });
const articles = useSuspense(ArticleResource.getList);
```

```typescript
const [article, setArticle] = useState();
useEffect(() => {
  setArticle(await ArticleResource.get({ id: 5 }));
}, []);
```

#### Mutates

```typescript
const ctrl = useController();
const updateArticle = data => ctrl.fetch(ArticleResource.update, { id }, data);
const partialUpdateArticle = data =>
  ctrl.fetch(ArticleResource.partialUpdate, { id }, data);
const createArticle = data => ctrl.fetch(ArticleResource.create, data);
const deleteArticle = data => ctrl.fetch(ArticleResource.delete, { id });
```

### Use with Node

```typescript
const article = await ArticleResource.get({ id: 5 });
const articles = await ArticleResource.getList();
```

### [Programmatic queries](https://resthooks.io/rest/api/Query)

```tsx
const sortedArticles = new Query(
  new schema.All(Article),
  (entries, { asc } = { asc: false }) => {
    const sorted = [...entries].sort((a, b) => a.title.localeCompare(b.title));
    if (asc) return sorted;
    return sorted.reverse();
  },
);

const articlesUnsorted = useCache(sortedArticles);
const articlesAscending = useCache(sortedArticles, { asc: true });
const articlesDescending = useCache(sortedArticles, { asc: false });
```

### TypeScript requirements

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.

Version 5.x can be used for older TypeScript versions.

### Prior Art

- [Backbone Model](https://backbonejs.org/#Model)
- [ImmutableJS Record](https://immutable-js.github.io/immutable-js/docs/#/Record)

---
_Source: https://npm.io/package/@rest-hooks/rest · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
