Skip to main content

hookifyResource

hookifyResource() Turns any Resource (collection of RestEndpoints) into a collection of hooks that return RestEndpoints.

info

TypeScript >=4.3 is required for generative types to work correctly.

api/ArticleResource.ts
import React from 'react';
import { createResource, hookifyResource, Entity } from '@data-client/rest';

class Article extends Entity {
  id = '';
  title = '';
  content = '';

  pk() {
    return this.id;
  }
}
const AuthContext = React.createContext('');

const ArticleResourceBase = createResource({
  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,
      },
    };
  },
);
ArticleDetail.tsx
import { ArticleResource } from './api/ArticleResource';

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" />);

Members

Assuming you use the unchanged result of createResource(), these will be your methods

useGet()

  • method: 'GET'
  • path: path
  • schema: schema
// GET //test.com/api/abc/xyz
hookifyResource(
createResource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGet()({
group: 'abc',
id: 'xyz',
});

Commonly used with useSuspense(), Controller.invalidate

useGetList()

  • method: 'GET'
  • path: shortenPath(path)
    • Removes the last argument:
      hookifyResource(createResource({ path: '/:first/:second' })).useGetList()
      .path === '/:first';
      hookifyResource(createResource({ path: '/:first' })).useGetList().path ===
      '/';
  • schema: [schema]
// GET //test.com/api/abc?isExtra=xyz
hookifyResource(
createResource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList()({
group: 'abc',
isExtra: 'xyz',
});

Commonly used with useSuspense(), Controller.invalidate

useGetList().push

push creates a new entity and pushes it to the end of useGetList().

  • method: 'POST'
  • path: shortenPath(path)
  • schema: useGetList().schema.push
// POST //test.com/api/abc
// BODY { "title": "winning" }
createResource({
urlPrefix: '//test.com',
path: '/api/:group/:id',
}).useGetList().push({ group: 'abc' }, { title: 'winning' });

Commonly used with Controller.fetch

useGetList().unshift

unshift creates a new entity and pushes it to the beginning of useGetList().

  • method: 'POST'
  • path: shortenPath(path)
  • schema: useGetList().schema.unshift
// POST //test.com/api/abc
// BODY { "title": "winning" }
createResource({
urlPrefix: '//test.com',
path: '/api/:group/:id',
}).useGetList().push({ group: 'abc' }, { title: 'winning' });

Commonly used with Controller.fetch

useGetList().getPage

getPage retrieves another page appending to useGetList() ensuring there are no duplicates.

// GET //test.com/api/abc?isExtra=xyz&page=2
createResource({
urlPrefix: '//test.com',
path: '/api/:group/:id',
paginationField: 'page',
}).useGetList().getPage({
group: 'abc',
isExtra: 'xyz',
page: '2',
});

Commonly used with Controller.fetch

useUpdate()

  • method: 'PUT'
  • path: path
  • schema: schema
// PUT //test.com/api/abc/xyz
// BODY { "title": "winning" }
hookifyResource(
createResource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useUpdate()({ group: 'abc', id: 'xyz' }, { title: 'winning' });

Commonly used with Controller.fetch

usePartialUpdate()

  • method: 'PATCH'
  • path: path
  • schema: schema
// PATCH //test.com/api/abc/xyz
// BODY { "title": "winning" }
hookifyResource(
createResource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).usePartialUpdate()({ group: 'abc', id: 'xyz' }, { title: 'winning' });

Commonly used with Controller.fetch

useDelete()

  • method: 'DELETE'
  • path: path
  • schema: new schema.Invalidate(schema)
  • process:
    (value, params) {
    return value && Object.keys(value).length ? value : params;
    },
// DELETE //test.com/api/abc/xyz
hookifyResource(
createResource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useDelete()({
group: 'abc',
id: 'xyz',
});

Commonly used with Controller.fetch