Skip to content

Commit 222e658

Browse files
chore: prevent injecting plugins if URL is not supplied (#19)
* chore: prevent injecting plugins if URL is not supplied * chore: notify the user via error log if URL is missing
1 parent dfff575 commit 222e658

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

example/nuxt.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export default {
22
buildModules: ['@nuxtjs/tailwindcss'],
3-
modules: ['@nuxt/typescript-build', '../src/module.ts']
3+
modules: ['@nuxt/typescript-build', '../src/module.ts'],
4+
websocket: {
5+
url: 'wss://echo.websocket.events/'
6+
}
47
}

example/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<button
7070
:disabled="isSendDisabled"
7171
class="btn btn-blue"
72-
:class="{ 'cursor-not-allowed opacity-50': isSendDisabled}"
72+
:class="{ 'cursor-not-allowed opacity-50': isSendDisabled }"
7373
@click.prevent="sendMessage"
7474
>
7575
Send message

src/templates/plugin.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ import WebSocketManager from './WebSocketManager'
1717

1818
const reconnectInterval = Number('<%= options.reconnectInterval %>') || 1000
1919
const urlFromOptions = '<%= options.url %>'
20+
2021
export default ({ app }: { app: NuxtAppOptions }, inject: Inject): void => {
2122
/* istanbul ignore next */
2223
const runtimeConfig = (app.$config && app.$config.websocket) || {}
2324

2425
/* istanbul ignore next */
25-
const url =
26-
runtimeConfig.url || urlFromOptions || 'wss://echo.websocket.events/'
26+
const url = runtimeConfig.url || urlFromOptions
27+
28+
/* istanbul ignore next */
29+
if (!url) {
30+
// eslint-disable-next-line no-console
31+
return console.error(
32+
'WebSocket connection URL is required. Please specify it via options or runtime configuration.'
33+
)
34+
}
2735

2836
const emitter = new Vue()
2937
const manager = new WebSocketManager(url, emitter, reconnectInterval)

test/module.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { expectModuleToBeCalledWith, setupTest } from '@nuxt/test-utils'
1+
import {
2+
expectModuleToBeCalledWith,
3+
getNuxt,
4+
setupTest
5+
} from '@nuxt/test-utils'
26

37
describe('module', () => {
48
setupTest({
@@ -10,15 +14,15 @@ describe('module', () => {
1014
expectModuleToBeCalledWith('addPlugin', {
1115
src: 'src/templates/plugin.ts',
1216
fileName: 'nuxt-websocket/websocket.client.ts',
13-
options: {}
17+
options: getNuxt().options.websocket
1418
})
1519
})
1620

1721
test('should render the template', () => {
1822
expectModuleToBeCalledWith('addTemplate', {
1923
src: 'src/templates/WebSocketManager.ts',
2024
fileName: 'nuxt-websocket/WebSocketManager.ts',
21-
options: {}
25+
options: getNuxt().options.websocket
2226
})
2327
})
2428
})

0 commit comments

Comments
 (0)