@akc42/app-utils 3.3.4 → 3.6.0

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/README.md CHANGED
@@ -86,22 +86,6 @@ debug has been replaced with a version that works differently and is incompatibl
86
86
  `this.keys.lastPressed`, and it will return the complete binding object
87
87
 
88
88
 
89
- # config-promise
90
-
91
- This module provides two functions:-
92
-
93
- 1. This first is the default function which when called returns a promise.
94
- 2. a `mockConfig` function which can be used by a test harness to provide the promise returned by the default function
95
-
96
- If a request is made for the promise and it hasn't yet been created a new promise is made with a fetch get request to `/api/config`
97
- to retrieve the configuration data. If the server is down, this request will continue attempting to get the value once a minute ad
98
- infinitum.
99
-
100
- The configuration data is returned to resolve the promise, but local session
101
- storage also has one item added to it for each of the first level properties
102
- of this data. If the property is a String, this is stored in the session
103
- storage as is, otherwise the value is passed through `JSON.stringify` and that is stored.
104
-
105
89
  # csv
106
90
 
107
91
  the modules default export is a function which can be called with a name and optionally a parameters object. The name is added to `/api/csv/` to make a download request and if they exist the parameters object is turned into a query string. The response from that uri is downloaded (expected to be a csv file).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akc42/app-utils",
3
- "version": "3.3.4",
3
+ "version": "3.6.0",
4
4
  "description": "General Utilities for SPAs",
5
5
  "exports": {
6
6
  ".": "./*.js"
package/post-api.js CHANGED
@@ -34,7 +34,7 @@ export default async function api(url, params, blob, signal) {
34
34
  let text;
35
35
  try {
36
36
  const response = await window.fetch(address, options);
37
- if (!response.ok) throw new CustomEvent('api-error', {composed: true, bubbles: true , detail:response.status});
37
+ if (!response.ok) throw new CustomEvent('api-error', {composed: true, bubbles: true , detail:{status:response.status, url:address}});
38
38
  performance.mark('fetchdone', {detail: address});
39
39
  performance.measure('apicalltime',{start: 'fetchapi', end:'fetchdone', detail: address});
40
40
  if (blob) {
@@ -54,7 +54,7 @@ export default async function api(url, params, blob, signal) {
54
54
  if (!options.signal || !options.signal.aborted) {
55
55
  if (err.type === 'api-error') throw err; //just throw whatever error we had
56
56
  //we failed to parse the json - the actual code should be in the text near the end;
57
- throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: parseInt((text?? '---502---').slice(-6, -3), 10) });
57
+ throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: {status:parseInt((text?? '---502---').slice(-6, -3), 10), url:address }});
58
58
  }
59
59
  }
60
60
  }
package/config-promise.js DELETED
@@ -1,72 +0,0 @@
1
- /**
2
- @licence
3
- Copyright (c) 2020 Alan Chandler, all rights reserved
4
-
5
- This file is part of @akc42/app-utils.
6
-
7
- @akc42/app-utils is free software: you can redistribute it and/or modify
8
- it under the terms of the GNU General Public License as published by
9
- the Free Software Foundation, either version 3 of the License, or
10
- (at your option) any later version.
11
-
12
- @akc42/app-utils is distributed in the hope that it will be useful,
13
- but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- GNU General Public License for more details.
16
-
17
- You should have received a copy of the GNU General Public License
18
- along with @akc42/app-utils. If not, see <http://www.gnu.org/licenses/>.
19
- */
20
-
21
- let configPromise;
22
-
23
- export function setConfig(promise) {
24
- configPromise = promise; //set to undefined to allow config call to request from server again.
25
- }
26
-
27
- async function config() {
28
- if (typeof configPromise === 'undefined') {
29
- let resolved = false;
30
- let resolver;
31
- configPromise = new Promise(accept => resolver = accept);
32
- let text;
33
- let config;
34
- while (!resolved) {
35
- try {
36
- const response = await window.fetch('/api/config', { method: 'get' })
37
- if (!response.ok) throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: response.status });
38
- text = await response.text();
39
- config = JSON.parse(text);
40
- /*
41
- some uses of this put the config in sessionStorage, but that only works if every config value is a string
42
- so we check if they are a string, otherwise we stringify them
43
- */
44
- for (const p in config) {
45
- const v = config[p];
46
- if (typeof v === 'string') {
47
- sessionStorage.setItem(p, v);
48
- } else {
49
- sessionStorage.setItem(p,JSON.stringify(v));
50
- }
51
- }
52
- resolver(config);
53
- resolved = true;
54
- } catch (err) {
55
- if (err.detail === 502) {
56
- //server down so wait a minute and try again;
57
- await new Promise(accept => {
58
- setTimeout(accept, 60000);
59
- });
60
- } else {
61
- if (err.type === 'api-error') throw err; //just throw whatever error we had
62
- //we failed to parse the json - the actual code should be in the text near the end;
63
- throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: parseInt(text.substr(-6, 3), 10) });
64
- }
65
- }
66
- }
67
- }
68
- return configPromise;
69
- }
70
-
71
-
72
- export default config;