Skip to main content

hookfiyResource

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.

resources/Article
import React from 'react';
import { resource, hookifyResource, Entity } 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,
      },
    };
  },
);
ArticleDetail
import { ArticleResource } from './resources/Article';

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 resource(), these will be your methods

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',
});

Commonly used with useSuspense(), Controller.invalidate

useGetList()

  • method: 'GET'
  • path: shortenPath(path)
    • Removes the last argument:
      hookifyResource(resource({ path: '/:first/:second' })).useGetList()
      .path === '/:first';
      hookifyResource(resource({ path: '/:first' })).useGetList().path ===
      '/';
  • schema: [schema]
// GET //test.com/api/abc?isExtra=xyz
hookifyResource(
resource({ 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" }
resource({
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" }
resource({
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
resource({
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(
resource({ 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(
resource({ 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(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useDelete()({
group: 'abc',
id: 'xyz',
});

Commonly used with Controller.fetch