Skip to content

Commit

Permalink
feat: Controller.fetch returns denoramlized form (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Mar 30, 2023
1 parent 0a6c93c commit 775352c
Show file tree
Hide file tree
Showing 29 changed files with 1,667 additions and 540 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-kiwis-promise.md
@@ -0,0 +1,5 @@
---
'@rest-hooks/react': minor
---

useController().fetch resolves to denormalized form
5 changes: 5 additions & 0 deletions .changeset/good-spoons-look.md
@@ -0,0 +1,5 @@
---
'@rest-hooks/rest': patch
---

Add commonjs export for /next
6 changes: 6 additions & 0 deletions .changeset/perfect-grapes-glow.md
@@ -0,0 +1,6 @@
---
'@rest-hooks/react': minor
'@rest-hooks/core': minor
---

Add /next export for early adoption of breaking changes
5 changes: 5 additions & 0 deletions .changeset/smart-dragons-rhyme.md
@@ -0,0 +1,5 @@
---
'@rest-hooks/core': minor
---

Controller.fetch resolves to denormalized form and is fully typed
15 changes: 7 additions & 8 deletions docs/core/api/Controller.md
Expand Up @@ -113,16 +113,15 @@ function PostListItem({ post }: { post: PostResource }) {
:::tip

`fetch` has the same return value as the [Endpoint](/rest/api/Endpoint) passed to it.
When using schemas, the denormalized value can be retrieved using a combination of
[Controller.getResponse](#getResponse) and [Controller.getState](#getState)
When using schemas, the denormalized value can be retrieved using the future-compatible /next import

```ts
await controller.fetch(PostResource.create, createPayload);
const { data: denormalizedResponse } = controller.getResponse(
PostResource.create,
createPayload,
controller.getState(),
);
// highlight-next-line
import { useController } from '@rest-hooks/react/next';

const post = await controller.fetch(PostResource.create, createPayload);
post.title;
post.pk();
```

:::
Expand Down
15 changes: 15 additions & 0 deletions docs/core/api/useController.md
Expand Up @@ -7,6 +7,8 @@ title: useController()
<meta name="docsearch:pagerank" content="10"/>
</head>

import TypeScriptEditor from '@site/src/components/TypeScriptEditor';

[Controller](./Controller.md) provides type-safe methods to manipulate the store.

For instance [fetch](./Controller.md#fetch), [invalidate](./Controller.md#invalidate),
Expand Down Expand Up @@ -43,6 +45,19 @@ function MyComponent({ id }) {

## Examples

### /next

`@rest-hooks/react/next` contains the version of `useController()` that will ship with the next version.
This provides a return value that matches [useSuspense()](./useSuspense.md) - utilizing the [Endpoint.schema](/rest/api/RestEndpoint#schema)

```ts
import { useController } from '@rest-hooks/react/next';

const post = await controller.fetch(PostResource.create, createPayload);
post.title;
post.pk();
```

### Todo App

<iframe
Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Expand Up @@ -25,6 +25,8 @@ const baseConfig = {
'packages/redux',
'packages/ssr',
'packages/rest/src/next',
'packages/core/src/next',
'packages/react/src/next',
'packages/legacy/src/resource',
'packages/legacy/src/rest-3',
'packages/rest-hooks/src/hooks/hasUsableData',
Expand Down
1 change: 1 addition & 0 deletions packages/core/.gitignore
Expand Up @@ -2,3 +2,4 @@
/dist
/legacy
/index.d.ts
/next.d.ts
11 changes: 11 additions & 0 deletions packages/core/package.json
Expand Up @@ -13,6 +13,9 @@
"": [
"lib/index.d.ts"
],
"next": [
"lib/next/index.d.ts"
],
"*": [
"lib/index.d.ts"
]
Expand All @@ -21,6 +24,9 @@
"": [
"ts3.4/index.d.ts"
],
"next": [
"ts3.4/next/index.d.ts"
],
"*": [
"ts3.4/index.d.ts"
]
Expand All @@ -33,6 +39,11 @@
"require": "./dist/index.js",
"default": "./lib/index.js"
},
"./next": {
"types": "./lib/next/index.d.ts",
"require": "./dist/next.js",
"default": "./lib/next/index.js"
},
"./package.json": "./package.json"
},
"type": "module",
Expand Down
46 changes: 26 additions & 20 deletions packages/core/rollup.config.js
@@ -1,13 +1,13 @@
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import { terser } from 'rollup-plugin-terser';
import commonjs from 'rollup-plugin-commonjs';
import filesize from 'rollup-plugin-filesize';
import json from 'rollup-plugin-json';
import resolve from 'rollup-plugin-node-resolve';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';

import { typeConfig } from '../../rollup-utils';
import pkg from './package.json';
import { typeConfig, typeConfigNext } from '../../rollup-utils';

const dependencies = Object.keys(pkg.dependencies)
//.concat(Object.keys(pkg.peerDependencies))
Expand Down Expand Up @@ -44,23 +44,29 @@ if (process.env.BROWSERSLIST_ENV !== 'node12') {
],
});
configs.push(typeConfig);
configs.push(typeConfigNext);
} else {
// node-friendly commonjs build
configs.push({
input: 'lib/index.js',
external: isExternal,
output: [{ file: pkg.main, format: 'cjs' }],
plugins: [
babel({
exclude: ['node_modules/**', '**/__tests__/**', '**/*.d.ts'],
extensions: nativeExtensions,
rootMode: 'upward',
runtimeHelpers: true,
}),
replace({ 'process.env.CJS': 'true' }),
resolve({ extensions: nativeExtensions }),
commonjs({ extensions: nativeExtensions }),
],
[
{ input: 'lib/index.js', output: pkg.main },
{ input: 'lib/next/index.js', output: 'dist/next.js' },
].forEach(({ input, output }) => {
configs.push({
input,
external: isExternal,
output: [{ file: output, format: 'cjs' }],
plugins: [
babel({
exclude: ['node_modules/**', '**/__tests__/**', '**/*.d.ts'],
extensions: nativeExtensions,
rootMode: 'upward',
runtimeHelpers: true,
}),
replace({ 'process.env.CJS': 'true' }),
resolve({ extensions: nativeExtensions }),
commonjs({ extensions: nativeExtensions }),
],
});
});
}
export default configs;

1 comment on commit 775352c

@vercel
Copy link

@vercel vercel bot commented on 775352c Mar 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.