@defjs/vue 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 +120 -0
- package/index.d.ts +36 -0
- package/index.js +44 -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,120 @@
|
|
|
1
|
+
# @defjs/vue
|
|
2
|
+
|
|
3
|
+
Thin Vue 3 adapter for `@defjs/core`. It provides an existing Defjs client through Vue injection.
|
|
4
|
+
|
|
5
|
+
Supports Vue 3+.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add @defjs/core @defjs/vue vue
|
|
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
|
+
`createClientPlugin(client)` creates a Vue plugin that provides the exact `Client` instance supplied by the application. `injectClient()` reads the nearest provided instance. `HTTP_CLIENT` remains public for native subtree overrides.
|
|
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 transport behavior, state management, retries, or a Nuxt module.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
Create and own the client at the application entry point:
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
// main.ts
|
|
27
|
+
import { createClient, withEndpoint } from '@defjs/core'
|
|
28
|
+
import { createClientPlugin } from '@defjs/vue'
|
|
29
|
+
import { createApp } from 'vue'
|
|
30
|
+
import App from './App.vue'
|
|
31
|
+
|
|
32
|
+
const client = createClient(withEndpoint('https://api.example.com'))
|
|
33
|
+
const app = createApp(App)
|
|
34
|
+
|
|
35
|
+
app.use(createClientPlugin(client))
|
|
36
|
+
app.mount('#app')
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Use the injected client inside a component or composable and own cleanup where the work starts:
|
|
40
|
+
|
|
41
|
+
```vue
|
|
42
|
+
<script setup lang="ts">
|
|
43
|
+
import { ref, watch } from 'vue'
|
|
44
|
+
import { injectClient } from '@defjs/vue'
|
|
45
|
+
import { getUser } from './api'
|
|
46
|
+
|
|
47
|
+
const props = defineProps<{ id: number }>()
|
|
48
|
+
const client = injectClient()
|
|
49
|
+
const name = ref('')
|
|
50
|
+
|
|
51
|
+
watch(
|
|
52
|
+
() => props.id,
|
|
53
|
+
(id, _previousId, onCleanup) => {
|
|
54
|
+
const abort = new AbortController()
|
|
55
|
+
onCleanup(() => abort.abort())
|
|
56
|
+
|
|
57
|
+
void client.execute(getUser({ path: { id } }), { signal: abort.signal }).then(([error, user]) => {
|
|
58
|
+
if (!abort.signal.aborted) name.value = error ? error.message : user.name
|
|
59
|
+
})
|
|
60
|
+
},
|
|
61
|
+
{ immediate: true },
|
|
62
|
+
)
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<template>
|
|
66
|
+
<div>{{ name }}</div>
|
|
67
|
+
</template>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Configure interceptors in core before installing the plugin:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { createClient, createHttpInterceptor, withEndpoint, withInterceptors } from '@defjs/core'
|
|
74
|
+
import { createClientPlugin } from '@defjs/vue'
|
|
75
|
+
|
|
76
|
+
const auth = createHttpInterceptor((request, next) => {
|
|
77
|
+
const headers = new Headers(request.headers)
|
|
78
|
+
headers.set('Authorization', `Bearer ${getAccessToken()}`)
|
|
79
|
+
return next({ ...request, headers })
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const client = createClient(withEndpoint('https://api.example.com'), withInterceptors(auth))
|
|
83
|
+
app.use(createClientPlugin(client))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
For a subtree override, use Vue's native provider with the public key:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { createClient, withEndpoint } from '@defjs/core'
|
|
90
|
+
import { HTTP_CLIENT } from '@defjs/vue'
|
|
91
|
+
import { provide } from 'vue'
|
|
92
|
+
|
|
93
|
+
provide(HTTP_CLIENT, createClient(withEndpoint('https://preview.example.com')))
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Descendants receive that exact client; siblings outside the subtree continue to receive the app-level client.
|
|
97
|
+
|
|
98
|
+
## API
|
|
99
|
+
|
|
100
|
+
### `createClientPlugin(client: Client): Plugin`
|
|
101
|
+
|
|
102
|
+
Creates a Vue plugin that provides the supplied client instance.
|
|
103
|
+
|
|
104
|
+
### `injectClient(): Client`
|
|
105
|
+
|
|
106
|
+
Returns the client from the nearest Vue provider. Throws if no client was provided.
|
|
107
|
+
|
|
108
|
+
### `HTTP_CLIENT: InjectionKey<Client>`
|
|
109
|
+
|
|
110
|
+
Public injection key for native subtree providers.
|
|
111
|
+
|
|
112
|
+
## Notes
|
|
113
|
+
|
|
114
|
+
- Create request-specific clients at the application boundary for SSR data that contains credentials, cookies, or tenant state.
|
|
115
|
+
- The owner that creates a client also owns all requests, SSE streams, and WebSocket sessions started through it. App unmount does not clean them up automatically.
|
|
116
|
+
- Import `createClient`, `withEndpoint`, `withInterceptors`, and all other client options from `@defjs/core`.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT License.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { InjectionKey, Plugin } from "vue";
|
|
2
|
+
import { Client } from "@defjs/core";
|
|
3
|
+
//#region src/core.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Vue injection key for the Defjs {@link Client} provided by {@link createClientPlugin}.
|
|
6
|
+
*
|
|
7
|
+
* Prefer {@link injectClient} over calling `inject(HTTP_CLIENT)` directly.
|
|
8
|
+
*/
|
|
9
|
+
export declare const HTTP_CLIENT: InjectionKey<Client>;
|
|
10
|
+
/**
|
|
11
|
+
* Create a Vue plugin that provides an existing Defjs client via {@link HTTP_CLIENT}.
|
|
12
|
+
*
|
|
13
|
+
* Does not create, cache, or dispose the client — pass an instance from `createClient`.
|
|
14
|
+
*
|
|
15
|
+
* @param client - The Client instance to provide to the app.
|
|
16
|
+
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* app.use(createClientPlugin(client))
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function createClientPlugin(client: Client): Plugin;
|
|
24
|
+
/**
|
|
25
|
+
* Inject the Defjs client provided by {@link createClientPlugin}.
|
|
26
|
+
*
|
|
27
|
+
* @returns The injected `Client` instance.
|
|
28
|
+
* @throws If no client was provided (plugin not installed).
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const client = injectClient()
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function injectClient(): Client;
|
|
36
|
+
//#endregion
|
package/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { inject } from "vue";
|
|
2
|
+
//#region src/core.ts
|
|
3
|
+
/**
|
|
4
|
+
* Vue injection key for the Defjs {@link Client} provided by {@link createClientPlugin}.
|
|
5
|
+
*
|
|
6
|
+
* Prefer {@link injectClient} over calling `inject(HTTP_CLIENT)` directly.
|
|
7
|
+
*/
|
|
8
|
+
const HTTP_CLIENT = Symbol("HTTP_CLIENT");
|
|
9
|
+
/**
|
|
10
|
+
* Create a Vue plugin that provides an existing Defjs client via {@link HTTP_CLIENT}.
|
|
11
|
+
*
|
|
12
|
+
* Does not create, cache, or dispose the client — pass an instance from `createClient`.
|
|
13
|
+
*
|
|
14
|
+
* @param client - The Client instance to provide to the app.
|
|
15
|
+
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* app.use(createClientPlugin(client))
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
function createClientPlugin(client) {
|
|
23
|
+
return { install(app) {
|
|
24
|
+
app.provide(HTTP_CLIENT, client);
|
|
25
|
+
} };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Inject the Defjs client provided by {@link createClientPlugin}.
|
|
29
|
+
*
|
|
30
|
+
* @returns The injected `Client` instance.
|
|
31
|
+
* @throws If no client was provided (plugin not installed).
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const client = injectClient()
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function injectClient() {
|
|
39
|
+
const client = inject(HTTP_CLIENT);
|
|
40
|
+
if (!client) throw new Error("No Defjs client provided. Did you forget to call app.use(createClientPlugin(client))?");
|
|
41
|
+
return client;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
export { HTTP_CLIENT, createClientPlugin, injectClient };
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@defjs/vue",
|
|
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
|
+
"vue": ">=3.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
|
+
}
|