@defjs/vue 0.0.1 → 0.0.2
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 +34 -72
- package/index.d.ts +6 -4
- package/index.js +6 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
# @defjs/vue
|
|
2
2
|
|
|
3
|
-
Thin Vue 3 plugin for `@defjs/core
|
|
3
|
+
Thin Vue 3 plugin for `@defjs/core`: it provides an application-owned client through Vue injection. It adds no transport behavior, state management, retries, or Nuxt module, and never creates, replaces, or disposes a client.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
This plugin is optional: import a fixed application-owned client, pass a `Client` explicitly, use Vue injection for tree-level defaults and overrides, or choose your own organization. SDK definitions do not require this plugin. Share an instance only when its captured configuration and state are safe to share; request-specific identity or credentials belong to the appropriate request boundary.
|
|
8
|
-
|
|
9
|
-
Supports Vue 3+.
|
|
5
|
+
Keeping SDK definitions in their own package means the SDK needs no Vue dependency. Requires Vue 3 or later.
|
|
10
6
|
|
|
11
7
|
## Install
|
|
12
8
|
|
|
@@ -14,20 +10,12 @@ Supports Vue 3+.
|
|
|
14
10
|
bun add @defjs/core @defjs/vue vue
|
|
15
11
|
```
|
|
16
12
|
|
|
17
|
-
The package is ESM and requires `@defjs/core` as a peer in the `^0.
|
|
18
|
-
|
|
19
|
-
## What this package does
|
|
20
|
-
|
|
21
|
-
`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.
|
|
22
|
-
|
|
23
|
-
Client creation and configuration stay in `@defjs/core`. The plugin 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.
|
|
13
|
+
The package is ESM and requires `@defjs/core` as a peer in the `^0.5.0` range.
|
|
24
14
|
|
|
25
|
-
## Quick
|
|
26
|
-
|
|
27
|
-
Create and own the client at the application entry point:
|
|
15
|
+
## Quick start
|
|
28
16
|
|
|
29
17
|
```ts
|
|
30
|
-
// main.ts
|
|
18
|
+
// main.ts — application entry point
|
|
31
19
|
import { createClient, withEndpoint } from "@defjs/core";
|
|
32
20
|
import { createClientPlugin } from "@defjs/vue";
|
|
33
21
|
import { createApp } from "vue";
|
|
@@ -40,7 +28,7 @@ app.use(createClientPlugin(client));
|
|
|
40
28
|
app.mount("#app");
|
|
41
29
|
```
|
|
42
30
|
|
|
43
|
-
|
|
31
|
+
Read the client with `injectClient()` and own cleanup in the watcher:
|
|
44
32
|
|
|
45
33
|
```vue
|
|
46
34
|
<script setup lang="ts">
|
|
@@ -58,17 +46,16 @@ watch(
|
|
|
58
46
|
const abort = new AbortController();
|
|
59
47
|
onCleanup(() => abort.abort());
|
|
60
48
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.execute(command, { signal: abort.signal })
|
|
64
|
-
.then(([error, user]) => {
|
|
49
|
+
void getUser(client({ abort: abort.signal }), { id }).then(
|
|
50
|
+
([error, user]) => {
|
|
65
51
|
if (abort.signal.aborted) return;
|
|
66
52
|
if (error) {
|
|
67
53
|
name.value = error.message;
|
|
68
54
|
return;
|
|
69
55
|
}
|
|
70
56
|
name.value = user.name;
|
|
71
|
-
}
|
|
57
|
+
},
|
|
58
|
+
);
|
|
72
59
|
},
|
|
73
60
|
{ immediate: true },
|
|
74
61
|
);
|
|
@@ -79,72 +66,47 @@ watch(
|
|
|
79
66
|
</template>
|
|
80
67
|
```
|
|
81
68
|
|
|
82
|
-
|
|
69
|
+
## API
|
|
83
70
|
|
|
84
|
-
|
|
85
|
-
import {
|
|
86
|
-
createClient,
|
|
87
|
-
createHttpInterceptor,
|
|
88
|
-
withEndpoint,
|
|
89
|
-
withInterceptors,
|
|
90
|
-
} from "@defjs/core";
|
|
91
|
-
import { createClientPlugin } from "@defjs/vue";
|
|
71
|
+
### `createClientPlugin(client): Plugin`
|
|
92
72
|
|
|
93
|
-
|
|
94
|
-
const headers = new Headers(request.headers);
|
|
95
|
-
headers.set("Authorization", `Bearer ${getAccessToken()}`);
|
|
96
|
-
return next({ ...request, headers });
|
|
97
|
-
});
|
|
73
|
+
Creates a Vue plugin that provides the supplied client as `HTTP_CLIENT`.
|
|
98
74
|
|
|
99
|
-
|
|
100
|
-
withEndpoint("https://api.example.com"),
|
|
101
|
-
withInterceptors(auth),
|
|
102
|
-
);
|
|
103
|
-
app.use(createClientPlugin(client));
|
|
104
|
-
```
|
|
75
|
+
### `injectClient(key?): Client`
|
|
105
76
|
|
|
106
|
-
|
|
77
|
+
Returns the nearest client for `key`, defaulting to `HTTP_CLIENT`. Call it in a valid injection context: synchronous `setup()` or `app.runWithContext()` (Vue 3.3+). Throws without an active context or a client for the key; a missing custom key never falls back to `HTTP_CLIENT`.
|
|
107
78
|
|
|
108
|
-
|
|
109
|
-
import { createClient, withEndpoint } from "@defjs/core";
|
|
110
|
-
import { HTTP_CLIENT } from "@defjs/vue";
|
|
111
|
-
import { provide } from "vue";
|
|
79
|
+
### `HTTP_CLIENT`
|
|
112
80
|
|
|
81
|
+
Public `InjectionKey<Client>` for native subtree overrides:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
113
84
|
provide(HTTP_CLIENT, createClient(withEndpoint("https://preview.example.com")));
|
|
114
85
|
```
|
|
115
86
|
|
|
116
|
-
Descendants receive that
|
|
117
|
-
|
|
118
|
-
## API
|
|
119
|
-
|
|
120
|
-
### `createClientPlugin(client: Client): Plugin`
|
|
87
|
+
Descendants receive that client; siblings outside the subtree keep the app-level one.
|
|
121
88
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
### `injectClient(): Client`
|
|
89
|
+
Interceptors and every other client option belong to `@defjs/core` and are configured before the plugin is installed:
|
|
125
90
|
|
|
126
91
|
```ts
|
|
127
|
-
|
|
92
|
+
const client = createClient(
|
|
93
|
+
withEndpoint("https://api.example.com"),
|
|
94
|
+
withInterceptors(auth),
|
|
95
|
+
);
|
|
96
|
+
app.use(createClientPlugin(client));
|
|
128
97
|
```
|
|
129
98
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
Call in a valid Vue injection context, such as synchronous `setup()` or `app.runWithContext()` (Vue 3.3+). The latter is synchronous app-level context only: no component subtree overrides and no context across ordinary `await`. Compiler-restored contexts after top-level `await` in `<script setup>` remain valid.
|
|
133
|
-
|
|
134
|
-
Throws an `Error` without an active injection context or a client for the selected key; a missing custom key never falls back to `HTTP_CLIENT`. Installing the plugin alone does not establish an injection context.
|
|
135
|
-
|
|
136
|
-
An application function that only retrieves a client may use `explicitClient ?? injectClient()`: only the fallback branch needs injection context. Other lifecycle APIs in that composable retain their own context requirements. Read the client before async work and reuse the object.
|
|
137
|
-
|
|
138
|
-
### `HTTP_CLIENT: InjectionKey<Client>`
|
|
99
|
+
## Notes
|
|
139
100
|
|
|
140
|
-
|
|
101
|
+
- `Client` is a function. `injectClient()` returns that same function. Pass it straight to `createClientPlugin` or `provide`. Do not pass it to `watch` or `computed` as the source: Vue calls a function source. Do not wrap it in `ref` or `reactive`; the plugin stores the function as-is.
|
|
102
|
+
- Create request-specific clients (SSR data with credentials, cookies, or tenant state) at the application boundary.
|
|
103
|
+
- The owner that creates a client also owns its requests, SSE streams, and WebSocket sessions. App unmount does not clean them up.
|
|
104
|
+
- Import `createClient`, `withEndpoint`, `withInterceptors`, and other client options from `@defjs/core`.
|
|
141
105
|
|
|
142
|
-
##
|
|
106
|
+
## Documentation
|
|
143
107
|
|
|
144
|
-
-
|
|
145
|
-
- 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.
|
|
146
|
-
- Import `createClient`, `withEndpoint`, `withInterceptors`, and all other client options from `@defjs/core`.
|
|
108
|
+
- [Vue API](https://defjs.lyz.cloud/api/vue) · [Vue guide](https://defjs.lyz.cloud/docs/integrations/vue)
|
|
147
109
|
|
|
148
110
|
## License
|
|
149
111
|
|
|
150
|
-
MIT
|
|
112
|
+
MIT.
|
package/index.d.ts
CHANGED
|
@@ -13,9 +13,9 @@ export declare const HTTP_CLIENT: InjectionKey<Client>;
|
|
|
13
13
|
/**
|
|
14
14
|
* Create a Vue plugin that provides an existing Defjs client via {@link HTTP_CLIENT}.
|
|
15
15
|
*
|
|
16
|
-
* Does not create, cache, or dispose the client — pass
|
|
16
|
+
* Does not create, cache, or dispose the client — pass the function from `createClient`. Installing it calls `app.provide(HTTP_CLIENT, client)`, so components read the client with `injectClient()`. The client is stored as that function: it is not wrapped in `ref`, `reactive`, or `markRaw`.
|
|
17
17
|
*
|
|
18
|
-
* @param client - The
|
|
18
|
+
* @param client - The client function to provide to the app.
|
|
19
19
|
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
20
20
|
*
|
|
21
21
|
* @group vue
|
|
@@ -30,11 +30,13 @@ export declare function createClientPlugin(client: Client): Plugin;
|
|
|
30
30
|
*
|
|
31
31
|
* For a custom `InjectionKey<Client>`, use `app.provide(key, client)` or an ancestor’s `provide(key, client)`. Share the same `Symbol` object; matching descriptions do not make keys equal. `createClientPlugin(client)` still takes only the client and provides `HTTP_CLIENT`.
|
|
32
32
|
*
|
|
33
|
-
* It returns the exact client provided for the selected key, and throws an `Error` without an active injection context or a client for the selected key. Installing the plugin alone does not establish an injection context.
|
|
33
|
+
* It returns the exact client function provided for the selected key, and throws an `Error` without an active injection context or a client for the selected key. Installing the plugin alone does not establish an injection context.
|
|
34
|
+
*
|
|
35
|
+
* `Client` is callable. Pass it to `provide` as-is. Do not pass it to `watch` or `computed` as the source: Vue calls a function source. Do not wrap it in `ref` or `reactive`.
|
|
34
36
|
*
|
|
35
37
|
* @param key - Injection key; use the same object with `app.provide` or `provide`.
|
|
36
38
|
* Defaults to `HTTP_CLIENT`, as provided by {@link createClientPlugin}.
|
|
37
|
-
* @returns The injected
|
|
39
|
+
* @returns The injected client function.
|
|
38
40
|
* @throws If called outside a Vue injection context or no client was provided for
|
|
39
41
|
* the requested key. A missing custom key does not fall back to `HTTP_CLIENT`.
|
|
40
42
|
*
|
package/index.js
CHANGED
|
@@ -13,9 +13,9 @@ const HTTP_CLIENT = Symbol("HTTP_CLIENT");
|
|
|
13
13
|
/**
|
|
14
14
|
* Create a Vue plugin that provides an existing Defjs client via {@link HTTP_CLIENT}.
|
|
15
15
|
*
|
|
16
|
-
* Does not create, cache, or dispose the client — pass
|
|
16
|
+
* Does not create, cache, or dispose the client — pass the function from `createClient`. Installing it calls `app.provide(HTTP_CLIENT, client)`, so components read the client with `injectClient()`. The client is stored as that function: it is not wrapped in `ref`, `reactive`, or `markRaw`.
|
|
17
17
|
*
|
|
18
|
-
* @param client - The
|
|
18
|
+
* @param client - The client function to provide to the app.
|
|
19
19
|
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
20
20
|
*
|
|
21
21
|
* @group vue
|
|
@@ -34,11 +34,13 @@ function createClientPlugin(client) {
|
|
|
34
34
|
*
|
|
35
35
|
* For a custom `InjectionKey<Client>`, use `app.provide(key, client)` or an ancestor’s `provide(key, client)`. Share the same `Symbol` object; matching descriptions do not make keys equal. `createClientPlugin(client)` still takes only the client and provides `HTTP_CLIENT`.
|
|
36
36
|
*
|
|
37
|
-
* It returns the exact client provided for the selected key, and throws an `Error` without an active injection context or a client for the selected key. Installing the plugin alone does not establish an injection context.
|
|
37
|
+
* It returns the exact client function provided for the selected key, and throws an `Error` without an active injection context or a client for the selected key. Installing the plugin alone does not establish an injection context.
|
|
38
|
+
*
|
|
39
|
+
* `Client` is callable. Pass it to `provide` as-is. Do not pass it to `watch` or `computed` as the source: Vue calls a function source. Do not wrap it in `ref` or `reactive`.
|
|
38
40
|
*
|
|
39
41
|
* @param key - Injection key; use the same object with `app.provide` or `provide`.
|
|
40
42
|
* Defaults to `HTTP_CLIENT`, as provided by {@link createClientPlugin}.
|
|
41
|
-
* @returns The injected
|
|
43
|
+
* @returns The injected client function.
|
|
42
44
|
* @throws If called outside a Vue injection context or no client was provided for
|
|
43
45
|
* the requested key. A missing custom key does not fall back to `HTTP_CLIENT`.
|
|
44
46
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defjs/vue",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Provide an existing Defjs client to a Vue application.",
|
|
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.5.0",
|
|
18
18
|
"vue": ">=3.0.0"
|
|
19
19
|
},
|
|
20
20
|
"module": "./index.js",
|