zero-query 0.2.2 → 0.2.4
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 +14 -0
- package/dist/zquery.dist.zip +0 -0
- package/dist/zquery.js +2 -2
- package/dist/zquery.min.js +2 -2
- package/index.d.ts +1076 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
[](https://github.com/tonywied17/zero-query)
|
|
12
12
|
[](https://opensource.org/licenses/MIT)
|
|
13
13
|
[](package.json)
|
|
14
|
+
[](https://marketplace.visualstudio.com/items?itemName=zQuery.zquery-vs-code)
|
|
14
15
|
|
|
15
16
|
</p>
|
|
16
17
|
|
|
@@ -1382,6 +1383,19 @@ For full method signatures and options, see [API.md](API.md).
|
|
|
1382
1383
|
|
|
1383
1384
|
---
|
|
1384
1385
|
|
|
1386
|
+
## Editor Support
|
|
1387
|
+
|
|
1388
|
+
The official **[zQuery for VS Code](https://marketplace.visualstudio.com/items?itemName=zQuery.zquery-vs-code)** extension for Visual Studio Code provides:
|
|
1389
|
+
|
|
1390
|
+
- **Autocomplete** for `$.*`, `$.http.*`, `$.storage.*`, `$.bus.*`, and 50+ collection chain methods
|
|
1391
|
+
- **Hover documentation** with signatures and code examples for every method and directive
|
|
1392
|
+
- **HTML directive support** — completions and docs for `@event` handlers, `z-model`, `z-ref`, `z-link`
|
|
1393
|
+
- **55+ code snippets** — type `zq-` for components, router, store, HTTP, signals, and more
|
|
1394
|
+
|
|
1395
|
+
Install it from the [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=zQuery.zquery-vs-code) or search **"zQuery for VS Code"** in the Extensions view.
|
|
1396
|
+
|
|
1397
|
+
---
|
|
1398
|
+
|
|
1385
1399
|
## License
|
|
1386
1400
|
|
|
1387
1401
|
MIT — [Anthony Wiedman / Molex](https://github.com/tonywied17)
|
package/dist/zquery.dist.zip
CHANGED
|
Binary file
|
package/dist/zquery.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* zQuery (zeroQuery) v0.2.
|
|
2
|
+
* zQuery (zeroQuery) v0.2.4
|
|
3
3
|
* Lightweight Frontend Library
|
|
4
4
|
* https://github.com/tonywied17/zero-query
|
|
5
5
|
* (c) 2026 Anthony Wiedman — MIT License
|
|
@@ -2576,7 +2576,7 @@ $.session = session;
|
|
|
2576
2576
|
$.bus = bus;
|
|
2577
2577
|
|
|
2578
2578
|
// --- Meta ------------------------------------------------------------------
|
|
2579
|
-
$.version = '0.2.
|
|
2579
|
+
$.version = '0.2.4';
|
|
2580
2580
|
$.meta = {}; // populated at build time by CLI bundler
|
|
2581
2581
|
|
|
2582
2582
|
$.noConflict = () => {
|
package/dist/zquery.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* zQuery (zeroQuery) v0.2.
|
|
2
|
+
* zQuery (zeroQuery) v0.2.4
|
|
3
3
|
* Lightweight Frontend Library
|
|
4
4
|
* https://github.com/tonywied17/zero-query
|
|
5
5
|
* (c) 2026 Anthony Wiedman — MIT License
|
|
@@ -13,5 +13,5 @@ class Router { constructor(config = {}) { this._el = null; const isFile = typeof
|
|
|
13
13
|
class Store { constructor(config = {}) { this._subscribers = new Map(); this._wildcards = new Set(); this._actions = config.actions || {}; this._getters = config.getters || {}; this._middleware = []; this._history = []; this._debug = config.debug || false; const initial = typeof config.state === 'function' ? config.state() : { ...(config.state || {}) }; this.state = reactive(initial, (key, value, old) => { const subs = this._subscribers.get(key); if (subs) subs.forEach(fn => fn(value, old, key)); this._wildcards.forEach(fn => fn(key, value, old)); }); this.getters = {}; for (const [name, fn] of Object.entries(this._getters)) { Object.defineProperty(this.getters, name, { get: () => fn(this.state.__raw || this.state), enumerable: true }); } } dispatch(name, ...args) { const action = this._actions[name]; if (!action) { console.warn(`zQuery Store: Unknown action "${name}"`); return; } for (const mw of this._middleware) { const result = mw(name, args, this.state); if (result === false) return; } if (this._debug) { console.log(`%c[Store] ${name}`, 'color: #4CAF50; font-weight: bold;', ...args); } const result = action(this.state, ...args); this._history.push({ action: name, args, timestamp: Date.now() }); return result; } subscribe(keyOrFn, fn) { if (typeof keyOrFn === 'function') { this._wildcards.add(keyOrFn); return () => this._wildcards.delete(keyOrFn); } if (!this._subscribers.has(keyOrFn)) { this._subscribers.set(keyOrFn, new Set()); } this._subscribers.get(keyOrFn).add(fn); return () => this._subscribers.get(keyOrFn)?.delete(fn); } snapshot() { return JSON.parse(JSON.stringify(this.state.__raw || this.state)); } replaceState(newState) { const raw = this.state.__raw || this.state; for (const key of Object.keys(raw)) { delete this.state[key]; } Object.assign(this.state, newState); } use(fn) { this._middleware.push(fn); return this; } get history() { return [...this._history]; } reset(initialState) { this.replaceState(initialState); this._history = []; }
|
|
14
14
|
const _config = { baseURL: '', headers: { 'Content-Type': 'application/json' }, timeout: 30000,
|
|
15
15
|
function debounce(fn, ms = 250) { let timer; const debounced = (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), ms); }; debounced.cancel = () => clearTimeout(timer); return debounced;
|
|
16
|
-
function $(selector, context) { if (typeof selector === 'function') { query.ready(selector); return; } return query(selector, context);
|
|
16
|
+
function $(selector, context) { if (typeof selector === 'function') { query.ready(selector); return; } return query(selector, context);
|
|
17
17
|
})(typeof window !== 'undefined' ? window : globalThis);
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,1076 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* zQuery (zeroQuery) — TypeScript Declarations
|
|
3
|
+
*
|
|
4
|
+
* Lightweight modern frontend library — jQuery-like selectors, reactive
|
|
5
|
+
* components, SPA router, state management, HTTP client & utilities.
|
|
6
|
+
*
|
|
7
|
+
* @version 0.2.4
|
|
8
|
+
* @license MIT
|
|
9
|
+
* @see https://z-query.com/docs
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
13
|
+
// ZQueryCollection
|
|
14
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Chainable wrapper around an array of DOM elements, similar to a jQuery object.
|
|
18
|
+
* Returned by `$.all()` and many traversal / filtering methods.
|
|
19
|
+
*/
|
|
20
|
+
export class ZQueryCollection {
|
|
21
|
+
/** Number of elements in the collection. */
|
|
22
|
+
readonly length: number;
|
|
23
|
+
|
|
24
|
+
/** Access element by numeric index. */
|
|
25
|
+
readonly [index: number]: Element;
|
|
26
|
+
|
|
27
|
+
constructor(elements: Element | Element[]);
|
|
28
|
+
|
|
29
|
+
// ── Iteration ───────────────────────────────────────────────────────────
|
|
30
|
+
/**
|
|
31
|
+
* Iterate over each element. `this` inside the callback is the element.
|
|
32
|
+
* @returns The collection (for chaining).
|
|
33
|
+
*/
|
|
34
|
+
each(fn: (this: Element, index: number, element: Element) => void): this;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Map over elements and return a plain array.
|
|
38
|
+
*/
|
|
39
|
+
map<T>(fn: (this: Element, index: number, element: Element) => T): T[];
|
|
40
|
+
|
|
41
|
+
/** First raw element, or `null`. */
|
|
42
|
+
first(): Element | null;
|
|
43
|
+
|
|
44
|
+
/** Last raw element, or `null`. */
|
|
45
|
+
last(): Element | null;
|
|
46
|
+
|
|
47
|
+
/** New collection containing only the element at `index`. */
|
|
48
|
+
eq(index: number): ZQueryCollection;
|
|
49
|
+
|
|
50
|
+
/** Convert to a plain `Element[]`. */
|
|
51
|
+
toArray(): Element[];
|
|
52
|
+
|
|
53
|
+
/** Iterable protocol — works with `for...of` and spread. */
|
|
54
|
+
[Symbol.iterator](): IterableIterator<Element>;
|
|
55
|
+
|
|
56
|
+
// ── Traversal ───────────────────────────────────────────────────────────
|
|
57
|
+
/** Descendants matching `selector`. */
|
|
58
|
+
find(selector: string): ZQueryCollection;
|
|
59
|
+
|
|
60
|
+
/** Unique parent elements. */
|
|
61
|
+
parent(): ZQueryCollection;
|
|
62
|
+
|
|
63
|
+
/** Nearest ancestor matching `selector`. */
|
|
64
|
+
closest(selector: string): ZQueryCollection;
|
|
65
|
+
|
|
66
|
+
/** Direct children, optionally filtered by `selector`. */
|
|
67
|
+
children(selector?: string): ZQueryCollection;
|
|
68
|
+
|
|
69
|
+
/** All sibling elements. */
|
|
70
|
+
siblings(): ZQueryCollection;
|
|
71
|
+
|
|
72
|
+
/** Next sibling of each element. */
|
|
73
|
+
next(): ZQueryCollection;
|
|
74
|
+
|
|
75
|
+
/** Previous sibling of each element. */
|
|
76
|
+
prev(): ZQueryCollection;
|
|
77
|
+
|
|
78
|
+
// ── Filtering ───────────────────────────────────────────────────────────
|
|
79
|
+
/** Keep elements matching a CSS selector or predicate. */
|
|
80
|
+
filter(selector: string): ZQueryCollection;
|
|
81
|
+
filter(fn: (element: Element, index: number) => boolean): ZQueryCollection;
|
|
82
|
+
|
|
83
|
+
/** Remove elements matching a CSS selector or predicate. */
|
|
84
|
+
not(selector: string): ZQueryCollection;
|
|
85
|
+
not(fn: (this: Element, index: number, element: Element) => boolean): ZQueryCollection;
|
|
86
|
+
|
|
87
|
+
/** Keep elements that have a descendant matching `selector`. */
|
|
88
|
+
has(selector: string): ZQueryCollection;
|
|
89
|
+
|
|
90
|
+
// ── Classes ─────────────────────────────────────────────────────────────
|
|
91
|
+
/** Add one or more classes (space-separated strings accepted). */
|
|
92
|
+
addClass(...names: string[]): this;
|
|
93
|
+
|
|
94
|
+
/** Remove one or more classes. */
|
|
95
|
+
removeClass(...names: string[]): this;
|
|
96
|
+
|
|
97
|
+
/** Toggle a class. Optional `force` boolean. */
|
|
98
|
+
toggleClass(name: string, force?: boolean): this;
|
|
99
|
+
|
|
100
|
+
/** Check whether the first element has the given class. */
|
|
101
|
+
hasClass(name: string): boolean;
|
|
102
|
+
|
|
103
|
+
// ── Attributes & Properties ─────────────────────────────────────────────
|
|
104
|
+
/** Get attribute value of the first element. */
|
|
105
|
+
attr(name: string): string | null;
|
|
106
|
+
/** Set attribute on all elements. */
|
|
107
|
+
attr(name: string, value: string): this;
|
|
108
|
+
|
|
109
|
+
/** Remove attribute from all elements. */
|
|
110
|
+
removeAttr(name: string): this;
|
|
111
|
+
|
|
112
|
+
/** Get JS property of the first element. */
|
|
113
|
+
prop(name: string): any;
|
|
114
|
+
/** Set JS property on all elements. */
|
|
115
|
+
prop(name: string, value: any): this;
|
|
116
|
+
|
|
117
|
+
/** Get data attribute value (JSON auto-parsed). No key → full dataset. */
|
|
118
|
+
data(): DOMStringMap;
|
|
119
|
+
data(key: string): any;
|
|
120
|
+
/** Set data attribute on all elements. Objects are JSON-stringified. */
|
|
121
|
+
data(key: string, value: any): this;
|
|
122
|
+
|
|
123
|
+
// ── CSS & Dimensions ────────────────────────────────────────────────────
|
|
124
|
+
/** Get computed style property of the first element. */
|
|
125
|
+
css(property: string): string;
|
|
126
|
+
/** Set inline styles on all elements. */
|
|
127
|
+
css(props: Partial<CSSStyleDeclaration>): this;
|
|
128
|
+
|
|
129
|
+
/** First element's width (from `getBoundingClientRect`). */
|
|
130
|
+
width(): number | undefined;
|
|
131
|
+
|
|
132
|
+
/** First element's height. */
|
|
133
|
+
height(): number | undefined;
|
|
134
|
+
|
|
135
|
+
/** Position relative to the document. */
|
|
136
|
+
offset(): { top: number; left: number; width: number; height: number } | null;
|
|
137
|
+
|
|
138
|
+
/** Position relative to the offset parent. */
|
|
139
|
+
position(): { top: number; left: number } | null;
|
|
140
|
+
|
|
141
|
+
// ── Content ─────────────────────────────────────────────────────────────
|
|
142
|
+
/** Get `innerHTML` of the first element. */
|
|
143
|
+
html(): string;
|
|
144
|
+
/** Set `innerHTML` on all elements. */
|
|
145
|
+
html(content: string): this;
|
|
146
|
+
|
|
147
|
+
/** Get `textContent` of the first element. */
|
|
148
|
+
text(): string;
|
|
149
|
+
/** Set `textContent` on all elements. */
|
|
150
|
+
text(content: string): this;
|
|
151
|
+
|
|
152
|
+
/** Get value of the first input/select/textarea. */
|
|
153
|
+
val(): string;
|
|
154
|
+
/** Set value on all inputs. */
|
|
155
|
+
val(value: string): this;
|
|
156
|
+
|
|
157
|
+
// ── DOM Manipulation ────────────────────────────────────────────────────
|
|
158
|
+
/** Insert content at the end of each element. */
|
|
159
|
+
append(content: string | Node | ZQueryCollection): this;
|
|
160
|
+
|
|
161
|
+
/** Insert content at the beginning of each element. */
|
|
162
|
+
prepend(content: string | Node): this;
|
|
163
|
+
|
|
164
|
+
/** Insert content after each element. */
|
|
165
|
+
after(content: string | Node): this;
|
|
166
|
+
|
|
167
|
+
/** Insert content before each element. */
|
|
168
|
+
before(content: string | Node): this;
|
|
169
|
+
|
|
170
|
+
/** Wrap each element with the given HTML string or Node. */
|
|
171
|
+
wrap(wrapper: string | Node): this;
|
|
172
|
+
|
|
173
|
+
/** Remove all elements from the DOM. */
|
|
174
|
+
remove(): this;
|
|
175
|
+
|
|
176
|
+
/** Clear `innerHTML` of all elements. */
|
|
177
|
+
empty(): this;
|
|
178
|
+
|
|
179
|
+
/** Clone elements (default: deep clone). */
|
|
180
|
+
clone(deep?: boolean): ZQueryCollection;
|
|
181
|
+
|
|
182
|
+
/** Replace elements with new content. */
|
|
183
|
+
replaceWith(content: string | Node): this;
|
|
184
|
+
|
|
185
|
+
// ── Visibility ──────────────────────────────────────────────────────────
|
|
186
|
+
/** Show elements. Optional display value (default: `''`). */
|
|
187
|
+
show(display?: string): this;
|
|
188
|
+
|
|
189
|
+
/** Set `display: none` on all elements. */
|
|
190
|
+
hide(): this;
|
|
191
|
+
|
|
192
|
+
/** Toggle visibility. */
|
|
193
|
+
toggle(display?: string): this;
|
|
194
|
+
|
|
195
|
+
// ── Events ──────────────────────────────────────────────────────────────
|
|
196
|
+
/** Attach event handler. Space-separated events accepted. */
|
|
197
|
+
on(events: string, handler: (event: Event) => void): this;
|
|
198
|
+
/** Delegated event handler. */
|
|
199
|
+
on(events: string, selector: string, handler: (this: Element, event: Event) => void): this;
|
|
200
|
+
|
|
201
|
+
/** Remove event handler. */
|
|
202
|
+
off(events: string, handler: (event: Event) => void): this;
|
|
203
|
+
|
|
204
|
+
/** One-time event handler. */
|
|
205
|
+
one(event: string, handler: (event: Event) => void): this;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Dispatch a `CustomEvent` with optional detail payload.
|
|
209
|
+
* Bubbles by default.
|
|
210
|
+
*/
|
|
211
|
+
trigger(event: string, detail?: any): this;
|
|
212
|
+
|
|
213
|
+
/** Attach click handler, or trigger a click when called with no arguments. */
|
|
214
|
+
click(fn?: (event: Event) => void): this;
|
|
215
|
+
|
|
216
|
+
/** Attach submit handler, or trigger submit when called with no arguments. */
|
|
217
|
+
submit(fn?: (event: Event) => void): this;
|
|
218
|
+
|
|
219
|
+
/** Focus the first element. */
|
|
220
|
+
focus(): this;
|
|
221
|
+
|
|
222
|
+
/** Blur the first element. */
|
|
223
|
+
blur(): this;
|
|
224
|
+
|
|
225
|
+
// ── Animation ───────────────────────────────────────────────────────────
|
|
226
|
+
/**
|
|
227
|
+
* CSS transition animation.
|
|
228
|
+
* @param props CSS properties to animate to.
|
|
229
|
+
* @param duration Duration in ms (default 300).
|
|
230
|
+
* @param easing CSS easing function (default `'ease'`).
|
|
231
|
+
*/
|
|
232
|
+
animate(
|
|
233
|
+
props: Partial<CSSStyleDeclaration>,
|
|
234
|
+
duration?: number,
|
|
235
|
+
easing?: string,
|
|
236
|
+
): Promise<ZQueryCollection>;
|
|
237
|
+
|
|
238
|
+
/** Fade in (opacity 0→1). Default 300 ms. */
|
|
239
|
+
fadeIn(duration?: number): Promise<ZQueryCollection>;
|
|
240
|
+
|
|
241
|
+
/** Fade out (opacity 1→0) then hide. Default 300 ms. */
|
|
242
|
+
fadeOut(duration?: number): Promise<ZQueryCollection>;
|
|
243
|
+
|
|
244
|
+
/** Toggle height with a slide animation. Default 300 ms. */
|
|
245
|
+
slideToggle(duration?: number): this;
|
|
246
|
+
|
|
247
|
+
// ── Form Helpers ────────────────────────────────────────────────────────
|
|
248
|
+
/** URL-encoded form data string. */
|
|
249
|
+
serialize(): string;
|
|
250
|
+
|
|
251
|
+
/** Form data as key/value object. Duplicate keys become arrays. */
|
|
252
|
+
serializeObject(): Record<string, string | string[]>;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
257
|
+
// Reactive
|
|
258
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
259
|
+
|
|
260
|
+
/** Marker properties added to every reactive proxy. */
|
|
261
|
+
interface ReactiveProxy<T extends object = object> {
|
|
262
|
+
/** Always `true` — indicates this object is wrapped in a reactive Proxy. */
|
|
263
|
+
readonly __isReactive: true;
|
|
264
|
+
/** The original un-proxied object. */
|
|
265
|
+
readonly __raw: T;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Wrap an object in a deep Proxy that fires `onChange` on every set / delete.
|
|
270
|
+
*/
|
|
271
|
+
export function reactive<T extends object>(
|
|
272
|
+
target: T,
|
|
273
|
+
onChange: (key: string, value: any, oldValue: any) => void,
|
|
274
|
+
): T & ReactiveProxy<T>;
|
|
275
|
+
|
|
276
|
+
/** A lightweight reactive primitive (inspired by Solid / Preact signals). */
|
|
277
|
+
export class Signal<T = any> {
|
|
278
|
+
constructor(value: T);
|
|
279
|
+
|
|
280
|
+
/** Get/set the current value. The getter auto-tracks inside `effect()`. */
|
|
281
|
+
value: T;
|
|
282
|
+
|
|
283
|
+
/** Read the value without creating a subscription. */
|
|
284
|
+
peek(): T;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Manually subscribe to changes.
|
|
288
|
+
* @returns An unsubscribe function.
|
|
289
|
+
*/
|
|
290
|
+
subscribe(fn: () => void): () => void;
|
|
291
|
+
|
|
292
|
+
toString(): string;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Create a new `Signal`. */
|
|
296
|
+
export function signal<T>(initial: T): Signal<T>;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Create a derived signal that recomputes when its dependencies change.
|
|
300
|
+
* The returned signal is effectively read-only.
|
|
301
|
+
*/
|
|
302
|
+
export function computed<T>(fn: () => T): Signal<T>;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Run a side-effect that auto-subscribes to signals read during execution.
|
|
306
|
+
* @returns A dispose function that stops tracking.
|
|
307
|
+
*/
|
|
308
|
+
export function effect(fn: () => void): () => void;
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
312
|
+
// Component System
|
|
313
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
/** Item in a `pages` config — either a string id or an `{ id, label }` object. */
|
|
316
|
+
type PageItem = string | { id: string; label?: string };
|
|
317
|
+
|
|
318
|
+
/** Declarative multi-page configuration for a component. */
|
|
319
|
+
interface PagesConfig {
|
|
320
|
+
/** Directory containing the page HTML files (resolved relative to `base`). */
|
|
321
|
+
dir?: string;
|
|
322
|
+
/** Route parameter name to read (e.g. `'section'` for `/docs/:section`). */
|
|
323
|
+
param?: string;
|
|
324
|
+
/** Default page id when the param is absent. */
|
|
325
|
+
default?: string;
|
|
326
|
+
/** File extension appended to each page id (default `'.html'`). */
|
|
327
|
+
ext?: string;
|
|
328
|
+
/** List of page ids and/or `{ id, label }` objects. */
|
|
329
|
+
items?: PageItem[];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** The object passed to `$.component()` to define a component. */
|
|
333
|
+
interface ComponentDefinition {
|
|
334
|
+
/**
|
|
335
|
+
* Initial reactive state.
|
|
336
|
+
* A function form is recommended so each instance gets its own copy.
|
|
337
|
+
*/
|
|
338
|
+
state?: Record<string, any> | (() => Record<string, any>);
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Returns an HTML string. Called on every state change.
|
|
342
|
+
* `this` is the component instance.
|
|
343
|
+
*/
|
|
344
|
+
render?(this: ComponentInstance): string;
|
|
345
|
+
|
|
346
|
+
/** CSS string — scoped to the component root on first render. */
|
|
347
|
+
styles?: string;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* URL (or array / object map of URLs) to external HTML template file(s).
|
|
351
|
+
* If `render()` is also defined, `render()` takes priority.
|
|
352
|
+
*/
|
|
353
|
+
templateUrl?: string | string[] | Record<string, string>;
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* URL (or array of URLs) to external CSS file(s).
|
|
357
|
+
* Fetched and auto-scoped on first mount; merged with `styles` if both present.
|
|
358
|
+
*/
|
|
359
|
+
styleUrl?: string | string[];
|
|
360
|
+
|
|
361
|
+
/** High-level multi-page configuration shorthand. */
|
|
362
|
+
pages?: PagesConfig;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Override the base path for resolving relative `templateUrl`, `styleUrl`,
|
|
366
|
+
* and `pages.dir` paths. Normally auto-detected from the calling file.
|
|
367
|
+
*/
|
|
368
|
+
base?: string;
|
|
369
|
+
|
|
370
|
+
/** Called before first render (during construction). */
|
|
371
|
+
init?(this: ComponentInstance): void;
|
|
372
|
+
|
|
373
|
+
/** Called once after first render and DOM insertion. */
|
|
374
|
+
mounted?(this: ComponentInstance): void;
|
|
375
|
+
|
|
376
|
+
/** Called after every subsequent re-render. */
|
|
377
|
+
updated?(this: ComponentInstance): void;
|
|
378
|
+
|
|
379
|
+
/** Called when the component is destroyed. Clean up subscriptions here. */
|
|
380
|
+
destroyed?(this: ComponentInstance): void;
|
|
381
|
+
|
|
382
|
+
/** Additional keys become instance methods (bound to the instance). */
|
|
383
|
+
[method: string]: any;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** The runtime instance of a mounted component. */
|
|
387
|
+
interface ComponentInstance {
|
|
388
|
+
/** Reactive state proxy. Mutating triggers re-render. */
|
|
389
|
+
state: Record<string, any> & ReactiveProxy;
|
|
390
|
+
|
|
391
|
+
/** Frozen props passed from parent / router. */
|
|
392
|
+
readonly props: Readonly<Record<string, any>>;
|
|
393
|
+
|
|
394
|
+
/** Map of `z-ref` name → DOM element. Populated after each render. */
|
|
395
|
+
refs: Record<string, Element>;
|
|
396
|
+
|
|
397
|
+
/** Keyed template map (when using multi-`templateUrl` or `pages`). */
|
|
398
|
+
templates: Record<string, string>;
|
|
399
|
+
|
|
400
|
+
/** Normalized page metadata (when using `pages` config). */
|
|
401
|
+
pages: Array<{ id: string; label: string }>;
|
|
402
|
+
|
|
403
|
+
/** Active page id derived from route param (when using `pages` config). */
|
|
404
|
+
activePage: string;
|
|
405
|
+
|
|
406
|
+
/** Merge partial state (triggers re-render). */
|
|
407
|
+
setState(partial: Record<string, any>): void;
|
|
408
|
+
|
|
409
|
+
/** Dispatch a bubbling `CustomEvent` from the component root. */
|
|
410
|
+
emit(name: string, detail?: any): void;
|
|
411
|
+
|
|
412
|
+
/** Teardown: removes listeners, scoped styles, clears DOM. */
|
|
413
|
+
destroy(): void;
|
|
414
|
+
|
|
415
|
+
/** Manually queue a re-render (microtask-batched). */
|
|
416
|
+
_scheduleUpdate(): void;
|
|
417
|
+
|
|
418
|
+
/** Any user-defined methods from the component definition. */
|
|
419
|
+
[method: string]: any;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Register a new component.
|
|
424
|
+
* @param name Must contain a hyphen (Web Component convention).
|
|
425
|
+
* @param definition Component definition object.
|
|
426
|
+
*/
|
|
427
|
+
export function component(name: string, definition: ComponentDefinition): void;
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Mount a registered component into a target element.
|
|
431
|
+
* @returns The component instance.
|
|
432
|
+
*/
|
|
433
|
+
export function mount(
|
|
434
|
+
target: string | Element,
|
|
435
|
+
componentName: string,
|
|
436
|
+
props?: Record<string, any>,
|
|
437
|
+
): ComponentInstance;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Scan `root` for elements whose tag matches a registered component and mount them.
|
|
441
|
+
* @param root Defaults to `document.body`.
|
|
442
|
+
*/
|
|
443
|
+
export function mountAll(root?: Element): void;
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Get the component instance mounted on `target`.
|
|
447
|
+
*/
|
|
448
|
+
export function getInstance(target: string | Element): ComponentInstance | null;
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Destroy the component at the given target.
|
|
452
|
+
*/
|
|
453
|
+
export function destroy(target: string | Element): void;
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Returns an object of all registered component definitions (for debugging).
|
|
457
|
+
*/
|
|
458
|
+
export function getRegistry(): Record<string, ComponentDefinition>;
|
|
459
|
+
|
|
460
|
+
/** Handle returned by `$.style()`. */
|
|
461
|
+
interface StyleHandle {
|
|
462
|
+
/** Remove all injected `<link>` elements. */
|
|
463
|
+
remove(): void;
|
|
464
|
+
/** Resolves when all stylesheets have loaded. */
|
|
465
|
+
ready: Promise<void>;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Options for `$.style()`. */
|
|
469
|
+
interface StyleOptions {
|
|
470
|
+
/** Hide page until loaded to prevent FOUC (default `true`). */
|
|
471
|
+
critical?: boolean;
|
|
472
|
+
/** Background color while hidden during critical load (default `'#0d1117'`). */
|
|
473
|
+
bg?: string;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Dynamically load global (unscoped) stylesheet file(s) into `<head>`.
|
|
478
|
+
* Relative paths resolve relative to the calling file.
|
|
479
|
+
*/
|
|
480
|
+
export function style(urls: string | string[], opts?: StyleOptions): StyleHandle;
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
484
|
+
// Router
|
|
485
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
486
|
+
|
|
487
|
+
/** A single route definition. */
|
|
488
|
+
interface RouteDefinition {
|
|
489
|
+
/**
|
|
490
|
+
* URL pattern. Supports `:param` segments and `*` wildcard.
|
|
491
|
+
* @example '/user/:id'
|
|
492
|
+
*/
|
|
493
|
+
path: string;
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Registered component name (auto-mounted), or a render function.
|
|
497
|
+
*/
|
|
498
|
+
component: string | ((route: NavigationContext) => string);
|
|
499
|
+
|
|
500
|
+
/** Async function called before mounting (for lazy-loading modules). */
|
|
501
|
+
load?: () => Promise<any>;
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* An additional path that also matches this route.
|
|
505
|
+
* Missing `:param` values will be `undefined`.
|
|
506
|
+
*/
|
|
507
|
+
fallback?: string;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** Navigation context passed to guards and `onChange` listeners. */
|
|
511
|
+
interface NavigationContext {
|
|
512
|
+
/** The matched route definition. */
|
|
513
|
+
route: RouteDefinition;
|
|
514
|
+
/** Parsed `:param` values. */
|
|
515
|
+
params: Record<string, string>;
|
|
516
|
+
/** Parsed query-string values. */
|
|
517
|
+
query: Record<string, string>;
|
|
518
|
+
/** Matched path (base-stripped in history mode). */
|
|
519
|
+
path: string;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** Router configuration. */
|
|
523
|
+
interface RouterConfig {
|
|
524
|
+
/** Outlet element where route components are rendered. */
|
|
525
|
+
el?: string | Element;
|
|
526
|
+
/** Routing mode (default: `'history'`; `'hash'` for file:// or hash routing). */
|
|
527
|
+
mode?: 'history' | 'hash';
|
|
528
|
+
/**
|
|
529
|
+
* Base path prefix (e.g. `'/my-app'`).
|
|
530
|
+
* Auto-detected from `<base href>` or `window.__ZQ_BASE` if not set.
|
|
531
|
+
*/
|
|
532
|
+
base?: string;
|
|
533
|
+
/** Route definitions. */
|
|
534
|
+
routes?: RouteDefinition[];
|
|
535
|
+
/** Component name to render when no route matches (404). */
|
|
536
|
+
fallback?: string;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** The SPA router instance. */
|
|
540
|
+
interface RouterInstance {
|
|
541
|
+
/** Push a new state and resolve the route. */
|
|
542
|
+
navigate(path: string, options?: { state?: any }): RouterInstance;
|
|
543
|
+
/** Replace the current state (no new history entry). */
|
|
544
|
+
replace(path: string, options?: { state?: any }): RouterInstance;
|
|
545
|
+
/** `history.back()` */
|
|
546
|
+
back(): RouterInstance;
|
|
547
|
+
/** `history.forward()` */
|
|
548
|
+
forward(): RouterInstance;
|
|
549
|
+
/** `history.go(n)` */
|
|
550
|
+
go(n: number): RouterInstance;
|
|
551
|
+
|
|
552
|
+
/** Add a route dynamically. Chainable. */
|
|
553
|
+
add(route: RouteDefinition): RouterInstance;
|
|
554
|
+
/** Remove a route by path. */
|
|
555
|
+
remove(path: string): RouterInstance;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Navigation guard — runs before each route change.
|
|
559
|
+
* Return `false` to cancel, or a `string` to redirect.
|
|
560
|
+
*/
|
|
561
|
+
beforeEach(
|
|
562
|
+
fn: (to: NavigationContext, from: NavigationContext | null) => boolean | string | void | Promise<boolean | string | void>,
|
|
563
|
+
): RouterInstance;
|
|
564
|
+
|
|
565
|
+
/** Post-navigation hook. */
|
|
566
|
+
afterEach(
|
|
567
|
+
fn: (to: NavigationContext, from: NavigationContext | null) => void | Promise<void>,
|
|
568
|
+
): RouterInstance;
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Subscribe to route changes.
|
|
572
|
+
* @returns An unsubscribe function.
|
|
573
|
+
*/
|
|
574
|
+
onChange(
|
|
575
|
+
fn: (to: NavigationContext, from: NavigationContext | null) => void,
|
|
576
|
+
): () => void;
|
|
577
|
+
|
|
578
|
+
/** Teardown the router and mounted component. */
|
|
579
|
+
destroy(): void;
|
|
580
|
+
|
|
581
|
+
/** Resolve a path applying the base prefix. */
|
|
582
|
+
resolve(path: string): string;
|
|
583
|
+
|
|
584
|
+
// ── Properties ──────────────────────────────────────────────────────────
|
|
585
|
+
/** Current navigation context. */
|
|
586
|
+
readonly current: NavigationContext | null;
|
|
587
|
+
/** Current path (base-stripped in history mode). */
|
|
588
|
+
readonly path: string;
|
|
589
|
+
/** Parsed query-string object. */
|
|
590
|
+
readonly query: Record<string, string>;
|
|
591
|
+
/** Configured base path. */
|
|
592
|
+
readonly base: string;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** Create and activate a client-side SPA router. */
|
|
596
|
+
export function createRouter(config: RouterConfig): RouterInstance;
|
|
597
|
+
|
|
598
|
+
/** Get the currently active router instance. */
|
|
599
|
+
export function getRouter(): RouterInstance | null;
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
603
|
+
// Store
|
|
604
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
605
|
+
|
|
606
|
+
/** Store configuration. */
|
|
607
|
+
interface StoreConfig<
|
|
608
|
+
S extends Record<string, any> = Record<string, any>,
|
|
609
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
610
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
611
|
+
> {
|
|
612
|
+
/** Initial state. Function form creates a fresh copy. */
|
|
613
|
+
state?: S | (() => S);
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Named action functions. The first argument is always the reactive `state`.
|
|
617
|
+
*/
|
|
618
|
+
actions?: A;
|
|
619
|
+
|
|
620
|
+
/** Computed getters derived from state (lazily evaluated). */
|
|
621
|
+
getters?: G;
|
|
622
|
+
|
|
623
|
+
/** Log dispatched actions to the console. */
|
|
624
|
+
debug?: boolean;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** A store action history entry. */
|
|
628
|
+
interface StoreHistoryEntry {
|
|
629
|
+
action: string;
|
|
630
|
+
args: any[];
|
|
631
|
+
timestamp: number;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** The reactive store instance. */
|
|
635
|
+
interface StoreInstance<
|
|
636
|
+
S extends Record<string, any> = Record<string, any>,
|
|
637
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
638
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
639
|
+
> {
|
|
640
|
+
/** Reactive state proxy. Read / write triggers subscriptions. */
|
|
641
|
+
state: S & ReactiveProxy<S>;
|
|
642
|
+
|
|
643
|
+
/** Computed getters object. */
|
|
644
|
+
readonly getters: { readonly [K in keyof G]: ReturnType<G[K]> };
|
|
645
|
+
|
|
646
|
+
/** Log of all dispatched actions. */
|
|
647
|
+
readonly history: ReadonlyArray<StoreHistoryEntry>;
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Execute a named action.
|
|
651
|
+
* @returns The action's return value.
|
|
652
|
+
*/
|
|
653
|
+
dispatch<K extends keyof A>(
|
|
654
|
+
name: K,
|
|
655
|
+
...args: A[K] extends (state: any, ...rest: infer P) => any ? P : any[]
|
|
656
|
+
): ReturnType<A[K]>;
|
|
657
|
+
dispatch(name: string, ...args: any[]): any;
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Subscribe to changes on a specific state key.
|
|
661
|
+
* @returns An unsubscribe function.
|
|
662
|
+
*/
|
|
663
|
+
subscribe(key: string, fn: (value: any, oldValue: any, key: string) => void): () => void;
|
|
664
|
+
/** Subscribe to all state changes (wildcard). */
|
|
665
|
+
subscribe(fn: (key: string, value: any, oldValue: any) => void): () => void;
|
|
666
|
+
|
|
667
|
+
/** Deep clone of the current state. */
|
|
668
|
+
snapshot(): S;
|
|
669
|
+
|
|
670
|
+
/** Replace the entire state. */
|
|
671
|
+
replaceState(newState: Partial<S>): void;
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Add middleware. Return `false` from `fn` to block the action.
|
|
675
|
+
* Chainable.
|
|
676
|
+
*/
|
|
677
|
+
use(fn: (actionName: string, args: any[], state: S) => boolean | void): StoreInstance<S, A, G>;
|
|
678
|
+
|
|
679
|
+
/** Replace state and clear action history. */
|
|
680
|
+
reset(initialState: Partial<S>): void;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/** Create a new global reactive store. */
|
|
684
|
+
export function createStore<
|
|
685
|
+
S extends Record<string, any>,
|
|
686
|
+
A extends Record<string, (state: S, ...args: any[]) => any>,
|
|
687
|
+
G extends Record<string, (state: S) => any>,
|
|
688
|
+
>(config: StoreConfig<S, A, G>): StoreInstance<S, A, G>;
|
|
689
|
+
export function createStore<
|
|
690
|
+
S extends Record<string, any>,
|
|
691
|
+
A extends Record<string, (state: S, ...args: any[]) => any>,
|
|
692
|
+
G extends Record<string, (state: S) => any>,
|
|
693
|
+
>(name: string, config: StoreConfig<S, A, G>): StoreInstance<S, A, G>;
|
|
694
|
+
|
|
695
|
+
/** Retrieve a previously created store by name (default: `'default'`). */
|
|
696
|
+
export function getStore<
|
|
697
|
+
S extends Record<string, any> = Record<string, any>,
|
|
698
|
+
A extends Record<string, (state: S, ...args: any[]) => any> = Record<string, (state: S, ...args: any[]) => any>,
|
|
699
|
+
G extends Record<string, (state: S) => any> = Record<string, (state: S) => any>,
|
|
700
|
+
>(name?: string): StoreInstance<S, A, G> | null;
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
704
|
+
// HTTP Client
|
|
705
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
706
|
+
|
|
707
|
+
/** The response object resolved by all HTTP request methods (except `raw`). */
|
|
708
|
+
interface HttpResponse<T = any> {
|
|
709
|
+
/** `true` if status 200-299. */
|
|
710
|
+
ok: boolean;
|
|
711
|
+
/** HTTP status code. */
|
|
712
|
+
status: number;
|
|
713
|
+
/** HTTP status text. */
|
|
714
|
+
statusText: string;
|
|
715
|
+
/** Response headers as a plain object. */
|
|
716
|
+
headers: Record<string, string>;
|
|
717
|
+
/** Auto-parsed body (JSON, text, or Blob depending on content type). */
|
|
718
|
+
data: T;
|
|
719
|
+
/** Raw `fetch` Response object. */
|
|
720
|
+
response: Response;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** Per-request options passed to HTTP methods. */
|
|
724
|
+
interface HttpRequestOptions extends Omit<RequestInit, 'body' | 'method'> {
|
|
725
|
+
/** Additional headers (merged with defaults). */
|
|
726
|
+
headers?: Record<string, string>;
|
|
727
|
+
/** Override default timeout (ms). */
|
|
728
|
+
timeout?: number;
|
|
729
|
+
/** Abort signal for cancellation. */
|
|
730
|
+
signal?: AbortSignal;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/** Global HTTP configuration options. */
|
|
734
|
+
interface HttpConfigureOptions {
|
|
735
|
+
/** Prepended to non-absolute URLs. */
|
|
736
|
+
baseURL?: string;
|
|
737
|
+
/** Default headers (merged, not replaced). */
|
|
738
|
+
headers?: Record<string, string>;
|
|
739
|
+
/** Default timeout in ms (default 30 000). Set `0` to disable. */
|
|
740
|
+
timeout?: number;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** Request interceptor function. */
|
|
744
|
+
type HttpRequestInterceptor = (
|
|
745
|
+
fetchOpts: RequestInit & { headers: Record<string, string> },
|
|
746
|
+
url: string,
|
|
747
|
+
) => void | false | { url?: string; options?: RequestInit } | Promise<void | false | { url?: string; options?: RequestInit }>;
|
|
748
|
+
|
|
749
|
+
/** Response interceptor function. */
|
|
750
|
+
type HttpResponseInterceptor = (result: HttpResponse) => void | Promise<void>;
|
|
751
|
+
|
|
752
|
+
/** The `$.http` namespace. */
|
|
753
|
+
interface HttpClient {
|
|
754
|
+
/** GET request. `params` appended as query string. */
|
|
755
|
+
get<T = any>(url: string, params?: Record<string, any> | null, opts?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
756
|
+
/** POST request. `data` sent as JSON body (or FormData). */
|
|
757
|
+
post<T = any>(url: string, data?: any, opts?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
758
|
+
/** PUT request. */
|
|
759
|
+
put<T = any>(url: string, data?: any, opts?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
760
|
+
/** PATCH request. */
|
|
761
|
+
patch<T = any>(url: string, data?: any, opts?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
762
|
+
/** DELETE request. */
|
|
763
|
+
delete<T = any>(url: string, data?: any, opts?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
764
|
+
|
|
765
|
+
/** Update default configuration for all subsequent requests. */
|
|
766
|
+
configure(options: HttpConfigureOptions): void;
|
|
767
|
+
|
|
768
|
+
/** Add a request interceptor (called before every request). */
|
|
769
|
+
onRequest(fn: HttpRequestInterceptor): void;
|
|
770
|
+
|
|
771
|
+
/** Add a response interceptor (called after every response, before error check). */
|
|
772
|
+
onResponse(fn: HttpResponseInterceptor): void;
|
|
773
|
+
|
|
774
|
+
/** Create a new `AbortController` for manual request cancellation. */
|
|
775
|
+
createAbort(): AbortController;
|
|
776
|
+
|
|
777
|
+
/** Direct passthrough to native `fetch()` — no JSON handling, no interceptors. */
|
|
778
|
+
raw(url: string, opts?: RequestInit): Promise<Response>;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
export const http: HttpClient;
|
|
782
|
+
|
|
783
|
+
|
|
784
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
785
|
+
// Utilities — Functions
|
|
786
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
787
|
+
|
|
788
|
+
/** Debounced function with a `.cancel()` helper. */
|
|
789
|
+
interface DebouncedFunction<T extends (...args: any[]) => any> {
|
|
790
|
+
(...args: Parameters<T>): void;
|
|
791
|
+
/** Cancel the pending invocation. */
|
|
792
|
+
cancel(): void;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Returns a debounced function that delays execution until `ms` ms of inactivity.
|
|
797
|
+
* @param ms Default `250`.
|
|
798
|
+
*/
|
|
799
|
+
export function debounce<T extends (...args: any[]) => any>(fn: T, ms?: number): DebouncedFunction<T>;
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Returns a throttled function that executes at most once per `ms` ms.
|
|
803
|
+
* @param ms Default `250`.
|
|
804
|
+
*/
|
|
805
|
+
export function throttle<T extends (...args: any[]) => any>(fn: T, ms?: number): (...args: Parameters<T>) => void;
|
|
806
|
+
|
|
807
|
+
/** Left-to-right function composition. */
|
|
808
|
+
export function pipe<T>(...fns: Array<(value: T) => T>): (input: T) => T;
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Returns a function that only executes once, caching the result.
|
|
812
|
+
*/
|
|
813
|
+
export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
|
|
814
|
+
|
|
815
|
+
/** Returns a `Promise` that resolves after `ms` milliseconds. */
|
|
816
|
+
export function sleep(ms: number): Promise<void>;
|
|
817
|
+
|
|
818
|
+
|
|
819
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
820
|
+
// Utilities — Strings
|
|
821
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
822
|
+
|
|
823
|
+
/** Escape HTML entities: `&`, `<`, `>`, `"`, `'`. */
|
|
824
|
+
export function escapeHtml(str: string): string;
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Tagged template literal that auto-escapes interpolated values.
|
|
828
|
+
* Use `$.trust()` to mark values as safe.
|
|
829
|
+
*/
|
|
830
|
+
export function html(strings: TemplateStringsArray, ...values: any[]): string;
|
|
831
|
+
|
|
832
|
+
/** Wrapper that marks an HTML string as trusted (not escaped by `$.html`). */
|
|
833
|
+
interface TrustedHTML {
|
|
834
|
+
toString(): string;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** Mark an HTML string as trusted so it won't be escaped in `$.html`. */
|
|
838
|
+
export function trust(htmlStr: string): TrustedHTML;
|
|
839
|
+
|
|
840
|
+
/** Generate a UUID v4 string. */
|
|
841
|
+
export function uuid(): string;
|
|
842
|
+
|
|
843
|
+
/** Convert kebab-case to camelCase. */
|
|
844
|
+
export function camelCase(str: string): string;
|
|
845
|
+
|
|
846
|
+
/** Convert camelCase to kebab-case. */
|
|
847
|
+
export function kebabCase(str: string): string;
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
851
|
+
// Utilities — Objects
|
|
852
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
853
|
+
|
|
854
|
+
/** Deep clone using `structuredClone` (JSON fallback). */
|
|
855
|
+
export function deepClone<T>(obj: T): T;
|
|
856
|
+
|
|
857
|
+
/** Recursively merge objects. Arrays are replaced, not merged. */
|
|
858
|
+
export function deepMerge<T extends object>(target: T, ...sources: Partial<T>[]): T;
|
|
859
|
+
|
|
860
|
+
/** Deep equality comparison. */
|
|
861
|
+
export function isEqual(a: any, b: any): boolean;
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
865
|
+
// Utilities — URL
|
|
866
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
867
|
+
|
|
868
|
+
/** Serialize an object to a URL query string. */
|
|
869
|
+
export function param(obj: Record<string, any>): string;
|
|
870
|
+
|
|
871
|
+
/** Parse a URL query string into an object. */
|
|
872
|
+
export function parseQuery(str: string): Record<string, string>;
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
876
|
+
// Utilities — Storage
|
|
877
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
878
|
+
|
|
879
|
+
/** JSON-aware `localStorage` wrapper. */
|
|
880
|
+
interface StorageWrapper {
|
|
881
|
+
/** Get and JSON-parse a value. Returns `fallback` on missing / error. */
|
|
882
|
+
get<T = any>(key: string, fallback?: T): T;
|
|
883
|
+
/** JSON-stringify and store. */
|
|
884
|
+
set(key: string, value: any): void;
|
|
885
|
+
/** Remove a key. */
|
|
886
|
+
remove(key: string): void;
|
|
887
|
+
/** Clear all entries. */
|
|
888
|
+
clear(): void;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/** `localStorage` wrapper. */
|
|
892
|
+
export const storage: StorageWrapper;
|
|
893
|
+
|
|
894
|
+
/** `sessionStorage` wrapper (same API as `storage`). */
|
|
895
|
+
export const session: StorageWrapper;
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
899
|
+
// Utilities — Event Bus
|
|
900
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
901
|
+
|
|
902
|
+
/** Singleton pub/sub event bus for cross-component communication. */
|
|
903
|
+
interface EventBus {
|
|
904
|
+
/** Subscribe. Returns an unsubscribe function. */
|
|
905
|
+
on(event: string, fn: (...args: any[]) => void): () => void;
|
|
906
|
+
/** Unsubscribe a specific handler. */
|
|
907
|
+
off(event: string, fn: (...args: any[]) => void): void;
|
|
908
|
+
/** Emit an event with arguments. */
|
|
909
|
+
emit(event: string, ...args: any[]): void;
|
|
910
|
+
/** Subscribe for a single invocation. Returns an unsubscribe function. */
|
|
911
|
+
once(event: string, fn: (...args: any[]) => void): () => void;
|
|
912
|
+
/** Remove all listeners. */
|
|
913
|
+
clear(): void;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
export const bus: EventBus;
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
920
|
+
// $ — Main function & namespace
|
|
921
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Main selector / DOM-ready function.
|
|
925
|
+
*
|
|
926
|
+
* - `$('selector')` → single Element via `querySelector`
|
|
927
|
+
* - `$('<div>…</div>')` → create element from HTML
|
|
928
|
+
* - `$(element)` → return as-is
|
|
929
|
+
* - `$(fn)` → DOMContentLoaded shorthand
|
|
930
|
+
*/
|
|
931
|
+
interface ZQueryStatic {
|
|
932
|
+
(selector: string, context?: string | Element): Element | null;
|
|
933
|
+
(element: Element): Element;
|
|
934
|
+
(nodeList: NodeList | HTMLCollection | Element[]): Element | null;
|
|
935
|
+
(fn: () => void): void;
|
|
936
|
+
|
|
937
|
+
// ── Collection selector ─────────────────────────────────────────────────
|
|
938
|
+
/**
|
|
939
|
+
* Collection selector — returns a `ZQueryCollection`.
|
|
940
|
+
*
|
|
941
|
+
* - `$.all('.card')` → all matching elements
|
|
942
|
+
* - `$.all('<div>…</div>')` → create elements as collection
|
|
943
|
+
* - `$.all(element)` → wrap single element
|
|
944
|
+
* - `$.all(nodeList)` → wrap NodeList
|
|
945
|
+
*/
|
|
946
|
+
all(selector: string, context?: string | Element): ZQueryCollection;
|
|
947
|
+
all(element: Element): ZQueryCollection;
|
|
948
|
+
all(nodeList: NodeList | HTMLCollection | Element[]): ZQueryCollection;
|
|
949
|
+
|
|
950
|
+
// ── Quick-ref shortcuts ─────────────────────────────────────────────────
|
|
951
|
+
/** `document.getElementById(id)` */
|
|
952
|
+
id(id: string): Element | null;
|
|
953
|
+
/** `document.querySelector('.name')` */
|
|
954
|
+
class(name: string): Element | null;
|
|
955
|
+
/** `document.getElementsByClassName(name)` as array. */
|
|
956
|
+
classes(name: string): Element[];
|
|
957
|
+
/** `document.getElementsByTagName(name)` as array. */
|
|
958
|
+
tag(name: string): Element[];
|
|
959
|
+
/** Children of `#parentId` as array. */
|
|
960
|
+
children(parentId: string): Element[];
|
|
961
|
+
|
|
962
|
+
// ── Static helpers ──────────────────────────────────────────────────────
|
|
963
|
+
/**
|
|
964
|
+
* Create a DOM element.
|
|
965
|
+
* Special `attrs` keys: `class`, `style` (object), `on*` (handler), `data` (object).
|
|
966
|
+
*/
|
|
967
|
+
create(
|
|
968
|
+
tag: string,
|
|
969
|
+
attrs?: Record<string, any>,
|
|
970
|
+
...children: Array<string | Node>
|
|
971
|
+
): HTMLElement;
|
|
972
|
+
|
|
973
|
+
/** Register a DOMContentLoaded callback (fires immediately if already loaded). */
|
|
974
|
+
ready(fn: () => void): void;
|
|
975
|
+
|
|
976
|
+
/** Global event delegation on `document`. */
|
|
977
|
+
on(event: string, selector: string, handler: (this: Element, e: Event) => void): void;
|
|
978
|
+
|
|
979
|
+
/** Alias for `ZQueryCollection.prototype` — extend to add custom collection methods. */
|
|
980
|
+
fn: typeof ZQueryCollection.prototype;
|
|
981
|
+
|
|
982
|
+
// ── Reactive ────────────────────────────────────────────────────────────
|
|
983
|
+
reactive: typeof reactive;
|
|
984
|
+
signal: typeof signal;
|
|
985
|
+
computed: typeof computed;
|
|
986
|
+
effect: typeof effect;
|
|
987
|
+
|
|
988
|
+
// ── Components ──────────────────────────────────────────────────────────
|
|
989
|
+
component: typeof component;
|
|
990
|
+
mount: typeof mount;
|
|
991
|
+
mountAll: typeof mountAll;
|
|
992
|
+
getInstance: typeof getInstance;
|
|
993
|
+
destroy: typeof destroy;
|
|
994
|
+
/** Returns all registered component definitions. */
|
|
995
|
+
components: typeof getRegistry;
|
|
996
|
+
style: typeof style;
|
|
997
|
+
|
|
998
|
+
// ── Router ──────────────────────────────────────────────────────────────
|
|
999
|
+
router: typeof createRouter;
|
|
1000
|
+
getRouter: typeof getRouter;
|
|
1001
|
+
|
|
1002
|
+
// ── Store ───────────────────────────────────────────────────────────────
|
|
1003
|
+
store: typeof createStore;
|
|
1004
|
+
getStore: typeof getStore;
|
|
1005
|
+
|
|
1006
|
+
// ── HTTP ────────────────────────────────────────────────────────────────
|
|
1007
|
+
http: HttpClient;
|
|
1008
|
+
get: HttpClient['get'];
|
|
1009
|
+
post: HttpClient['post'];
|
|
1010
|
+
put: HttpClient['put'];
|
|
1011
|
+
patch: HttpClient['patch'];
|
|
1012
|
+
delete: HttpClient['delete'];
|
|
1013
|
+
|
|
1014
|
+
// ── Utilities ───────────────────────────────────────────────────────────
|
|
1015
|
+
debounce: typeof debounce;
|
|
1016
|
+
throttle: typeof throttle;
|
|
1017
|
+
pipe: typeof pipe;
|
|
1018
|
+
once: typeof once;
|
|
1019
|
+
sleep: typeof sleep;
|
|
1020
|
+
|
|
1021
|
+
escapeHtml: typeof escapeHtml;
|
|
1022
|
+
html: typeof html;
|
|
1023
|
+
trust: typeof trust;
|
|
1024
|
+
uuid: typeof uuid;
|
|
1025
|
+
camelCase: typeof camelCase;
|
|
1026
|
+
kebabCase: typeof kebabCase;
|
|
1027
|
+
|
|
1028
|
+
deepClone: typeof deepClone;
|
|
1029
|
+
deepMerge: typeof deepMerge;
|
|
1030
|
+
isEqual: typeof isEqual;
|
|
1031
|
+
|
|
1032
|
+
param: typeof param;
|
|
1033
|
+
parseQuery: typeof parseQuery;
|
|
1034
|
+
|
|
1035
|
+
storage: StorageWrapper;
|
|
1036
|
+
session: StorageWrapper;
|
|
1037
|
+
bus: EventBus;
|
|
1038
|
+
|
|
1039
|
+
// ── Meta ────────────────────────────────────────────────────────────────
|
|
1040
|
+
/** Library version string. */
|
|
1041
|
+
version: string;
|
|
1042
|
+
/** Populated at build time by the CLI bundler. */
|
|
1043
|
+
meta: Record<string, any>;
|
|
1044
|
+
/** Remove `$` from `window` and return the library object. */
|
|
1045
|
+
noConflict(): ZQueryStatic;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/** The main `$` / `zQuery` function + namespace. */
|
|
1049
|
+
export const $: ZQueryStatic;
|
|
1050
|
+
export { $ as zQuery };
|
|
1051
|
+
export const queryAll: typeof ZQueryCollection;
|
|
1052
|
+
|
|
1053
|
+
export default $;
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
1057
|
+
// Global augmentation (browser)
|
|
1058
|
+
// ───────────────────────────────────────────────────────────────────────────
|
|
1059
|
+
|
|
1060
|
+
declare global {
|
|
1061
|
+
interface Window {
|
|
1062
|
+
/** zQuery main function & namespace. */
|
|
1063
|
+
$: ZQueryStatic;
|
|
1064
|
+
/** Alias for `$`. */
|
|
1065
|
+
zQuery: ZQueryStatic;
|
|
1066
|
+
/** Optional base path override read by the router. */
|
|
1067
|
+
__ZQ_BASE?: string;
|
|
1068
|
+
/** Inline template / style cache populated by the CLI bundler. */
|
|
1069
|
+
__zqInline?: Record<string, string>;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/** Global `$` — available when loaded via `<script>` tag. */
|
|
1073
|
+
const $: ZQueryStatic;
|
|
1074
|
+
/** Global alias for `$`. */
|
|
1075
|
+
const zQuery: ZQueryStatic;
|
|
1076
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zero-query",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Lightweight modern frontend library — jQuery-like selectors, reactive components, SPA router, and state management with zero dependencies.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
6
7
|
"bin": {
|
|
7
8
|
"zquery": "./cli.js"
|
|
8
9
|
},
|
|
@@ -10,6 +11,7 @@
|
|
|
10
11
|
"src",
|
|
11
12
|
"dist",
|
|
12
13
|
"index.js",
|
|
14
|
+
"index.d.ts",
|
|
13
15
|
"cli.js",
|
|
14
16
|
"LICENSE",
|
|
15
17
|
"README.md"
|
|
@@ -45,7 +47,7 @@
|
|
|
45
47
|
"bugs": {
|
|
46
48
|
"url": "https://github.com/tonywied17/zero-query/issues"
|
|
47
49
|
},
|
|
48
|
-
"homepage": "https://
|
|
50
|
+
"homepage": "https://z-query.com/docs",
|
|
49
51
|
"publishConfig": {
|
|
50
52
|
"access": "public"
|
|
51
53
|
},
|