-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5193ba1
commit 4cef1a8
Showing
18 changed files
with
286 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ dist | |
coverage | ||
|
||
# Plugin | ||
templates/plugin.js | ||
src/runtime/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
preset: '@nuxt/test-utils', | ||
collectCoverageFrom: ['src/**'] | ||
collectCoverageFrom: ['src/**', '!src/runtime/**'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import BaseEcho from 'laravel-echo' | ||
import defu from 'defu' | ||
import { Context } from '@nuxt/types' | ||
import { ModuleOptions } from './types' | ||
|
||
export class Echo extends BaseEcho { | ||
ctx: Context; | ||
config: Partial<ModuleOptions>; | ||
|
||
constructor (ctx: Context, config: Partial<ModuleOptions> = {}) { | ||
// when enabled authModule, start broadcaster with null | ||
// because laravel-echo auto connect https://github.com/laravel/echo/blob/master/src/echo.ts#L23 | ||
// the connection should be made only when the user logs in | ||
super(defu(config.authModule && config.connectOnLogin ? { broadcaster: 'null' } : {}, config)) | ||
|
||
this.ctx = ctx | ||
this.ctx.$config = this.ctx.$config || {} // fallback for Nuxt < 2.13 | ||
this.config = defu(this.ctx.$config.echo || {}, { auth: { headers: {} } }, config) | ||
} | ||
|
||
async init () { | ||
this.options.auth = this.options.auth || {} | ||
this.options.auth.headers = await this.getHeaders() | ||
this.watchAuthState() | ||
} | ||
|
||
async getHeaders () { | ||
let headers = this.config.auth.headers || {} | ||
|
||
if (typeof headers === 'function') { | ||
headers = await headers(this.ctx) | ||
} | ||
|
||
if (this.config.authModule && this.ctx.app.$auth) { | ||
const strategy = this.ctx.app.$auth.strategy | ||
|
||
if (strategy.options.name === 'laravelSanctum') { | ||
headers.referer = location.origin | ||
headers['X-XSRF-TOKEN'] = this.ctx.app.$auth.$storage.getCookies()['XSRF-TOKEN'] | ||
} else { | ||
const tokenName = strategy.options.token.name || 'Authorization' | ||
const token = strategy.token.get() | ||
|
||
if (token) { | ||
headers[tokenName] = token | ||
} | ||
} | ||
} | ||
|
||
return defu(headers, this.options.auth.headers) | ||
} | ||
|
||
watchAuthState () { | ||
if (this.config.authModule && this.ctx.app.$auth) { | ||
this.ctx.app.$auth.$storage.watchState('loggedIn', async (loggedIn: boolean) => { | ||
this.options.auth.headers = await this.getHeaders() | ||
|
||
if (this.config.connectOnLogin && loggedIn) { | ||
// set broadcaster when user logged in | ||
this.options.broadcaster = this.config.broadcaster | ||
this.connect() | ||
} | ||
|
||
if (this.config.disconnectOnLogout && !loggedIn && this.connector) { | ||
this.disconnect() | ||
} | ||
}).bind(this) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Echo } from './echo' | ||
import { ModuleOptions } from './types' | ||
|
||
declare module '@nuxt/types' { | ||
interface NuxtAppOptions { | ||
$echo: Echo; | ||
} | ||
|
||
interface Context { | ||
$echo: Echo; | ||
} | ||
|
||
interface NuxtConfig { | ||
echo?: Partial<ModuleOptions> | ||
} | ||
|
||
interface Configuration { | ||
echo?: Partial<ModuleOptions> | ||
} | ||
} | ||
|
||
declare module 'vue/types/vue' { | ||
interface Vue { | ||
$echo: Echo | ||
} | ||
} | ||
|
||
declare module 'vuex/types/index' { | ||
// eslint-disable-next-line no-unused-vars,@typescript-eslint/no-unused-vars | ||
interface Store<S> { | ||
$echo: Echo | ||
} | ||
} | ||
|
||
export * from './echo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default <%= serializeFunction(options) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import options from './options' | ||
import { Echo } from '~echo' | ||
|
||
/** | ||
* @type {import('@nuxt/types').Plugin} | ||
*/ | ||
export default async function (ctx, inject) { | ||
const echoOptions = typeof options === 'function' ? await options(ctx) : options | ||
|
||
if (process.client) { | ||
<% if (options.broadcaster === 'pusher' && !options.encrypted) { %> | ||
if (!window.Pusher) window.Pusher = require('pusher-js') | ||
<% } %> | ||
|
||
<% if (options.broadcaster === 'pusher' && options.encrypted) { %> | ||
if (!window.Pusher) window.Pusher = require('pusher-js/with-encryption') | ||
<% } %> | ||
|
||
<% if (options.broadcaster === 'socket.io') { %> | ||
if (!window.io) window.io = require('socket.io-client') | ||
<% } %> | ||
} | ||
|
||
const echo = new Echo(ctx, echoOptions) | ||
await echo.init() | ||
|
||
inject('echo', echo) | ||
ctx.$echo = echo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type EchoOptions = Record<string, any> | ||
|
||
export interface ModuleOptions extends EchoOptions { | ||
broadcaster?: string, | ||
encrypted?: boolean, | ||
plugins?: string[], | ||
authModule?: boolean, | ||
connectOnLogin?: boolean, | ||
disconnectOnLogout?: boolean, | ||
optionsPath?: string, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
import { NuxtConfig } from '@nuxt/types' | ||
|
||
export default <NuxtConfig> { | ||
buildModules: [ | ||
'@nuxt/typescript-build', | ||
'../../../src/module.ts' | ||
] | ||
} |
Oops, something went wrong.