injectClient
Vue composable that injects the Defjs client for a key, defaulting to HTTP_CLIENT.
@defjs/vue · Function
Signature
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(), 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.
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.
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.
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.
Parameters
Prop
Type
key? InjectionKey<DerivableClient>Injection key; use the same object with app.provide or provide.
Defaults to HTTP_CLIENT, as provided by createClientPlugin.
Returns
The injected derivable client.
Throws
If called outside a Vue injection context or no client was provided for
the requested key. A missing custom key does not fall back to HTTP_CLIENT.
Examples
const client = injectClient()