hookifyResource
hookifyResource() transforma qualquer Resource (coleção de RestEndpoints) em uma coleção
de hooks que retornam RestEndpoints.
TypeScript >=4.3 é necessário para que os tipos generativos funcionem corretamente.
import React from 'react'; import { Collection, Entity, Invalidate, hookifyResource, resource } from '@data-client/rest'; class Article extends Entity { id = ''; title = ''; content = ''; } const AuthContext = React.createContext(''); const ArticleResourceBase = resource({ urlPrefix: 'http://test.com', path: '/article/:id', schema: Article, }); export const ArticleResource = hookifyResource( ArticleResourceBase, function useInit() { const accessToken = React.useContext(AuthContext); return { headers: { 'Access-Token': accessToken, }, }; }, );
import { useSuspense, useController } from '@data-client/react'; import { ArticleResource } from './resources/Article'; import ArticleForm from './ArticleForm'; function ArticleDetail({ id }) { const article = useSuspense(ArticleResource.useGet(), { id }); const updateArticle = ArticleResource.useUpdate(); const ctrl = useController(); const onSubmit = (body: any) => ctrl.fetch(updateArticle, { id }, body); return <ArticleForm onSubmit={onSubmit} initialValues={article} />; } render(<ArticleDetail id="1" />);
Membros
Assumindo que você use o resultado inalterado de resource(), estes serão os seus métodos
useGet()
- method: 'GET'
- path:
path - schema: schema
// GET //test.com/api/abc/xyz
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGet()({
group: 'abc',
id: 'xyz',
});
Normalmente usado com useSuspense(), Controller.invalidate
useGetList()
- method: 'GET'
- path:
shortenPath(path)- Remove o último token
:paramou*wildcard:hookifyResource(resource({ path: '/:first/:second' })).useGetList().path === '/:first';hookifyResource(resource({ path: '/:first' })).useGetList().path ==='/';hookifyResource(resource({ path: '/:owner/*path' })).useGetList().path === '/:owner';
- Remove o último token
- schema: [schema]
// GET //test.com/api/abc?isExtra=xyz
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList()({
group: 'abc',
isExtra: 'xyz',
});
Normalmente usado com useSuspense(), Controller.invalidate
useGetList().push
push cria uma nova entity e a adiciona ao final de useGetList().
- method: 'POST'
- path:
shortenPath(path) - schema:
useGetList().schema.push
// POST //test.com/api/abc
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList().push({ group: 'abc' }, { title: 'winning' });
Normalmente usado com Controller.fetch
useGetList().unshift
unshift cria uma nova entity e a adiciona ao início de useGetList().
- method: 'POST'
- path:
shortenPath(path) - schema:
useGetList().schema.unshift
// POST //test.com/api/abc
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList().unshift({ group: 'abc' }, { title: 'winning' });
Normalmente usado com Controller.fetch
useGetList().getPage
getPage busca outra página, anexando-a a useGetList() e garantindo que não haja duplicatas.
- method: 'GET'
- args:
shortenPath(path) & { [paginationField]: string | number } & searchParams - schema: veja new Collection([schema]).addWith(paginatedMerge, paginatedFilter(removeCursor))
// GET //test.com/api/abc?isExtra=xyz&page=2
hookifyResource(
resource({
urlPrefix: '//test.com',
path: '/api/:group/:id',
paginationField: 'page',
}),
).useGetList().getPage({
group: 'abc',
isExtra: 'xyz',
page: '2',
});
Normalmente usado com Controller.fetch
useUpdate()
- method: 'PUT'
- path:
path - schema:
schema
// PUT //test.com/api/abc/xyz
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useUpdate()({ group: 'abc', id: 'xyz' }, { title: 'winning' });
Normalmente usado com Controller.fetch
usePartialUpdate()
- method: 'PATCH'
- path:
path - schema:
schema
// PATCH //test.com/api/abc/xyz
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).usePartialUpdate()({ group: 'abc', id: 'xyz' }, { title: 'winning' });
Normalmente usado com Controller.fetch
useDelete()
- method: 'DELETE'
- path:
path - schema: veja new Invalidate(schema)
- process:
(value, params) {return value && Object.keys(value).length ? value : params;},
// DELETE //test.com/api/abc/xyz
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useDelete()({
group: 'abc',
id: 'xyz',
});
Normalmente usado com Controller.fetch