hookifyResource
hookifyResource() convierte cualquier Resource (colección de RestEndpoints) en una colección
de hooks que devuelven RestEndpoints.
Se requiere TypeScript >=4.3 para que los tipos generativos funcionen correctamente.
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" />);
Miembros
Suponiendo que uses el resultado sin modificar de resource(), estos serán tus 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',
});
Se usa comúnmente con useSuspense() y Controller.invalidate
useGetList()
- method: 'GET'
- path:
shortenPath(path)- Elimina el último token
:paramo*wildcard:hookifyResource(resource({ path: '/:first/:second' })).useGetList().path === '/:first';hookifyResource(resource({ path: '/:first' })).useGetList().path ==='/';hookifyResource(resource({ path: '/:owner/*path' })).useGetList().path === '/:owner';
- Elimina el ú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',
});
Se usa comúnmente con useSuspense() y Controller.invalidate
useGetList().push
push crea una nueva entidad y la agrega al 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' });
Se usa comúnmente con Controller.fetch
useGetList().unshift
unshift crea una nueva entidad y la agrega al principio 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' });
Se usa comúnmente con Controller.fetch
useGetList().getPage
getPage obtiene otra página y la añade a useGetList() asegurando que no haya duplicados.
- method: 'GET'
- args:
shortenPath(path) & { [paginationField]: string | number } & searchParams - Schema: 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',
});
Se usa comúnmente con 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' });
Se usa comúnmente con 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' });
Se usa comúnmente con Controller.fetch
useDelete()
- method: 'DELETE'
- path:
path - Schema: 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',
});
Se usa comúnmente con Controller.fetch