@defjs/vue 0.0.1 → 0.0.3
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 +15 -13
- package/index.js +11 -9
- 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.6.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({ signal: 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?): DerivableClient`
|
|
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<DerivableClient>` 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
|
+
- `injectClient()` returns the `DerivableClient` that was provided, a function. Call it with request options where the operation runs to derive a `Client` value. 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 it as-is. A derived `Client` value is not accepted.
|
|
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
|
@@ -1,40 +1,42 @@
|
|
|
1
1
|
import { InjectionKey, Plugin } from "vue";
|
|
2
|
-
import {
|
|
2
|
+
import { DerivableClient } from "@defjs/core";
|
|
3
3
|
/**
|
|
4
|
-
* Vue injection key for the Defjs {@link
|
|
4
|
+
* Vue injection key for the Defjs {@link DerivableClient} provided by {@link createClientPlugin}.
|
|
5
5
|
*
|
|
6
6
|
* Prefer {@link injectClient} over calling `inject(HTTP_CLIENT)` directly. `inject(HTTP_CLIENT)` returns `undefined` when nothing was provided. Override a subtree with `provide(HTTP_CLIENT, childClient)`.
|
|
7
7
|
*
|
|
8
8
|
* @group vue
|
|
9
|
-
* @see {@link
|
|
9
|
+
* @see {@link DerivableClient}
|
|
10
10
|
* @see {@link injectClient}
|
|
11
11
|
*/
|
|
12
|
-
export declare const HTTP_CLIENT: InjectionKey<
|
|
12
|
+
export declare const HTTP_CLIENT: InjectionKey<DerivableClient>;
|
|
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 client returned by `createClient`. Installing it calls `app.provide(HTTP_CLIENT, client)`, so components read the client with `injectClient()`. The client is stored as given: it is not wrapped in `ref`, `reactive`, or `markRaw`.
|
|
17
17
|
*
|
|
18
|
-
* @param client - The
|
|
18
|
+
* @param client - The client returned by `createClient` to provide to the app.
|
|
19
19
|
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
20
20
|
*
|
|
21
21
|
* @group vue
|
|
22
|
-
* @see {@link
|
|
22
|
+
* @see {@link DerivableClient}
|
|
23
23
|
* @exampleFile examples/api/create-client-plugin.ts#example
|
|
24
24
|
*/
|
|
25
|
-
export declare function createClientPlugin(client:
|
|
25
|
+
export declare function createClientPlugin(client: DerivableClient): Plugin;
|
|
26
26
|
/**
|
|
27
27
|
* Vue composable that injects the Defjs client for a key, defaulting to {@link HTTP_CLIENT}.
|
|
28
28
|
*
|
|
29
29
|
* `injectClient(key)` reads the nearest provider for that key; omitted or `undefined` uses `HTTP_CLIENT`. Call in a valid Vue injection context: synchronous `setup()`, compiler-restored `<script setup>` after top-level `await`, or a synchronous `app.runWithContext()` callback (Vue 3.3+). `app.runWithContext()` supplies only synchronous app-level context, not subtree overrides or context across ordinary `await`.
|
|
30
30
|
*
|
|
31
|
-
* For a custom `InjectionKey<
|
|
31
|
+
* For a custom `InjectionKey<DerivableClient>`, 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 value 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
|
+
* The injected `DerivableClient` is a function. Call it with request options at the call site to derive a `Client` value for one call: `getUser(client({ signal }), input)`. 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 provider holds only a root client; a derived `Client` value is not accepted, so create it where the operation is called.
|
|
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 derivable client.
|
|
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
|
*
|
|
@@ -43,6 +45,6 @@ export declare function createClientPlugin(client: Client): Plugin;
|
|
|
43
45
|
* const client = injectClient()
|
|
44
46
|
* ```
|
|
45
47
|
* @group vue
|
|
46
|
-
* @see {@link
|
|
48
|
+
* @see {@link DerivableClient}
|
|
47
49
|
*/
|
|
48
|
-
export declare function injectClient(key?: InjectionKey<
|
|
50
|
+
export declare function injectClient(key?: InjectionKey<DerivableClient>): DerivableClient;
|
package/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import { inject } from "vue";
|
|
2
2
|
// src/core.ts
|
|
3
3
|
/**
|
|
4
|
-
* Vue injection key for the Defjs {@link
|
|
4
|
+
* Vue injection key for the Defjs {@link DerivableClient} provided by {@link createClientPlugin}.
|
|
5
5
|
*
|
|
6
6
|
* Prefer {@link injectClient} over calling `inject(HTTP_CLIENT)` directly. `inject(HTTP_CLIENT)` returns `undefined` when nothing was provided. Override a subtree with `provide(HTTP_CLIENT, childClient)`.
|
|
7
7
|
*
|
|
8
8
|
* @group vue
|
|
9
|
-
* @see {@link
|
|
9
|
+
* @see {@link DerivableClient}
|
|
10
10
|
* @see {@link injectClient}
|
|
11
11
|
*/
|
|
12
12
|
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 client returned by `createClient`. Installing it calls `app.provide(HTTP_CLIENT, client)`, so components read the client with `injectClient()`. The client is stored as given: it is not wrapped in `ref`, `reactive`, or `markRaw`.
|
|
17
17
|
*
|
|
18
|
-
* @param client - The
|
|
18
|
+
* @param client - The client returned by `createClient` to provide to the app.
|
|
19
19
|
* @returns A Vue `Plugin` for `app.use(...)`.
|
|
20
20
|
*
|
|
21
21
|
* @group vue
|
|
22
|
-
* @see {@link
|
|
22
|
+
* @see {@link DerivableClient}
|
|
23
23
|
* @exampleFile examples/api/create-client-plugin.ts#example
|
|
24
24
|
*/
|
|
25
25
|
function createClientPlugin(client) {
|
|
@@ -32,13 +32,15 @@ function createClientPlugin(client) {
|
|
|
32
32
|
*
|
|
33
33
|
* `injectClient(key)` reads the nearest provider for that key; omitted or `undefined` uses `HTTP_CLIENT`. Call in a valid Vue injection context: synchronous `setup()`, compiler-restored `<script setup>` after top-level `await`, or a synchronous `app.runWithContext()` callback (Vue 3.3+). `app.runWithContext()` supplies only synchronous app-level context, not subtree overrides or context across ordinary `await`.
|
|
34
34
|
*
|
|
35
|
-
* For a custom `InjectionKey<
|
|
35
|
+
* For a custom `InjectionKey<DerivableClient>`, 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 value 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
|
+
* The injected `DerivableClient` is a function. Call it with request options at the call site to derive a `Client` value for one call: `getUser(client({ signal }), input)`. 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 provider holds only a root client; a derived `Client` value is not accepted, so create it where the operation is called.
|
|
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 derivable client.
|
|
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
|
*
|
|
@@ -47,7 +49,7 @@ function createClientPlugin(client) {
|
|
|
47
49
|
* const client = injectClient()
|
|
48
50
|
* ```
|
|
49
51
|
* @group vue
|
|
50
|
-
* @see {@link
|
|
52
|
+
* @see {@link DerivableClient}
|
|
51
53
|
*/
|
|
52
54
|
function injectClient(key = HTTP_CLIENT) {
|
|
53
55
|
const client = inject(key);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defjs/vue",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
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.6.0",
|
|
18
18
|
"vue": ">=3.0.0"
|
|
19
19
|
},
|
|
20
20
|
"module": "./index.js",
|