Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@data-client/rest
Advanced tools
Extensible CRUD patterns for REST APIs.
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.
const article = useSuspense(ArticleResource.get, { id: 5 });
const articles = useSuspense(ArticleResource.getList);
const [article, setArticle] = useState();
useEffect(() => {
setArticle(await ArticleResource.get({ id: 5 }));
}, []);
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 });
const article = await ArticleResource.get({ id: 5 });
const articles = await ArticleResource.getList();
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 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.
FAQs
Quickly define typed REST resources and endpoints
The npm package @data-client/rest receives a total of 612 weekly downloads. As such, @data-client/rest popularity was classified as not 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 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
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.