Skip to main content

7.4 - Resource.extend(), paginationField

· 2 min read
Nathaniel Tucker

Highlights of 7.1-7.4 of @rest-hooks/rest include

Resource.extend()

createResource builds a great starting point, but often endpoints need to be further customized.

extend() is polymorphic with three forms:

Batch extension of known members

export const CommentResource = createResource({
path: '/repos/:owner/:repo/issues/comments/:id',
schema: Comment,
}).extend({
getList: { path: '/repos/:owner/:repo/issues/:number/comments' },
update: { body: { body: '' } },
});

Adding new members

export const UserResource = createResource({
path: '/users/:login',
schema: User,
}).extend('current', {
path: '/user',
schema: User,
});

Function form (to get BaseResource/super)

export const IssueResource= createResource({
path: '/repos/:owner/:repo/issues/:number',
schema: Issue,
pollFrequency: 60000,
searchParams: {} as IssueFilters | undefined,
}).extend(BaseResource => ({
search: BaseResource.getList.extend({
path: '/search/issues\\?q=:q?%20repo\\::owner/:repo&page=:page?',
schema: {
results: {
incompleteResults: false,
items: BaseIssueResource.getList.schema.results,
totalCount: 0,
},
link: '',
},
})
)});

Resource.extend() PR

paginationField

paginationField is added to both createResource and RestEndpoint

When provided, adds a specialized extender (like push/unshift/assign) to the list. This also only works with collections.

const TodoResource = createResource({
path: '/todos/:id',
schema: Todo,
paginationField: 'page',
}).getList.getPage({ page: '2' });

Pagination example with data client

paginationField PR