@osimatic/helpers-js 1.0.61 → 1.0.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -32,7 +32,7 @@ const { ShoppingCart } = require('./shopping_cart');
32
32
  const { FlashMessage } = require('./flash_message');
33
33
  const { CountDown } = require('./count_down');
34
34
  const { ImportFromCsv } = require('./import_from_csv');
35
- const { JwtToken, JwtSession } = require('./jwt');
35
+ const { JwtToken, JwtSession, ApiTokenSession } = require('./jwt');
36
36
  const { ListBox } = require('./list_box');
37
37
  const { WebRTC } = require('./web_rtc');
38
38
  const { EventBus } = require('./event_bus');
@@ -47,7 +47,7 @@ const { WebSocket } = require('./web_socket');
47
47
  module.exports = {
48
48
  Array, Object, Number, String,
49
49
  HTTPRequest, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, SocialNetwork,
50
- Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ListBox, WebRTC, WebSocket, EventBus,
50
+ Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ApiTokenSession, ListBox, WebRTC, WebSocket, EventBus,
51
51
  sleep, refresh, chr, ord, trim, empty,
52
52
  GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
53
53
  };
package/jwt.js CHANGED
@@ -93,4 +93,74 @@ class JwtSession {
93
93
  }
94
94
  }
95
95
 
96
- module.exports = { JwtToken, JwtSession };
96
+ class ApiTokenSession {
97
+ static denyAccessUnlessGranted(roles) {
98
+ let hasRole = false;
99
+
100
+ roles.forEach(role => {
101
+ if (ApiTokenSession.isGranted(role)) {
102
+ hasRole = true;
103
+ }
104
+ });
105
+
106
+ return hasRole;
107
+ }
108
+
109
+ static getToken() {
110
+ return localStorage.getItem('api_token');
111
+ }
112
+ static setToken(token) {
113
+ localStorage.setItem('api_token', token);
114
+ }
115
+
116
+ static getTokenData() {
117
+ let tokenData = localStorage.getItem('token_data');
118
+ if (null == tokenData) {
119
+ return null;
120
+ }
121
+ return JSON.parse(tokenData);
122
+ }
123
+ static setTokenData(data) {
124
+ localStorage.setItem('token_data', JSON.stringify(data));
125
+ }
126
+
127
+ static logout() {
128
+ localStorage.removeItem('api_token');
129
+ localStorage.removeItem('token_data');
130
+ }
131
+
132
+ static getData(key) {
133
+ let tokenData = ApiTokenSession.getTokenData();
134
+ if (tokenData == null) {
135
+ return null;
136
+ }
137
+
138
+ if (typeof tokenData[key] != 'undefined') {
139
+ return tokenData[key];
140
+ }
141
+ return null;
142
+ }
143
+
144
+ static isAnonymous() {
145
+ return ApiTokenSession.getToken() == null;
146
+ }
147
+
148
+ static isGranted(role) {
149
+ if (ApiTokenSession.getToken() == null) {
150
+ return false;
151
+ }
152
+
153
+ let roles = [];
154
+ if (null !== ApiTokenSession.getData('role')) {
155
+ roles = ApiTokenSession.getData('role');
156
+ }
157
+ if (null !== ApiTokenSession.getData('roles')) {
158
+ roles = ApiTokenSession.getData('roles');
159
+ }
160
+ roles = Array.isArray(roles) ? roles : [roles];
161
+
162
+ return roles.indexOf(role) !== -1;
163
+ }
164
+ }
165
+
166
+ module.exports = { JwtToken, JwtSession, ApiTokenSession };
package/network.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  class HTTPRequest {
3
2
  static init() {
4
3
  require('whatwg-fetch'); //fetch polyfill loaded in window.fetch
@@ -138,10 +137,8 @@ class HTTPRequest {
138
137
  let jsonData = {};
139
138
  try {
140
139
  jsonData = await response.json();
141
- //console.log(url, jsonData);
142
- //console.log(response.status, response.statusText, jsonData['error']);
143
140
 
144
- if (response.status === 401 && (response.statusText === 'Expired JWT Token' || typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token')) {
141
+ if (response.status === 401 && (response.statusText === 'Expired JWT Token' || (typeof jsonData['message'] != 'undefined' && jsonData['message'] === 'Expired JWT Token') || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
145
142
  HTTPRequest.refreshToken(() => HTTPRequest.get(url, data, successCallback, errorCallback));
146
143
  return;
147
144
  }
@@ -303,7 +300,7 @@ class HTTPRequest {
303
300
  }
304
301
  //console.log(url, jsonData);
305
302
 
306
- if (response.status === 401 && url !== HTTPRequest.refreshTokenUrl && (response.statusText === 'Expired JWT Token' || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
303
+ if (response.status === 401 && url !== HTTPRequest.refreshTokenUrl && (response.statusText === 'Expired JWT Token' || (typeof jqxhr.responseJSON['message'] != 'undefined' && jqxhr.responseJSON['message'] === 'Expired JWT Token') || (typeof jsonData['error'] != 'undefined' && jsonData['error'] === 'expired_token'))) {
307
304
  HTTPRequest.refreshToken(() => HTTPRequest.post(url, formData, successCallback, errorCallback, formErrorCallback));
308
305
  return;
309
306
  }
@@ -386,6 +383,9 @@ class HTTPRequest {
386
383
  (data) => {
387
384
  JwtSession.setToken(data.token);
388
385
  JwtSession.setRefreshToken(data.refresh_token);
386
+
387
+ HTTPRequest.setAuthorizationHeader(JwtSession.getToken());
388
+
389
389
  onCompleteCallback();
390
390
  },
391
391
  () => {
package/number.js CHANGED
@@ -1,10 +1,6 @@
1
1
 
2
2
  Number.prototype.format = Number.prototype.format || function(nbDecimal, locale) {
3
- nbDecimal = (typeof nbDecimal != 'undefined'?nbDecimal:2);
4
- return new Intl.NumberFormat(locale, {
5
- minimumFractionDigits: nbDecimal,
6
- maximumFractionDigits: nbDecimal
7
- }).format(this);
3
+ return Number.format(this, nbDecimal, locale);
8
4
  }
9
5
 
10
6
  if (!Number.format) {
@@ -32,32 +28,44 @@ Number.prototype.formatForDisplay = Number.prototype.formatForDisplay || functio
32
28
  };
33
29
 
34
30
  Number.prototype.formatCurrency = Number.prototype.formatCurrency || function(currency, nbDecimal, locale) {
31
+ return Number.formatCurrency(this, currency, nbDecimal, locale);
32
+ }
33
+ Number.formatCurrency = Number.formatCurrency || function(number, currency, nbDecimal, locale) {
35
34
  nbDecimal = (typeof nbDecimal != 'undefined'?nbDecimal:2);
36
35
  return new Intl.NumberFormat(locale, {
37
36
  style: 'currency',
38
37
  currency: currency,
39
38
  minimumFractionDigits: nbDecimal,
40
39
  maximumFractionDigits: nbDecimal
41
- }).format(this);
40
+ }).format(number);
42
41
  }
43
42
 
44
43
  Number.prototype.formatPercent = Number.prototype.formatPercent || function(nbDecimal, locale) {
44
+ return Number.formatPercent(this, nbDecimal, locale);
45
+ }
46
+ Number.formatPercent = Number.formatPercent || function(number, nbDecimal, locale) {
45
47
  nbDecimal = (typeof nbDecimal != 'undefined'?nbDecimal:2);
46
48
  return new Intl.NumberFormat(locale, {
47
49
  style: 'percent',
48
50
  minimumFractionDigits: nbDecimal,
49
51
  maximumFractionDigits: nbDecimal
50
- }).format(this);
52
+ }).format(number);
51
53
  }
52
54
 
53
- Number.prototype.padLeft2 = Number.prototype.padLeft2 || function(n) {
54
- return this > 9 ? "" + this : "0" + this;
55
+ Number.prototype.padLeft2 = Number.prototype.padLeft2 || function() {
56
+ return Number.padLeft2(this);
57
+ }
58
+ Number.padLeft2 = Number.padLeft2 || function(n) {
59
+ return n > 9 ? "" + n : "0" + n;
55
60
  }
56
61
 
57
62
  Number.prototype.roundDecimal = Number.prototype.roundDecimal || function(precision) {
63
+ return Number.roundDecimal(this, precision);
64
+ }
65
+ Number.roundDecimal = Number.roundDecimal || function(number, precision) {
58
66
  precision = precision || 2;
59
- var tmp = Math.pow(10, precision);
60
- return Math.round(this*tmp) / tmp;
67
+ let tmp = Math.pow(10, precision);
68
+ return Math.round(number*tmp) / tmp;
61
69
  }
62
70
 
63
71
  if (!Number.random) {
@@ -65,24 +73,3 @@ if (!Number.random) {
65
73
  return Math.floor(Math.random() * (max - min + 1)) + min;
66
74
  };
67
75
  }
68
-
69
- /** @deprecated */
70
- Number.prototype.formatAsString = function(locale, minimumFractionDigits) {
71
- minimumFractionDigits = (typeof minimumFractionDigits != 'undefined'?minimumFractionDigits:0);
72
- return new Intl.NumberFormat(locale, {minimumFractionDigits: minimumFractionDigits}).format(this);
73
- };
74
- /** @deprecated */
75
- Number.prototype.formatAsCurrency = function(locale, currency, nbFractionDigits) {
76
- nbFractionDigits = (typeof nbFractionDigits != 'undefined'?nbFractionDigits:2);
77
- return new Intl.NumberFormat(locale, {
78
- style: 'currency',
79
- currency: 'EUR',
80
- minimumFractionDigits: nbFractionDigits,
81
- maximumFractionDigits: nbFractionDigits
82
- }).format(this);
83
- };
84
- /** @deprecated */
85
- Number.prototype.formatAsPercent = function(locale, minimumFractionDigits) {
86
- minimumFractionDigits = (typeof minimumFractionDigits != 'undefined'?minimumFractionDigits:0);
87
- return new Intl.NumberFormat(locale, {style: 'percent', minimumFractionDigits:minimumFractionDigits}).format(this);
88
- };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.61",
3
+ "version": "1.0.64",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"