From 311e911523192f2537a12003c7559bf6549612b4 Mon Sep 17 00:00:00 2001 From: Anton Schubert Date: Wed, 30 Aug 2017 13:28:19 +0200 Subject: [PATCH] add some eslint-config - works fine for js now, reindent main --- .eslintrc | 23 ++- package.json | 1 + src/main.js | 368 +++++++++++++++++++++++----------------------- webpack.config.js | 1 + 4 files changed, 205 insertions(+), 188 deletions(-) diff --git a/.eslintrc b/.eslintrc index 7003deb..71c5252 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,8 +1,23 @@ { - "parser": "babel-eslint", - "rules": { + "parserOptions": { + "parser": "babel-eslint", + "ecmaVersion": 6, + "sourceType": "module", + "impliedStrict": true + }, + "env": { + "browser": true, + "es6": true }, "plugins": [ - "html" - ] + "html", + ], + extends: [ + "eslint:recommended", + "plugin:vue/recommended" + ], + "rules": { + "indent": ["error", 4], + "no-console": 0 + }, } \ No newline at end of file diff --git a/package.json b/package.json index 63b768a..efd4952 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "eslint": "^4.5.0", "eslint-loader": "^1.9.0", "eslint-plugin-html": "^3.2.0", + "eslint-plugin-vue": "^3.12.0", "less": "^2.7.2", "less-loader": "^4.0.5", "vue-hot-reload-api": "^2.1.0", diff --git a/src/main.js b/src/main.js index 2eb0345..3f76e4c 100644 --- a/src/main.js +++ b/src/main.js @@ -13,212 +13,212 @@ Vue.use(Vuex); const router = new VueRouter(); const store = new Vuex.Store({ - state: { - api: {}, - navigation: [], - persist: { - login: {}, - authToken: '', - jwt: {}, - credentials: {} - } - }, - mutations: { - setAPI(state, payload) { - state.api = payload; + state: { + api: {}, + navigation: [], + persist: { + login: {}, + authToken: '', + jwt: {}, + credentials: {} + } }, - setNavigation(state, payload) { - for (var i = 0; i < payload.length; i++) { - state.navigation.push(payload[i]); // = payload; - } - }, - setCredentials(state, payload) { - state.persist.credentials = { - username: payload.username, - password: payload.password - }; - objectToPersist(state.persist, 'persistantStore'); - }, - setLogin(state, payload) { - state.persist.login = payload.User; - state.persist.authToken = payload.AuthToken; - state.persist.jwt = JwtDecode(payload.AuthToken); - objectToPersist(state.persist, 'persistantStore'); - }, - clearLogin(state) { - state.persist.login = {}; - state.persist.authToken = ''; - state.persist.jwt = {}; - state.persist.credentials = {}; - objectToPersist(state.persist, 'persistantStore'); - } - }, - actions: { - apiRequest(context, payload) { - let doRequest = () => { - return new Promise((resolve, reject) => { - Axios({ - method: payload.method ? payload.method : 'get', - baseURL: context.state.api.baseURL, - url: payload.endpoint, - params: payload.params, - data: payload.data, - headers: { - 'X-Auth-Token': context.state.persist.authToken + mutations: { + setAPI(state, payload) { + state.api = payload; + }, + setNavigation(state, payload) { + for (var i = 0; i < payload.length; i++) { + state.navigation.push(payload[i]); // = payload; } - }) - .then(response => { - console.log(response); - resolve(response.data); - }) - .catch(error => { - console.dir(error); - reject(error); - }) - }); - }; - - if (payload.endpoint != 'login') { - // no jwt check for login call - - // empty username = not logged in - if (!context.state.persist.credentials.username) { - return new Promise((resolve, reject) => { - // show login page - router.push('/login'); - reject(['not logged in']); - }); + }, + setCredentials(state, payload) { + state.persist.credentials = { + username: payload.username, + password: payload.password + }; + objectToPersist(state.persist, 'persistantStore'); + }, + setLogin(state, payload) { + state.persist.login = payload.User; + state.persist.authToken = payload.AuthToken; + state.persist.jwt = JwtDecode(payload.AuthToken); + objectToPersist(state.persist, 'persistantStore'); + }, + clearLogin(state) { + state.persist.login = {}; + state.persist.authToken = ''; + state.persist.jwt = {}; + state.persist.credentials = {}; + objectToPersist(state.persist, 'persistantStore'); } - - let now = Math.round(Date.now() / 1000); - if (context.state.persist.jwt.exp < (now + 300)) { - // too old jwt, logout - return new Promise((resolve, reject) => { - context.commit('clearLogin'); - router.push('/login'); - reject(['jwt too old, logout']); - }); - } - - if ((context.state.persist.jwt.exp - 60) < now) { - // jwt near expire, get new one - console.log("getting new jwt"); - - return context.dispatch('login', context.state.persist.credentials) - .then(() => { - console.log("LOOOGIIIINNN"); - return doRequest(); - }) - .catch(error => { - throw error; - }) - } - } - - return doRequest(); }, - login(context, payload) { - return new Promise((resolve, reject) => { - context.dispatch('apiRequest', { - method: 'post', - endpoint: 'login', - data: payload - }) - .then(data => { - context.commit('setCredentials', payload); - context.commit('setLogin', data); - resolve(data.User); - }) - .catch(error => { - if (error.response && error.response.data && error.response.data.error) { - reject(error.response.data.error); - } else { - reject([]); + actions: { + apiRequest(context, payload) { + let doRequest = () => { + return new Promise((resolve, reject) => { + Axios({ + method: payload.method ? payload.method : 'get', + baseURL: context.state.api.baseURL, + url: payload.endpoint, + params: payload.params, + data: payload.data, + headers: { + 'X-Auth-Token': context.state.persist.authToken + } + }) + .then(response => { + console.log(response); + resolve(response.data); + }) + .catch(error => { + console.dir(error); + reject(error); + }) + }); + }; + + if (payload.endpoint != 'login') { + // no jwt check for login call + + // empty username = not logged in + if (!context.state.persist.credentials.username) { + return new Promise((resolve, reject) => { + // show login page + router.push('/login'); + reject(['not logged in']); + }); + } + + let now = Math.round(Date.now() / 1000); + if (context.state.persist.jwt.exp < (now + 300)) { + // too old jwt, logout + return new Promise((resolve, reject) => { + context.commit('clearLogin'); + router.push('/login'); + reject(['jwt too old, logout']); + }); + } + + if ((context.state.persist.jwt.exp - 60) < now) { + // jwt near expire, get new one + console.log("getting new jwt"); + + return context.dispatch('login', context.state.persist.credentials) + .then(() => { + console.log("LOOOGIIIINNN"); + return doRequest(); + }) + .catch(error => { + throw error; + }) + } } - }); - }); + + return doRequest(); + }, + login(context, payload) { + return new Promise((resolve, reject) => { + context.dispatch('apiRequest', { + method: 'post', + endpoint: 'login', + data: payload + }) + .then(data => { + context.commit('setCredentials', payload); + context.commit('setLogin', data); + resolve(data.User); + }) + .catch(error => { + if (error.response && error.response.data && error.response.data.error) { + reject(error.response.data.error); + } else { + reject([]); + } + }); + }); + } } - } }); function objectToPersist(obj, key) { - try { - const serialized = JSON.stringify(obj); - localStorage.setItem(key ? key : 'persistant', serialized); - } catch (error) { - console.error(error); - } -}; + try { + const serialized = JSON.stringify(obj); + localStorage.setItem(key ? key : 'persistant', serialized); + } catch (error) { + console.error(error); + } +} function persistToObject(key) { - try { - const serialized = localStorage.getItem(key ? key : 'persistant'); - if (serialized === null) { - return undefined; + try { + const serialized = localStorage.getItem(key ? key : 'persistant'); + if (serialized === null) { + return undefined; + } + return JSON.parse(serialized); + } catch (error) { + console.log(error); + return undefined; } - return JSON.parse(serialized); - } catch (error) { - console.log(error); - return undefined; - } } // get store persist part from localStorage let persist = persistToObject('persistantStore'); if (persist) { - store.state.persist = persist; + store.state.persist = persist; } // load init Axios.get('conf/init.json') - .then(results => { - // set navigation - if (!Array.isArray(results.data.navigation)) { - alert('invalid data in init.json'); - return; - } - const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}}); + .then(results => { + // set navigation + if (!Array.isArray(results.data.navigation)) { + alert('invalid data in init.json'); + return; + } + const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}}); - store.commit("setNavigation", navigation); + store.commit("setNavigation", navigation); - // set api config in store - store.commit("setAPI", results.data.api); + // set api config in store + store.commit("setAPI", results.data.api); - // add routes - let routes = []; - let rIdx = 0; - results.data.navigation.forEach(({name, to, content, data}) => routes.push({ - name: name, - path: to, - meta: { - title: name - }, - component: Vue.component(name + rIdx++, { - template: '
' + content + '
', - components: Views, - data: function(data) { - if (typeof data != 'object') { - return () => { return {}; }; - } - return () => { return data; }; - }(data) - }) - })); - router.addRoutes(routes); - router.beforeEach((to, from, next) => { - console.log(to); - document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title; - next(); - }); + // add routes + let routes = []; + let rIdx = 0; + results.data.navigation.forEach(({name, to, content, data}) => routes.push({ + name: name, + path: to, + meta: { + title: name + }, + component: Vue.component(name + rIdx++, { + template: '
' + content + '
', + components: Views, + data: function(data) { + if (typeof data != 'object') { + return () => { return {}; }; + } + return () => { return data; }; + }(data) + }) + })); + router.addRoutes(routes); + router.beforeEach((to, from, next) => { + console.log(to); + document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title; + next(); + }); - // load app, when init finishs - new Vue({ - el: '#vue-app', - render: h => h(App), - router, - store - }); - }) - .catch(error => { - alert('error loading: ' + error.message); - }); \ No newline at end of file + // load app, when init finishs + new Vue({ + el: '#vue-app', + render: h => h(App), + router, + store + }); + }) + .catch(error => { + alert('error loading: ' + error.message); + }); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 1fed357..d3bd182 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -47,6 +47,7 @@ module.exports = { }, module: { loaders: [{ + enforce: "pre", test: /\.(vue|js)$/, loader: 'eslint-loader', exclude: /node_modules/