@taybart/corvid 0.1.20 → 0.1.22
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/dist/index.js +23 -10
- package/dist/local_storage.d.ts +10 -0
- package/dist/ls.js +10 -1
- package/dist/network.d.ts +4 -3
- package/dist/network.js +13 -10
- package/dist/utils.js +95 -0
- package/package.json +8 -4
package/dist/index.js
CHANGED
|
@@ -61,6 +61,7 @@ var local_storage_namespaceObject = {};
|
|
|
61
61
|
__webpack_require__.r(local_storage_namespaceObject);
|
|
62
62
|
__webpack_require__.d(local_storage_namespaceObject, {
|
|
63
63
|
clear: ()=>clear,
|
|
64
|
+
default: ()=>local_storage,
|
|
64
65
|
get: ()=>get,
|
|
65
66
|
getJSON: ()=>getJSON,
|
|
66
67
|
listen: ()=>listen,
|
|
@@ -493,10 +494,8 @@ class network_params {
|
|
|
493
494
|
}
|
|
494
495
|
}
|
|
495
496
|
class request {
|
|
496
|
-
auth(
|
|
497
|
-
|
|
498
|
-
this.log.debug(`adding auth token header ${header}`);
|
|
499
|
-
this.opts.headers.Authorization = header;
|
|
497
|
+
auth(a) {
|
|
498
|
+
this.opts.auth = a;
|
|
500
499
|
return this;
|
|
501
500
|
}
|
|
502
501
|
basicAuth(username, password) {
|
|
@@ -510,7 +509,7 @@ class request {
|
|
|
510
509
|
return this;
|
|
511
510
|
}
|
|
512
511
|
build({ path, params: passedParams, override } = {}) {
|
|
513
|
-
if (this.opts.auth
|
|
512
|
+
if (this.opts.auth) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
|
|
514
513
|
else this.opts.headers.Authorization = `Basic ${btoa(`${this.opts.auth.username}:${this.opts.auth.password}`)}`;
|
|
515
514
|
if (!override) override = {};
|
|
516
515
|
const body = override.body || this.opts.body;
|
|
@@ -539,20 +538,26 @@ class request {
|
|
|
539
538
|
}
|
|
540
539
|
};
|
|
541
540
|
}
|
|
542
|
-
async do({ path, params: passedParams, override = {} } = {}) {
|
|
541
|
+
async do({ path, params: passedParams, override = {}, _recurselevel = 0 } = {}) {
|
|
543
542
|
const { url, options } = this.build({
|
|
544
543
|
path,
|
|
545
544
|
params: passedParams,
|
|
546
545
|
override
|
|
547
546
|
});
|
|
548
547
|
this.log.debug(`${this.opts.method} ${url}`);
|
|
549
|
-
|
|
548
|
+
let res;
|
|
549
|
+
res = this.opts.fetch ? await this.opts.fetch(url, options) : await fetch(url, options);
|
|
550
550
|
const expect = override.expect || this.opts.expect;
|
|
551
551
|
if (res.status !== expect) {
|
|
552
552
|
const body = await res.text();
|
|
553
|
-
if (401 === res.status && this.opts.onUnauthorized) {
|
|
553
|
+
if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
|
|
554
554
|
this.log.warn("unauthorized");
|
|
555
|
-
this.opts.onUnauthorized(body)
|
|
555
|
+
if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
|
|
556
|
+
path,
|
|
557
|
+
params: passedParams,
|
|
558
|
+
override,
|
|
559
|
+
_recurselevel: _recurselevel + 1
|
|
560
|
+
});
|
|
556
561
|
} else throw {
|
|
557
562
|
message: `bad wesponse ${res.status} !== ${expect}, body: ${body}`,
|
|
558
563
|
status: res.status,
|
|
@@ -578,7 +583,6 @@ class request {
|
|
|
578
583
|
this.log.debug("converting object params to class");
|
|
579
584
|
this.opts.params = new network_params(this.opts.params);
|
|
580
585
|
}
|
|
581
|
-
if (!this.opts.fetch) this.opts.fetch = window.fetch;
|
|
582
586
|
this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
|
|
583
587
|
}
|
|
584
588
|
}
|
|
@@ -775,4 +779,13 @@ function unlisten(key, cb) {
|
|
|
775
779
|
function clear(key) {
|
|
776
780
|
localStorage.removeItem(key);
|
|
777
781
|
}
|
|
782
|
+
const local_storage = {
|
|
783
|
+
get,
|
|
784
|
+
getJSON,
|
|
785
|
+
set,
|
|
786
|
+
update: local_storage_update,
|
|
787
|
+
listen,
|
|
788
|
+
unlisten,
|
|
789
|
+
clear
|
|
790
|
+
};
|
|
778
791
|
export { clipboard, dom_namespaceObject as dom, genID, utils_logLevel as logLevel, logger, local_storage_namespaceObject as ls, network_namespaceObject as network, strings_namespaceObject as strings, style_namespaceObject as style };
|
package/dist/local_storage.d.ts
CHANGED
|
@@ -13,3 +13,13 @@ export declare function unlisten(key: string, cb: (update: {
|
|
|
13
13
|
value: any;
|
|
14
14
|
}) => void | el): void;
|
|
15
15
|
export declare function clear(key: string): void;
|
|
16
|
+
declare const _default_1: {
|
|
17
|
+
get: typeof get;
|
|
18
|
+
getJSON: typeof getJSON;
|
|
19
|
+
set: typeof set;
|
|
20
|
+
update: typeof update;
|
|
21
|
+
listen: typeof listen;
|
|
22
|
+
unlisten: typeof unlisten;
|
|
23
|
+
clear: typeof clear;
|
|
24
|
+
};
|
|
25
|
+
export default _default_1;
|
package/dist/ls.js
CHANGED
|
@@ -333,4 +333,13 @@ function unlisten(key, cb) {
|
|
|
333
333
|
function clear(key) {
|
|
334
334
|
localStorage.removeItem(key);
|
|
335
335
|
}
|
|
336
|
-
|
|
336
|
+
const local_storage = {
|
|
337
|
+
get,
|
|
338
|
+
getJSON,
|
|
339
|
+
set,
|
|
340
|
+
update: local_storage_update,
|
|
341
|
+
listen,
|
|
342
|
+
unlisten,
|
|
343
|
+
clear
|
|
344
|
+
};
|
|
345
|
+
export { clear, local_storage as default, get, getJSON, listen, set, setObj, unlisten, local_storage_update as update };
|
package/dist/network.d.ts
CHANGED
|
@@ -27,14 +27,14 @@ export type RequestOpts = {
|
|
|
27
27
|
expect?: number;
|
|
28
28
|
credentials?: RequestCredentials;
|
|
29
29
|
insecureNoVerify?: boolean;
|
|
30
|
-
onUnauthorized?: (body: any) =>
|
|
30
|
+
onUnauthorized?: (client: request, body: any) => Promise<boolean>;
|
|
31
31
|
fetch?: (url: string, init: RequestInit) => Promise<any>;
|
|
32
32
|
};
|
|
33
33
|
export declare class request {
|
|
34
34
|
opts: RequestOpts;
|
|
35
35
|
log: logger;
|
|
36
36
|
constructor(opts?: RequestOpts, verbose?: boolean);
|
|
37
|
-
auth(
|
|
37
|
+
auth(a: string | {
|
|
38
38
|
username: string;
|
|
39
39
|
password: string;
|
|
40
40
|
}): this;
|
|
@@ -67,7 +67,7 @@ export declare class request {
|
|
|
67
67
|
body: string;
|
|
68
68
|
};
|
|
69
69
|
};
|
|
70
|
-
do({ path, params: passedParams, override, }?: {
|
|
70
|
+
do({ path, params: passedParams, override, _recurselevel, }?: {
|
|
71
71
|
path?: string;
|
|
72
72
|
params?: Object;
|
|
73
73
|
method?: string;
|
|
@@ -78,6 +78,7 @@ export declare class request {
|
|
|
78
78
|
body?: Object;
|
|
79
79
|
expect?: number;
|
|
80
80
|
};
|
|
81
|
+
_recurselevel?: number;
|
|
81
82
|
}): Promise<any>;
|
|
82
83
|
}
|
|
83
84
|
/*** websocket ***/
|
package/dist/network.js
CHANGED
|
@@ -84,10 +84,8 @@ class network_params {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
class request {
|
|
87
|
-
auth(
|
|
88
|
-
|
|
89
|
-
this.log.debug(`adding auth token header ${header}`);
|
|
90
|
-
this.opts.headers.Authorization = header;
|
|
87
|
+
auth(a) {
|
|
88
|
+
this.opts.auth = a;
|
|
91
89
|
return this;
|
|
92
90
|
}
|
|
93
91
|
basicAuth(username, password) {
|
|
@@ -101,7 +99,7 @@ class request {
|
|
|
101
99
|
return this;
|
|
102
100
|
}
|
|
103
101
|
build({ path, params: passedParams, override } = {}) {
|
|
104
|
-
if (this.opts.auth
|
|
102
|
+
if (this.opts.auth) if ('string' == typeof this.opts.auth) this.opts.headers.Authorization = `Bearer ${this.opts.auth}`;
|
|
105
103
|
else this.opts.headers.Authorization = `Basic ${btoa(`${this.opts.auth.username}:${this.opts.auth.password}`)}`;
|
|
106
104
|
if (!override) override = {};
|
|
107
105
|
const body = override.body || this.opts.body;
|
|
@@ -130,20 +128,26 @@ class request {
|
|
|
130
128
|
}
|
|
131
129
|
};
|
|
132
130
|
}
|
|
133
|
-
async do({ path, params: passedParams, override = {} } = {}) {
|
|
131
|
+
async do({ path, params: passedParams, override = {}, _recurselevel = 0 } = {}) {
|
|
134
132
|
const { url, options } = this.build({
|
|
135
133
|
path,
|
|
136
134
|
params: passedParams,
|
|
137
135
|
override
|
|
138
136
|
});
|
|
139
137
|
this.log.debug(`${this.opts.method} ${url}`);
|
|
140
|
-
|
|
138
|
+
let res;
|
|
139
|
+
res = this.opts.fetch ? await this.opts.fetch(url, options) : await fetch(url, options);
|
|
141
140
|
const expect = override.expect || this.opts.expect;
|
|
142
141
|
if (res.status !== expect) {
|
|
143
142
|
const body = await res.text();
|
|
144
|
-
if (401 === res.status && this.opts.onUnauthorized) {
|
|
143
|
+
if (401 === res.status && this.opts.onUnauthorized && _recurselevel < 1) {
|
|
145
144
|
this.log.warn("unauthorized");
|
|
146
|
-
this.opts.onUnauthorized(body)
|
|
145
|
+
if (await this.opts.onUnauthorized.call(this, this, body)) return this.do({
|
|
146
|
+
path,
|
|
147
|
+
params: passedParams,
|
|
148
|
+
override,
|
|
149
|
+
_recurselevel: _recurselevel + 1
|
|
150
|
+
});
|
|
147
151
|
} else throw {
|
|
148
152
|
message: `bad wesponse ${res.status} !== ${expect}, body: ${body}`,
|
|
149
153
|
status: res.status,
|
|
@@ -169,7 +173,6 @@ class request {
|
|
|
169
173
|
this.log.debug("converting object params to class");
|
|
170
174
|
this.opts.params = new network_params(this.opts.params);
|
|
171
175
|
}
|
|
172
|
-
if (!this.opts.fetch) this.opts.fetch = window.fetch;
|
|
173
176
|
this.log.debug(`with options: ${JSON.stringify(this.opts)}`);
|
|
174
177
|
}
|
|
175
178
|
}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
3
|
+
value: value,
|
|
4
|
+
enumerable: true,
|
|
5
|
+
configurable: true,
|
|
6
|
+
writable: true
|
|
7
|
+
});
|
|
8
|
+
else obj[key] = value;
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
function genID(len = 15, alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
|
|
12
|
+
return [
|
|
13
|
+
...crypto.getRandomValues(new Uint8Array(len))
|
|
14
|
+
].map((value)=>alphabet[Math.floor(value / 255 * alphabet.length)]).join('');
|
|
15
|
+
}
|
|
16
|
+
const clipboard = {
|
|
17
|
+
check () {
|
|
18
|
+
if (!navigator.clipboard) throw new Error('Clipboard API not supported, or context is not https');
|
|
19
|
+
},
|
|
20
|
+
async copy (text) {
|
|
21
|
+
this.check();
|
|
22
|
+
await navigator.clipboard.writeText(text);
|
|
23
|
+
},
|
|
24
|
+
async copyArbitrary (data) {
|
|
25
|
+
this.check();
|
|
26
|
+
await navigator.clipboard.write(data);
|
|
27
|
+
},
|
|
28
|
+
async read () {
|
|
29
|
+
this.check();
|
|
30
|
+
return await navigator.clipboard.readText();
|
|
31
|
+
},
|
|
32
|
+
async readArbitrary () {
|
|
33
|
+
this.check();
|
|
34
|
+
return await navigator.clipboard.read();
|
|
35
|
+
},
|
|
36
|
+
listen (query, cb) {
|
|
37
|
+
let el;
|
|
38
|
+
el = 'string' == typeof query ? document.querySelector(query) : query;
|
|
39
|
+
if (!el) throw new Error(`no element from query: ${query}`);
|
|
40
|
+
el.addEventListener('copy', (ev)=>{
|
|
41
|
+
const cbEv = ev;
|
|
42
|
+
const selection = document.getSelection();
|
|
43
|
+
if (selection) {
|
|
44
|
+
const text = selection.toString();
|
|
45
|
+
if (text && cbEv.clipboardData) cbEv.clipboardData.setData('text/plain', cb(text));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var utils_logLevel = /*#__PURE__*/ function(logLevel) {
|
|
51
|
+
logLevel[logLevel["none"] = -1] = "none";
|
|
52
|
+
logLevel[logLevel["error"] = 0] = "error";
|
|
53
|
+
logLevel[logLevel["warn"] = 1] = "warn";
|
|
54
|
+
logLevel[logLevel["info"] = 2] = "info";
|
|
55
|
+
logLevel[logLevel["debug"] = 3] = "debug";
|
|
56
|
+
logLevel[logLevel["trace"] = 4] = "trace";
|
|
57
|
+
return logLevel;
|
|
58
|
+
}({});
|
|
59
|
+
class logger {
|
|
60
|
+
error(...args) {
|
|
61
|
+
if (this.level >= 0) console.error(`[corvid] ${this.prefix}`, ...args);
|
|
62
|
+
}
|
|
63
|
+
warn(...args) {
|
|
64
|
+
if (this.level >= 1) console.warn(`[corvid] ${this.prefix}`, ...args);
|
|
65
|
+
}
|
|
66
|
+
info(...args) {
|
|
67
|
+
if (this.level >= 2) console.info(`[corvid] ${this.prefix}`, ...args);
|
|
68
|
+
}
|
|
69
|
+
debug(...args) {
|
|
70
|
+
if (this.level >= 3) console.debug(`[corvid] ${this.prefix}`, ...args);
|
|
71
|
+
}
|
|
72
|
+
trace(...args) {
|
|
73
|
+
if (this.level >= 4) console.trace(`[corvid] ${this.prefix}`, ...args);
|
|
74
|
+
}
|
|
75
|
+
log(...args) {
|
|
76
|
+
console.log(`[corvid] ${this.prefix}`, ...args);
|
|
77
|
+
}
|
|
78
|
+
constructor(level = 2, prefix){
|
|
79
|
+
_define_property(this, "level", void 0);
|
|
80
|
+
_define_property(this, "prefix", void 0);
|
|
81
|
+
this.level = level;
|
|
82
|
+
this.prefix = prefix ? `(${prefix}):` : ':';
|
|
83
|
+
if (-1 === this.level) [
|
|
84
|
+
'error',
|
|
85
|
+
'warn',
|
|
86
|
+
'info',
|
|
87
|
+
'debug',
|
|
88
|
+
'trace',
|
|
89
|
+
'log'
|
|
90
|
+
].forEach((methodName)=>{
|
|
91
|
+
this[methodName] = ()=>{};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export { clipboard, genID, utils_logLevel as logLevel, logger };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taybart/corvid",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"types": "./dist/style.d.ts",
|
|
17
17
|
"import": "./dist/style.js"
|
|
18
18
|
},
|
|
19
|
-
"./
|
|
20
|
-
"types": "./dist/
|
|
19
|
+
"./ls": {
|
|
20
|
+
"types": "./dist/local_storage.d.ts",
|
|
21
21
|
"import": "./dist/ls.js"
|
|
22
22
|
},
|
|
23
23
|
"./network": {
|
|
@@ -25,8 +25,12 @@
|
|
|
25
25
|
"import": "./dist/network.js"
|
|
26
26
|
},
|
|
27
27
|
"./qr": {
|
|
28
|
-
"types": "./dist/qr.d.ts",
|
|
28
|
+
"types": "./dist/qr/index.d.ts",
|
|
29
29
|
"import": "./dist/qr.js"
|
|
30
|
+
},
|
|
31
|
+
"./utils": {
|
|
32
|
+
"types": "./dist/utils.d.ts",
|
|
33
|
+
"import": "./dist/utils.js"
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"module": "./dist/index.js",
|