@torpor/build 0.1.23 → 0.1.25
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/CHANGELOG.md +18 -0
- package/dist/bin/index.cjs +1 -1
- package/dist/bin/index.cjs.map +1 -1
- package/dist/bin/index.js +3 -3
- package/dist/{chunk-WMHM5RGZ.js → chunk-565UOT3F.js} +5 -2
- package/dist/chunk-565UOT3F.js.map +1 -0
- package/dist/{chunk-D6LHMH2C.js → chunk-B2UNTYN5.js} +8 -8
- package/dist/chunk-B2UNTYN5.js.map +1 -0
- package/dist/{chunk-LHRCEWBG.js → chunk-UCA7KIGG.js} +2 -2
- package/dist/{chunk-65K3R3CP.js → chunk-UN2WQH2T.js} +4 -2
- package/dist/{chunk-65K3R3CP.js.map → chunk-UN2WQH2T.js.map} +1 -1
- package/dist/{chunk-2QQN3D3K.js → chunk-WVXAN4IV.js} +2 -2
- package/dist/{chunk-2QQN3D3K.js.map → chunk-WVXAN4IV.js.map} +1 -1
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/response.cjs +7 -7
- package/dist/response.cjs.map +1 -1
- package/dist/response.js +2 -2
- package/dist/run.cjs +1 -1
- package/dist/run.cjs.map +1 -1
- package/dist/run.js +3 -3
- package/dist/state.cjs +4 -1
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +4 -0
- package/dist/state.d.ts +4 -0
- package/dist/state.js +1 -1
- package/dist/test.cjs +31 -14
- package/dist/test.cjs.map +1 -1
- package/dist/test.js +29 -15
- package/dist/test.js.map +1 -1
- package/package.json +4 -4
- package/src/response/badRequest.ts +1 -1
- package/src/response/forbidden.ts +1 -1
- package/src/response/methodNotAllowed.ts +1 -1
- package/src/response/notFound.ts +1 -1
- package/src/response/serverError.ts +1 -1
- package/src/response/unauthorized.ts +1 -1
- package/src/response/unprocessable.ts +1 -1
- package/src/site/Site.ts +3 -0
- package/src/site/clientEntry.ts +34 -17
- package/src/site/serverEntry.ts +68 -17
- package/src/state/$page.ts +4 -5
- package/src/state/Page.ts +8 -0
- package/src/test/runTest.ts +26 -12
- package/src/types/RouteType.ts +1 -0
- package/dist/chunk-D6LHMH2C.js.map +0 -1
- package/dist/chunk-WMHM5RGZ.js.map +0 -1
- /package/dist/{chunk-LHRCEWBG.js.map → chunk-UCA7KIGG.js.map} +0 -0
package/src/site/serverEntry.ts
CHANGED
|
@@ -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,14 +177,15 @@ 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
|
|
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) {
|
|
148
185
|
const layoutEndPoint: PageEndPoint | undefined = (await layout.endPoint())?.default;
|
|
149
186
|
const layoutServerEndPoint: PageServerEndPoint | undefined =
|
|
150
187
|
layout.serverEndPoint && (await layout.serverEndPoint())?.default;
|
|
151
|
-
const
|
|
188
|
+
const layoutResponse = await loadClientAndServerData(
|
|
152
189
|
url,
|
|
153
190
|
params,
|
|
154
191
|
serverParams,
|
|
@@ -156,10 +193,12 @@ async function loadView(
|
|
|
156
193
|
layoutEndPoint,
|
|
157
194
|
layoutServerEndPoint,
|
|
158
195
|
);
|
|
159
|
-
|
|
196
|
+
if (layoutResponse?.ok === false) {
|
|
197
|
+
return layoutResponse;
|
|
198
|
+
}
|
|
160
199
|
}
|
|
161
200
|
}
|
|
162
|
-
let
|
|
201
|
+
let endPointResponse = await loadClientAndServerData(
|
|
163
202
|
url,
|
|
164
203
|
params,
|
|
165
204
|
serverParams,
|
|
@@ -167,7 +206,9 @@ async function loadView(
|
|
|
167
206
|
clientEndPoint,
|
|
168
207
|
serverEndPoint,
|
|
169
208
|
);
|
|
170
|
-
|
|
209
|
+
if (endPointResponse?.ok === false) {
|
|
210
|
+
return endPointResponse;
|
|
211
|
+
}
|
|
171
212
|
let $props: Record<string, any> = { data };
|
|
172
213
|
|
|
173
214
|
// If there are layouts, work our way upwards, pushing each component into
|
|
@@ -258,21 +299,31 @@ async function loadClientAndServerData(
|
|
|
258
299
|
clientEndPoint?: PageEndPoint,
|
|
259
300
|
serverEndPoint?: PageServerEndPoint,
|
|
260
301
|
) {
|
|
261
|
-
let newData = {};
|
|
262
302
|
if (clientEndPoint?.load) {
|
|
263
303
|
const clientParams = buildClientParams(url, data, params);
|
|
264
|
-
const
|
|
265
|
-
if (
|
|
266
|
-
|
|
304
|
+
const clientResponse = await clientEndPoint.load(clientParams);
|
|
305
|
+
if (clientResponse) {
|
|
306
|
+
if (clientResponse.ok) {
|
|
307
|
+
if (clientResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
308
|
+
Object.assign(data, await clientResponse.json());
|
|
309
|
+
}
|
|
310
|
+
} else {
|
|
311
|
+
return clientResponse;
|
|
312
|
+
}
|
|
267
313
|
}
|
|
268
314
|
}
|
|
269
315
|
if (serverEndPoint?.load) {
|
|
270
|
-
const
|
|
271
|
-
if (
|
|
272
|
-
|
|
316
|
+
const serverResponse = await serverEndPoint.load(serverParams);
|
|
317
|
+
if (serverResponse) {
|
|
318
|
+
if (serverResponse.ok) {
|
|
319
|
+
if (serverResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
320
|
+
Object.assign(data, await serverResponse.json());
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
return serverResponse;
|
|
324
|
+
}
|
|
273
325
|
}
|
|
274
326
|
}
|
|
275
|
-
return newData;
|
|
276
327
|
}
|
|
277
328
|
|
|
278
329
|
function buildClientParams(url: URL, params: Record<string, string>, data: Record<string, any>) {
|
package/src/state/$page.ts
CHANGED
|
@@ -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
|
);
|
package/src/test/runTest.ts
CHANGED
|
@@ -175,7 +175,7 @@ async function loadView(
|
|
|
175
175
|
const layoutEndPoint: PageEndPoint | undefined = (await layout.endPoint())?.default;
|
|
176
176
|
const layoutServerEndPoint: PageServerEndPoint | undefined =
|
|
177
177
|
layout.serverEndPoint && (await layout.serverEndPoint())?.default;
|
|
178
|
-
const
|
|
178
|
+
const layoutResponse = await loadClientAndServerData(
|
|
179
179
|
url,
|
|
180
180
|
params,
|
|
181
181
|
serverParams,
|
|
@@ -183,10 +183,12 @@ async function loadView(
|
|
|
183
183
|
layoutEndPoint,
|
|
184
184
|
layoutServerEndPoint,
|
|
185
185
|
);
|
|
186
|
-
|
|
186
|
+
if (layoutResponse?.ok === false) {
|
|
187
|
+
return layoutResponse;
|
|
188
|
+
}
|
|
187
189
|
}
|
|
188
190
|
}
|
|
189
|
-
let
|
|
191
|
+
let endPointResponse = await loadClientAndServerData(
|
|
190
192
|
url,
|
|
191
193
|
params,
|
|
192
194
|
serverParams,
|
|
@@ -194,7 +196,9 @@ async function loadView(
|
|
|
194
196
|
clientEndPoint,
|
|
195
197
|
serverEndPoint,
|
|
196
198
|
);
|
|
197
|
-
|
|
199
|
+
if (endPointResponse?.ok === false) {
|
|
200
|
+
return endPointResponse;
|
|
201
|
+
}
|
|
198
202
|
let $props: Record<string, any> = { data };
|
|
199
203
|
|
|
200
204
|
// If there are layouts, work our way upwards, pushing each component into
|
|
@@ -285,21 +289,31 @@ async function loadClientAndServerData(
|
|
|
285
289
|
clientEndPoint?: PageEndPoint,
|
|
286
290
|
serverEndPoint?: PageServerEndPoint,
|
|
287
291
|
) {
|
|
288
|
-
let newData = {};
|
|
289
292
|
if (clientEndPoint?.load) {
|
|
290
293
|
const clientParams = buildClientParams(url, data, params);
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
+
const clientResponse = await clientEndPoint.load(clientParams);
|
|
295
|
+
if (clientResponse) {
|
|
296
|
+
if (clientResponse.ok) {
|
|
297
|
+
if (clientResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
298
|
+
Object.assign(data, await clientResponse.json());
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
return clientResponse;
|
|
302
|
+
}
|
|
294
303
|
}
|
|
295
304
|
}
|
|
296
305
|
if (serverEndPoint?.load) {
|
|
297
|
-
const
|
|
298
|
-
if (
|
|
299
|
-
|
|
306
|
+
const serverResponse = await serverEndPoint.load(serverParams);
|
|
307
|
+
if (serverResponse) {
|
|
308
|
+
if (serverResponse.ok) {
|
|
309
|
+
if (serverResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
310
|
+
Object.assign(data, await serverResponse.json());
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
return serverResponse;
|
|
314
|
+
}
|
|
300
315
|
}
|
|
301
316
|
}
|
|
302
|
-
return newData;
|
|
303
317
|
}
|
|
304
318
|
|
|
305
319
|
function buildClientParams(url: URL, params: Record<string, string>, data: Record<string, any>) {
|
package/src/types/RouteType.ts
CHANGED
|
@@ -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":[]}
|
|
File without changes
|