reactive/data-client / .agents/skills/data-client-rest-setup
data-client-rest-setup
VerifiedSet up and migrate to @data-client/rest for REST APIs. Detects existing HTTP patterns (axios, fetch, ky, superagent, got) and migrates them. Creates custom RestEndpoint base class with common behaviors. Use when adopting @data-client/rest in a new or existing project.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.5 KB |
| evals/evals.json | config | 2.8 KB |
| references.json | config | 386 B |
| references/RestEndpoint.md | markdown | 45.2 KB |
| references/RestEndpoint.vue.md | markdown | 46.1 KB |
| references/auth.md | markdown | 9.4 KB |
| references/auth.vue.md | markdown | 9.7 KB |
| references/axios-migration.md | markdown | 25.4 KB |
| references/axios-migration.vue.md | markdown | 25.5 KB |
| references/django.md | markdown | 2.3 KB |
| references/fetch-migration.md | markdown | 3.8 KB |
| references/got-migration.md | markdown | 4.3 KB |
| references/hookifyResource.md | markdown | 6.0 KB |
| references/hookifyResource.vue.md | markdown | 6.3 KB |
| references/ky-migration.md | markdown | 3.8 KB |
| references/resource.md | markdown | 27.0 KB |
| references/resource.vue.md | markdown | 27.1 KB |
| references/superagent-migration.md | markdown | 4.0 KB |
references/hookifyResource.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/rest/api/hookifyResource.md (react). Edit the source doc, not this file. -->
# hookifyResource
`hookifyResource()` Turns any [Resource](./resource.md) (collection of [RestEndpoints](./RestEndpoint.md)) into a collection
of hooks that return [RestEndpoints](./RestEndpoint.md).
> **Info**
>
> TypeScript >=4.3 is required for generative types to work correctly.
```ts title="resources/Article"
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,
},
};
},
);
```
```tsx title="ArticleDetail"
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" />);
```
## Members
Assuming you use the unchanged result of [resource()](./resource.md), these will be your methods
### useGet()
- method: 'GET'
- path: `path`
- schema: [schema](https://dataclient.io/rest/api/Entity)
```typescript
// GET //test.com/api/abc/xyz
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGet()({
group: 'abc',
id: 'xyz',
});
```
Commonly used with [useSuspense()](https://dataclient.io/docs/api/useSuspense), [Controller.invalidate](https://dataclient.io/docs/api/Controller#invalidate)
### useGetList()
- method: 'GET'
- path: `shortenPath(path)`
- Removes the last `:param` or `*wildcard` token:
```ts
hookifyResource(resource({ path: '/:first/:second' })).useGetList()
.path === '/:first';
hookifyResource(resource({ path: '/:first' })).useGetList().path ===
'/';
hookifyResource(resource({ path: '/:owner/*path' })).useGetList()
.path === '/:owner';
```
- schema: [\[schema\]](https://dataclient.io/rest/api/Array)
```typescript
// 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()](https://dataclient.io/docs/api/useSuspense), [Controller.invalidate](https://dataclient.io/docs/api/Controller#invalidate)
### useGetList().push {#push}
[push](./RestEndpoint.md#push) creates a new entity and pushes it to the end of useGetList().
- method: 'POST'
- path: `shortenPath(path)`
- schema: `useGetList().schema.push`
```typescript
// POST //test.com/api/abc
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList().push({ group: 'abc' }, { title: 'winning' });
```
Commonly used with [Controller.fetch](https://dataclient.io/docs/api/Controller#fetch)
### useGetList().unshift {#unshift}
[unshift](./RestEndpoint.md#unshift) creates a new entity and pushes it to the beginning of useGetList().
- method: 'POST'
- path: `shortenPath(path)`
- schema: `useGetList().schema.unshift`
```typescript
// POST //test.com/api/abc
// BODY { "title": "winning" }
hookifyResource(
resource({ urlPrefix: '//test.com', path: '/api/:group/:id' }),
).useGetList().unshift({ group: 'abc' }, { title: 'winning' });
```
Commonly used with [Controller.fetch](https://dataclient.io/docs/api/Controller#fetch)
### useGetList().getPage {#getpage}
[getPage](./RestEndpoint.md#getpage) retrieves another [page](https://dataclient.io/rest/guides/pagination#infinite-scrolling) appending to useGetList() ensuring there are no duplicates.
- method: 'GET'
- args: `shortenPath(path) & { [paginationField]: string | number } & searchParams`
- schema: [new Collection(\[schema\]).addWith(paginatedMerge, paginatedFilter(removeCursor))](https://dataclient.io/rest/api/Collection)
```typescript
// 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',
});
```
Commonly used with [Controller.fetch](https://dataclient.io/docs/api/Controller#fetch)
### useUpdate()
- method: 'PUT'
- path: `path`
- schema: `schema`
```typescript
// 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](https://dataclient.io/docs/api/Controller#fetch)
### usePartialUpdate()
- method: 'PATCH'
- path: `path`
- schema: `schema`
```typescript
// 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](https://dataclient.io/docs/api/Controller#fetch)
### useDelete()
- method: 'DELETE'
- path: `path`
- schema: [new Invalidate(schema)](https://dataclient.io/rest/api/Invalidate)
- process:
```ts
(value, params) {
return value && Object.keys(value).length ? value : params;
},
```
```typescript
// 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](https://dataclient.io/docs/api/Controller#fetch)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |