fix(ws): resolve a function `url` lazily — don't invoke the getter at construction
createWsClient eagerly invoked a function-form `config.url` to print a dev-time
URL-validation warning. For a module-scope singleton built with
`createVueWsClient({ url: () => useRuntimeConfig().public... })`, that called
useRuntimeConfig() at import time — outside any Nuxt context — and threw
"A composable that requires access to the Nuxt instance was called outside of a
plugin, Nuxt hook, Nuxt middleware, or Vue setup function" (regression vs 0.2.x).
Now only STRING urls are validated eagerly; a function url stays lazy and is
resolved in _getUrl() at connect time (as in 0.2.x). Bump 0.3.1.
This commit is contained in:
parent
1d8f43148f
commit
c21553536f
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@blax-software/networking",
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"description": "Plug-and-play API + WebSocket client. Framework-agnostic core with optional Vue, Nuxt, and React bindings.",
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
|
|
|
|||
36
src/ws.ts
36
src/ws.ts
|
|
@ -642,20 +642,28 @@ export function createWsClient(
|
|||
: (config.isServer ?? false)
|
||||
if (isServer) return createSsrStub(createRef)
|
||||
|
||||
// Validate configuration and warn developers about common issues
|
||||
const url = typeof config.url === 'function' ? config.url() : config.url
|
||||
if (!url || url === 'wss:///app/' || url === 'ws:///app/') {
|
||||
console.error(
|
||||
'[blax-networking] WebSocket URL is empty or malformed: ' + JSON.stringify(url) + '\n' +
|
||||
'Ensure WEBS_URL and PUSHER_APP_KEY are configured. ' +
|
||||
'Expected format: wss://your-ws-host/app/{appKey}',
|
||||
)
|
||||
} else if (url.endsWith('/app/') || url.endsWith('/app')) {
|
||||
console.error(
|
||||
'[blax-networking] WebSocket URL is missing the app key: ' + url + '\n' +
|
||||
'Ensure PUSHER_APP_KEY is set. The URL must end with /app/{appKey} ' +
|
||||
'where {appKey} matches the PUSHER_APP_KEY on the backend.',
|
||||
)
|
||||
// Validate configuration and warn developers about common issues.
|
||||
// IMPORTANT: only resolve a STRING url here. A function url is lazy by design —
|
||||
// it may depend on a runtime context that does not exist yet at construction
|
||||
// time (e.g. Nuxt's `useRuntimeConfig()`, which throws when called outside a
|
||||
// plugin/setup/lifecycle). Calling it eagerly here crashed module-scope
|
||||
// singletons built with `createVueWsClient({ url: () => useRuntimeConfig()... })`.
|
||||
// Function urls are resolved lazily at connect time instead.
|
||||
if (typeof config.url === 'string') {
|
||||
const url = config.url
|
||||
if (!url || url === 'wss:///app/' || url === 'ws:///app/') {
|
||||
console.error(
|
||||
'[blax-networking] WebSocket URL is empty or malformed: ' + JSON.stringify(url) + '\n' +
|
||||
'Ensure WEBS_URL and PUSHER_APP_KEY are configured. ' +
|
||||
'Expected format: wss://your-ws-host/app/{appKey}',
|
||||
)
|
||||
} else if (url.endsWith('/app/') || url.endsWith('/app')) {
|
||||
console.error(
|
||||
'[blax-networking] WebSocket URL is missing the app key: ' + url + '\n' +
|
||||
'Ensure PUSHER_APP_KEY is set. The URL must end with /app/{appKey} ' +
|
||||
'where {appKey} matches the PUSHER_APP_KEY on the backend.',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return new WsClientImpl(config, createRef)
|
||||
|
|
|
|||
Loading…
Reference in New Issue