@torpor/build 0.1.24 → 0.1.26

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 (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/{Site-DgXPSQoO.d.ts → Site-4tGiw2-I.d.ts} +23 -2
  3. package/dist/{Site-BeCLcGj_.d.cts → Site-BXX-S541.d.cts} +23 -2
  4. package/dist/bin/index.cjs +1 -1
  5. package/dist/bin/index.cjs.map +1 -1
  6. package/dist/bin/index.js +3 -3
  7. package/dist/{chunk-WMHM5RGZ.js → chunk-565UOT3F.js} +5 -2
  8. package/dist/chunk-565UOT3F.js.map +1 -0
  9. package/dist/{chunk-D6LHMH2C.js → chunk-B2UNTYN5.js} +8 -8
  10. package/dist/chunk-B2UNTYN5.js.map +1 -0
  11. package/dist/{chunk-65K3R3CP.js → chunk-PTVHEJZ5.js} +4 -2
  12. package/dist/{chunk-65K3R3CP.js.map → chunk-PTVHEJZ5.js.map} +1 -1
  13. package/dist/{chunk-LHRCEWBG.js → chunk-UCA7KIGG.js} +2 -2
  14. package/dist/{chunk-2QQN3D3K.js → chunk-WVXAN4IV.js} +2 -2
  15. package/dist/{chunk-2QQN3D3K.js.map → chunk-WVXAN4IV.js.map} +1 -1
  16. package/dist/index.cjs +64 -26
  17. package/dist/index.cjs.map +1 -1
  18. package/dist/index.d.cts +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.js +48 -10
  21. package/dist/index.js.map +1 -1
  22. package/dist/metafile-cjs.json +1 -1
  23. package/dist/metafile-esm.json +1 -1
  24. package/dist/response.cjs +7 -7
  25. package/dist/response.cjs.map +1 -1
  26. package/dist/response.js +2 -2
  27. package/dist/run.cjs +1 -1
  28. package/dist/run.cjs.map +1 -1
  29. package/dist/run.d.cts +1 -1
  30. package/dist/run.d.ts +1 -1
  31. package/dist/run.js +3 -3
  32. package/dist/state.cjs +4 -1
  33. package/dist/state.cjs.map +1 -1
  34. package/dist/state.d.cts +4 -0
  35. package/dist/state.d.ts +4 -0
  36. package/dist/state.js +1 -1
  37. package/dist/test.cjs +5 -2
  38. package/dist/test.cjs.map +1 -1
  39. package/dist/test.d.cts +1 -1
  40. package/dist/test.d.ts +1 -1
  41. package/dist/test.js +3 -3
  42. package/package.json +4 -4
  43. package/src/response/badRequest.ts +1 -1
  44. package/src/response/forbidden.ts +1 -1
  45. package/src/response/methodNotAllowed.ts +1 -1
  46. package/src/response/notFound.ts +1 -1
  47. package/src/response/serverError.ts +1 -1
  48. package/src/response/unauthorized.ts +1 -1
  49. package/src/response/unprocessable.ts +1 -1
  50. package/src/site/Site.ts +82 -20
  51. package/src/site/clientEntry.ts +11 -4
  52. package/src/site/serverEntry.ts +42 -5
  53. package/src/state/$page.ts +4 -5
  54. package/src/state/Page.ts +8 -0
  55. package/src/types/RouteType.ts +12 -0
  56. package/dist/chunk-D6LHMH2C.js.map +0 -1
  57. package/dist/chunk-WMHM5RGZ.js.map +0 -1
  58. /package/dist/{chunk-LHRCEWBG.js.map → chunk-UCA7KIGG.js.map} +0 -0
@@ -10,6 +10,7 @@ import type PageEndPoint from "../types/PageEndPoint.ts";
10
10
  import type PageServerEndPoint from "../types/PageServerEndPoint.ts";
11
11
  import RouteHandler from "../types/RouteHandler.ts";
12
12
  import {
13
+ ERROR_ROUTE,
13
14
  HOOK_ROUTE,
14
15
  HOOK_SERVER_ROUTE,
15
16
  LAYOUT_ROUTE,
@@ -29,6 +30,33 @@ router.addPages(manifest.routes);
29
30
 
30
31
  //console.log(`routes:\n ${router.routes.map((r) => r.path).join("\n ")}`);
31
32
 
33
+ async function handleResponse(response: Response): Promise<Response> {
34
+ $page.status = response.status;
35
+
36
+ // Success codes and redirect codes are acceptable
37
+ if (response.status >= 200 && response.status <= 399) {
38
+ return response;
39
+ }
40
+
41
+ // It's an error, so redirect to the error page
42
+ // We're just pushing the status and message in the URL, but maybe there's a
43
+ // more sophisticated way to do this?
44
+ // TODO: Should be returning loadView rather than redirecting
45
+ // TODO: Should be returning the NEAREST error page to this path, including layouts
46
+ let params = new URLSearchParams();
47
+ params.append("status", $page.status.toString());
48
+ let message = await response.text();
49
+ if (response.headers.get("Content-Type")?.includes("application/json")) {
50
+ const data = JSON.parse(message);
51
+ message = data.message ?? message;
52
+ }
53
+ if (message) {
54
+ $page.error.message = message;
55
+ params.append("message", message);
56
+ }
57
+ return seeOther(`/_error?${params.toString()}`);
58
+ }
59
+
32
60
  export async function load(ev: ServerEvent, template: string): Promise<Response> {
33
61
  //const url = new URL(`http://${process.env.HOST ?? "localhost"}${ev.request.url}`);
34
62
  const url = new URL(ev.request.url);
@@ -39,12 +67,17 @@ export async function load(ev: ServerEvent, template: string): Promise<Response>
39
67
 
40
68
  const route = router.match(path, query);
41
69
  if (!route) {
42
- return notFound();
70
+ return handleResponse(notFound());
43
71
  }
44
72
 
45
73
  // Update $page before building the components
46
- // TODO: Find somewhere better to put this
47
74
  $page.url = url;
75
+ if (path.endsWith("/_error")) {
76
+ $page.status = parseInt(query.get("status") ?? "404");
77
+ $page.error.message = query.get("message") ?? "";
78
+ } else {
79
+ $page.status = 200;
80
+ }
48
81
 
49
82
  const handler = route.handler;
50
83
  const params = route.params || {};
@@ -57,9 +90,9 @@ export async function load(ev: ServerEvent, template: string): Promise<Response>
57
90
  case LAYOUT_ROUTE: {
58
91
  // It's a /+page.ts or /_layout.ts endpoint
59
92
  if (ev.request.method === "GET") {
60
- return await loadView(ev, url, handler, params, template);
93
+ return handleResponse(await loadView(ev, url, handler, params, template));
61
94
  } else if (ev.request.method === "POST") {
62
- return await runAction(ev, url, handler, params, query);
95
+ return handleResponse(await runAction(ev, url, handler, params, query));
63
96
  }
64
97
  break;
65
98
  }
@@ -80,6 +113,9 @@ export async function load(ev: ServerEvent, template: string): Promise<Response>
80
113
  case HOOK_SERVER_ROUTE: {
81
114
  break;
82
115
  }
116
+ case ERROR_ROUTE: {
117
+ return await loadView(ev, url, handler, params, template);
118
+ }
83
119
  }
84
120
 
85
121
  return notFound();
@@ -141,7 +177,8 @@ async function loadView(
141
177
 
142
178
  // Pass the data into $props
143
179
  // TODO: Promise.all
144
- // NOTE: We're loading data from top to bottom, overriding as we go, and I'm not sure if this is the best way to go
180
+ // NOTE: We're loading data from top to bottom, overriding as we go, and I'm
181
+ // not sure if this is the best way to go
145
182
  let data = {};
146
183
  if (handler.layouts) {
147
184
  for (let layout of handler.layouts) {
@@ -1,14 +1,13 @@
1
1
  import { $watch } from "@torpor/view";
2
-
3
- type Page = {
4
- status: number;
5
- url: URL;
6
- };
2
+ import Page from "./Page";
7
3
 
8
4
  const $page: Page = $watch(
9
5
  {
10
6
  status: 404,
11
7
  url: new URL("http://localhost"),
8
+ error: {
9
+ message: "",
10
+ },
12
11
  },
13
12
  { shallow: true },
14
13
  );
@@ -0,0 +1,8 @@
1
+ type Page = {
2
+ status: number;
3
+ url: URL;
4
+ error: {
5
+ message: string;
6
+ };
7
+ };
8
+ export default Page;
@@ -5,3 +5,15 @@ export const LAYOUT_ROUTE = 4;
5
5
  export const LAYOUT_SERVER_ROUTE = 5;
6
6
  export const HOOK_ROUTE = 6;
7
7
  export const HOOK_SERVER_ROUTE = 7;
8
+ export const ERROR_ROUTE = 8;
9
+
10
+ export type RouteType =
11
+ | typeof PAGE_ROUTE
12
+ | typeof PAGE_SERVER_ROUTE
13
+ | typeof SERVER_ROUTE
14
+ | typeof LAYOUT_ROUTE
15
+ | typeof LAYOUT_SERVER_ROUTE
16
+ | typeof HOOK_ROUTE
17
+ | typeof HOOK_SERVER_ROUTE
18
+ | typeof ERROR_ROUTE
19
+ | -1;
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/response/badRequest.ts","../src/response/created.ts","../src/response/forbidden.ts","../src/response/methodNotAllowed.ts","../src/response/moved.ts","../src/response/notModified.ts","../src/response/redirect.ts","../src/response/serverError.ts","../src/response/unauthorized.ts","../src/response/unprocessable.ts"],"sourcesContent":["import response from \"./response\";\n\n/**\n * 400 Bad Request\n *\n * The server cannot or will not process the request due to something that is\n * perceived to be a client error (e.g., malformed request syntax, invalid\n * request message framing, or deceptive request routing).\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400\n */\nexport default function badRequest(body?: object | string): Response {\n\treturn response(400, body);\n}\n","import response from \"./response\";\n\n/**\n * 201 Created\n *\n * The request succeeded, and a new resource was created as a result. This is\n * typically the response sent after POST requests, or some PUT requests.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201\n */\nexport default function created(body?: object | string): Response {\n\treturn response(201, body);\n}\n","import response from \"./response\";\n\n/**\n * 403 Forbidden\n *\n * The client does not have access rights to the content; that is, it is\n * unauthorized, so the server is refusing to give the requested resource.\n * Unlike 401 Unauthorized, the client's identity is known to the server.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403\n */\nexport default function forbidden(body?: object | string): Response {\n\treturn response(403, body);\n}\n","import response from \"./response\";\n\n/**\n * 405 Method Not Allowed\n *\n * The request method is known by the server but is not supported by the target\n * resource. For example, an API may not allow calling DELETE to remove a\n * resource.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405\n */\nexport default function methodNotAllowed(body?: object | string): Response {\n\treturn response(405, body);\n}\n","import transfer from \"./transfer\";\n\n/**\n * 308 Permanent Redirect\n *\n * This means that the resource is now permanently located at another URI,\n * specified by the Location: HTTP Response header. This has the same semantics\n * as the 301 Moved Permanently HTTP response code, with the exception that the\n * user agent must not change the HTTP method used: if a POST was used in the\n * first request, a POST must be used in the second request.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308\n */\nexport default function moved(location: string): Response {\n\treturn transfer(308, location);\n}\n","import transfer from \"./transfer\";\n\n/**\n * 304 Not Modified\n *\n * This is used for caching purposes. It tells the client that the response has\n * not been modified, so the client can continue to use the same cached version\n * of the response.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304\n */\nexport default function notModified(location: string): Response {\n\treturn transfer(304, location);\n}\n","import transfer from \"./transfer\";\n\n/**\n * 307 Temporary Redirect\n *\n * The server sends this response to direct the client to get the requested\n * resource at another URI with the same method that was used in the prior\n * request. This has the same semantics as the 302 Found HTTP response code,\n * with the exception that the user agent must not change the HTTP method used:\n * if a POST was used in the first request, a POST must be used in the second\n * request.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307\n */\nexport default function redirect(location: string): Response {\n\treturn transfer(307, location);\n}\n","import response from \"./response\";\n\n/**\n * 500 Server Error\n *\n * The server has encountered a situation it does not know how to handle.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500\n */\nexport default function serverError(body?: object | string): Response {\n\treturn response(500, body);\n}\n","import response from \"./response\";\n\n/**\n * 401 Unauthorized\n *\n * Although the HTTP standard specifies \"unauthorized\", semantically this\n * response means \"unauthenticated\". That is, the client must authenticate\n * itself to get the requested response.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401\n */\nexport default function unauthorized(body?: object | string): Response {\n\treturn response(401, body);\n}\n","import response from \"./response\";\n\n/**\n * 422 Unprocessable Content\n *\n * The request was well-formed but was unable to be followed due to semantic errors.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422\n */\nexport default function unprocessable(body?: object | string): Response {\n\treturn response(422, body);\n}\n"],"mappings":";;;;;;AAWe,SAAR,WAA4B,MAAkC;AACpE,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACHe,SAAR,QAAyB,MAAkC;AACjE,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACDe,SAAR,UAA2B,MAAkC;AACnE,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACFe,SAAR,iBAAkC,MAAkC;AAC1E,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACAe,SAAR,MAAuB,UAA4B;AACzD,SAAO,SAAS,KAAK,QAAQ;AAC9B;;;ACJe,SAAR,YAA6B,UAA4B;AAC/D,SAAO,SAAS,KAAK,QAAQ;AAC9B;;;ACCe,SAAR,SAA0B,UAA4B;AAC5D,SAAO,SAAS,KAAK,QAAQ;AAC9B;;;ACPe,SAAR,YAA6B,MAAkC;AACrE,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACAe,SAAR,aAA8B,MAAkC;AACtE,SAAO,SAAS,KAAK,IAAI;AAC1B;;;ACJe,SAAR,cAA+B,MAAkC;AACvE,SAAO,SAAS,KAAK,IAAI;AAC1B;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/state/$page.ts"],"sourcesContent":["import { $watch } from \"@torpor/view\";\n\ntype Page = {\n\tstatus: number;\n\turl: URL;\n};\n\nconst $page: Page = $watch(\n\t{\n\t\tstatus: 404,\n\t\turl: new URL(\"http://localhost\"),\n\t},\n\t{ shallow: true },\n);\n\nexport default $page;\n"],"mappings":";AAAA,SAAS,cAAc;AAOvB,IAAM,QAAc;AAAA,EACnB;AAAA,IACC,QAAQ;AAAA,IACR,KAAK,IAAI,IAAI,kBAAkB;AAAA,EAChC;AAAA,EACA,EAAE,SAAS,KAAK;AACjB;AAEA,IAAO,eAAQ;","names":[]}