@taybart/corvid 0.1.12 → 0.1.14
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 +5 -6
- package/dist/local_storage.d.ts +1 -1
- package/dist/ls.js +313 -0
- package/dist/network.d.ts +2 -2
- package/dist/network.js +4 -4
- package/package.json +5 -1
package/dist/index.js
CHANGED
|
@@ -541,10 +541,10 @@ class request {
|
|
|
541
541
|
});
|
|
542
542
|
this.log.debug(`${this.opts.method} ${url}`);
|
|
543
543
|
const res = await fetch(url, options);
|
|
544
|
-
const
|
|
545
|
-
if (res.status !==
|
|
544
|
+
const expect = override.expect || this.opts.expect;
|
|
545
|
+
if (res.status !== expect) {
|
|
546
546
|
const body = await res.json();
|
|
547
|
-
throw new Error(`bad response ${res.status} !== ${
|
|
547
|
+
throw new Error(`bad response ${res.status} !== ${expect}, body: ${body}`);
|
|
548
548
|
}
|
|
549
549
|
return await res.json();
|
|
550
550
|
}
|
|
@@ -555,7 +555,7 @@ class request {
|
|
|
555
555
|
if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
|
|
556
556
|
if (!opts.url) throw new Error('must provide url');
|
|
557
557
|
this.opts = opts;
|
|
558
|
-
if (!this.opts.
|
|
558
|
+
if (!this.opts.expect) this.opts.expect = 200;
|
|
559
559
|
if (!this.opts.method) this.opts.method = 'GET';
|
|
560
560
|
if (!this.opts.headers) this.opts.headers = {};
|
|
561
561
|
if (!this.opts.credentials) this.opts.credentials = 'omit';
|
|
@@ -674,7 +674,7 @@ const network = {
|
|
|
674
674
|
};
|
|
675
675
|
function get(key, _default) {
|
|
676
676
|
let ret = localStorage.getItem(key);
|
|
677
|
-
if (!ret && _default) {
|
|
677
|
+
if (!ret && null != _default) {
|
|
678
678
|
ret = _default;
|
|
679
679
|
if ('function' == typeof _default) ret = _default();
|
|
680
680
|
set(key, ret);
|
|
@@ -696,7 +696,6 @@ function local_storage_update(key, update1, broadcast = false) {
|
|
|
696
696
|
localStorage.setItem(key, value);
|
|
697
697
|
}
|
|
698
698
|
function set(key, value, broadcast = false) {
|
|
699
|
-
if ('object' == typeof key) return void setObj(key, value, broadcast);
|
|
700
699
|
const prev = get(key);
|
|
701
700
|
if (prev !== value || broadcast) {
|
|
702
701
|
const event = new CustomEvent('@corvid/ls-update', {
|
package/dist/local_storage.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { el } from './dom';
|
|
2
2
|
export declare function get(key: string, _default?: any): any;
|
|
3
3
|
export declare function update(key: string, update: (current: any) => any, broadcast?: boolean): void;
|
|
4
|
-
export declare function set(key: string
|
|
4
|
+
export declare function set(key: string, value: any, broadcast?: boolean): void;
|
|
5
5
|
export declare function setObj(update: object, prefix?: string, broadcast?: boolean): void;
|
|
6
6
|
export declare function listen(key: string, cb: (update: {
|
|
7
7
|
key: string;
|
package/dist/ls.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
function toKebab(str) {
|
|
2
|
+
return str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, (s, ofs)=>(ofs ? '-' : '') + s.toLowerCase());
|
|
3
|
+
}
|
|
4
|
+
function render(style) {
|
|
5
|
+
let s = '';
|
|
6
|
+
Object.entries(style).forEach(([k, v])=>s += `${toKebab(k)}:${v};`);
|
|
7
|
+
return s;
|
|
8
|
+
}
|
|
9
|
+
function _define_property(obj, key, value) {
|
|
10
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
11
|
+
value: value,
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true
|
|
15
|
+
});
|
|
16
|
+
else obj[key] = value;
|
|
17
|
+
return obj;
|
|
18
|
+
}
|
|
19
|
+
var utils_logLevel = /*#__PURE__*/ function(logLevel) {
|
|
20
|
+
logLevel[logLevel["none"] = -1] = "none";
|
|
21
|
+
logLevel[logLevel["error"] = 0] = "error";
|
|
22
|
+
logLevel[logLevel["warn"] = 1] = "warn";
|
|
23
|
+
logLevel[logLevel["info"] = 2] = "info";
|
|
24
|
+
logLevel[logLevel["debug"] = 3] = "debug";
|
|
25
|
+
logLevel[logLevel["trace"] = 4] = "trace";
|
|
26
|
+
return logLevel;
|
|
27
|
+
}({});
|
|
28
|
+
class utils_logger {
|
|
29
|
+
error(...args) {
|
|
30
|
+
if (this.level >= 0) console.error(`[corvid] ${this.prefix}`, ...args);
|
|
31
|
+
}
|
|
32
|
+
warn(...args) {
|
|
33
|
+
if (this.level >= 1) console.warn(`[corvid] ${this.prefix}`, ...args);
|
|
34
|
+
}
|
|
35
|
+
info(...args) {
|
|
36
|
+
if (this.level >= 2) console.info(`[corvid] ${this.prefix}`, ...args);
|
|
37
|
+
}
|
|
38
|
+
debug(...args) {
|
|
39
|
+
if (this.level >= 3) console.debug(`[corvid] ${this.prefix}`, ...args);
|
|
40
|
+
}
|
|
41
|
+
trace(...args) {
|
|
42
|
+
if (this.level >= 4) console.trace(`[corvid] ${this.prefix}`, ...args);
|
|
43
|
+
}
|
|
44
|
+
log(...args) {
|
|
45
|
+
console.log(`[corvid] ${this.prefix}`, ...args);
|
|
46
|
+
}
|
|
47
|
+
constructor(level = 2, prefix){
|
|
48
|
+
_define_property(this, "level", void 0);
|
|
49
|
+
_define_property(this, "prefix", void 0);
|
|
50
|
+
this.level = level;
|
|
51
|
+
this.prefix = prefix ? `(${prefix}):` : ':';
|
|
52
|
+
if (-1 === this.level) [
|
|
53
|
+
'error',
|
|
54
|
+
'warn',
|
|
55
|
+
'info',
|
|
56
|
+
'debug',
|
|
57
|
+
'trace',
|
|
58
|
+
'log'
|
|
59
|
+
].forEach((methodName)=>{
|
|
60
|
+
this[methodName] = ()=>{};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function dom_define_property(obj, key, value) {
|
|
65
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
66
|
+
value: value,
|
|
67
|
+
enumerable: true,
|
|
68
|
+
configurable: true,
|
|
69
|
+
writable: true
|
|
70
|
+
});
|
|
71
|
+
else obj[key] = value;
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
class dom_el {
|
|
75
|
+
static query(query, verbose = false) {
|
|
76
|
+
return new dom_el(query, verbose);
|
|
77
|
+
}
|
|
78
|
+
value(update) {
|
|
79
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
80
|
+
if (void 0 !== update) {
|
|
81
|
+
if ('value' in this.el) this.el.value = update;
|
|
82
|
+
if ('src' in this.el) this.el.src = update;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
if ('value' in this.el) return this.el.value;
|
|
86
|
+
if ('innerText' in this.el) return this.el.innerText;
|
|
87
|
+
if ('innerHTML' in this.el) return this.el.innerHTML;
|
|
88
|
+
this.log.warn(`element (${this.query}) does not contain value, returning empty string`);
|
|
89
|
+
return '';
|
|
90
|
+
}
|
|
91
|
+
parent(parent) {
|
|
92
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
93
|
+
parent.appendChild(this.el);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
appendChild(ch) {
|
|
97
|
+
return this.child(ch);
|
|
98
|
+
}
|
|
99
|
+
child(ch) {
|
|
100
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
101
|
+
if (ch instanceof dom_el) this.el.appendChild(ch.el);
|
|
102
|
+
else this.el.appendChild(ch);
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
prependChild(ch) {
|
|
106
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
107
|
+
if (ch instanceof dom_el) this.el.prepend(ch.el);
|
|
108
|
+
else this.el.prepend(ch);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
empty() {
|
|
112
|
+
if (this.el) this.el.innerHTML = '';
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
content(content, { text = false } = {}) {
|
|
116
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
117
|
+
if (text) this.el.textContent = content;
|
|
118
|
+
else this.el.innerHTML = content;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
src(url) {
|
|
122
|
+
if (this.el && 'src' in this.el) this.el.src = url;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
style(update, stringify = false) {
|
|
126
|
+
if (this.el) {
|
|
127
|
+
if ('string' == typeof update) this.el.style = update;
|
|
128
|
+
else if ('object' == typeof update) {
|
|
129
|
+
if (!stringify) {
|
|
130
|
+
for (const [k, v] of Object.entries(update))this.el.style[k] = v;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const s = render(update);
|
|
134
|
+
this.log.debug(`set style: ${this.el.style} -> ${s}`);
|
|
135
|
+
this.el.style = s;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
hasClass(className) {
|
|
141
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
142
|
+
return this.el.classList.contains(className);
|
|
143
|
+
}
|
|
144
|
+
addClass(className) {
|
|
145
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
146
|
+
if ('string' == typeof className) this.el.classList.add(className);
|
|
147
|
+
else for (const sc of className)this.el.classList.add(sc);
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
removeClass(className) {
|
|
151
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
152
|
+
if ('string' == typeof className) this.el.classList.remove(className);
|
|
153
|
+
else for (const sc of className)this.el.classList.remove(sc);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
html(content) {
|
|
157
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
158
|
+
this.el.innerHTML = content;
|
|
159
|
+
}
|
|
160
|
+
render(vars = {}) {
|
|
161
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
162
|
+
try {
|
|
163
|
+
return interpolate(this.el.innerHTML, vars);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
throw new Error(`could not render template ${this.query}: ${e}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
appendTemplate(template, vars) {
|
|
169
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
170
|
+
if (!template.el) throw new Error("template does not contain element");
|
|
171
|
+
const tmpl = template.render(vars);
|
|
172
|
+
this.el.insertAdjacentHTML('beforeend', tmpl);
|
|
173
|
+
}
|
|
174
|
+
on(event, cb) {
|
|
175
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
176
|
+
if (!this.listeners[event]) this.listeners[event] = [];
|
|
177
|
+
this.listeners[event].push(cb);
|
|
178
|
+
this.el.addEventListener(event, cb);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
listen(event, cb) {
|
|
182
|
+
return this.on(event, cb);
|
|
183
|
+
}
|
|
184
|
+
removeListeners(event) {
|
|
185
|
+
if (!this.el) throw new Error(`no element from query: ${this.query}`);
|
|
186
|
+
if (!this.listeners[event]) return this;
|
|
187
|
+
for (const cb of this.listeners[event])this.el.removeEventListener(event, cb);
|
|
188
|
+
this.listeners[event] = [];
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
constructor(opts, verbose = false){
|
|
192
|
+
dom_define_property(this, "el", void 0);
|
|
193
|
+
dom_define_property(this, "query", '');
|
|
194
|
+
dom_define_property(this, "log", void 0);
|
|
195
|
+
dom_define_property(this, "listeners", {});
|
|
196
|
+
this.log = new utils_logger(verbose ? utils_logLevel.debug : utils_logLevel.none, 'element');
|
|
197
|
+
if ('string' == typeof opts) {
|
|
198
|
+
this.query = opts;
|
|
199
|
+
this.el = document.querySelector(opts);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (opts instanceof HTMLElement) {
|
|
203
|
+
this.log.debug(`using existing element: ${opts}`);
|
|
204
|
+
this.el = opts;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const { query, element, type, class: styleClass, style, id, content, parent } = opts;
|
|
208
|
+
if (query) {
|
|
209
|
+
this.log.debug(`using query: ${query}`);
|
|
210
|
+
this.query = query;
|
|
211
|
+
this.el = document.querySelector(query);
|
|
212
|
+
if (!this.el) throw new Error(`no element from query: ${query}`);
|
|
213
|
+
} else if (element) {
|
|
214
|
+
this.log.debug(`using existing element: ${element}`);
|
|
215
|
+
this.el = element;
|
|
216
|
+
} else if (type) {
|
|
217
|
+
this.query = type;
|
|
218
|
+
this.log.debug(`creating element: ${type}`);
|
|
219
|
+
this.el = document.createElement(type);
|
|
220
|
+
} else throw new Error('no query or type provided');
|
|
221
|
+
if (this.el) {
|
|
222
|
+
if (id) {
|
|
223
|
+
this.log.debug(`setting id: ${id}`);
|
|
224
|
+
this.el.id = id;
|
|
225
|
+
}
|
|
226
|
+
if (styleClass) if ('string' == typeof styleClass) this.el.classList.add(styleClass);
|
|
227
|
+
else for (const sc of styleClass)this.el.classList.add(sc);
|
|
228
|
+
if (style) this.style(style);
|
|
229
|
+
if (content) {
|
|
230
|
+
this.log.debug(`setting content: ${content}`);
|
|
231
|
+
this.el.innerHTML = content;
|
|
232
|
+
}
|
|
233
|
+
if (parent) {
|
|
234
|
+
this.log.debug("adding to parent");
|
|
235
|
+
parent.appendChild(this.el);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function interpolate(str, params) {
|
|
241
|
+
let names = Object.keys(params).map((k)=>`_${k}`);
|
|
242
|
+
let vals = Object.values(params);
|
|
243
|
+
return new Function(...names, `return \`${str.replace(/\$\{(\w*)\}/g, '${_$1}')}\`;`)(...vals);
|
|
244
|
+
}
|
|
245
|
+
function get(key, _default) {
|
|
246
|
+
let ret = localStorage.getItem(key);
|
|
247
|
+
if (!ret && null != _default) {
|
|
248
|
+
ret = _default;
|
|
249
|
+
if ('function' == typeof _default) ret = _default();
|
|
250
|
+
set(key, ret);
|
|
251
|
+
}
|
|
252
|
+
return ret;
|
|
253
|
+
}
|
|
254
|
+
function local_storage_update(key, update1, broadcast = false) {
|
|
255
|
+
const prev = get(key);
|
|
256
|
+
const value = update1(prev);
|
|
257
|
+
if (prev !== value || broadcast) {
|
|
258
|
+
const event = new CustomEvent('@corvid/ls-update', {
|
|
259
|
+
detail: {
|
|
260
|
+
key,
|
|
261
|
+
value
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
document.dispatchEvent(event);
|
|
265
|
+
}
|
|
266
|
+
localStorage.setItem(key, value);
|
|
267
|
+
}
|
|
268
|
+
function set(key, value, broadcast = false) {
|
|
269
|
+
const prev = get(key);
|
|
270
|
+
if (prev !== value || broadcast) {
|
|
271
|
+
const event = new CustomEvent('@corvid/ls-update', {
|
|
272
|
+
detail: {
|
|
273
|
+
key,
|
|
274
|
+
value
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
document.dispatchEvent(event);
|
|
278
|
+
}
|
|
279
|
+
localStorage.setItem(key, value);
|
|
280
|
+
}
|
|
281
|
+
function setObj(update, prefix, broadcast = false) {
|
|
282
|
+
const flatten = (ob)=>{
|
|
283
|
+
const ret = {};
|
|
284
|
+
for(let i in ob)if (ob.hasOwnProperty(i)) if ('object' == typeof ob[i] && null !== ob[i]) {
|
|
285
|
+
const flat = flatten(ob[i]);
|
|
286
|
+
for(let x in flat)if (flat.hasOwnProperty(x)) ret[`${i}.${x}`] = flat[x];
|
|
287
|
+
} else ret[i] = ob[i];
|
|
288
|
+
return ret;
|
|
289
|
+
};
|
|
290
|
+
for (let [k, v] of Object.entries(flatten(update))){
|
|
291
|
+
let key = k;
|
|
292
|
+
if (prefix) key = `${prefix}.${k}`;
|
|
293
|
+
set(key, v, broadcast);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function listen(key, cb) {
|
|
297
|
+
document.addEventListener('@corvid/ls-update', (ev)=>{
|
|
298
|
+
if (ev.detail.key === key || '*' === key) {
|
|
299
|
+
if (cb instanceof dom_el) {
|
|
300
|
+
if (ev.detail.key === key) cb.content(ev.detail.value);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
cb({
|
|
304
|
+
key: ev.detail.key,
|
|
305
|
+
value: ev.detail.value
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
function clear(key) {
|
|
311
|
+
localStorage.removeItem(key);
|
|
312
|
+
}
|
|
313
|
+
export { clear, get, listen, set, setObj, local_storage_update as update };
|
package/dist/network.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type requestOpts = {
|
|
|
19
19
|
password: string;
|
|
20
20
|
};
|
|
21
21
|
body?: Object;
|
|
22
|
-
|
|
22
|
+
expect?: number;
|
|
23
23
|
credentials?: RequestCredentials;
|
|
24
24
|
insecureNoVerify?: boolean;
|
|
25
25
|
};
|
|
@@ -64,7 +64,7 @@ export declare class request {
|
|
|
64
64
|
path?: string;
|
|
65
65
|
params?: Object;
|
|
66
66
|
override?: {
|
|
67
|
-
|
|
67
|
+
expect?: number;
|
|
68
68
|
params?: Object;
|
|
69
69
|
headers?: Object;
|
|
70
70
|
body?: Object;
|
package/dist/network.js
CHANGED
|
@@ -138,10 +138,10 @@ class request {
|
|
|
138
138
|
});
|
|
139
139
|
this.log.debug(`${this.opts.method} ${url}`);
|
|
140
140
|
const res = await fetch(url, options);
|
|
141
|
-
const
|
|
142
|
-
if (res.status !==
|
|
141
|
+
const expect = override.expect || this.opts.expect;
|
|
142
|
+
if (res.status !== expect) {
|
|
143
143
|
const body = await res.json();
|
|
144
|
-
throw new Error(`bad response ${res.status} !== ${
|
|
144
|
+
throw new Error(`bad response ${res.status} !== ${expect}, body: ${body}`);
|
|
145
145
|
}
|
|
146
146
|
return await res.json();
|
|
147
147
|
}
|
|
@@ -152,7 +152,7 @@ class request {
|
|
|
152
152
|
if (opts.type && 'json' !== opts.type) throw new Error('this class only provides json requests');
|
|
153
153
|
if (!opts.url) throw new Error('must provide url');
|
|
154
154
|
this.opts = opts;
|
|
155
|
-
if (!this.opts.
|
|
155
|
+
if (!this.opts.expect) this.opts.expect = 200;
|
|
156
156
|
if (!this.opts.method) this.opts.method = 'GET';
|
|
157
157
|
if (!this.opts.headers) this.opts.headers = {};
|
|
158
158
|
if (!this.opts.credentials) this.opts.credentials = 'omit';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taybart/corvid",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
"types": "./dist/dom.d.ts",
|
|
13
13
|
"import": "./dist/dom.js"
|
|
14
14
|
},
|
|
15
|
+
"./local_storage": {
|
|
16
|
+
"types": "./dist/ls.d.ts",
|
|
17
|
+
"import": "./dist/ls.js"
|
|
18
|
+
},
|
|
15
19
|
"./network": {
|
|
16
20
|
"types": "./dist/network.d.ts",
|
|
17
21
|
"import": "./dist/network.js"
|