zero-query 1.1.1 → 1.2.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/LICENSE +21 -21
- package/README.md +2 -0
- package/cli/args.js +33 -33
- package/cli/commands/build-api.js +443 -442
- package/cli/commands/build.js +254 -247
- package/cli/commands/bundle.js +1228 -1224
- package/cli/commands/create.js +137 -121
- package/cli/commands/dev/devtools/index.js +56 -56
- package/cli/commands/dev/devtools/js/components.js +49 -49
- package/cli/commands/dev/devtools/js/core.js +423 -423
- package/cli/commands/dev/devtools/js/elements.js +421 -421
- package/cli/commands/dev/devtools/js/network.js +166 -166
- package/cli/commands/dev/devtools/js/performance.js +73 -73
- package/cli/commands/dev/devtools/js/router.js +105 -105
- package/cli/commands/dev/devtools/js/source.js +132 -132
- package/cli/commands/dev/devtools/js/stats.js +35 -35
- package/cli/commands/dev/devtools/js/tabs.js +79 -79
- package/cli/commands/dev/devtools/panel.html +95 -95
- package/cli/commands/dev/devtools/styles.css +244 -244
- package/cli/commands/dev/index.js +107 -107
- package/cli/commands/dev/logger.js +75 -75
- package/cli/commands/dev/overlay.js +858 -858
- package/cli/commands/dev/server.js +220 -220
- package/cli/commands/dev/validator.js +94 -94
- package/cli/commands/dev/watcher.js +172 -172
- package/cli/help.js +114 -112
- package/cli/index.js +52 -52
- package/cli/scaffold/default/LICENSE +21 -21
- package/cli/scaffold/default/app/app.js +207 -207
- package/cli/scaffold/default/app/components/about.js +201 -201
- package/cli/scaffold/default/app/components/api-demo.js +143 -143
- package/cli/scaffold/default/app/components/contact-card.js +231 -231
- package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
- package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
- package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
- package/cli/scaffold/default/app/components/counter.js +127 -127
- package/cli/scaffold/default/app/components/home.js +249 -249
- package/cli/scaffold/default/app/components/not-found.js +16 -16
- package/cli/scaffold/default/app/components/playground/playground.css +115 -115
- package/cli/scaffold/default/app/components/playground/playground.html +161 -161
- package/cli/scaffold/default/app/components/playground/playground.js +116 -116
- package/cli/scaffold/default/app/components/todos.js +225 -225
- package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
- package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
- package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
- package/cli/scaffold/default/app/routes.js +15 -15
- package/cli/scaffold/default/app/store.js +101 -101
- package/cli/scaffold/default/global.css +552 -552
- package/cli/scaffold/default/index.html +99 -99
- package/cli/scaffold/minimal/app/app.js +85 -85
- package/cli/scaffold/minimal/app/components/about.js +68 -68
- package/cli/scaffold/minimal/app/components/counter.js +122 -122
- package/cli/scaffold/minimal/app/components/home.js +68 -68
- package/cli/scaffold/minimal/app/components/not-found.js +16 -16
- package/cli/scaffold/minimal/app/routes.js +9 -9
- package/cli/scaffold/minimal/app/store.js +36 -36
- package/cli/scaffold/minimal/global.css +300 -300
- package/cli/scaffold/minimal/index.html +44 -44
- package/cli/scaffold/ssr/app/app.js +41 -41
- package/cli/scaffold/ssr/app/components/about.js +55 -55
- package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
- package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
- package/cli/scaffold/ssr/app/components/home.js +37 -37
- package/cli/scaffold/ssr/app/components/not-found.js +15 -15
- package/cli/scaffold/ssr/app/routes.js +8 -8
- package/cli/scaffold/ssr/global.css +228 -228
- package/cli/scaffold/ssr/index.html +37 -37
- package/cli/scaffold/ssr/package.json +8 -8
- package/cli/scaffold/ssr/server/data/posts.js +144 -144
- package/cli/scaffold/ssr/server/index.js +213 -213
- package/cli/scaffold/webrtc/app/app.js +11 -0
- package/cli/scaffold/webrtc/app/components/video-room.js +295 -0
- package/cli/scaffold/webrtc/app/lib/room.js +252 -0
- package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
- package/cli/scaffold/webrtc/global.css +250 -0
- package/cli/scaffold/webrtc/index.html +21 -0
- package/cli/utils.js +305 -287
- package/dist/API.md +661 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +10313 -6614
- package/dist/zquery.min.js +8 -631
- package/index.d.ts +570 -371
- package/index.js +311 -240
- package/package.json +76 -70
- package/src/component.js +1709 -1691
- package/src/core.js +921 -921
- package/src/diff.js +497 -497
- package/src/errors.js +209 -209
- package/src/expression.js +922 -922
- package/src/http.js +242 -242
- package/src/package.json +1 -1
- package/src/reactive.js +255 -255
- package/src/router.js +843 -843
- package/src/ssr.js +418 -418
- package/src/store.js +318 -318
- package/src/utils.js +515 -515
- package/src/webrtc/e2ee.js +351 -0
- package/src/webrtc/errors.js +116 -0
- package/src/webrtc/ice.js +301 -0
- package/src/webrtc/index.js +131 -0
- package/src/webrtc/joinToken.js +119 -0
- package/src/webrtc/observe.js +172 -0
- package/src/webrtc/peer.js +351 -0
- package/src/webrtc/reactive.js +268 -0
- package/src/webrtc/room.js +625 -0
- package/src/webrtc/sdp.js +302 -0
- package/src/webrtc/sfu/index.js +43 -0
- package/src/webrtc/sfu/livekit.js +131 -0
- package/src/webrtc/sfu/mediasoup.js +150 -0
- package/src/webrtc/signaling.js +373 -0
- package/src/webrtc/turn.js +237 -0
- package/tests/_helpers/webrtcFakes.js +289 -0
- package/tests/audit.test.js +4158 -4158
- package/tests/cli.test.js +1136 -1103
- package/tests/compare.test.js +497 -486
- package/tests/component.test.js +3969 -3938
- package/tests/core.test.js +1910 -1910
- package/tests/dev-server.test.js +489 -489
- package/tests/diff.test.js +1416 -1416
- package/tests/docs.test.js +1664 -1650
- package/tests/electron-features.test.js +864 -864
- package/tests/errors.test.js +619 -619
- package/tests/expression.test.js +1056 -1056
- package/tests/http.test.js +648 -648
- package/tests/reactive.test.js +819 -819
- package/tests/router.test.js +2327 -2327
- package/tests/ssr.test.js +870 -870
- package/tests/store.test.js +830 -830
- package/tests/test-minifier.js +153 -153
- package/tests/test-ssr.js +27 -27
- package/tests/utils.test.js +1377 -1377
- package/tests/webrtc/e2ee.test.js +283 -0
- package/tests/webrtc/ice.test.js +202 -0
- package/tests/webrtc/joinToken.test.js +89 -0
- package/tests/webrtc/observe.test.js +111 -0
- package/tests/webrtc/peer.test.js +373 -0
- package/tests/webrtc/reactive.test.js +235 -0
- package/tests/webrtc/room.test.js +406 -0
- package/tests/webrtc/sdp.test.js +151 -0
- package/tests/webrtc/sfu-livekit.test.js +119 -0
- package/tests/webrtc/sfu.test.js +160 -0
- package/tests/webrtc/signaling.test.js +251 -0
- package/tests/webrtc/turn.test.js +256 -0
- package/types/collection.d.ts +383 -383
- package/types/component.d.ts +186 -186
- package/types/errors.d.ts +135 -135
- package/types/http.d.ts +92 -92
- package/types/misc.d.ts +201 -201
- package/types/reactive.d.ts +98 -98
- package/types/router.d.ts +190 -190
- package/types/ssr.d.ts +102 -102
- package/types/store.d.ts +146 -146
- package/types/utils.d.ts +245 -245
- package/types/webrtc.d.ts +653 -0
|
@@ -1,280 +1,280 @@
|
|
|
1
|
-
// toolkit.js - HTTP, utilities, and advanced store features
|
|
2
|
-
//
|
|
3
|
-
// Features used:
|
|
4
|
-
// $.http / $.post / $.put / $.delete - HTTP client + interceptors
|
|
5
|
-
// $.pipe / $.memoize / $.retry - functional utilities
|
|
6
|
-
// $.groupBy / $.chunk / $.unique - collection helpers
|
|
7
|
-
// store.use / snapshot / history - middleware & time-travel
|
|
8
|
-
// templateUrl / styleUrl - external template & styles
|
|
9
|
-
|
|
10
|
-
$.component('toolkit-page', {
|
|
11
|
-
templateUrl: 'toolkit.html',
|
|
12
|
-
styleUrl: 'toolkit.css',
|
|
13
|
-
|
|
14
|
-
state: () => ({
|
|
15
|
-
activeTab: 'http',
|
|
16
|
-
httpLog: [],
|
|
17
|
-
httpMethod: '',
|
|
18
|
-
httpEndpoint: '',
|
|
19
|
-
httpStatusCode: null,
|
|
20
|
-
httpOutput: '',
|
|
21
|
-
searchQuery: '',
|
|
22
|
-
searchResults: [],
|
|
23
|
-
searchLoading: false,
|
|
24
|
-
activeUtil: '',
|
|
25
|
-
utilOutput: '',
|
|
26
|
-
storeLog: [],
|
|
27
|
-
storeSnap: null,
|
|
28
|
-
storeHistory: null,
|
|
29
|
-
// Derived state for template expressions
|
|
30
|
-
methodClass: 'get',
|
|
31
|
-
statusOk: false,
|
|
32
|
-
actionCount: 0,
|
|
33
|
-
}),
|
|
34
|
-
|
|
35
|
-
// init() lifecycle - runs before the first render
|
|
36
|
-
init() {
|
|
37
|
-
this._prevBaseURL = $.http.getConfig().baseURL || '';
|
|
38
|
-
$.http.configure({ baseURL: 'https://jsonplaceholder.typicode.com' });
|
|
39
|
-
|
|
40
|
-
this._unsubReq = $.http.onRequest((opts, url) => {
|
|
41
|
-
this._pushLog('→ ' + (opts.method || 'GET') + ' ' + url);
|
|
42
|
-
});
|
|
43
|
-
this._unsubRes = $.http.onResponse((result) => {
|
|
44
|
-
this._pushLog('← ' + result.status + ' ' + (result.statusText || 'OK'));
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
this._mwActive = true;
|
|
48
|
-
$.getStore('main').use((action, args) => {
|
|
49
|
-
if (!this._mwActive) return;
|
|
50
|
-
const raw = this.state.storeLog.__raw || this.state.storeLog;
|
|
51
|
-
this.state.storeLog = [...raw, {
|
|
52
|
-
id: Date.now(), action,
|
|
53
|
-
args: JSON.stringify(args).slice(0, 60),
|
|
54
|
-
time: new Date().toLocaleTimeString(),
|
|
55
|
-
}].slice(-8);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Keep actionCount in sync with store history
|
|
59
|
-
this._syncActionCount();
|
|
60
|
-
this._storeSub = $.getStore('main').subscribe(() => this._syncActionCount());
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
destroyed() {
|
|
64
|
-
this._mwActive = false;
|
|
65
|
-
if (this._unsubReq) this._unsubReq();
|
|
66
|
-
if (this._unsubRes) this._unsubRes();
|
|
67
|
-
if (this._abortCtrl) this._abortCtrl.abort();
|
|
68
|
-
if (this._storeSub) this._storeSub();
|
|
69
|
-
$.http.configure({ baseURL: this._prevBaseURL });
|
|
70
|
-
},
|
|
71
|
-
|
|
72
|
-
_pushLog(msg) {
|
|
73
|
-
const raw = this.state.httpLog.__raw || this.state.httpLog;
|
|
74
|
-
this.state.httpLog = [...raw, { id: Date.now(), msg }].slice(-10);
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
_syncActionCount() {
|
|
78
|
-
const store = $.getStore('main');
|
|
79
|
-
this.state.actionCount = store.history ? store.history.length : 0;
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
_updateHttpMeta(method, endpoint) {
|
|
83
|
-
this.state.httpMethod = method;
|
|
84
|
-
this.state.httpEndpoint = endpoint;
|
|
85
|
-
this.state.httpOutput = '';
|
|
86
|
-
this.state.httpStatusCode = null;
|
|
87
|
-
this.state.methodClass = method === 'DELETE' ? 'del' : (method || 'get').toLowerCase();
|
|
88
|
-
this.state.statusOk = false;
|
|
89
|
-
},
|
|
90
|
-
|
|
91
|
-
_setHttpResult(status, output) {
|
|
92
|
-
this.state.httpStatusCode = status;
|
|
93
|
-
this.state.statusOk = status >= 200 && status < 400;
|
|
94
|
-
this.state.httpOutput = output;
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
setTab(tab) {
|
|
98
|
-
this.state.activeTab = tab;
|
|
99
|
-
this.state.utilOutput = '';
|
|
100
|
-
this.state.activeUtil = '';
|
|
101
|
-
this.state.httpOutput = '';
|
|
102
|
-
this.state.httpMethod = '';
|
|
103
|
-
this.state.httpStatusCode = null;
|
|
104
|
-
this.state.storeSnap = null;
|
|
105
|
-
this.state.storeHistory = null;
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
/* -- HTTP demos --------------------------------------------- */
|
|
109
|
-
|
|
110
|
-
async doGet() {
|
|
111
|
-
this._updateHttpMeta('GET', '/posts/1');
|
|
112
|
-
try {
|
|
113
|
-
const { data, status } = await $.get('/posts/1');
|
|
114
|
-
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
115
|
-
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
async doPost() {
|
|
119
|
-
this._updateHttpMeta('POST', '/posts');
|
|
120
|
-
try {
|
|
121
|
-
const { data, status } = await $.post('/posts', {
|
|
122
|
-
title: 'Created with zQuery', body: 'Sent via $.post()', userId: 1,
|
|
123
|
-
});
|
|
124
|
-
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
125
|
-
$.bus.emit('toast', { message: 'POST successful!', type: 'success' });
|
|
126
|
-
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
127
|
-
},
|
|
128
|
-
|
|
129
|
-
async doPut() {
|
|
130
|
-
this._updateHttpMeta('PUT', '/posts/1');
|
|
131
|
-
try {
|
|
132
|
-
const { data, status } = await $.put('/posts/1', {
|
|
133
|
-
id: 1, title: 'Updated via $.put()', body: 'Full resource replacement', userId: 1,
|
|
134
|
-
});
|
|
135
|
-
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
136
|
-
$.bus.emit('toast', { message: 'PUT successful!', type: 'success' });
|
|
137
|
-
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
async doDelete() {
|
|
141
|
-
this._updateHttpMeta('DELETE', '/posts/1');
|
|
142
|
-
try {
|
|
143
|
-
const { status } = await $.delete('/posts/1');
|
|
144
|
-
this._setHttpResult(status, 'Resource deleted successfully.');
|
|
145
|
-
$.bus.emit('toast', { message: 'DELETE sent', type: 'info' });
|
|
146
|
-
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
async doSearch(e) {
|
|
150
|
-
const q = e.target.value.trim().toLowerCase();
|
|
151
|
-
this.state.searchQuery = e.target.value;
|
|
152
|
-
if (!q) { this.state.searchResults = []; return; }
|
|
153
|
-
|
|
154
|
-
if (this._abortCtrl) this._abortCtrl.abort();
|
|
155
|
-
this._abortCtrl = $.http.createAbort();
|
|
156
|
-
this.state.searchLoading = true;
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
const { data } = await $.get('/users', null, { signal: this._abortCtrl.signal });
|
|
160
|
-
this.state.searchResults = data.filter(u =>
|
|
161
|
-
u.name.toLowerCase().includes(q) || u.email.toLowerCase().includes(q)
|
|
162
|
-
).slice(0, 5);
|
|
163
|
-
} catch (err) {
|
|
164
|
-
if (err.name !== 'AbortError') this.state.searchResults = [];
|
|
165
|
-
} finally { this.state.searchLoading = false; }
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
/* -- Utility demos ------------------------------------------- */
|
|
169
|
-
|
|
170
|
-
runUtil(name) {
|
|
171
|
-
this.state.activeUtil = name;
|
|
172
|
-
const demos = {
|
|
173
|
-
pipe: () => {
|
|
174
|
-
const slugify = $.pipe(
|
|
175
|
-
s => s.trim(), s => s.toLowerCase(),
|
|
176
|
-
s => s.replace(/[^a-z0-9]+/g, '-'), s => s.replace(/^-|-$/g, ''),
|
|
177
|
-
);
|
|
178
|
-
const input = ' Hello World! Creating Slugs ';
|
|
179
|
-
return `$.pipe(trim, lower, replace, strip)\n\nInput: "${input}"\nOutput: "${slugify(input)}"`;
|
|
180
|
-
},
|
|
181
|
-
memoize: () => {
|
|
182
|
-
const slowFib = (n) => n <= 1 ? n : slowFib(n - 1) + slowFib(n - 2);
|
|
183
|
-
const t0 = performance.now();
|
|
184
|
-
const result = slowFib(35);
|
|
185
|
-
const slowTime = performance.now() - t0;
|
|
186
|
-
|
|
187
|
-
const memoFib = $.memoize((n) => n <= 1 ? n : memoFib(n - 1) + memoFib(n - 2));
|
|
188
|
-
const t1 = performance.now();
|
|
189
|
-
memoFib(35);
|
|
190
|
-
const fastTime = performance.now() - t1;
|
|
191
|
-
|
|
192
|
-
const t2 = performance.now();
|
|
193
|
-
memoFib(35);
|
|
194
|
-
const cachedTime = performance.now() - t2;
|
|
195
|
-
|
|
196
|
-
return [
|
|
197
|
-
`$.memoize(fibonacci)`,
|
|
198
|
-
``,
|
|
199
|
-
`fib(35) = ${result}`,
|
|
200
|
-
`----------------------------`,
|
|
201
|
-
`No memoize: ${slowTime.toFixed(1)} ms`,
|
|
202
|
-
`Memoized: ${fastTime.toFixed(3)} ms`,
|
|
203
|
-
`Cached: ${cachedTime.toFixed(4)} ms ⚡`,
|
|
204
|
-
``,
|
|
205
|
-
`Speedup: ${(slowTime / Math.max(fastTime, 0.001)).toFixed(0)}× faster`,
|
|
206
|
-
].join('\n');
|
|
207
|
-
},
|
|
208
|
-
retry: async () => {
|
|
209
|
-
this.state.utilOutput = '⏳ Running with exponential backoff…';
|
|
210
|
-
const log = [];
|
|
211
|
-
try {
|
|
212
|
-
await $.retry(async (attempt) => {
|
|
213
|
-
log.push(` #${attempt} ${attempt < 3 ? '✗ failed' : '✓ succeeded!'}`);
|
|
214
|
-
if (attempt < 3) throw new Error('Simulated failure');
|
|
215
|
-
return 'ok';
|
|
216
|
-
}, { attempts: 3, delay: 400, backoff: 2 });
|
|
217
|
-
} catch (e) { log.push(' All attempts failed.'); }
|
|
218
|
-
this.state.utilOutput = '$.retry({ attempts: 3, delay: 400, backoff: 2 })\n\n' + log.join('\n');
|
|
219
|
-
},
|
|
220
|
-
arrays: () => {
|
|
221
|
-
const data = [
|
|
222
|
-
{ name: 'Alice', dept: 'eng' }, { name: 'Bob', dept: 'qa' },
|
|
223
|
-
{ name: 'Carol', dept: 'eng' }, { name: 'Dave', dept: 'design' },
|
|
224
|
-
];
|
|
225
|
-
return [
|
|
226
|
-
'$.groupBy(people, p => p.dept)',
|
|
227
|
-
JSON.stringify($.groupBy(data, p => p.dept), null, 2),
|
|
228
|
-
'',
|
|
229
|
-
'$.chunk($.range(1,9), 3) → ' + JSON.stringify($.chunk($.range(1, 9), 3)),
|
|
230
|
-
'$.unique([3,1,4,1,5,9]) → ' + JSON.stringify($.unique([3, 1, 4, 1, 5, 9, 2, 5])),
|
|
231
|
-
'$.range(0, 10, 2) → ' + JSON.stringify($.range(0, 10, 2)),
|
|
232
|
-
].join('\n');
|
|
233
|
-
},
|
|
234
|
-
objects: () => {
|
|
235
|
-
const obj = { name: 'Tony', email: 'tony@dev.io', role: 'admin', secret: 'abc' };
|
|
236
|
-
const a = { x: 1, nested: { y: 2 } };
|
|
237
|
-
return [
|
|
238
|
-
`$.pick(obj, ["name","email"]) → ${JSON.stringify($.pick(obj, ['name', 'email']))}`,
|
|
239
|
-
`$.omit(obj, ["secret"]) → ${JSON.stringify($.omit(obj, ['secret']))}`,
|
|
240
|
-
`$.isEqual(a, b) → ${$.isEqual(a, { x: 1, nested: { y: 2 } })}`,
|
|
241
|
-
``,
|
|
242
|
-
`$.deepMerge({}, a, { nested: { z: 3 } })`,
|
|
243
|
-
JSON.stringify($.deepMerge({}, a, { nested: { z: 3 } }), null, 2),
|
|
244
|
-
].join('\n');
|
|
245
|
-
},
|
|
246
|
-
strings: () => [
|
|
247
|
-
`$.capitalize('hello') → "${$.capitalize('hello')}"`,
|
|
248
|
-
`$.truncate('Hello World!', 8) → "${$.truncate('Hello World!', 8)}"`,
|
|
249
|
-
`$.camelCase('my-component') → "${$.camelCase('my-component')}"`,
|
|
250
|
-
`$.kebabCase('myComponent') → "${$.kebabCase('myComponent')}"`,
|
|
251
|
-
``,
|
|
252
|
-
`$.param({ q: 'test', page: 2 })`,
|
|
253
|
-
` → "${$.param({ q: 'test', page: 2 })}"`,
|
|
254
|
-
``,
|
|
255
|
-
`$.parseQuery('?q=test&page=2')`,
|
|
256
|
-
` → ${JSON.stringify($.parseQuery('?q=test&page=2'))}`,
|
|
257
|
-
].join('\n'),
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
const fn = demos[name];
|
|
261
|
-
if (!fn) return;
|
|
262
|
-
const result = fn();
|
|
263
|
-
if (result && typeof result.then === 'function') return;
|
|
264
|
-
this.state.utilOutput = result;
|
|
265
|
-
},
|
|
266
|
-
|
|
267
|
-
/* -- Store demos -------------------------------------------- */
|
|
268
|
-
|
|
269
|
-
takeSnapshot() {
|
|
270
|
-
this.state.storeSnap = JSON.stringify($.getStore('main').snapshot(), null, 2);
|
|
271
|
-
this.state.storeHistory = null;
|
|
272
|
-
$.bus.emit('toast', { message: 'Snapshot captured!', type: 'info' });
|
|
273
|
-
},
|
|
274
|
-
|
|
275
|
-
viewHistory() {
|
|
276
|
-
const h = $.getStore('main').history.slice(-6);
|
|
277
|
-
this.state.storeHistory = JSON.stringify(h, null, 2);
|
|
278
|
-
this.state.storeSnap = null;
|
|
279
|
-
},
|
|
280
|
-
});
|
|
1
|
+
// toolkit.js - HTTP, utilities, and advanced store features
|
|
2
|
+
//
|
|
3
|
+
// Features used:
|
|
4
|
+
// $.http / $.post / $.put / $.delete - HTTP client + interceptors
|
|
5
|
+
// $.pipe / $.memoize / $.retry - functional utilities
|
|
6
|
+
// $.groupBy / $.chunk / $.unique - collection helpers
|
|
7
|
+
// store.use / snapshot / history - middleware & time-travel
|
|
8
|
+
// templateUrl / styleUrl - external template & styles
|
|
9
|
+
|
|
10
|
+
$.component('toolkit-page', {
|
|
11
|
+
templateUrl: 'toolkit.html',
|
|
12
|
+
styleUrl: 'toolkit.css',
|
|
13
|
+
|
|
14
|
+
state: () => ({
|
|
15
|
+
activeTab: 'http',
|
|
16
|
+
httpLog: [],
|
|
17
|
+
httpMethod: '',
|
|
18
|
+
httpEndpoint: '',
|
|
19
|
+
httpStatusCode: null,
|
|
20
|
+
httpOutput: '',
|
|
21
|
+
searchQuery: '',
|
|
22
|
+
searchResults: [],
|
|
23
|
+
searchLoading: false,
|
|
24
|
+
activeUtil: '',
|
|
25
|
+
utilOutput: '',
|
|
26
|
+
storeLog: [],
|
|
27
|
+
storeSnap: null,
|
|
28
|
+
storeHistory: null,
|
|
29
|
+
// Derived state for template expressions
|
|
30
|
+
methodClass: 'get',
|
|
31
|
+
statusOk: false,
|
|
32
|
+
actionCount: 0,
|
|
33
|
+
}),
|
|
34
|
+
|
|
35
|
+
// init() lifecycle - runs before the first render
|
|
36
|
+
init() {
|
|
37
|
+
this._prevBaseURL = $.http.getConfig().baseURL || '';
|
|
38
|
+
$.http.configure({ baseURL: 'https://jsonplaceholder.typicode.com' });
|
|
39
|
+
|
|
40
|
+
this._unsubReq = $.http.onRequest((opts, url) => {
|
|
41
|
+
this._pushLog('→ ' + (opts.method || 'GET') + ' ' + url);
|
|
42
|
+
});
|
|
43
|
+
this._unsubRes = $.http.onResponse((result) => {
|
|
44
|
+
this._pushLog('← ' + result.status + ' ' + (result.statusText || 'OK'));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
this._mwActive = true;
|
|
48
|
+
$.getStore('main').use((action, args) => {
|
|
49
|
+
if (!this._mwActive) return;
|
|
50
|
+
const raw = this.state.storeLog.__raw || this.state.storeLog;
|
|
51
|
+
this.state.storeLog = [...raw, {
|
|
52
|
+
id: Date.now(), action,
|
|
53
|
+
args: JSON.stringify(args).slice(0, 60),
|
|
54
|
+
time: new Date().toLocaleTimeString(),
|
|
55
|
+
}].slice(-8);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Keep actionCount in sync with store history
|
|
59
|
+
this._syncActionCount();
|
|
60
|
+
this._storeSub = $.getStore('main').subscribe(() => this._syncActionCount());
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
destroyed() {
|
|
64
|
+
this._mwActive = false;
|
|
65
|
+
if (this._unsubReq) this._unsubReq();
|
|
66
|
+
if (this._unsubRes) this._unsubRes();
|
|
67
|
+
if (this._abortCtrl) this._abortCtrl.abort();
|
|
68
|
+
if (this._storeSub) this._storeSub();
|
|
69
|
+
$.http.configure({ baseURL: this._prevBaseURL });
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
_pushLog(msg) {
|
|
73
|
+
const raw = this.state.httpLog.__raw || this.state.httpLog;
|
|
74
|
+
this.state.httpLog = [...raw, { id: Date.now(), msg }].slice(-10);
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
_syncActionCount() {
|
|
78
|
+
const store = $.getStore('main');
|
|
79
|
+
this.state.actionCount = store.history ? store.history.length : 0;
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
_updateHttpMeta(method, endpoint) {
|
|
83
|
+
this.state.httpMethod = method;
|
|
84
|
+
this.state.httpEndpoint = endpoint;
|
|
85
|
+
this.state.httpOutput = '';
|
|
86
|
+
this.state.httpStatusCode = null;
|
|
87
|
+
this.state.methodClass = method === 'DELETE' ? 'del' : (method || 'get').toLowerCase();
|
|
88
|
+
this.state.statusOk = false;
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
_setHttpResult(status, output) {
|
|
92
|
+
this.state.httpStatusCode = status;
|
|
93
|
+
this.state.statusOk = status >= 200 && status < 400;
|
|
94
|
+
this.state.httpOutput = output;
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
setTab(tab) {
|
|
98
|
+
this.state.activeTab = tab;
|
|
99
|
+
this.state.utilOutput = '';
|
|
100
|
+
this.state.activeUtil = '';
|
|
101
|
+
this.state.httpOutput = '';
|
|
102
|
+
this.state.httpMethod = '';
|
|
103
|
+
this.state.httpStatusCode = null;
|
|
104
|
+
this.state.storeSnap = null;
|
|
105
|
+
this.state.storeHistory = null;
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
/* -- HTTP demos --------------------------------------------- */
|
|
109
|
+
|
|
110
|
+
async doGet() {
|
|
111
|
+
this._updateHttpMeta('GET', '/posts/1');
|
|
112
|
+
try {
|
|
113
|
+
const { data, status } = await $.get('/posts/1');
|
|
114
|
+
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
115
|
+
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
async doPost() {
|
|
119
|
+
this._updateHttpMeta('POST', '/posts');
|
|
120
|
+
try {
|
|
121
|
+
const { data, status } = await $.post('/posts', {
|
|
122
|
+
title: 'Created with zQuery', body: 'Sent via $.post()', userId: 1,
|
|
123
|
+
});
|
|
124
|
+
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
125
|
+
$.bus.emit('toast', { message: 'POST successful!', type: 'success' });
|
|
126
|
+
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
async doPut() {
|
|
130
|
+
this._updateHttpMeta('PUT', '/posts/1');
|
|
131
|
+
try {
|
|
132
|
+
const { data, status } = await $.put('/posts/1', {
|
|
133
|
+
id: 1, title: 'Updated via $.put()', body: 'Full resource replacement', userId: 1,
|
|
134
|
+
});
|
|
135
|
+
this._setHttpResult(status, JSON.stringify(data, null, 2));
|
|
136
|
+
$.bus.emit('toast', { message: 'PUT successful!', type: 'success' });
|
|
137
|
+
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async doDelete() {
|
|
141
|
+
this._updateHttpMeta('DELETE', '/posts/1');
|
|
142
|
+
try {
|
|
143
|
+
const { status } = await $.delete('/posts/1');
|
|
144
|
+
this._setHttpResult(status, 'Resource deleted successfully.');
|
|
145
|
+
$.bus.emit('toast', { message: 'DELETE sent', type: 'info' });
|
|
146
|
+
} catch (e) { this._setHttpResult(0, 'Error: ' + e.message); }
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
async doSearch(e) {
|
|
150
|
+
const q = e.target.value.trim().toLowerCase();
|
|
151
|
+
this.state.searchQuery = e.target.value;
|
|
152
|
+
if (!q) { this.state.searchResults = []; return; }
|
|
153
|
+
|
|
154
|
+
if (this._abortCtrl) this._abortCtrl.abort();
|
|
155
|
+
this._abortCtrl = $.http.createAbort();
|
|
156
|
+
this.state.searchLoading = true;
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const { data } = await $.get('/users', null, { signal: this._abortCtrl.signal });
|
|
160
|
+
this.state.searchResults = data.filter(u =>
|
|
161
|
+
u.name.toLowerCase().includes(q) || u.email.toLowerCase().includes(q)
|
|
162
|
+
).slice(0, 5);
|
|
163
|
+
} catch (err) {
|
|
164
|
+
if (err.name !== 'AbortError') this.state.searchResults = [];
|
|
165
|
+
} finally { this.state.searchLoading = false; }
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/* -- Utility demos ------------------------------------------- */
|
|
169
|
+
|
|
170
|
+
runUtil(name) {
|
|
171
|
+
this.state.activeUtil = name;
|
|
172
|
+
const demos = {
|
|
173
|
+
pipe: () => {
|
|
174
|
+
const slugify = $.pipe(
|
|
175
|
+
s => s.trim(), s => s.toLowerCase(),
|
|
176
|
+
s => s.replace(/[^a-z0-9]+/g, '-'), s => s.replace(/^-|-$/g, ''),
|
|
177
|
+
);
|
|
178
|
+
const input = ' Hello World! Creating Slugs ';
|
|
179
|
+
return `$.pipe(trim, lower, replace, strip)\n\nInput: "${input}"\nOutput: "${slugify(input)}"`;
|
|
180
|
+
},
|
|
181
|
+
memoize: () => {
|
|
182
|
+
const slowFib = (n) => n <= 1 ? n : slowFib(n - 1) + slowFib(n - 2);
|
|
183
|
+
const t0 = performance.now();
|
|
184
|
+
const result = slowFib(35);
|
|
185
|
+
const slowTime = performance.now() - t0;
|
|
186
|
+
|
|
187
|
+
const memoFib = $.memoize((n) => n <= 1 ? n : memoFib(n - 1) + memoFib(n - 2));
|
|
188
|
+
const t1 = performance.now();
|
|
189
|
+
memoFib(35);
|
|
190
|
+
const fastTime = performance.now() - t1;
|
|
191
|
+
|
|
192
|
+
const t2 = performance.now();
|
|
193
|
+
memoFib(35);
|
|
194
|
+
const cachedTime = performance.now() - t2;
|
|
195
|
+
|
|
196
|
+
return [
|
|
197
|
+
`$.memoize(fibonacci)`,
|
|
198
|
+
``,
|
|
199
|
+
`fib(35) = ${result}`,
|
|
200
|
+
`----------------------------`,
|
|
201
|
+
`No memoize: ${slowTime.toFixed(1)} ms`,
|
|
202
|
+
`Memoized: ${fastTime.toFixed(3)} ms`,
|
|
203
|
+
`Cached: ${cachedTime.toFixed(4)} ms ⚡`,
|
|
204
|
+
``,
|
|
205
|
+
`Speedup: ${(slowTime / Math.max(fastTime, 0.001)).toFixed(0)}× faster`,
|
|
206
|
+
].join('\n');
|
|
207
|
+
},
|
|
208
|
+
retry: async () => {
|
|
209
|
+
this.state.utilOutput = '⏳ Running with exponential backoff…';
|
|
210
|
+
const log = [];
|
|
211
|
+
try {
|
|
212
|
+
await $.retry(async (attempt) => {
|
|
213
|
+
log.push(` #${attempt} ${attempt < 3 ? '✗ failed' : '✓ succeeded!'}`);
|
|
214
|
+
if (attempt < 3) throw new Error('Simulated failure');
|
|
215
|
+
return 'ok';
|
|
216
|
+
}, { attempts: 3, delay: 400, backoff: 2 });
|
|
217
|
+
} catch (e) { log.push(' All attempts failed.'); }
|
|
218
|
+
this.state.utilOutput = '$.retry({ attempts: 3, delay: 400, backoff: 2 })\n\n' + log.join('\n');
|
|
219
|
+
},
|
|
220
|
+
arrays: () => {
|
|
221
|
+
const data = [
|
|
222
|
+
{ name: 'Alice', dept: 'eng' }, { name: 'Bob', dept: 'qa' },
|
|
223
|
+
{ name: 'Carol', dept: 'eng' }, { name: 'Dave', dept: 'design' },
|
|
224
|
+
];
|
|
225
|
+
return [
|
|
226
|
+
'$.groupBy(people, p => p.dept)',
|
|
227
|
+
JSON.stringify($.groupBy(data, p => p.dept), null, 2),
|
|
228
|
+
'',
|
|
229
|
+
'$.chunk($.range(1,9), 3) → ' + JSON.stringify($.chunk($.range(1, 9), 3)),
|
|
230
|
+
'$.unique([3,1,4,1,5,9]) → ' + JSON.stringify($.unique([3, 1, 4, 1, 5, 9, 2, 5])),
|
|
231
|
+
'$.range(0, 10, 2) → ' + JSON.stringify($.range(0, 10, 2)),
|
|
232
|
+
].join('\n');
|
|
233
|
+
},
|
|
234
|
+
objects: () => {
|
|
235
|
+
const obj = { name: 'Tony', email: 'tony@dev.io', role: 'admin', secret: 'abc' };
|
|
236
|
+
const a = { x: 1, nested: { y: 2 } };
|
|
237
|
+
return [
|
|
238
|
+
`$.pick(obj, ["name","email"]) → ${JSON.stringify($.pick(obj, ['name', 'email']))}`,
|
|
239
|
+
`$.omit(obj, ["secret"]) → ${JSON.stringify($.omit(obj, ['secret']))}`,
|
|
240
|
+
`$.isEqual(a, b) → ${$.isEqual(a, { x: 1, nested: { y: 2 } })}`,
|
|
241
|
+
``,
|
|
242
|
+
`$.deepMerge({}, a, { nested: { z: 3 } })`,
|
|
243
|
+
JSON.stringify($.deepMerge({}, a, { nested: { z: 3 } }), null, 2),
|
|
244
|
+
].join('\n');
|
|
245
|
+
},
|
|
246
|
+
strings: () => [
|
|
247
|
+
`$.capitalize('hello') → "${$.capitalize('hello')}"`,
|
|
248
|
+
`$.truncate('Hello World!', 8) → "${$.truncate('Hello World!', 8)}"`,
|
|
249
|
+
`$.camelCase('my-component') → "${$.camelCase('my-component')}"`,
|
|
250
|
+
`$.kebabCase('myComponent') → "${$.kebabCase('myComponent')}"`,
|
|
251
|
+
``,
|
|
252
|
+
`$.param({ q: 'test', page: 2 })`,
|
|
253
|
+
` → "${$.param({ q: 'test', page: 2 })}"`,
|
|
254
|
+
``,
|
|
255
|
+
`$.parseQuery('?q=test&page=2')`,
|
|
256
|
+
` → ${JSON.stringify($.parseQuery('?q=test&page=2'))}`,
|
|
257
|
+
].join('\n'),
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const fn = demos[name];
|
|
261
|
+
if (!fn) return;
|
|
262
|
+
const result = fn();
|
|
263
|
+
if (result && typeof result.then === 'function') return;
|
|
264
|
+
this.state.utilOutput = result;
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
/* -- Store demos -------------------------------------------- */
|
|
268
|
+
|
|
269
|
+
takeSnapshot() {
|
|
270
|
+
this.state.storeSnap = JSON.stringify($.getStore('main').snapshot(), null, 2);
|
|
271
|
+
this.state.storeHistory = null;
|
|
272
|
+
$.bus.emit('toast', { message: 'Snapshot captured!', type: 'info' });
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
viewHistory() {
|
|
276
|
+
const h = $.getStore('main').history.slice(-6);
|
|
277
|
+
this.state.storeHistory = JSON.stringify(h, null, 2);
|
|
278
|
+
this.state.storeSnap = null;
|
|
279
|
+
},
|
|
280
|
+
});
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
// routes.js - Route definitions
|
|
2
|
-
//
|
|
3
|
-
// Maps URL paths to component tag names.
|
|
4
|
-
// Also supports :params, wildcards, and lazy loading via `load`.
|
|
5
|
-
|
|
6
|
-
export const routes = [
|
|
7
|
-
{ path: '/', component: 'home-page' },
|
|
8
|
-
{ path: '/counter', component: 'counter-page' },
|
|
9
|
-
{ path: '/todos', component: 'todos-page' },
|
|
10
|
-
{ path: '/contacts', component: 'contacts-page' },
|
|
11
|
-
{ path: '/api', component: 'api-demo' },
|
|
12
|
-
{ path: '/playground', component: 'playground-page' },
|
|
13
|
-
{ path: '/toolkit', component: 'toolkit-page' },
|
|
14
|
-
{ path: '/about', component: 'about-page' },
|
|
15
|
-
];
|
|
1
|
+
// routes.js - Route definitions
|
|
2
|
+
//
|
|
3
|
+
// Maps URL paths to component tag names.
|
|
4
|
+
// Also supports :params, wildcards, and lazy loading via `load`.
|
|
5
|
+
|
|
6
|
+
export const routes = [
|
|
7
|
+
{ path: '/', component: 'home-page' },
|
|
8
|
+
{ path: '/counter', component: 'counter-page' },
|
|
9
|
+
{ path: '/todos', component: 'todos-page' },
|
|
10
|
+
{ path: '/contacts', component: 'contacts-page' },
|
|
11
|
+
{ path: '/api', component: 'api-demo' },
|
|
12
|
+
{ path: '/playground', component: 'playground-page' },
|
|
13
|
+
{ path: '/toolkit', component: 'toolkit-page' },
|
|
14
|
+
{ path: '/about', component: 'about-page' },
|
|
15
|
+
];
|