2
0

client-app.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* global siteConfig */
  2. import Vue from 'vue'
  3. import VueRouter from 'vue-router'
  4. import VueClipboards from 'vue-clipboards'
  5. import VueSimpleBreakpoints from 'vue-simple-breakpoints'
  6. import VeeValidate from 'vee-validate'
  7. import { ApolloClient } from 'apollo-client'
  8. import { createPersistedQueryLink } from 'apollo-link-persisted-queries'
  9. import { BatchHttpLink } from 'apollo-link-batch-http'
  10. import { ApolloLink, split } from 'apollo-link'
  11. // import { createHttpLink } from 'apollo-link-http'
  12. import { WebSocketLink } from 'apollo-link-ws'
  13. import { ErrorLink } from 'apollo-link-error'
  14. import { InMemoryCache } from 'apollo-cache-inmemory'
  15. import { getMainDefinition } from 'apollo-utilities'
  16. import VueApollo from 'vue-apollo'
  17. import Vuetify from 'vuetify'
  18. import Velocity from 'velocity-animate'
  19. import Hammer from 'hammerjs'
  20. import moment from 'moment'
  21. import VueMoment from 'vue-moment'
  22. import VueTour from 'vue-tour'
  23. import store from './store'
  24. import Cookies from 'js-cookie'
  25. // ====================================
  26. // Load Modules
  27. // ====================================
  28. import boot from './modules/boot'
  29. import localization from './modules/localization'
  30. // ====================================
  31. // Load Helpers
  32. // ====================================
  33. import helpers from './helpers'
  34. // ====================================
  35. // Initialize Global Vars
  36. // ====================================
  37. window.WIKI = null
  38. window.boot = boot
  39. window.Hammer = Hammer
  40. moment.locale(siteConfig.lang)
  41. // ====================================
  42. // Initialize Apollo Client (GraphQL)
  43. // ====================================
  44. const graphQLEndpoint = window.location.protocol + '//' + window.location.host + '/graphql'
  45. const graphQLWSEndpoint = ((window.location.protocol === 'https:') ? 'wss:' : 'ws:') + '//' + window.location.host + '/graphql-subscriptions'
  46. const graphQLLink = ApolloLink.from([
  47. new ErrorLink(({ graphQLErrors, networkError }) => {
  48. if (graphQLErrors) {
  49. graphQLErrors.map(({ message, locations, path }) =>
  50. console.error(
  51. `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
  52. )
  53. )
  54. store.commit('showNotification', {
  55. style: 'red',
  56. message: `An expected error occured.`,
  57. icon: 'warning'
  58. })
  59. }
  60. if (networkError) {
  61. console.error(networkError)
  62. store.commit('showNotification', {
  63. style: 'red',
  64. message: `Network Error: ${networkError.message}`,
  65. icon: 'error'
  66. })
  67. }
  68. }),
  69. createPersistedQueryLink(),
  70. new BatchHttpLink({
  71. includeExtensions: true,
  72. uri: graphQLEndpoint,
  73. credentials: 'include',
  74. fetch: async (uri, options) => {
  75. // Strip __typename fields from variables
  76. let body = JSON.parse(options.body)
  77. body = body.map(bd => {
  78. return ({
  79. ...bd,
  80. variables: JSON.parse(JSON.stringify(bd.variables), (key, value) => { return key === '__typename' ? undefined : value })
  81. })
  82. })
  83. // body = {
  84. // ...body,
  85. // variables: JSON.parse(JSON.stringify(body.variables), (key, value) => { return key === '__typename' ? undefined : value })
  86. // }
  87. options.body = JSON.stringify(body)
  88. // Inject authentication token
  89. const jwtToken = Cookies.get('jwt')
  90. if (jwtToken) {
  91. options.headers.Authorization = `Bearer ${jwtToken}`
  92. }
  93. const resp = await fetch(uri, options)
  94. // Handle renewed JWT
  95. const newJWT = resp.headers.get('new-jwt')
  96. if (newJWT) {
  97. Cookies.set('jwt', newJWT, { expires: 365 })
  98. }
  99. return resp
  100. }
  101. })
  102. ])
  103. const graphQLWSLink = new WebSocketLink({
  104. uri: graphQLWSEndpoint,
  105. options: {
  106. reconnect: true,
  107. lazy: true
  108. }
  109. })
  110. window.graphQL = new ApolloClient({
  111. link: split(({ query }) => {
  112. const { kind, operation } = getMainDefinition(query)
  113. return kind === 'OperationDefinition' && operation === 'subscription'
  114. }, graphQLWSLink, graphQLLink),
  115. cache: new InMemoryCache(),
  116. connectToDevTools: (process.env.node_env === 'development')
  117. })
  118. // ====================================
  119. // Initialize Vue Modules
  120. // ====================================
  121. Vue.config.productionTip = false
  122. Vue.use(VueRouter)
  123. Vue.use(VueApollo)
  124. Vue.use(VueClipboards)
  125. Vue.use(VueSimpleBreakpoints)
  126. Vue.use(localization.VueI18Next)
  127. Vue.use(helpers)
  128. Vue.use(VeeValidate, { events: '' })
  129. Vue.use(Vuetify)
  130. Vue.use(VueMoment, { moment })
  131. Vue.use(VueTour)
  132. Vue.prototype.Velocity = Velocity
  133. // ====================================
  134. // Register Vue Components
  135. // ====================================
  136. Vue.component('admin', () => import(/* webpackChunkName: "admin" */ './components/admin.vue'))
  137. Vue.component('editor', () => import(/* webpackPrefetch: -100, webpackChunkName: "editor" */ './components/editor.vue'))
  138. Vue.component('history', () => import(/* webpackChunkName: "history" */ './components/history.vue'))
  139. Vue.component('login', () => import(/* webpackPrefetch: true, webpackChunkName: "login" */ './components/login.vue'))
  140. Vue.component('nav-header', () => import(/* webpackMode: "eager" */ './components/common/nav-header.vue'))
  141. Vue.component('page-selector', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/page-selector.vue'))
  142. Vue.component('profile', () => import(/* webpackChunkName: "profile" */ './components/profile.vue'))
  143. Vue.component('v-card-chin', () => import(/* webpackPrefetch: true, webpackChunkName: "ui-extra" */ './components/common/v-card-chin.vue'))
  144. Vue.component('nav-footer', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-footer.vue'))
  145. Vue.component('nav-sidebar', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/nav-sidebar.vue'))
  146. Vue.component('page', () => import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/components/page.vue'))
  147. let bootstrap = () => {
  148. // ====================================
  149. // Notifications
  150. // ====================================
  151. window.addEventListener('beforeunload', () => {
  152. store.dispatch('startLoading')
  153. })
  154. const apolloProvider = new VueApollo({
  155. defaultClient: window.graphQL
  156. })
  157. // ====================================
  158. // Bootstrap Vue
  159. // ====================================
  160. const i18n = localization.init()
  161. window.WIKI = new Vue({
  162. el: '#root',
  163. components: {},
  164. mixins: [helpers],
  165. apolloProvider,
  166. store,
  167. i18n
  168. })
  169. // ----------------------------------
  170. // Dispatch boot ready
  171. // ----------------------------------
  172. window.boot.notify('vue')
  173. // ====================================
  174. // Load theme-specific code
  175. // ====================================
  176. import(/* webpackChunkName: "theme-page" */ './themes/' + process.env.CURRENT_THEME + '/js/app.js')
  177. // ====================================
  178. // Load Icons
  179. // ====================================
  180. // import(/* webpackChunkName: "icons" */ './svg/icons.svg').then(icons => {
  181. // document.body.insertAdjacentHTML('beforeend', icons.default)
  182. // })
  183. }
  184. window.boot.onDOMReady(bootstrap)