@defjs/react 0.0.1-beta.0
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/LICENSE +21 -0
- package/README.md +109 -0
- package/index.d.ts +41 -0
- package/index.js +40 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MunMunMiao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# @defjs/react
|
|
2
|
+
|
|
3
|
+
Thin React adapter for `@defjs/core`. It provides an existing Defjs client through React Context.
|
|
4
|
+
|
|
5
|
+
Supports React 18+.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add @defjs/core @defjs/react react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The package is ESM and requires `@defjs/core` as a peer in the `^0.4.0` range. Its tarball retains this `README.md` and the repository `LICENSE`; repository-wide guides and examples remain outside the package.
|
|
14
|
+
|
|
15
|
+
## What this package does
|
|
16
|
+
|
|
17
|
+
`ClientProvider` exposes the exact `Client` instance supplied by the application. `useClient()` reads the nearest provided instance.
|
|
18
|
+
|
|
19
|
+
Client creation and configuration stay in `@defjs/core`. The adapter does not create, cache, replace, dispose, abort, or close anything on behalf of the application, and it does not add a query layer, Suspense integration, GraphQL client, or application state management.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
Define requests in a shared module:
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
// api.ts
|
|
27
|
+
import { defineRequest, struct } from '@defjs/core'
|
|
28
|
+
|
|
29
|
+
export const getUser = defineRequest({
|
|
30
|
+
method: 'GET',
|
|
31
|
+
path: '/users/:id',
|
|
32
|
+
input: struct.request({
|
|
33
|
+
path: struct.object({ id: struct.number() }),
|
|
34
|
+
}),
|
|
35
|
+
output: [
|
|
36
|
+
{
|
|
37
|
+
status: 200,
|
|
38
|
+
body: struct.object({ id: struct.number(), name: struct.string() }),
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Create and own the client at the application boundary, then provide it:
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
// app.tsx
|
|
48
|
+
import { createClient, withEndpoint } from '@defjs/core'
|
|
49
|
+
import { ClientProvider } from '@defjs/react'
|
|
50
|
+
import { UserProfile } from './user-profile'
|
|
51
|
+
|
|
52
|
+
const client = createClient(withEndpoint('https://api.example.com'))
|
|
53
|
+
|
|
54
|
+
export function App() {
|
|
55
|
+
return (
|
|
56
|
+
<ClientProvider client={client}>
|
|
57
|
+
<UserProfile id={1} />
|
|
58
|
+
</ClientProvider>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Read the client inside descendants and own request cleanup where the work starts:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { useEffect, useState } from 'react'
|
|
67
|
+
import { useClient } from '@defjs/react'
|
|
68
|
+
import { getUser } from './api'
|
|
69
|
+
|
|
70
|
+
export function UserProfile({ id }: { id: number }) {
|
|
71
|
+
const client = useClient()
|
|
72
|
+
const [name, setName] = useState('loading...')
|
|
73
|
+
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
const abort = new AbortController()
|
|
76
|
+
|
|
77
|
+
void client.execute(getUser({ path: { id } }), { signal: abort.signal }).then(([error, user]) => {
|
|
78
|
+
if (!abort.signal.aborted) setName(error ? error.message : user.name)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
return () => abort.abort()
|
|
82
|
+
}, [client, id])
|
|
83
|
+
|
|
84
|
+
return <div>{name}</div>
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Nested providers follow normal React Context rules: descendants receive the nearest provider's exact client. Supplying a different `client` prop changes the provided value; the adapter does not invent a remount or caching policy.
|
|
89
|
+
|
|
90
|
+
## API
|
|
91
|
+
|
|
92
|
+
### `<ClientProvider client: Client>`
|
|
93
|
+
|
|
94
|
+
Provides the supplied client to descendant components. `children` is optional.
|
|
95
|
+
|
|
96
|
+
### `useClient(): Client`
|
|
97
|
+
|
|
98
|
+
Returns the client from the nearest `ClientProvider`. Throws if called outside a provider.
|
|
99
|
+
|
|
100
|
+
## Notes
|
|
101
|
+
|
|
102
|
+
- The published entry is marked with `'use client'`, so React Server Component consumers treat this adapter as a client boundary.
|
|
103
|
+
- Create request-specific clients at the application boundary for SSR data that contains credentials, cookies, or tenant state.
|
|
104
|
+
- The owner that creates a client also owns all requests, SSE streams, and WebSocket sessions started through it. Provider unmount does not clean them up automatically.
|
|
105
|
+
- Import `createClient`, `withEndpoint`, `withInterceptors`, and all other client options from `@defjs/core`.
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT License.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
import { Client } from "@defjs/core";
|
|
4
|
+
//#region src/core.d.ts
|
|
5
|
+
/** Props for {@link ClientProvider}. */
|
|
6
|
+
interface ClientProviderProps {
|
|
7
|
+
/** Defjs client owned by the application. */
|
|
8
|
+
client: Client;
|
|
9
|
+
/** React tree that can call {@link useClient}. */
|
|
10
|
+
children?: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Provide a Defjs client to React descendants via context.
|
|
14
|
+
*
|
|
15
|
+
* Does not create, cache, or dispose the client — pass an existing instance.
|
|
16
|
+
*
|
|
17
|
+
* @param props - `client` and optional `children`.
|
|
18
|
+
* @returns A context provider element.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* <ClientProvider client={client}>
|
|
23
|
+
* <App />
|
|
24
|
+
* </ClientProvider>
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function ClientProvider({ client, children }: ClientProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<Client | null>>;
|
|
28
|
+
/**
|
|
29
|
+
* Read the Defjs client from the nearest {@link ClientProvider}.
|
|
30
|
+
*
|
|
31
|
+
* @returns The provided `Client`.
|
|
32
|
+
* @throws If called outside a `ClientProvider`.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const client = useClient()
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useClient(): Client;
|
|
40
|
+
//#endregion
|
|
41
|
+
export type { ClientProviderProps };
|
package/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { createContext, createElement, useContext } from "react";
|
|
3
|
+
//#region src/core.ts
|
|
4
|
+
const HttpClientContext = createContext(null);
|
|
5
|
+
/**
|
|
6
|
+
* Provide a Defjs client to React descendants via context.
|
|
7
|
+
*
|
|
8
|
+
* Does not create, cache, or dispose the client — pass an existing instance.
|
|
9
|
+
*
|
|
10
|
+
* @param props - `client` and optional `children`.
|
|
11
|
+
* @returns A context provider element.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* <ClientProvider client={client}>
|
|
16
|
+
* <App />
|
|
17
|
+
* </ClientProvider>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function ClientProvider({ client, children }) {
|
|
21
|
+
return createElement(HttpClientContext.Provider, { value: client }, children);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Read the Defjs client from the nearest {@link ClientProvider}.
|
|
25
|
+
*
|
|
26
|
+
* @returns The provided `Client`.
|
|
27
|
+
* @throws If called outside a `ClientProvider`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* const client = useClient()
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function useClient() {
|
|
35
|
+
const client = useContext(HttpClientContext);
|
|
36
|
+
if (!client) throw new Error("No Defjs client provided. Did you forget to wrap your app in <ClientProvider>?");
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { ClientProvider, useClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@defjs/react",
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"bugs": {
|
|
5
|
+
"url": "https://github.com/defjs/defjs/issues"
|
|
6
|
+
},
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/defjs/defjs.git"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@defjs/core": "0.4.0-beta.0",
|
|
17
|
+
"react": ">=18.0.0"
|
|
18
|
+
},
|
|
19
|
+
"module": "./index.js",
|
|
20
|
+
"typings": "./index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./index.d.ts",
|
|
25
|
+
"default": "./index.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|