htmx-router 1.0.7 → 1.0.10
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/cookies.js +2 -2
- package/css.d.ts +1 -1
- package/css.js +1 -1
- package/defer.d.ts +1 -1
- package/defer.js +2 -2
- package/endpoint.d.ts +1 -1
- package/endpoint.js +1 -1
- package/internal/mount.d.ts +1 -1
- package/internal/mount.js +1 -1
- package/internal/router.d.ts +1 -1
- package/internal/router.js +2 -2
- package/package.json +1 -1
- package/router.d.ts +12 -2
- package/router.js +26 -21
package/cookies.js
CHANGED
|
@@ -52,7 +52,7 @@ export class Cookies {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
string(name) {
|
|
55
|
-
return encodeURIComponent(name) + "=" + encodeURIComponent(this.map[name]) +
|
|
55
|
+
return encodeURIComponent(name) + "=" + encodeURIComponent(this.map[name]) + StringifyOptions(this.config[name]);
|
|
56
56
|
;
|
|
57
57
|
}
|
|
58
58
|
unset(name) {
|
|
@@ -82,7 +82,7 @@ function StringifyOptions(options) {
|
|
|
82
82
|
continue;
|
|
83
83
|
let value = String(raw);
|
|
84
84
|
value = value[0].toUpperCase() + value.slice(1);
|
|
85
|
-
config +=
|
|
85
|
+
config += `; ${prop}=${value}`;
|
|
86
86
|
}
|
|
87
87
|
return config;
|
|
88
88
|
}
|
package/css.d.ts
CHANGED
package/css.js
CHANGED
package/defer.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export declare function Deferral<T extends ParameterShaper>(func: RenderFunction
|
|
|
6
6
|
/**
|
|
7
7
|
* RouteTree mounting point
|
|
8
8
|
*/
|
|
9
|
-
export declare const path = "_/defer/$";
|
|
9
|
+
export declare const path = "/_/defer/$";
|
|
10
10
|
export declare const parameters: {
|
|
11
11
|
$: StringConstructor;
|
|
12
12
|
};
|
package/defer.js
CHANGED
|
@@ -52,7 +52,7 @@ export function Deferral(func, params) {
|
|
|
52
52
|
/**
|
|
53
53
|
* RouteTree mounting point
|
|
54
54
|
*/
|
|
55
|
-
export const path = "_/defer/$";
|
|
55
|
+
export const path = "/_/defer/$";
|
|
56
56
|
export const parameters = {
|
|
57
57
|
"$": String
|
|
58
58
|
};
|
|
@@ -69,7 +69,7 @@ export async function loader(ctx) {
|
|
|
69
69
|
return null;
|
|
70
70
|
}
|
|
71
71
|
ctx.headers.set("X-Partial", "true");
|
|
72
|
-
const forward = new RouteContext(ctx, prelude, entry.shape);
|
|
72
|
+
const forward = new RouteContext(ctx, prelude, entry.shape, "");
|
|
73
73
|
return await endpoint(forward);
|
|
74
74
|
}
|
|
75
75
|
export const action = loader;
|
package/endpoint.d.ts
CHANGED
package/endpoint.js
CHANGED
package/internal/mount.d.ts
CHANGED
package/internal/mount.js
CHANGED
package/internal/router.d.ts
CHANGED
|
@@ -12,6 +12,6 @@ export declare class GenericContext {
|
|
|
12
12
|
url: URL;
|
|
13
13
|
render: (res: JSX.Element, headers: Headers) => Promise<Rendered> | Rendered;
|
|
14
14
|
constructor(request: GenericContext["request"], url: GenericContext["url"], renderer: GenericContext["render"]);
|
|
15
|
-
shape<T extends ParameterShaper>(shape: T): RouteContext<T>;
|
|
15
|
+
shape<T extends ParameterShaper>(shape: T, path: string): RouteContext<T>;
|
|
16
16
|
}
|
|
17
17
|
export {};
|
package/internal/router.js
CHANGED
|
@@ -19,7 +19,7 @@ export class GenericContext {
|
|
|
19
19
|
this.headers.set("x-powered-by", "htmx-router");
|
|
20
20
|
this.headers.set("content-type", "text/html");
|
|
21
21
|
}
|
|
22
|
-
shape(shape) {
|
|
23
|
-
return new RouteContext(this, this.params, shape);
|
|
22
|
+
shape(shape, path) {
|
|
23
|
+
return new RouteContext(this, this.params, shape, path);
|
|
24
24
|
}
|
|
25
25
|
}
|
package/package.json
CHANGED
package/router.d.ts
CHANGED
|
@@ -7,13 +7,14 @@ export declare function GenerateRouteTree(props: {
|
|
|
7
7
|
scope: string;
|
|
8
8
|
}): RouteTree;
|
|
9
9
|
export declare class RouteContext<T extends ParameterShaper = {}> {
|
|
10
|
+
readonly path: string;
|
|
10
11
|
readonly request: Request;
|
|
11
12
|
readonly headers: Headers;
|
|
12
13
|
readonly cookie: Cookies;
|
|
13
14
|
readonly params: Parameterized<T>;
|
|
14
15
|
readonly url: URL;
|
|
15
16
|
render: GenericContext["render"];
|
|
16
|
-
constructor(base: GenericContext | RouteContext, params: ParameterPrelude<T>, shape: T);
|
|
17
|
+
constructor(base: GenericContext | RouteContext, params: ParameterPrelude<T>, shape: T, path: string);
|
|
17
18
|
}
|
|
18
19
|
export declare class RouteTree {
|
|
19
20
|
private nested;
|
|
@@ -22,7 +23,7 @@ export declare class RouteTree {
|
|
|
22
23
|
private wild;
|
|
23
24
|
private wildCard;
|
|
24
25
|
constructor();
|
|
25
|
-
ingest(
|
|
26
|
+
ingest(node: RouteLeaf, path?: string[]): void;
|
|
26
27
|
resolve(fragments: string[], ctx: GenericContext): Promise<Response | null>;
|
|
27
28
|
private _resolve;
|
|
28
29
|
private resolveIndex;
|
|
@@ -31,3 +32,12 @@ export declare class RouteTree {
|
|
|
31
32
|
private resolveSlug;
|
|
32
33
|
private unwrap;
|
|
33
34
|
}
|
|
35
|
+
declare class RouteLeaf {
|
|
36
|
+
private module;
|
|
37
|
+
readonly path: string;
|
|
38
|
+
constructor(module: RouteModule<any>, path: string);
|
|
39
|
+
resolve(ctx: GenericContext): Promise<Response | null>;
|
|
40
|
+
error(ctx: GenericContext, e: unknown): Promise<Response>;
|
|
41
|
+
private response;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
package/router.js
CHANGED
|
@@ -13,27 +13,32 @@ export function GenerateRouteTree(props) {
|
|
|
13
13
|
const tree = new RouteTree();
|
|
14
14
|
for (const path in props.modules) {
|
|
15
15
|
const mod = props.modules[path];
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
let tail = path.lastIndexOf(".");
|
|
17
|
+
if (path.endsWith("/_index", tail))
|
|
18
|
+
tail -= "/_index".length;
|
|
19
|
+
const url = path.slice(props.scope.length - 1, tail);
|
|
20
|
+
const leaf = new RouteLeaf(mod, url);
|
|
21
|
+
tree.ingest(leaf);
|
|
19
22
|
if (mod.route)
|
|
20
23
|
mod.route(url);
|
|
21
24
|
}
|
|
22
25
|
// ingest router builtins
|
|
23
|
-
tree.ingest(endpoint
|
|
24
|
-
tree.ingest(dynamic
|
|
25
|
-
tree.ingest(mount
|
|
26
|
-
tree.ingest(css
|
|
26
|
+
tree.ingest(new RouteLeaf(endpoint, endpoint.path));
|
|
27
|
+
tree.ingest(new RouteLeaf(dynamic, dynamic.path));
|
|
28
|
+
tree.ingest(new RouteLeaf(mount, mount.path));
|
|
29
|
+
tree.ingest(new RouteLeaf(css, css.path));
|
|
27
30
|
return tree;
|
|
28
31
|
}
|
|
29
32
|
export class RouteContext {
|
|
33
|
+
path;
|
|
30
34
|
request;
|
|
31
35
|
headers; // response headers
|
|
32
36
|
cookie;
|
|
33
37
|
params;
|
|
34
38
|
url;
|
|
35
39
|
render;
|
|
36
|
-
constructor(base, params, shape) {
|
|
40
|
+
constructor(base, params, shape, path) {
|
|
41
|
+
this.path = path;
|
|
37
42
|
this.cookie = base.cookie;
|
|
38
43
|
this.headers = base.headers;
|
|
39
44
|
this.request = base.request;
|
|
@@ -67,15 +72,15 @@ export class RouteTree {
|
|
|
67
72
|
this.wild = null;
|
|
68
73
|
this.slug = null;
|
|
69
74
|
}
|
|
70
|
-
ingest(
|
|
71
|
-
if (!
|
|
72
|
-
path = path.split("/");
|
|
73
|
-
if (path.length === 0
|
|
74
|
-
this.index =
|
|
75
|
+
ingest(node, path) {
|
|
76
|
+
if (!path)
|
|
77
|
+
path = node.path.length === 0 ? [] : node.path.slice(1).split("/");
|
|
78
|
+
if (path.length === 0) {
|
|
79
|
+
this.index = node;
|
|
75
80
|
return;
|
|
76
81
|
}
|
|
77
82
|
if (path[0] === "$") {
|
|
78
|
-
this.slug =
|
|
83
|
+
this.slug = node;
|
|
79
84
|
return;
|
|
80
85
|
}
|
|
81
86
|
if (path[0][0] === "$") {
|
|
@@ -88,8 +93,7 @@ export class RouteTree {
|
|
|
88
93
|
else if (wildCard !== this.wildCard) {
|
|
89
94
|
throw new Error(`Redefinition of wild card ${this.wildCard} to ${wildCard}`);
|
|
90
95
|
}
|
|
91
|
-
|
|
92
|
-
this.wild.ingest(path, module);
|
|
96
|
+
this.wild.ingest(node, path.slice(1));
|
|
93
97
|
return;
|
|
94
98
|
}
|
|
95
99
|
let next = this.nested.get(path[0]);
|
|
@@ -97,8 +101,7 @@ export class RouteTree {
|
|
|
97
101
|
next = new RouteTree();
|
|
98
102
|
this.nested.set(path[0], next);
|
|
99
103
|
}
|
|
100
|
-
|
|
101
|
-
next.ingest(path, module);
|
|
104
|
+
next.ingest(node, path.slice(1));
|
|
102
105
|
}
|
|
103
106
|
async resolve(fragments, ctx) {
|
|
104
107
|
if (!this.slug)
|
|
@@ -173,8 +176,10 @@ export class RouteTree {
|
|
|
173
176
|
}
|
|
174
177
|
class RouteLeaf {
|
|
175
178
|
module;
|
|
176
|
-
|
|
179
|
+
path;
|
|
180
|
+
constructor(module, path) {
|
|
177
181
|
this.module = module;
|
|
182
|
+
this.path = path;
|
|
178
183
|
}
|
|
179
184
|
async resolve(ctx) {
|
|
180
185
|
const jsx = await this.response(ctx);
|
|
@@ -190,7 +195,7 @@ class RouteLeaf {
|
|
|
190
195
|
async error(ctx, e) {
|
|
191
196
|
if (!this.module.error)
|
|
192
197
|
throw e;
|
|
193
|
-
let jsx = await this.module.error(ctx, e);
|
|
198
|
+
let jsx = await this.module.error(ctx.shape({}, this.path), e);
|
|
194
199
|
const caught = jsx instanceof Response ? jsx
|
|
195
200
|
: await ctx.render(jsx, ctx.headers);
|
|
196
201
|
if (caught instanceof Response) {
|
|
@@ -204,7 +209,7 @@ class RouteLeaf {
|
|
|
204
209
|
try {
|
|
205
210
|
if (!this.module.loader && !this.module.action)
|
|
206
211
|
return null;
|
|
207
|
-
const context = ctx.shape(this.module.parameters || {});
|
|
212
|
+
const context = ctx.shape(this.module.parameters || {}, this.path);
|
|
208
213
|
if (ctx.request.method === "HEAD" || ctx.request.method === "GET") {
|
|
209
214
|
if (this.module.loader)
|
|
210
215
|
return await this.module.loader(context);
|