From 38b6bc76032fe04af929cbc1f4cd7b3c98a92199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romel=20P=C3=A9rez?= Date: Fri, 28 Jul 2017 15:48:38 -0500 Subject: [PATCH] [update] client --- README.md | 2 +- client/tools/storage.js | 41 ++++++++++++++++++++++++++++++++++++++ client/tools/validators.js | 10 ++++++++++ client/tools/xhr.js | 9 +++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 client/tools/storage.js create mode 100644 client/tools/validators.js create mode 100644 client/tools/xhr.js diff --git a/README.md b/README.md index bbe9faa..5236329 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![prhone](https://img.shields.io/badge/prhone-project-1b38a9.svg)](http://romelperez.com) [![license](https://img.shields.io/github/license/romelperez/web-boilerplate.svg?maxAge=2592000)](./LICENSE) -This is a personal reference based: +This is a personal reference based on: ## Tech diff --git a/client/tools/storage.js b/client/tools/storage.js new file mode 100644 index 0000000..c2fa4d6 --- /dev/null +++ b/client/tools/storage.js @@ -0,0 +1,41 @@ +function isSupported () { + try { + window.localStorage.setItem('__tk', 'value'); + window.localStorage.getItem('__tk'); + window.localStorage.removeItem('__tk'); + return 'localStorage' in window && window.localStorage !== null; + } catch (e) { + return false; + } +} + +const store = { + local: null +}; + +function resetLocal () { + if (!isSupported()) { + var db = { + setItem: function (id, val) { + return (db[id] = String(val)); + }, + getItem: function (id) { + return db.hasOwnProperty(id) ? String(db[id]) : void 0; + }, + removeItem: function (id) { + return delete db[id]; + }, + clear: function () { + resetLocal(); + } + }; + + store.local = db; + } else { + store.local = window.localStorage; + } +} + +resetLocal(); + +module.exports = store; diff --git a/client/tools/validators.js b/client/tools/validators.js new file mode 100644 index 0000000..76f4e77 --- /dev/null +++ b/client/tools/validators.js @@ -0,0 +1,10 @@ +window.vulcanval.extendLocale({ + id: 'es', + msgs: { + isTime: 'Una hora vĂ¡lida es requerida.', + } +}); + +window.vulcanval.addValidator('isTime', function (value = '') { + return (/^\d\d\:\d\d$/).test(String(value)); +}); diff --git a/client/tools/xhr.js b/client/tools/xhr.js new file mode 100644 index 0000000..cf9e759 --- /dev/null +++ b/client/tools/xhr.js @@ -0,0 +1,9 @@ +import axios from 'axios'; +import storage from 'client/tools/storage'; + +axios.interceptors.request.use(function (req) { + if ((/^\/.+/).test(req.url)) { + req.headers['x-token-auth'] = storage.local.getItem('token'); + } + return req; +});