htmx-router 0.2.0 → 1.0.0-alpha.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.
Files changed (55) hide show
  1. package/bin/cli/config.d.ts +10 -0
  2. package/bin/cli/config.js +4 -0
  3. package/bin/cli/index.js +54 -10
  4. package/bin/client/index.d.ts +4 -0
  5. package/bin/client/index.js +100 -0
  6. package/bin/client/mount.d.ts +2 -0
  7. package/bin/client/mount.js +75 -0
  8. package/bin/client/watch.d.ts +1 -0
  9. package/bin/client/watch.js +19 -0
  10. package/bin/helper.d.ts +1 -2
  11. package/bin/helper.js +25 -14
  12. package/bin/index.d.ts +8 -6
  13. package/bin/index.js +7 -16
  14. package/bin/request/http.d.ts +10 -0
  15. package/bin/request/http.js +46 -0
  16. package/bin/request/index.d.ts +16 -0
  17. package/bin/request/index.js +6 -0
  18. package/bin/request/native.d.ts +9 -0
  19. package/bin/request/native.js +56 -0
  20. package/bin/router.d.ts +41 -16
  21. package/bin/router.js +176 -236
  22. package/bin/types.d.ts +10 -0
  23. package/bin/types.js +1 -0
  24. package/bin/util/cookies.d.ts +22 -0
  25. package/bin/util/cookies.js +57 -0
  26. package/bin/util/css.d.ts +9 -0
  27. package/bin/util/css.js +47 -0
  28. package/bin/util/dynamic.d.ts +5 -0
  29. package/bin/util/dynamic.js +26 -0
  30. package/bin/util/endpoint.d.ts +9 -0
  31. package/bin/util/endpoint.js +28 -0
  32. package/bin/util/event-source.d.ts +16 -0
  33. package/bin/util/event-source.js +85 -0
  34. package/bin/util/hash.d.ts +1 -0
  35. package/bin/util/hash.js +7 -0
  36. package/bin/util/index.d.ts +1 -0
  37. package/bin/util/index.js +7 -0
  38. package/bin/util/parameters.d.ts +7 -0
  39. package/bin/util/parameters.js +14 -0
  40. package/bin/util/shell.d.ts +32 -0
  41. package/bin/util/shell.js +1 -0
  42. package/package.json +9 -7
  43. package/readme.md +149 -213
  44. package/bin/404-route.d.ts +0 -2
  45. package/bin/404-route.js +0 -8
  46. package/bin/cli/dynamic.d.ts +0 -2
  47. package/bin/cli/dynamic.js +0 -47
  48. package/bin/cli/static.d.ts +0 -2
  49. package/bin/cli/static.js +0 -49
  50. package/bin/components.d.ts +0 -8
  51. package/bin/components.js +0 -11
  52. package/bin/render-args.d.ts +0 -35
  53. package/bin/render-args.js +0 -120
  54. package/bin/shared.d.ts +0 -28
  55. package/bin/shared.js +0 -28
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.RenderArgs = exports.MaskType = void 0;
7
- const html_1 = __importDefault(require("@kitajs/html"));
8
- const titleScript = `<script>` +
9
- `document.addEventListener("DOMContentLoaded",function(){` +
10
- `document.body.addEventListener("setTitle",function(evt){document.title=decodeURIComponent(evt.detail.value);})` +
11
- `});` +
12
- `</script>`;
13
- const attrRegex = /^[A-z][A-z\-0-9]+$/;
14
- function ValidateMetaHTML(val) {
15
- for (const key in val) {
16
- if (!attrRegex.test(key))
17
- return false;
18
- }
19
- return true;
20
- }
21
- function ValidateMetaHTMLs(val) {
22
- for (const meta of val) {
23
- if (!ValidateMetaHTML(meta))
24
- return false;
25
- }
26
- return true;
27
- }
28
- var MaskType;
29
- (function (MaskType) {
30
- MaskType[MaskType["show"] = 0] = "show";
31
- MaskType[MaskType["headless"] = 1] = "headless";
32
- MaskType[MaskType["hide"] = 2] = "hide";
33
- })(MaskType || (exports.MaskType = MaskType = {}));
34
- class RenderArgs {
35
- constructor(req, res, url) {
36
- this.setTitle = (value) => {
37
- this.title = value;
38
- };
39
- this.addLinks = (links, override = false) => {
40
- if (!ValidateMetaHTMLs(links))
41
- throw new Error(`Provided links have invalid attribute`);
42
- if (override) {
43
- this.links = links;
44
- }
45
- else {
46
- this.links.push(...links);
47
- }
48
- };
49
- this.addMeta = (links, override = false) => {
50
- if (!ValidateMetaHTMLs(links))
51
- throw new Error(`Provided links have invalid attribute`);
52
- if (override) {
53
- this.meta = links;
54
- }
55
- else {
56
- this.meta.push(...links);
57
- }
58
- };
59
- // unpacking Outlet caused this to be undefined
60
- // hence the weird def
61
- this.Outlet = () => {
62
- const depth = this._maxChain - this._outletChain.length;
63
- const route = this._outletChain.pop();
64
- let mask = this._maskChain.pop();
65
- if (mask === undefined) {
66
- mask = MaskType.show;
67
- }
68
- if (!route)
69
- return new Promise((res) => res(""));
70
- const routeName = `hx-route-${depth.toString(16)}`;
71
- return route.render(this, mask, routeName);
72
- };
73
- this.renderHeadHTML = () => {
74
- let out = html_1.default.createElement("title", null, this.title);
75
- for (const elm of this.links) {
76
- out += "<link";
77
- for (const attr in elm) {
78
- out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
79
- }
80
- out += "></link>";
81
- }
82
- for (const elm of this.meta) {
83
- out += "<meta";
84
- for (const attr in elm) {
85
- out += ` ${attr}="${elm[attr].replace(/"/g, "\\\"")}"`;
86
- }
87
- out += "></meta>";
88
- }
89
- return out + titleScript;
90
- };
91
- this.req = req;
92
- this.res = res;
93
- this.url = url;
94
- this.params = {};
95
- this.title = "";
96
- this.shared = {};
97
- this.links = [];
98
- this.meta = [];
99
- this._outletChain = [];
100
- this._maskChain = [];
101
- this._maxChain = 0;
102
- }
103
- _addOutlet(route) {
104
- this._outletChain.push(route);
105
- }
106
- _applyMask(mask, depth) {
107
- const padded = new Array(Math.max(0, this._outletChain.length - mask.length)).fill(false);
108
- for (let i = mask.length - 1; i >= 0; i--) {
109
- padded.push(mask[i]);
110
- }
111
- this._maskChain = padded
112
- .map((x, i) => x === true ?
113
- MaskType.hide :
114
- padded.length - i > depth ?
115
- MaskType.show :
116
- MaskType.headless);
117
- this._maxChain = this._maskChain.length;
118
- }
119
- }
120
- exports.RenderArgs = RenderArgs;
package/bin/shared.d.ts DELETED
@@ -1,28 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import type http from "node:http";
4
- import { RenderArgs } from "./render-args";
5
- export type Outlet = () => Promise<string>;
6
- export type CatchFunction = (routeName: string, args: RenderArgs, err: ErrorResponse) => Promise<string>;
7
- export type RenderFunction = (routeName: string, args: RenderArgs) => Promise<string>;
8
- export type AuthFunction = (args: RenderArgs) => Promise<void>;
9
- export type RouteModule = {
10
- Render?: RenderFunction;
11
- CatchError?: CatchFunction;
12
- Auth?: AuthFunction;
13
- };
14
- export declare class ErrorResponse {
15
- code: number;
16
- status: string;
17
- data: any;
18
- constructor(statusCode: number, statusMessage: string, data?: any);
19
- }
20
- export declare class Redirect {
21
- location: string;
22
- constructor(location: string);
23
- run(res: http.ServerResponse): http.ServerResponse<http.IncomingMessage>;
24
- }
25
- export declare class Override {
26
- data: string | Buffer | Uint8Array;
27
- constructor(data: string | Buffer | Uint8Array);
28
- }
package/bin/shared.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Override = exports.Redirect = exports.ErrorResponse = void 0;
4
- class ErrorResponse {
5
- constructor(statusCode, statusMessage, data) {
6
- this.code = statusCode;
7
- this.status = statusMessage;
8
- this.data = data || "";
9
- }
10
- }
11
- exports.ErrorResponse = ErrorResponse;
12
- class Redirect {
13
- constructor(location) {
14
- this.location = location;
15
- }
16
- run(res) {
17
- res.statusCode = 302;
18
- res.setHeader('Location', this.location);
19
- return res.end();
20
- }
21
- }
22
- exports.Redirect = Redirect;
23
- class Override {
24
- constructor(data) {
25
- this.data = data;
26
- }
27
- }
28
- exports.Override = Override;