Vue plugin, default or custom injection keys, and injectClient.
Vue
Provide an existing @defjs/core client to a Vue app. This package does not create, cache, or dispose the client. Vue 3 or later is required.
See the Vue guide.
createClientPlugin()
function createClientPlugin(client: DerivableClient): Plugin;- client — Instance from
createClient. - Returns a Vue plugin for
app.use(...). Installing it callsapp.provide(HTTP_CLIENT, client).
import { createClient, withEndpoint } from "@defjs/core";
import { createClientPlugin } from "@defjs/vue";
import { createApp } from "vue";
const client = createClient(withEndpoint("https://api.example.com"));
const app = createApp(App);
app.use(createClientPlugin(client));injectClient()
function injectClient(
key?: InjectionKey<DerivableClient>,
): DerivableClient;injectClient(key) reads the nearest provider for that key; omitted or undefined uses HTTP_CLIENT. Call in a valid Vue injection context: synchronous setup() or app.runWithContext() (Vue 3.3+). The latter supplies only synchronous app-level context, not subtree overrides or context across ordinary await. Compiler-restored contexts after top-level await in <script setup> remain valid. Pass that client into an operation. The injected value is the DerivableClient that was provided; call it with request options where the operation runs to derive a Client value. Do not use it as a watch or computed source, because those call a function. A derived Client value is not accepted by the plugin or injectClient.
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.
- Returns the exact client provided for the selected key.
- Throws an
Errorwithout an active injection context or a client for the selected key. A missing custom key never falls back toHTTP_CLIENT; installing the plugin alone does not establish an injection context.
HTTP_CLIENT
const HTTP_CLIENT: InjectionKey<DerivableClient>;Vue injection key. Prefer injectClient() over inject(HTTP_CLIENT). Override a subtree with provide(HTTP_CLIENT, childClient).