@defjs/react 0.0.2 → 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.
Files changed (4) hide show
  1. package/README.md +15 -15
  2. package/index.d.ts +12 -12
  3. package/index.js +6 -6
  4. 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.5.0` range.
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.5.0` range.
18
18
  // api.ts — reusable definition
19
19
  import { defineRequest, struct } from "@defjs/core";
20
20
 
21
- export const getUser = defineRequest({
21
+ export const getOrder = defineRequest({
22
22
  method: "GET",
23
- path: "/users/:id",
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(), name: struct.string() }),
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 { UserProfile } from "./user-profile";
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
- <UserProfile id={1} />
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 { getUser } from "./api";
52
+ import { getOrder } from "./api";
53
53
 
54
- export function UserProfile({ id }: { id: number }) {
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 getUser(client({ abort: abort.signal }), { id }).then(
62
- ([error, user]) => {
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(user.name);
68
+ setName(order.status);
69
69
  },
70
70
  );
71
71
 
@@ -80,16 +80,16 @@ export function UserProfile({ id }: { id: number }) {
80
80
 
81
81
  ### `<ClientProvider client={client}>`
82
82
 
83
- Provides the exact supplied `Client` to descendants. Nested providers follow normal React Context rules.
83
+ Provides the exact supplied `DerivableClient` to descendants. Nested providers follow normal React Context rules.
84
84
 
85
- ### `useClient(overrideClient?): Client`
85
+ ### `useClient(overrideClient?): DerivableClient`
86
86
 
87
87
  Returns `overrideClient` when supplied, otherwise the nearest provided client. Throws only when neither source supplies one. Call it at the top level of a component or Hook — never as `explicitClient ?? useClient()`, which conditionally calls a Hook. The override does not write Context.
88
88
 
89
89
  ## Notes
90
90
 
91
- - `Client` is a function. `useClient()` returns that same function. 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 the client.
92
- - Create request-specific clients (SSR data with credentials, cookies, or tenant state) at the application boundary.
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 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,31 +1,31 @@
1
1
 
2
- import { ReactNode } from "react";
3
- import { Client } from "@defjs/core";
2
+ import { ReactElement, ReactNode } from "react";
3
+ import { DerivableClient } from "@defjs/core";
4
4
  /**
5
5
  * Props of {@link ClientProvider}: the required `client` and optional `children`.
6
6
  *
7
7
  * @group react
8
8
  */
9
9
  interface ClientProviderProps {
10
- /** Defjs client owned by the application. */
11
- client: Client;
10
+ /** Derivable client returned by `createClient`, owned by the application. */
11
+ client: DerivableClient;
12
12
  /** React tree that can call {@link useClient}. */
13
13
  children?: ReactNode;
14
14
  }
15
15
  /**
16
16
  * Provide a Defjs client to React descendants via context.
17
17
  *
18
- * Pass an existing client from `createClient` as `client`; descendants read it with `useClient`. The component does not create, cache, or dispose the client. A nested `ClientProvider` overrides the client for its own subtree, and a different `client` prop changes the context value for descendants.
18
+ * Pass the client returned by `createClient` as `client`; descendants read it with `useClient`. The component does not create, cache, or dispose the client. A nested `ClientProvider` overrides the client for its own subtree, and a different `client` prop changes the context value for descendants.
19
19
  *
20
20
  * @param props - `client` and optional `children`.
21
21
  * @returns A context provider element.
22
22
  *
23
23
  * @group react
24
- * @see {@link Client}
24
+ * @see {@link DerivableClient}
25
25
  * @see {@link ClientProviderProps}
26
26
  * @exampleFile examples/react/client-provider.tsx#example
27
27
  */
28
- export declare function ClientProvider({ client, children }: ClientProviderProps): React.FunctionComponentElement<React.ProviderProps<Client | null>>;
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,10 +33,10 @@ 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 that same function. `Client` is callable, so do not pass it to `useState` or a state setter: React calls a function argument as a lazy initializer or an updater.
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
- * @param overrideClient - Client to use before context; `undefined` uses the provider.
39
- * @returns The selected client function.
38
+ * @param overrideClient - Derivable client to use before context; `undefined` uses the provider.
39
+ * @returns The selected derivable client.
40
40
  * @throws If neither an explicit client nor a `ClientProvider` is available.
41
41
  *
42
42
  * @example
@@ -44,7 +44,7 @@ export declare function ClientProvider({ client, children }: ClientProviderProps
44
44
  * const client = useClient()
45
45
  * ```
46
46
  * @group react
47
- * @see {@link Client}
47
+ * @see {@link DerivableClient}
48
48
  */
49
- export declare function useClient(overrideClient?: Client): Client;
49
+ export declare function useClient(overrideClient?: DerivableClient): DerivableClient;
50
50
  export type { ClientProviderProps };
package/index.js CHANGED
@@ -5,13 +5,13 @@ const HttpClientContext = createContext(null);
5
5
  /**
6
6
  * Provide a Defjs client to React descendants via context.
7
7
  *
8
- * Pass an existing client from `createClient` as `client`; descendants read it with `useClient`. The component does not create, cache, or dispose the client. A nested `ClientProvider` overrides the client for its own subtree, and a different `client` prop changes the context value for descendants.
8
+ * Pass the client returned by `createClient` as `client`; descendants read it with `useClient`. The component does not create, cache, or dispose the client. A nested `ClientProvider` overrides the client for its own subtree, and a different `client` prop changes the context value for descendants.
9
9
  *
10
10
  * @param props - `client` and optional `children`.
11
11
  * @returns A context provider element.
12
12
  *
13
13
  * @group react
14
- * @see {@link Client}
14
+ * @see {@link DerivableClient}
15
15
  * @see {@link ClientProviderProps}
16
16
  * @exampleFile examples/react/client-provider.tsx#example
17
17
  */
@@ -25,10 +25,10 @@ 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 that same function. `Client` is callable, so do not pass it to `useState` or a state setter: React calls a function argument as a lazy initializer or an updater.
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
- * @param overrideClient - Client to use before context; `undefined` uses the provider.
31
- * @returns The selected client function.
30
+ * @param overrideClient - Derivable client to use before context; `undefined` uses the provider.
31
+ * @returns The selected derivable client.
32
32
  * @throws If neither an explicit client nor a `ClientProvider` is available.
33
33
  *
34
34
  * @example
@@ -36,7 +36,7 @@ function ClientProvider({ client, children }) {
36
36
  * const client = useClient()
37
37
  * ```
38
38
  * @group react
39
- * @see {@link Client}
39
+ * @see {@link DerivableClient}
40
40
  */
41
41
  function useClient(overrideClient) {
42
42
  const providedClient = useContext(HttpClientContext);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defjs/react",
3
- "version": "0.0.2",
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.5.0",
17
+ "@defjs/core": "^0.7.0",
18
18
  "react": ">=18.0.0"
19
19
  },
20
20
  "module": "./index.js",