htmx-router 2.0.3 → 2.0.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/package.json +1 -1
- package/response.js +23 -18
package/package.json
CHANGED
package/response.js
CHANGED
|
@@ -1,55 +1,60 @@
|
|
|
1
1
|
export function text(text, init) {
|
|
2
|
-
init
|
|
3
|
-
init.statusText ||= "ok";
|
|
4
|
-
init.status ||= 200;
|
|
2
|
+
init = FillResponseInit(200, "Ok", init);
|
|
5
3
|
const res = new Response(text, init);
|
|
6
4
|
res.headers.set("Content-Type", "text/plain");
|
|
7
5
|
res.headers.set("X-Caught", "true");
|
|
8
6
|
return res;
|
|
9
7
|
}
|
|
10
8
|
export function html(text, init) {
|
|
11
|
-
init
|
|
12
|
-
init.statusText ||= "ok";
|
|
13
|
-
init.status ||= 200;
|
|
9
|
+
init = FillResponseInit(200, "Ok", init);
|
|
14
10
|
const res = new Response(text, init);
|
|
15
11
|
res.headers.set("Content-Type", "text/html; charset=UTF-8");
|
|
16
12
|
res.headers.set("X-Caught", "true");
|
|
17
13
|
return res;
|
|
18
14
|
}
|
|
19
15
|
export function json(data, init) {
|
|
20
|
-
init
|
|
21
|
-
init.statusText ||= "ok";
|
|
22
|
-
init.status ||= 200;
|
|
16
|
+
init = FillResponseInit(200, "Ok", init);
|
|
23
17
|
const res = new Response(JSON.stringify(data), init);
|
|
24
18
|
res.headers.set("Content-Type", "application/json");
|
|
25
19
|
res.headers.set("X-Caught", "true");
|
|
26
20
|
return res;
|
|
27
21
|
}
|
|
28
22
|
export function redirect(url, init) {
|
|
29
|
-
init
|
|
30
|
-
init.statusText ||= "Temporary Redirect";
|
|
31
|
-
init.status ||= 307;
|
|
23
|
+
init = FillResponseInit(307, "Temporary Redirect", init);
|
|
32
24
|
const res = new Response("", init);
|
|
33
25
|
if (!init?.clientOnly)
|
|
34
26
|
res.headers.set("Location", url);
|
|
35
27
|
res.headers.set("HX-Location", url); // use hx-boost if applicable
|
|
28
|
+
res.headers.set("X-Caught", "true");
|
|
36
29
|
return res;
|
|
37
30
|
}
|
|
38
31
|
export function revalidate(init) {
|
|
39
|
-
init
|
|
40
|
-
init.statusText ||= "ok";
|
|
41
|
-
init.status ||= 200;
|
|
32
|
+
init = FillResponseInit(200, "Ok", init);
|
|
42
33
|
const res = new Response("", init);
|
|
43
34
|
res.headers.set("HX-Location", "");
|
|
35
|
+
res.headers.set("HX-Replace-Url", "");
|
|
36
|
+
res.headers.set("X-Caught", "true");
|
|
44
37
|
return res;
|
|
45
38
|
}
|
|
46
39
|
export function refresh(init) {
|
|
47
|
-
init
|
|
48
|
-
init.statusText ||= "ok";
|
|
49
|
-
init.status ||= 200;
|
|
40
|
+
init = FillResponseInit(200, "Ok", init);
|
|
50
41
|
const res = new Response("", init);
|
|
51
42
|
if (!init?.clientOnly)
|
|
52
43
|
res.headers.set("Refresh", "0"); // fallback
|
|
53
44
|
res.headers.set("HX-Refresh", "true");
|
|
45
|
+
res.headers.set("X-Caught", "true");
|
|
54
46
|
return res;
|
|
55
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* This is to fix issues with deno
|
|
50
|
+
* When you try and change the statusText on a Response object
|
|
51
|
+
*/
|
|
52
|
+
function FillResponseInit(status, statusText, init) {
|
|
53
|
+
if (init === undefined)
|
|
54
|
+
init = {};
|
|
55
|
+
if (init.statusText === undefined)
|
|
56
|
+
init.statusText = statusText;
|
|
57
|
+
if (init.status === undefined)
|
|
58
|
+
init.status = status;
|
|
59
|
+
return init;
|
|
60
|
+
}
|