@defjs/react 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/index.d.ts +3 -3
- package/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Keeping SDK definitions in their own package means the SDK needs no React depend
|
|
|
10
10
|
bun add @defjs/core @defjs/react react
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
The package is ESM and requires `@defjs/core` as a peer in the `^0.
|
|
13
|
+
The package is ESM and requires `@defjs/core` as a peer in the `^0.7.0` range.
|
|
14
14
|
|
|
15
15
|
## Quick start
|
|
16
16
|
|
|
@@ -18,12 +18,12 @@ The package is ESM and requires `@defjs/core` as a peer in the `^0.6.0` range.
|
|
|
18
18
|
// api.ts — reusable definition
|
|
19
19
|
import { defineRequest, struct } from "@defjs/core";
|
|
20
20
|
|
|
21
|
-
export const
|
|
21
|
+
export const getOrder = defineRequest({
|
|
22
22
|
method: "GET",
|
|
23
|
-
path: "/
|
|
23
|
+
path: "/orders/:id",
|
|
24
24
|
input: struct.object({ id: struct.number() }),
|
|
25
25
|
builder: (input) => ({ path: input }),
|
|
26
|
-
output: struct.object({ id: struct.number(),
|
|
26
|
+
output: struct.object({ id: struct.number(), status: struct.string() }),
|
|
27
27
|
});
|
|
28
28
|
```
|
|
29
29
|
|
|
@@ -31,14 +31,14 @@ export const getUser = defineRequest({
|
|
|
31
31
|
// app.tsx
|
|
32
32
|
import { createClient, withEndpoint } from "@defjs/core";
|
|
33
33
|
import { ClientProvider } from "@defjs/react";
|
|
34
|
-
import {
|
|
34
|
+
import { OrderProfile } from "./order-profile";
|
|
35
35
|
|
|
36
36
|
const client = createClient(withEndpoint("https://api.example.com"));
|
|
37
37
|
|
|
38
38
|
export function App() {
|
|
39
39
|
return (
|
|
40
40
|
<ClientProvider client={client}>
|
|
41
|
-
<
|
|
41
|
+
<OrderProfile id={1} />
|
|
42
42
|
</ClientProvider>
|
|
43
43
|
);
|
|
44
44
|
}
|
|
@@ -49,23 +49,23 @@ Read the client with `useClient()` and own request cleanup where the work starts
|
|
|
49
49
|
```tsx
|
|
50
50
|
import { useEffect, useState } from "react";
|
|
51
51
|
import { useClient } from "@defjs/react";
|
|
52
|
-
import {
|
|
52
|
+
import { getOrder } from "./api";
|
|
53
53
|
|
|
54
|
-
export function
|
|
54
|
+
export function OrderProfile({ id }: { id: number }) {
|
|
55
55
|
const client = useClient();
|
|
56
56
|
const [name, setName] = useState("loading...");
|
|
57
57
|
|
|
58
58
|
useEffect(() => {
|
|
59
59
|
const abort = new AbortController();
|
|
60
60
|
|
|
61
|
-
void
|
|
62
|
-
([error,
|
|
61
|
+
void getOrder(client({ signal: abort.signal }), { id }).then(
|
|
62
|
+
([error, order]) => {
|
|
63
63
|
if (abort.signal.aborted) return;
|
|
64
64
|
if (error) {
|
|
65
65
|
setName(error.message);
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
68
|
-
setName(
|
|
68
|
+
setName(order.status);
|
|
69
69
|
},
|
|
70
70
|
);
|
|
71
71
|
|
|
@@ -89,7 +89,7 @@ Returns `overrideClient` when supplied, otherwise the nearest provided client. T
|
|
|
89
89
|
## Notes
|
|
90
90
|
|
|
91
91
|
- `useClient()` returns the `DerivableClient` that was supplied, a function. Call it with request options where the operation runs to derive a `Client` value. Do not pass it to `useState` or a state setter: React calls a function argument as a lazy initializer or an updater and will invoke it. A derived `Client` value is not accepted by `ClientProvider` or `useClient`.
|
|
92
|
-
- Create request-specific clients (SSR
|
|
92
|
+
- Create request-specific clients (SSR with per-request credentials, cookies, or tenant state) at the application boundary.
|
|
93
93
|
- The owner that creates a client also owns its requests, SSE streams, and WebSocket sessions. Provider unmount does not clean them up.
|
|
94
94
|
- Import `createClient`, `withEndpoint`, `withInterceptors`, and other client options from `@defjs/core`.
|
|
95
95
|
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { ReactNode } from "react";
|
|
2
|
+
import { ReactElement, ReactNode } from "react";
|
|
3
3
|
import { DerivableClient } from "@defjs/core";
|
|
4
4
|
/**
|
|
5
5
|
* Props of {@link ClientProvider}: the required `client` and optional `children`.
|
|
@@ -25,7 +25,7 @@ interface ClientProviderProps {
|
|
|
25
25
|
* @see {@link ClientProviderProps}
|
|
26
26
|
* @exampleFile examples/react/client-provider.tsx#example
|
|
27
27
|
*/
|
|
28
|
-
export declare function ClientProvider({ client, children }: ClientProviderProps):
|
|
28
|
+
export declare function ClientProvider({ client, children }: ClientProviderProps): ReactElement;
|
|
29
29
|
/**
|
|
30
30
|
* React Hook that returns an explicit Defjs client or the nearest {@link ClientProvider}.
|
|
31
31
|
*
|
|
@@ -33,7 +33,7 @@ export declare function ClientProvider({ client, children }: ClientProviderProps
|
|
|
33
33
|
*
|
|
34
34
|
* Always call `useClient(explicitClient)` at the top level of a component or custom Hook, even with an explicit client; not in an ordinary function or event callback. Do not write `explicitClient ?? useClient()`, which conditionally calls a Hook. The override does not write Context or create a provider, and the Hook still subscribes to Context.
|
|
35
35
|
*
|
|
36
|
-
* The returned value is the exact `DerivableClient` that was supplied. Call it with request options at the call site to derive a `Client` value for one call: `
|
|
36
|
+
* The returned value is the exact `DerivableClient` that was supplied. Call it with request options at the call site to derive a `Client` value for one call: `getOrder(client({ signal }), input)`. It is a function, so do not pass it to `useState` or a state setter: React calls a function argument as a lazy initializer or an updater. The provider holds only a root client; a derived `Client` value is not accepted, so create it where the operation is called.
|
|
37
37
|
*
|
|
38
38
|
* @param overrideClient - Derivable client to use before context; `undefined` uses the provider.
|
|
39
39
|
* @returns The selected derivable client.
|
package/index.js
CHANGED
|
@@ -25,7 +25,7 @@ function ClientProvider({ client, children }) {
|
|
|
25
25
|
*
|
|
26
26
|
* Always call `useClient(explicitClient)` at the top level of a component or custom Hook, even with an explicit client; not in an ordinary function or event callback. Do not write `explicitClient ?? useClient()`, which conditionally calls a Hook. The override does not write Context or create a provider, and the Hook still subscribes to Context.
|
|
27
27
|
*
|
|
28
|
-
* The returned value is the exact `DerivableClient` that was supplied. Call it with request options at the call site to derive a `Client` value for one call: `
|
|
28
|
+
* The returned value is the exact `DerivableClient` that was supplied. Call it with request options at the call site to derive a `Client` value for one call: `getOrder(client({ signal }), input)`. It is a function, so do not pass it to `useState` or a state setter: React calls a function argument as a lazy initializer or an updater. The provider holds only a root client; a derived `Client` value is not accepted, so create it where the operation is called.
|
|
29
29
|
*
|
|
30
30
|
* @param overrideClient - Derivable client to use before context; `undefined` uses the provider.
|
|
31
31
|
* @returns The selected derivable client.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defjs/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Provide an existing Defjs client to a React tree.",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/defjs/defjs/issues"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"main": "./index.js",
|
|
15
15
|
"types": "./index.d.ts",
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@defjs/core": "^0.
|
|
17
|
+
"@defjs/core": "^0.7.0",
|
|
18
18
|
"react": ">=18.0.0"
|
|
19
19
|
},
|
|
20
20
|
"module": "./index.js",
|