Socket
Socket
Sign inDemoInstall

@data-client/rest

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@data-client/rest

Endpoints for REST APIs


Version published
Weekly downloads
739
decreased by-33.6%
Maintainers
1
Weekly downloads
 
Created
Source

Data Client for REST

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome Chat

Extensible CRUD patterns for REST APIs.

Simple TypeScript definition

import { Entity, createResource } from '@data-client/rest';

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

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

Entity defines a data model. createResource creates a collection of six RestEndpoints

RestEndpoints are functions (and more) that return a Promise. Both call parameters and return value are automatically inferred from the options used to construct them.

path is a templating language using path-to-regex compile.

Standard CRUD Endpoints

Reads
const article = useSuspense(ArticleResource.get, { id: 5 });
const articles = useSuspense(ArticleResource.getList);
const [article, setArticle] = useState();
useEffect(() => {
  setArticle(await ArticleResource.get({ id: 5 }));
}, []);
Mutates
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

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

Programmatic queries

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

Keywords

FAQs

Package last updated on 04 Jul 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc