-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathplugin.js
More file actions
165 lines (140 loc) · 6.27 KB
/
plugin.js
File metadata and controls
165 lines (140 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Vue from 'vue'
import VueApollo from 'vue-apollo'
import 'cross-fetch/polyfill'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client/src'
import Cookie from 'universal-cookie'
import { InMemoryCache } from 'apollo-cache-inmemory'
Vue.use(VueApollo)
export default (ctx, inject) => {
const providerOptions = { clients: {} }
const { app, beforeNuxtRender, req } = ctx
const AUTH_TOKEN_NAME = '<%= options.tokenName %>'
const COOKIE_ATTRIBUTES = <%= serialize(options.cookieAttributes) %>
const AUTH_TYPE = '<%= options.authenticationType %> '
const cookies = new Cookie(req && req.headers.cookie)
// Config
<% Object.keys(options.clientConfigs).forEach((key) => { %>
const <%= key %>TokenName = '<%= options.clientConfigs[key].tokenName %>' || AUTH_TOKEN_NAME
function <%= key %>GetAuth () {
const token = cookies.get(<%= key %>TokenName)
return token && <%= key %>ClientConfig.validateToken(token) ? AUTH_TYPE + token : ''
}
let <%= key %>ClientConfig
<% if (typeof options.clientConfigs[key] === 'object') { %>
<%= key %>ClientConfig = <%= JSON.stringify(options.clientConfigs[key], null, 2) %>
<% } else if (typeof options.clientConfigs[key] === 'string') { %>
<%= key %>ClientConfig = require('<%= options.clientConfigs[key] %>')
if ('default' in <%= key %>ClientConfig) {
<%= key %>ClientConfig = <%= key %>ClientConfig.default
}
<%= key %>ClientConfig = <%= key %>ClientConfig(ctx)
<% } %>
const <%= key %>ValidateToken = () => true
if (!<%= key %>ClientConfig.validateToken) {
<%= key %>ClientConfig.validateToken = <%= key %>ValidateToken
}
const <%= key %>Cache = <%= key %>ClientConfig.cache
? <%= key %>ClientConfig.cache
: new InMemoryCache(<%= key %>ClientConfig.inMemoryCacheOptions ? <%= key %>ClientConfig.inMemoryCacheOptions: undefined)
if (!process.server) {
<%= key %>Cache.restore(window.__NUXT__ && window.__NUXT__.apollo ? window.__NUXT__.apollo.<%= key === 'default' ? 'defaultClient' : key %> : null)
}
if (!<%= key %>ClientConfig.getAuth) {
<%= key %>ClientConfig.getAuth = <%= key %>GetAuth
}
if (process.client && <%= key %>ClientConfig.browserHttpEndpoint) {
<%= key %>ClientConfig.httpEndpoint = <%= key %>ClientConfig.browserHttpEndpoint
}
<% if (options.defaultOptions) { %>
<%= key %>ClientConfig.apollo = { defaultOptions: <%= JSON.stringify(options.defaultOptions) %> }
<% } %>
<%= key %>ClientConfig.ssr = !!process.server
<%= key %>ClientConfig.cache = <%= key %>Cache
<%= key %>ClientConfig.tokenName = <%= key %>TokenName
// if ssr we'd still like to have our webclient's cookies
if (process.server && req && req.headers && req.headers.cookie) {
if (!<%= key %>ClientConfig.httpLinkOptions) {
<%= key %>ClientConfig.httpLinkOptions = {}
}
if (!<%= key %>ClientConfig.httpLinkOptions.headers) {
<%= key %>ClientConfig.httpLinkOptions.headers = {}
}
<%= key %>ClientConfig.httpLinkOptions.headers.cookie = req.headers.cookie
}
// Create apollo client
let <%= key %>ApolloCreation = createApolloClient({
...<%= key %>ClientConfig
})
<%= key %>ApolloCreation.apolloClient.wsClient = <%= key %>ApolloCreation.wsClient
<% if (key === 'default') { %>
providerOptions.<%= key %>Client = <%= key %>ApolloCreation.apolloClient
<% } else { %>
providerOptions.clients.<%= key %> = <%= key %>ApolloCreation.apolloClient
<% } %>
<% }) %>
const vueApolloOptions = Object.assign(providerOptions, {
<% if (options.defaultOptions) { %>
defaultOptions: <%= JSON.stringify(options.defaultOptions) %>,
<% } %>
<% if (options.watchLoading) { %>
watchLoading (isLoading, countModifier) {
return require('<%= options.watchLoading %>').default(isLoading, countModifier, ctx)
},
<% } %>
errorHandler (error) {
<% if (options.errorHandler) { %>
return require('<%= options.errorHandler %>').default(error, ctx)
<% } else { %>
console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message)
<% } %>
}
})
const apolloProvider = new VueApollo(vueApolloOptions)
// Allow access to the provider in the context
app.apolloProvider = apolloProvider
inject('theApolloProvider', apolloProvider);
if (process.server) {
const ApolloSSR = require('vue-apollo/ssr')
beforeNuxtRender(({ nuxtState }) => {
nuxtState.apollo = ApolloSSR.getStates(apolloProvider)
})
}
inject('apolloHelpers', {
onLogin: async (token, apolloClient = apolloProvider.defaultClient, cookieAttributes = COOKIE_ATTRIBUTES, skipResetStore = false) => {
// Fallback for tokenExpires param
if (typeof cookieAttributes === 'number') cookieAttributes = { expires: cookieAttributes }
if (typeof cookieAttributes.expires === 'number') {
cookieAttributes.expires = new Date(Date.now()+ 86400*1000*cookieAttributes.expires)
}
if (token) {
cookies.set(AUTH_TOKEN_NAME, token, cookieAttributes)
} else {
cookies.remove(AUTH_TOKEN_NAME, cookieAttributes)
}
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
if (!skipResetStore) {
try {
await apolloClient.resetStore()
} catch (e) {
// eslint-disable-next-line no-console
console.log('%cError on cache reset (setToken)', 'color: orange;', e.message)
}
}
},
onLogout: async (apolloClient = apolloProvider.defaultClient, skipResetStore = false) => {
cookies.remove(AUTH_TOKEN_NAME, COOKIE_ATTRIBUTES)
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
if (!skipResetStore) {
try {
await apolloClient.resetStore()
} catch (e) {
// eslint-disable-next-line no-console
console.log('%cError on cache reset (logout)', 'color: orange;', e.message)
}
}
},
getToken: (tokenName = AUTH_TOKEN_NAME) => {
return cookies.get(tokenName)
}
})
}