@torpor/build 1.0.0 → 1.0.2
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/dist/{flattenHeaders-C_YYLdOq.mjs → Server-BnjLJleZ.mjs} +2 -12
- package/dist/Server-BnjLJleZ.mjs.map +1 -0
- package/dist/{Server-C_IKav_e.d.mts → Server-Cler2w5O.d.mts} +3 -48
- package/dist/Server-Cler2w5O.d.mts.map +1 -0
- package/dist/ServerEvent-CTx3Q-hk.d.mts +48 -0
- package/dist/ServerEvent-CTx3Q-hk.d.mts.map +1 -0
- package/dist/{Site-CMa153FA.d.mts → Site-CE0_o5Ci.d.mts} +3 -2
- package/dist/Site-CE0_o5Ci.d.mts.map +1 -0
- package/dist/bin/index.js +1 -1
- package/dist/clientEntry.d.mts +1 -0
- package/dist/clientEntry.mjs +59 -0
- package/dist/clientEntry.mjs.map +1 -0
- package/dist/clientEntryDev.d.mts +1 -0
- package/dist/clientEntryDev.mjs +7 -0
- package/dist/clientEntryDev.mjs.map +1 -0
- package/dist/dev.d.mts +3 -0
- package/dist/dev.mjs +3 -0
- package/dist/endpoint-EULRGCKA.mjs +108 -0
- package/dist/endpoint-EULRGCKA.mjs.map +1 -0
- package/dist/flattenHeaders-DM5qsSqO.mjs +13 -0
- package/dist/flattenHeaders-DM5qsSqO.mjs.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/load-BntzvmwL.mjs +228 -0
- package/dist/load-BntzvmwL.mjs.map +1 -0
- package/dist/nav.mjs +1 -225
- package/dist/nav.mjs.map +1 -1
- package/dist/openapi.d.mts +1 -1
- package/dist/run.d.mts +1 -1
- package/dist/run.d.mts.map +1 -1
- package/dist/run.mjs +1 -1
- package/dist/{runOpenApi-BUi1RH1l.mjs → runOpenApi-C8eI2Fh0.mjs} +54 -16
- package/dist/runOpenApi-C8eI2Fh0.mjs.map +1 -0
- package/dist/server/Server.d.mts +2 -0
- package/dist/server/Server.mjs +2 -0
- package/dist/server.d.mts +2 -1
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +2 -1
- package/dist/server.mjs.map +1 -1
- package/dist/serverEntry-1qY3wZig.mjs +351 -0
- package/dist/serverEntry-1qY3wZig.mjs.map +1 -0
- package/dist/serverEntry-BAO8m03V.d.mts +6 -0
- package/dist/serverEntry-BAO8m03V.d.mts.map +1 -0
- package/dist/serverEntry.d.mts +2 -0
- package/dist/serverEntry.mjs +2 -0
- package/dist/test.d.mts +2 -2
- package/dist/test.mjs +2 -104
- package/dist/test.mjs.map +1 -1
- package/package.json +9 -8
- package/dist/Server-C_IKav_e.d.mts.map +0 -1
- package/dist/Site-CMa153FA.d.mts.map +0 -1
- package/dist/flattenHeaders-C_YYLdOq.mjs.map +0 -1
- package/dist/runOpenApi-BUi1RH1l.mjs.map +0 -1
- package/src/bin/index.ts +0 -22
- package/src/dev.ts +0 -10
- package/src/form/formDataToRecord.ts +0 -23
- package/src/form/readForm.ts +0 -71
- package/src/form.ts +0 -4
- package/src/index.ts +0 -82
- package/src/nav/api.test-d.ts +0 -79
- package/src/nav/api.ts +0 -104
- package/src/nav/formSubmit.ts +0 -70
- package/src/nav/load.ts +0 -9
- package/src/nav/loadData.ts +0 -134
- package/src/nav/navigate.ts +0 -189
- package/src/nav/reload.ts +0 -9
- package/src/nav/route.ts +0 -38
- package/src/nav.ts +0 -6
- package/src/openapi/docsHtml.ts +0 -25
- package/src/openapi/document.ts +0 -154
- package/src/openapi/plugin.ts +0 -78
- package/src/openapi/types.ts +0 -82
- package/src/openapi.ts +0 -14
- package/src/response/TypedResponse.ts +0 -17
- package/src/response/badRequest.ts +0 -31
- package/src/response/created.ts +0 -31
- package/src/response/forbidden.ts +0 -21
- package/src/response/found.ts +0 -30
- package/src/response/methodNotAllowed.ts +0 -18
- package/src/response/movedPermanently.ts +0 -25
- package/src/response/notFound.ts +0 -24
- package/src/response/notModified.ts +0 -34
- package/src/response/ok.ts +0 -38
- package/src/response/permanentRedirect.ts +0 -30
- package/src/response/response.ts +0 -22
- package/src/response/seeOther.ts +0 -19
- package/src/response/serverError.ts +0 -24
- package/src/response/temporaryRedirect.ts +0 -31
- package/src/response/transfer.ts +0 -14
- package/src/response/unauthorized.ts +0 -21
- package/src/response/unprocessable.ts +0 -30
- package/src/response.ts +0 -37
- package/src/run/depCache.ts +0 -175
- package/src/run/devPlugin.ts +0 -128
- package/src/run/prepareTemplate.ts +0 -42
- package/src/run/run.ts +0 -86
- package/src/run/runBuild.ts +0 -128
- package/src/run/runDev.ts +0 -177
- package/src/run/runOpenApi.ts +0 -52
- package/src/run/runPreview.ts +0 -84
- package/src/run/staleTorpCopies.ts +0 -100
- package/src/run.ts +0 -7
- package/src/schema.ts +0 -7
- package/src/server/CookieHelper.ts +0 -46
- package/src/server/HeaderHelper.ts +0 -25
- package/src/server/Server.ts +0 -168
- package/src/server/ServerEvent.ts +0 -62
- package/src/server/connect/connectMiddleware.ts +0 -85
- package/src/server/connect/flattenHeaders.ts +0 -22
- package/src/server/connect/nodeMessageToNodeResponse.ts +0 -89
- package/src/server/connect/requestToNodeMessage.ts +0 -29
- package/src/server/contentType.ts +0 -85
- package/src/server/types/HttpMethod.ts +0 -12
- package/src/server/types/MiddlewareFunction.ts +0 -22
- package/src/server/types/ServerFunction.ts +0 -7
- package/src/server.ts +0 -7
- package/src/site/Router.ts +0 -175
- package/src/site/Site.ts +0 -388
- package/src/site/checkLayoutSlots.ts +0 -114
- package/src/site/checkRoutes.ts +0 -435
- package/src/site/clientEntry.ts +0 -121
- package/src/site/clientEntryDev.ts +0 -3
- package/src/site/defaultAdapter.ts +0 -11
- package/src/site/layoutSlots.ts +0 -53
- package/src/site/manifest.ts +0 -173
- package/src/site/serverEntry.ts +0 -648
- package/src/state/$page.ts +0 -25
- package/src/state/$serverPage.ts +0 -22
- package/src/state/client.ts +0 -15
- package/src/state.ts +0 -3
- package/src/test/runTest.ts +0 -538
- package/src/test.ts +0 -3
- package/src/types/Adapter.ts +0 -20
- package/src/types/ClientState.ts +0 -8
- package/src/types/InternalState.ts +0 -7
- package/src/types/Jsonify.ts +0 -20
- package/src/types/LayoutHandler.ts +0 -5
- package/src/types/LayoutPath.ts +0 -10
- package/src/types/ManifestRoute.ts +0 -6
- package/src/types/PageData.test-d.ts +0 -136
- package/src/types/PageData.ts +0 -35
- package/src/types/PageEndPoint.ts +0 -55
- package/src/types/PageForm.test-d.ts +0 -78
- package/src/types/PageForm.ts +0 -29
- package/src/types/PageLoadEvent.ts +0 -26
- package/src/types/PageLoadReturn.ts +0 -13
- package/src/types/PageProps.ts +0 -14
- package/src/types/PageServerAction.ts +0 -13
- package/src/types/PageServerEndPoint.test-d.ts +0 -105
- package/src/types/PageServerEndPoint.ts +0 -110
- package/src/types/PageServerLoad.ts +0 -15
- package/src/types/PageState.ts +0 -8
- package/src/types/ParseRouteParams.test-d.ts +0 -84
- package/src/types/ParseRouteParams.ts +0 -64
- package/src/types/Route.ts +0 -30
- package/src/types/RouteHandler.ts +0 -23
- package/src/types/RouteLayoutHandler.ts +0 -5
- package/src/types/RouteMatchResult.ts +0 -7
- package/src/types/RouteType.ts +0 -19
- package/src/types/ServerEndPoint.test-d.ts +0 -77
- package/src/types/ServerEndPoint.ts +0 -169
- package/src/types/ServerHook.ts +0 -18
- package/src/types/ServerLoadEvent.ts +0 -82
- package/src/types/ServerRequest.ts +0 -15
- package/src/types/SitePlugin.ts +0 -24
- package/src/types/StandardSchema.ts +0 -109
- package/src/utils/pathToRegex.ts +0 -17
- package/src/utils/pathTrie.ts +0 -182
- package/src/utils/searchParamsToRecord.ts +0 -18
- package/src/utils/torporPackages.ts +0 -150
- package/src/utils/tsconfigAliases.ts +0 -90
- package/src/validation/ValidationError.ts +0 -18
- package/src/validation/endpoint.ts +0 -83
- package/src/validation/validate.ts +0 -26
- package/src/vite-env.d.ts +0 -2
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { t as $page } from "./_page-4cQKs_Dn.mjs";
|
|
2
|
+
import { clearLayoutSlot, fillLayoutSlot, hydrate, mount, unmount } from "@torpor/view";
|
|
3
|
+
//#region src/state/client.ts
|
|
4
|
+
const CLIENT_SYMBOL = Symbol.for("torp.Client");
|
|
5
|
+
const client = globalThis[CLIENT_SYMBOL] ??= {
|
|
6
|
+
router: null,
|
|
7
|
+
layoutStack: [],
|
|
8
|
+
prefetchedData: {}
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/nav/reload.ts
|
|
12
|
+
/**
|
|
13
|
+
* Reloads data for the currently loaded page and rerenders it.
|
|
14
|
+
*/
|
|
15
|
+
async function reload() {
|
|
16
|
+
return await navigate(new URL(document.location.href));
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/nav/formSubmit.ts
|
|
20
|
+
async function formSubmit(e) {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
$page.form = void 0;
|
|
23
|
+
const form = e.target;
|
|
24
|
+
const button = e.submitter;
|
|
25
|
+
let action = form.action || document.URL;
|
|
26
|
+
if (button && button.formAction && button.formAction !== document.URL) action = button.formAction;
|
|
27
|
+
const options = {
|
|
28
|
+
body: new FormData(form),
|
|
29
|
+
method: "POST",
|
|
30
|
+
mode: "cors",
|
|
31
|
+
credentials: "same-origin",
|
|
32
|
+
headers: { "X-Torpor-Form-Submit": "" }
|
|
33
|
+
};
|
|
34
|
+
const response = await fetch(action, options);
|
|
35
|
+
if (response.status === 200 && response.headers.has("Content-Disposition") && /attachment;\s*filename="(.+?)"/.test(response.headers.get("Content-Disposition"))) {
|
|
36
|
+
const downloadFileName = response.headers.get("Content-Disposition")?.match(/attachment;\s*filename="(.+?)"/)[1] ?? "file";
|
|
37
|
+
const downloadLink = document.createElement("a");
|
|
38
|
+
downloadLink.href = window.URL.createObjectURL(await response.blob());
|
|
39
|
+
downloadLink.download = downloadFileName;
|
|
40
|
+
downloadLink.click();
|
|
41
|
+
} else if (response.status === 200 && response.headers.has("X-Torpor-Form-Redirect")) {
|
|
42
|
+
const redirect = response.headers.get("Location");
|
|
43
|
+
await load(redirect);
|
|
44
|
+
window.location.href = redirect;
|
|
45
|
+
} else if (response.status >= 200 && response.status <= 299 || response.status >= 400 && response.status <= 499) {
|
|
46
|
+
if (response.headers.get("Content-Type") === "application/json") $page.form = await response.json();
|
|
47
|
+
await reload();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/nav/loadData.ts
|
|
52
|
+
async function loadData(handler, params, path, query, newLayoutStack, clientEndPoint, serverEndPoint, prefetch = false) {
|
|
53
|
+
let data = {};
|
|
54
|
+
if (handler.layouts) {
|
|
55
|
+
let layoutStack = client.layoutStack;
|
|
56
|
+
for (let [i, layout] of handler.layouts.entries()) {
|
|
57
|
+
let layoutPath = layout.path;
|
|
58
|
+
for (let key in params) layoutPath = layoutPath.replace(`[${key}]`, params[key]);
|
|
59
|
+
if (layoutStack.at(i)?.path === layoutPath) {
|
|
60
|
+
if (!prefetch) layoutStack[i].reuse = true;
|
|
61
|
+
newLayoutStack[i] = layoutStack[i];
|
|
62
|
+
Object.assign(data, layoutStack[i].data);
|
|
63
|
+
} else {
|
|
64
|
+
const stackLayout = {
|
|
65
|
+
path: layoutPath,
|
|
66
|
+
data: {},
|
|
67
|
+
reuse: false,
|
|
68
|
+
slotRegion: null
|
|
69
|
+
};
|
|
70
|
+
const layoutEndPoint = (await layout.endPoint())?.default;
|
|
71
|
+
const layoutServerEndPoint = layout.serverEndPoint && (await layout.serverEndPoint())?.default;
|
|
72
|
+
if ((await loadClientAndServerData(stackLayout.data, document.location.origin + layoutPath, query, params, layoutEndPoint, layoutServerEndPoint))?.ok === false) return;
|
|
73
|
+
Object.assign(data, stackLayout.data);
|
|
74
|
+
newLayoutStack.push(stackLayout);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if ((await loadClientAndServerData(data, document.location.origin + path, query, params, clientEndPoint, serverEndPoint))?.ok === false) return;
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
async function loadClientAndServerData(data, location, query, params, clientEndPoint, serverEndPoint) {
|
|
82
|
+
let prefetchedData = client.prefetchedData;
|
|
83
|
+
if (clientEndPoint?.load) {
|
|
84
|
+
const clientKey = location + (query.size > 0 ? `?${query}` : "");
|
|
85
|
+
if (prefetchedData[clientKey]) Object.assign(data, prefetchedData[clientKey]);
|
|
86
|
+
else {
|
|
87
|
+
const clientUrl = new URL(document.location.href);
|
|
88
|
+
for (let [name, value] of query) clientUrl.searchParams.append(name, value);
|
|
89
|
+
const clientParams = buildClientParams(clientUrl, params, data);
|
|
90
|
+
const clientResponse = await clientEndPoint.load(clientParams);
|
|
91
|
+
if (clientResponse) if (clientResponse.ok) {
|
|
92
|
+
if (clientResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
93
|
+
const clientData = await clientResponse.json();
|
|
94
|
+
Object.assign(data, clientData);
|
|
95
|
+
prefetchedData[clientKey] = clientData;
|
|
96
|
+
}
|
|
97
|
+
} else return clientResponse;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (serverEndPoint?.load) {
|
|
101
|
+
const serverLocation = location.replace(/\/$/, "") + "/~server";
|
|
102
|
+
const serverKey = serverLocation + (query.size > 0 ? `?${query}` : "");
|
|
103
|
+
if (prefetchedData[serverKey]) Object.assign(data, prefetchedData[serverKey]);
|
|
104
|
+
else {
|
|
105
|
+
const serverUrl = new URL(serverLocation);
|
|
106
|
+
for (let [name, value] of query) serverUrl.searchParams.append(name, value);
|
|
107
|
+
const serverResponse = await fetch(serverUrl);
|
|
108
|
+
if (serverResponse) if (serverResponse.ok) {
|
|
109
|
+
if (serverResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
110
|
+
const serverData = await serverResponse.json();
|
|
111
|
+
Object.assign(data, serverData);
|
|
112
|
+
prefetchedData[serverKey] = serverData;
|
|
113
|
+
}
|
|
114
|
+
} else return serverResponse;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function buildClientParams(url, params, data) {
|
|
119
|
+
return {
|
|
120
|
+
url,
|
|
121
|
+
params,
|
|
122
|
+
data
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/nav/navigate.ts
|
|
127
|
+
async function navigate(url, withHydration = false) {
|
|
128
|
+
let parent = document.getElementById("app");
|
|
129
|
+
if (!parent) {
|
|
130
|
+
console.log("500");
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const path = url.pathname;
|
|
134
|
+
const query = url.searchParams;
|
|
135
|
+
const route = client.router.match(path, query);
|
|
136
|
+
if (!route) {
|
|
137
|
+
console.log("404");
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
$page.url = url;
|
|
141
|
+
if (path.endsWith("/_error")) {
|
|
142
|
+
$page.status = parseInt(query.get("status") ?? "404");
|
|
143
|
+
$page.error = { message: query.get("message") ?? "" };
|
|
144
|
+
window.history.replaceState({}, "", url.toString().split("?")[0]);
|
|
145
|
+
} else $page.status = 200;
|
|
146
|
+
const handler = route.handler;
|
|
147
|
+
const params = route.params || {};
|
|
148
|
+
const clientEndPoint = (await handler.endPoint()).default;
|
|
149
|
+
if (!clientEndPoint?.component) {
|
|
150
|
+
console.log("404");
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
const serverEndPoint = handler.serverEndPoint && (await handler.serverEndPoint())?.default;
|
|
154
|
+
let newLayoutStack = [];
|
|
155
|
+
const data = await loadData(handler, params, path, query, newLayoutStack, clientEndPoint, serverEndPoint);
|
|
156
|
+
let formInput = document.getElementById("t-form-data");
|
|
157
|
+
let form;
|
|
158
|
+
if (formInput) {
|
|
159
|
+
form = JSON.parse(formInput.value);
|
|
160
|
+
$page.form = form;
|
|
161
|
+
formInput.remove();
|
|
162
|
+
}
|
|
163
|
+
let $props = { data };
|
|
164
|
+
$props.form = $page.form;
|
|
165
|
+
const $context = { TorporBuildContext: { onformsubmit: formSubmit } };
|
|
166
|
+
client.layoutStack.push({
|
|
167
|
+
path: route.handler.path,
|
|
168
|
+
data: {},
|
|
169
|
+
reuse: false,
|
|
170
|
+
slotRegion: null
|
|
171
|
+
});
|
|
172
|
+
client.layoutStack = newLayoutStack;
|
|
173
|
+
let layoutStack = client.layoutStack;
|
|
174
|
+
let component = clientEndPoint.component;
|
|
175
|
+
let slots = void 0;
|
|
176
|
+
let reused = false;
|
|
177
|
+
let reusedIndex = -1;
|
|
178
|
+
if (handler.layouts) {
|
|
179
|
+
let slotFunctions = [];
|
|
180
|
+
slotFunctions[handler.layouts.length] = function clientComponent(parent, anchor) {
|
|
181
|
+
let i = layoutStack.length - 1;
|
|
182
|
+
layoutStack[i].slotRegion = fillLayoutSlot(clientEndPoint.component, slotFunctions[i + 1], parent, anchor, $props, $context);
|
|
183
|
+
};
|
|
184
|
+
for (let i = handler.layouts.length - 1; i >= 0; i--) {
|
|
185
|
+
const layoutEndPoint = (await handler.layouts[i].endPoint())?.default;
|
|
186
|
+
if (layoutEndPoint?.component) if (layoutStack[i].reuse) {
|
|
187
|
+
component = slotFunctions[i + 1];
|
|
188
|
+
reusedIndex = i;
|
|
189
|
+
reused = true;
|
|
190
|
+
break;
|
|
191
|
+
} else if (i === 0) {
|
|
192
|
+
component = layoutEndPoint.component;
|
|
193
|
+
slots = { _: slotFunctions[i + 1] };
|
|
194
|
+
} else slotFunctions[i] = function layoutComponent(parent, anchor, _, $context) {
|
|
195
|
+
layoutStack[i - 1].slotRegion = fillLayoutSlot(layoutEndPoint.component, slotFunctions[i + 1], parent, anchor, $props, $context);
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
if (reused) {
|
|
201
|
+
const slotRegion = layoutStack[reusedIndex].slotRegion;
|
|
202
|
+
parent = slotRegion.startNode.parentNode;
|
|
203
|
+
clearLayoutSlot(slotRegion);
|
|
204
|
+
component(parent, null);
|
|
205
|
+
} else if (withHydration) hydrate(parent, component, $props, slots);
|
|
206
|
+
else {
|
|
207
|
+
unmount(parent);
|
|
208
|
+
mount(parent, component, $props, slots);
|
|
209
|
+
}
|
|
210
|
+
} catch (error) {
|
|
211
|
+
parent.innerHTML = "<span style=\"color: red\">Script syntax error</span><p>" + error + "</p>";
|
|
212
|
+
console.log(error);
|
|
213
|
+
}
|
|
214
|
+
client.prefetchedData = {};
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/nav/load.ts
|
|
219
|
+
/**
|
|
220
|
+
* Loads and renders the page at the supplied path.
|
|
221
|
+
*/
|
|
222
|
+
async function load(path) {
|
|
223
|
+
return await navigate(new URL(path, document.location.href));
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
export { client as a, reload as i, navigate as n, loadData as r, load as t };
|
|
227
|
+
|
|
228
|
+
//# sourceMappingURL=load-BntzvmwL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load-BntzvmwL.mjs","names":[],"sources":["../src/state/client.ts","../src/nav/reload.ts","../src/nav/formSubmit.ts","../src/nav/loadData.ts","../src/nav/navigate.ts","../src/nav/load.ts"],"sourcesContent":["import type ClientState from \"../types/ClientState\";\n\nconst CLIENT_SYMBOL: unique symbol = Symbol.for(\"torp.Client\");\n\nconst client: ClientState =\n\t// @ts-ignore\n\t(globalThis[CLIENT_SYMBOL] ??= {\n\t\t// @ts-ignore This will definitely, 100% get set\n\t\trouter: null,\n\t\tlayoutStack: [],\n\t\t// TODO: Probably expire after 30 seconds or something?\n\t\tprefetchedData: {},\n\t});\n\nexport default client;\n","import navigate from \"./navigate\";\n\n/**\n * Reloads data for the currently loaded page and rerenders it.\n */\nexport default async function reload(): Promise<boolean> {\n\tconst url = new URL(document.location.href);\n\treturn await navigate(url);\n}\n","import $page from \"../state/$page\";\nimport load from \"./load\";\nimport reload from \"./reload\";\n\nexport default async function formSubmit(e: SubmitEvent): Promise<void> {\n\t// We'll take it from here\n\te.preventDefault();\n\n\t// Reset $page on submit and link navigation\n\t// TODO: Should we put data in $page.data??\n\t$page.form = undefined;\n\n\t// Perform a POST fetch to the form's action location\n\tconst form = e.target as HTMLFormElement;\n\tconst button = e.submitter as HTMLButtonElement;\n\t// HACK: submit buttons have their formAction set to the current page if not\n\t// set, so we can't just check whether it's empty and fallback to form action\n\t//const action = button?.formAction ?? form.action;\n\tlet action = form.action || document.URL;\n\tif (button && button.formAction && button.formAction !== document.URL) {\n\t\taction = button.formAction;\n\t}\n\tconst options = {\n\t\tbody: new FormData(form),\n\t\tmethod: \"POST\",\n\t\tmode: \"cors\",\n\t\tcredentials: \"same-origin\",\n\t\t// Add a special header which tells the server that we want the raw\n\t\t// error response (if applicable) rather than the reloaded page that you\n\t\t// would get if submitting a page without javascript\n\t\theaders: {\n\t\t\t\"X-Torpor-Form-Submit\": \"\",\n\t\t},\n\t};\n\tconst response = await fetch(\n\t\taction,\n\t\t// @ts-ignore -- don't know what it's complaining about here\n\t\toptions,\n\t);\n\n\tif (\n\t\tresponse.status === 200 &&\n\t\tresponse.headers.has(\"Content-Disposition\") &&\n\t\t/attachment;\\s*filename=\"(.+?)\"/.test(response.headers.get(\"Content-Disposition\")!)\n\t) {\n\t\t// TODO: Is this enough to check to see if we have a file that should download?\n\t\t// Download the file by creating a dummy <a> and clicking it\n\t\tconst downloadFileName =\n\t\t\tresponse.headers.get(\"Content-Disposition\")?.match(/attachment;\\s*filename=\"(.+?)\"/)![1] ??\n\t\t\t\"file\";\n\t\tconst downloadLink = document.createElement(\"a\");\n\t\tdownloadLink.href = window.URL.createObjectURL(await response.blob());\n\t\tdownloadLink.download = downloadFileName;\n\t\tdownloadLink.click();\n\t} else if (response.status === 200 && response.headers.has(\"X-Torpor-Form-Redirect\")) {\n\t\t// TODO: Should do the correct type of redirect e.g. seeOther etc\n\t\tconst redirect = response.headers.get(\"Location\")!;\n\t\tawait load(redirect);\n\t\twindow.location.href = redirect;\n\t} else if (\n\t\t(response.status >= 200 && response.status <= 299) ||\n\t\t(response.status >= 400 && response.status <= 499)\n\t) {\n\t\t// Put the response into `$page.form`, and it will be put into `$props` in reload\n\t\tif (response.headers.get(\"Content-Type\") === \"application/json\") {\n\t\t\t$page.form = await response.json();\n\t\t}\n\t\tawait reload();\n\t}\n}\n","import client from \"../state/client\";\nimport type LayoutPath from \"../types/LayoutPath\";\nimport type PageEndPoint from \"../types/PageEndPoint\";\nimport type PageServerEndPoint from \"../types/PageServerEndPoint\";\nimport type RouteHandler from \"../types/RouteHandler\";\n\nexport default async function loadData(\n\thandler: RouteHandler,\n\tparams: Record<string, string>,\n\tpath: string,\n\tquery: URLSearchParams,\n\tnewLayoutStack: LayoutPath[],\n\tclientEndPoint: PageEndPoint | undefined,\n\tserverEndPoint: PageServerEndPoint | undefined,\n\tprefetch = false,\n): Promise<Record<string, string> | void> {\n\tlet data = {};\n\tif (handler.layouts) {\n\t\tlet layoutStack = client.layoutStack;\n\t\tfor (let [i, layout] of handler.layouts.entries()) {\n\t\t\tlet layoutPath = layout.path;\n\t\t\tfor (let key in params) {\n\t\t\t\tlayoutPath = layoutPath.replace(`[${key}]`, params[key]);\n\t\t\t}\n\t\t\tif (layoutStack.at(i)?.path === layoutPath) {\n\t\t\t\t// We've already loaded this layout, we can just re-use its data and UI\n\t\t\t\tif (!prefetch) {\n\t\t\t\t\tlayoutStack[i].reuse = true;\n\t\t\t\t}\n\t\t\t\tnewLayoutStack[i] = layoutStack[i];\n\t\t\t\tObject.assign(data, layoutStack[i].data);\n\t\t\t} else {\n\t\t\t\tconst stackLayout = { path: layoutPath, data: {}, reuse: false, slotRegion: null };\n\t\t\t\tconst layoutEndPoint: PageEndPoint | undefined = (await layout.endPoint())?.default;\n\t\t\t\tconst layoutServerEndPoint: PageServerEndPoint | undefined =\n\t\t\t\t\tlayout.serverEndPoint && (await layout.serverEndPoint())?.default;\n\t\t\t\tconst layoutResponse = await loadClientAndServerData(\n\t\t\t\t\tstackLayout.data,\n\t\t\t\t\tdocument.location.origin + layoutPath,\n\t\t\t\t\tquery,\n\t\t\t\t\tparams,\n\t\t\t\t\tlayoutEndPoint,\n\t\t\t\t\tlayoutServerEndPoint,\n\t\t\t\t);\n\t\t\t\tif (layoutResponse?.ok === false) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tObject.assign(data, stackLayout.data);\n\t\t\t\tnewLayoutStack.push(stackLayout);\n\t\t\t}\n\t\t}\n\t}\n\tlet endPointResponse = await loadClientAndServerData(\n\t\tdata,\n\t\tdocument.location.origin + path,\n\t\tquery,\n\t\tparams,\n\t\tclientEndPoint,\n\t\tserverEndPoint,\n\t);\n\tif (endPointResponse?.ok === false) {\n\t\treturn;\n\t}\n\treturn data;\n}\n\nasync function loadClientAndServerData(\n\tdata: Record<string, any>,\n\tlocation: string,\n\tquery: URLSearchParams,\n\tparams: Record<string, string>,\n\tclientEndPoint?: PageEndPoint,\n\tserverEndPoint?: PageServerEndPoint,\n): Promise<Response | undefined | void> {\n\tlet prefetchedData = client.prefetchedData;\n\n\tif (clientEndPoint?.load) {\n\t\tconst clientKey = location + (query.size > 0 ? `?${query}` : \"\");\n\t\tif (prefetchedData[clientKey]) {\n\t\t\tObject.assign(data, prefetchedData[clientKey]);\n\t\t} else {\n\t\t\tconst clientUrl = new URL(document.location.href);\n\t\t\tfor (let [name, value] of query) {\n\t\t\t\tclientUrl.searchParams.append(name, value);\n\t\t\t}\n\t\t\tconst clientParams = buildClientParams(clientUrl, params, data);\n\t\t\tconst clientResponse = await clientEndPoint.load(clientParams);\n\t\t\tif (clientResponse) {\n\t\t\t\tif (clientResponse.ok) {\n\t\t\t\t\tif (clientResponse.headers.get(\"Content-Type\")?.includes(\"application/json\")) {\n\t\t\t\t\t\tconst clientData = await clientResponse.json();\n\t\t\t\t\t\tObject.assign(data, clientData);\n\t\t\t\t\t\tprefetchedData[clientKey] = clientData;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn clientResponse;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif (serverEndPoint?.load) {\n\t\tconst serverLocation = location.replace(/\\/$/, \"\") + \"/~server\";\n\t\tconst serverKey = serverLocation + (query.size > 0 ? `?${query}` : \"\");\n\t\tif (prefetchedData[serverKey]) {\n\t\t\tObject.assign(data, prefetchedData[serverKey]);\n\t\t} else {\n\t\t\tconst serverUrl = new URL(serverLocation);\n\t\t\tfor (let [name, value] of query) {\n\t\t\t\tserverUrl.searchParams.append(name, value);\n\t\t\t}\n\t\t\tconst serverResponse = await fetch(serverUrl);\n\t\t\tif (serverResponse) {\n\t\t\t\tif (serverResponse.ok) {\n\t\t\t\t\tif (serverResponse.headers.get(\"Content-Type\")?.includes(\"application/json\")) {\n\t\t\t\t\t\tconst serverData = await serverResponse.json();\n\t\t\t\t\t\tObject.assign(data, serverData);\n\t\t\t\t\t\tprefetchedData[serverKey] = serverData;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn serverResponse;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction buildClientParams(url: URL, params: Record<string, string>, data: Record<string, any>) {\n\treturn {\n\t\turl,\n\t\tparams,\n\t\tdata,\n\t};\n}\n","import { clearLayoutSlot, fillLayoutSlot, hydrate } from \"@torpor/view\";\nimport { type Component, type SlotRender } from \"@torpor/view\";\nimport { mount, unmount } from \"@torpor/view\";\nimport $page from \"../state/$page\";\nimport client from \"../state/client\";\nimport type LayoutPath from \"../types/LayoutPath\";\nimport type PageEndPoint from \"../types/PageEndPoint\";\nimport type PageServerEndPoint from \"../types/PageServerEndPoint\";\nimport formSubmit from \"./formSubmit\";\nimport loadData from \"./loadData\";\n\n// @ts-ignore\nexport default async function navigate(url: URL, withHydration = false): Promise<boolean> {\n\tlet parent = document.getElementById(\"app\");\n\tif (!parent) {\n\t\t// TODO: 500\n\t\tconsole.log(\"500\");\n\t\treturn false;\n\t}\n\n\tconst path = url.pathname;\n\tconst query = url.searchParams;\n\n\t//console.log(`navigating to '${path}'${query.size ? ` with ${query}` : \"\"}`);\n\n\tconst route = client.router.match(path, query);\n\tif (!route) {\n\t\t// TODO: 404\n\t\tconsole.log(\"404\");\n\t\treturn false;\n\t}\n\n\t// Update $page before building the components\n\t$page.url = url;\n\tif (path.endsWith(\"/_error\")) {\n\t\t$page.status = parseInt(query.get(\"status\") ?? \"404\");\n\t\t$page.error = { message: query.get(\"message\") ?? \"\" };\n\t\t// Make it look a bit classier by removing the query\n\t\twindow.history.replaceState({}, \"\", url.toString().split(\"?\")[0]);\n\t} else {\n\t\t$page.status = 200;\n\t}\n\tconst handler = route.handler;\n\tconst params = route.params || {};\n\n\t// There must be a client endpoint with a component\n\tconst clientEndPoint: PageEndPoint | undefined = (await handler.endPoint()).default;\n\tif (!clientEndPoint?.component) {\n\t\t// TODO: 404\n\t\tconsole.log(\"404\");\n\t\treturn false;\n\t}\n\n\t// There may be a server endpoint\n\tconst serverEndPoint: PageServerEndPoint | undefined =\n\t\thandler.serverEndPoint && (await handler.serverEndPoint())?.default;\n\n\tlet newLayoutStack: LayoutPath[] = [];\n\n\t// Pass the data into $props\n\t// TODO: Don't load if this is the first time -- it should have been passed\n\t// to us, somehow...\n\tconst data = await loadData(\n\t\thandler,\n\t\tparams,\n\t\tpath,\n\t\tquery,\n\t\tnewLayoutStack,\n\t\tclientEndPoint,\n\t\tserverEndPoint,\n\t);\n\t// We may have form data in a hidden input -- not sure if this is the best\n\t// way to do it\n\tlet formInput = document.getElementById(\"t-form-data\") as HTMLInputElement;\n\tlet form: Record<string, string> | undefined;\n\tif (formInput) {\n\t\tform = JSON.parse(formInput.value);\n\t\t$page.form = form;\n\t\tformInput.remove();\n\t}\n\tlet $props: Record<string, any> = { data };\n\n\t// We may have form data in $page.form, either from the hidden input\n\t// (above), or added via onformsubmit (below)\n\t$props.form = $page.form;\n\n\t// Add some special context for submitting Forms client-side\n\tconst $context = {\n\t\tTorporBuildContext: { onformsubmit: formSubmit },\n\t};\n\n\tclient.layoutStack.push({ path: route.handler.path, data: {}, reuse: false, slotRegion: null });\n\tclient.layoutStack = newLayoutStack;\n\tlet layoutStack = client.layoutStack;\n\n\t// If there are layouts, work our way upwards, pushing each component into\n\t// the default slot of its parent\n\t// TODO: There's probably a nicer way to do this with reducers or something\n\tlet component = clientEndPoint.component as Component;\n\tlet slots: Record<string, SlotRender> | undefined = undefined;\n\tlet reused = false;\n\t// The index of the innermost reused layout. Its slotRegion is the region\n\t// that must be cleared and refilled (it contains the next layout's or the\n\t// page's content). This is usually the last entry of the stack, but not\n\t// when an outer layout is reused while an inner one is new — e.g.\n\t// navigating between sections that share the root layout.\n\tlet reusedIndex = -1;\n\tif (handler.layouts) {\n\t\tlet slotFunctions: SlotRender[] = [];\n\t\t// The last slot function will render the client component\n\t\tslotFunctions[handler.layouts.length] = function clientComponent(parent, anchor) {\n\t\t\tlet i = layoutStack.length - 1;\n\t\t\tlayoutStack[i].slotRegion = fillLayoutSlot(\n\t\t\t\tclientEndPoint.component!,\n\t\t\t\tslotFunctions[i + 1],\n\t\t\t\tparent,\n\t\t\t\tanchor,\n\t\t\t\t$props,\n\t\t\t\t$context,\n\t\t\t);\n\t\t};\n\n\t\t// Each earlier slot function will render a layout\n\t\tfor (let i = handler.layouts.length - 1; i >= 0; i--) {\n\t\t\tconst layoutEndPoint: PageEndPoint | undefined = (await handler.layouts[i].endPoint())\n\t\t\t\t?.default;\n\t\t\tif (layoutEndPoint?.component) {\n\t\t\t\tif (layoutStack[i].reuse) {\n\t\t\t\t\t// Reuse this layout — clear and refill its slot (done in\n\t\t\t\t\t// the try block below so a failure doesn't leave the slot\n\t\t\t\t\t// half-cleared)\n\t\t\t\t\tcomponent = slotFunctions[i + 1] as Component;\n\t\t\t\t\treusedIndex = i;\n\t\t\t\t\treused = true;\n\t\t\t\t\tbreak;\n\t\t\t\t} else if (i === 0) {\n\t\t\t\t\tcomponent = layoutEndPoint.component as Component;\n\t\t\t\t\tslots = { _: slotFunctions[i + 1] };\n\t\t\t\t} else {\n\t\t\t\t\tslotFunctions[i] = function layoutComponent(parent, anchor, _, $context) {\n\t\t\t\t\t\tlayoutStack[i - 1].slotRegion = fillLayoutSlot(\n\t\t\t\t\t\t\tlayoutEndPoint.component!,\n\t\t\t\t\t\t\tslotFunctions[i + 1],\n\t\t\t\t\t\t\tparent,\n\t\t\t\t\t\t\tanchor,\n\t\t\t\t\t\t\t$props,\n\t\t\t\t\t\t\t$context,\n\t\t\t\t\t\t);\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\ttry {\n\t\tif (reused) {\n\t\t\t// The layout is being reused — clear the old slot content, then\n\t\t\t// call the slot function directly to fill it with the new page.\n\t\t\t// We must not go through `mount` here: the slot's container still\n\t\t\t// holds the layout's own children (e.g. a header), which `mount`\n\t\t\t// refuses to mount into. Both the clear and the fill are inside\n\t\t\t// the try so that a failure doesn't leave the slot half-cleared.\n\t\t\tconst slotRegion = layoutStack[reusedIndex].slotRegion;\n\t\t\tparent = slotRegion.startNode!.parentNode as HTMLElement;\n\t\t\tclearLayoutSlot(slotRegion);\n\t\t\tcomponent(parent, null);\n\t\t} else if (withHydration) {\n\t\t\thydrate(parent, component, $props, slots);\n\t\t} else {\n\t\t\t// The layout chain changed (or there was no previous layout to\n\t\t\t// reuse): tear down the previous UI entirely — disposing its\n\t\t\t// region tree and clearing `#app` — so `mount` starts fresh.\n\t\t\t// Without this, `mount` throws because `#app` still holds the\n\t\t\t// previous render's children, and it would reuse a stale root\n\t\t\t// region.\n\t\t\tunmount(parent);\n\t\t\tmount(parent, component, $props, slots);\n\t\t}\n\t} catch (error) {\n\t\t// TODO: Show a proper Error component\n\t\tparent.innerHTML = '<span style=\"color: red\">Script syntax error</span><p>' + error + \"</p>\";\n\t\tconsole.log(error);\n\t}\n\n\t// Reset prefetched data on each navigation\n\tclient.prefetchedData = {};\n\n\treturn true;\n}\n","import navigate from \"./navigate\";\n\n/**\n * Loads and renders the page at the supplied path.\n */\nexport default async function load(path: string): Promise<bool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DAA2D,QAAQ;EACtF,QAAQ,IAAI,KAAK;CAClB;CAGA,OAAO,iBAAiB,CAAC;CAEzB,OAAO;AACR;;;;;;ACvLA,eAA8B,KAAK,MAAgC;CAElE,OAAO,MAAM,SAAS,IADN,IAAI,MAAM,SAAS,SAAS,IACpB,CAAC;AAC1B"}
|
package/dist/nav.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import { clearLayoutSlot, fillLayoutSlot, hydrate, mount, unmount } from "@torpor/view";
|
|
1
|
+
import { i as reload, t as load } from "./load-BntzvmwL.mjs";
|
|
3
2
|
//#region src/nav/route.ts
|
|
4
3
|
/**
|
|
5
4
|
* Builds a route path in a type-safe manner, filling in dynamic segments, e.g.
|
|
@@ -69,229 +68,6 @@ async function request(method, url, body, init) {
|
|
|
69
68
|
return (response.headers.get("Content-Type") ?? "").includes("application/json") ? await response.json() : response;
|
|
70
69
|
}
|
|
71
70
|
//#endregion
|
|
72
|
-
//#region src/state/client.ts
|
|
73
|
-
const CLIENT_SYMBOL = Symbol.for("torp.Client");
|
|
74
|
-
const client = globalThis[CLIENT_SYMBOL] ??= {
|
|
75
|
-
router: null,
|
|
76
|
-
layoutStack: [],
|
|
77
|
-
prefetchedData: {}
|
|
78
|
-
};
|
|
79
|
-
//#endregion
|
|
80
|
-
//#region src/nav/reload.ts
|
|
81
|
-
/**
|
|
82
|
-
* Reloads data for the currently loaded page and rerenders it.
|
|
83
|
-
*/
|
|
84
|
-
async function reload() {
|
|
85
|
-
return await navigate(new URL(document.location.href));
|
|
86
|
-
}
|
|
87
|
-
//#endregion
|
|
88
|
-
//#region src/nav/formSubmit.ts
|
|
89
|
-
async function formSubmit(e) {
|
|
90
|
-
e.preventDefault();
|
|
91
|
-
$page.form = void 0;
|
|
92
|
-
const form = e.target;
|
|
93
|
-
const button = e.submitter;
|
|
94
|
-
let action = form.action || document.URL;
|
|
95
|
-
if (button && button.formAction && button.formAction !== document.URL) action = button.formAction;
|
|
96
|
-
const options = {
|
|
97
|
-
body: new FormData(form),
|
|
98
|
-
method: "POST",
|
|
99
|
-
mode: "cors",
|
|
100
|
-
credentials: "same-origin",
|
|
101
|
-
headers: { "X-Torpor-Form-Submit": "" }
|
|
102
|
-
};
|
|
103
|
-
const response = await fetch(action, options);
|
|
104
|
-
if (response.status === 200 && response.headers.has("Content-Disposition") && /attachment;\s*filename="(.+?)"/.test(response.headers.get("Content-Disposition"))) {
|
|
105
|
-
const downloadFileName = response.headers.get("Content-Disposition")?.match(/attachment;\s*filename="(.+?)"/)[1] ?? "file";
|
|
106
|
-
const downloadLink = document.createElement("a");
|
|
107
|
-
downloadLink.href = window.URL.createObjectURL(await response.blob());
|
|
108
|
-
downloadLink.download = downloadFileName;
|
|
109
|
-
downloadLink.click();
|
|
110
|
-
} else if (response.status === 200 && response.headers.has("X-Torpor-Form-Redirect")) {
|
|
111
|
-
const redirect = response.headers.get("Location");
|
|
112
|
-
await load(redirect);
|
|
113
|
-
window.location.href = redirect;
|
|
114
|
-
} else if (response.status >= 200 && response.status <= 299 || response.status >= 400 && response.status <= 499) {
|
|
115
|
-
if (response.headers.get("Content-Type") === "application/json") $page.form = await response.json();
|
|
116
|
-
await reload();
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/nav/loadData.ts
|
|
121
|
-
async function loadData(handler, params, path, query, newLayoutStack, clientEndPoint, serverEndPoint, prefetch = false) {
|
|
122
|
-
let data = {};
|
|
123
|
-
if (handler.layouts) {
|
|
124
|
-
let layoutStack = client.layoutStack;
|
|
125
|
-
for (let [i, layout] of handler.layouts.entries()) {
|
|
126
|
-
let layoutPath = layout.path;
|
|
127
|
-
for (let key in params) layoutPath = layoutPath.replace(`[${key}]`, params[key]);
|
|
128
|
-
if (layoutStack.at(i)?.path === layoutPath) {
|
|
129
|
-
if (!prefetch) layoutStack[i].reuse = true;
|
|
130
|
-
newLayoutStack[i] = layoutStack[i];
|
|
131
|
-
Object.assign(data, layoutStack[i].data);
|
|
132
|
-
} else {
|
|
133
|
-
const stackLayout = {
|
|
134
|
-
path: layoutPath,
|
|
135
|
-
data: {},
|
|
136
|
-
reuse: false,
|
|
137
|
-
slotRegion: null
|
|
138
|
-
};
|
|
139
|
-
const layoutEndPoint = (await layout.endPoint())?.default;
|
|
140
|
-
const layoutServerEndPoint = layout.serverEndPoint && (await layout.serverEndPoint())?.default;
|
|
141
|
-
if ((await loadClientAndServerData(stackLayout.data, document.location.origin + layoutPath, query, params, layoutEndPoint, layoutServerEndPoint))?.ok === false) return;
|
|
142
|
-
Object.assign(data, stackLayout.data);
|
|
143
|
-
newLayoutStack.push(stackLayout);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
if ((await loadClientAndServerData(data, document.location.origin + path, query, params, clientEndPoint, serverEndPoint))?.ok === false) return;
|
|
148
|
-
return data;
|
|
149
|
-
}
|
|
150
|
-
async function loadClientAndServerData(data, location, query, params, clientEndPoint, serverEndPoint) {
|
|
151
|
-
let prefetchedData = client.prefetchedData;
|
|
152
|
-
if (clientEndPoint?.load) {
|
|
153
|
-
const clientKey = location + (query.size > 0 ? `?${query}` : "");
|
|
154
|
-
if (prefetchedData[clientKey]) Object.assign(data, prefetchedData[clientKey]);
|
|
155
|
-
else {
|
|
156
|
-
const clientUrl = new URL(document.location.href);
|
|
157
|
-
for (let [name, value] of query) clientUrl.searchParams.append(name, value);
|
|
158
|
-
const clientParams = buildClientParams(clientUrl, params, data);
|
|
159
|
-
const clientResponse = await clientEndPoint.load(clientParams);
|
|
160
|
-
if (clientResponse) if (clientResponse.ok) {
|
|
161
|
-
if (clientResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
162
|
-
const clientData = await clientResponse.json();
|
|
163
|
-
Object.assign(data, clientData);
|
|
164
|
-
prefetchedData[clientKey] = clientData;
|
|
165
|
-
}
|
|
166
|
-
} else return clientResponse;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (serverEndPoint?.load) {
|
|
170
|
-
const serverLocation = location.replace(/\/$/, "") + "/~server";
|
|
171
|
-
const serverKey = serverLocation + (query.size > 0 ? `?${query}` : "");
|
|
172
|
-
if (prefetchedData[serverKey]) Object.assign(data, prefetchedData[serverKey]);
|
|
173
|
-
else {
|
|
174
|
-
const serverUrl = new URL(serverLocation);
|
|
175
|
-
for (let [name, value] of query) serverUrl.searchParams.append(name, value);
|
|
176
|
-
const serverResponse = await fetch(serverUrl);
|
|
177
|
-
if (serverResponse) if (serverResponse.ok) {
|
|
178
|
-
if (serverResponse.headers.get("Content-Type")?.includes("application/json")) {
|
|
179
|
-
const serverData = await serverResponse.json();
|
|
180
|
-
Object.assign(data, serverData);
|
|
181
|
-
prefetchedData[serverKey] = serverData;
|
|
182
|
-
}
|
|
183
|
-
} else return serverResponse;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
function buildClientParams(url, params, data) {
|
|
188
|
-
return {
|
|
189
|
-
url,
|
|
190
|
-
params,
|
|
191
|
-
data
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
//#endregion
|
|
195
|
-
//#region src/nav/navigate.ts
|
|
196
|
-
async function navigate(url, withHydration = false) {
|
|
197
|
-
let parent = document.getElementById("app");
|
|
198
|
-
if (!parent) {
|
|
199
|
-
console.log("500");
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
const path = url.pathname;
|
|
203
|
-
const query = url.searchParams;
|
|
204
|
-
const route = client.router.match(path, query);
|
|
205
|
-
if (!route) {
|
|
206
|
-
console.log("404");
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
$page.url = url;
|
|
210
|
-
if (path.endsWith("/_error")) {
|
|
211
|
-
$page.status = parseInt(query.get("status") ?? "404");
|
|
212
|
-
$page.error = { message: query.get("message") ?? "" };
|
|
213
|
-
window.history.replaceState({}, "", url.toString().split("?")[0]);
|
|
214
|
-
} else $page.status = 200;
|
|
215
|
-
const handler = route.handler;
|
|
216
|
-
const params = route.params || {};
|
|
217
|
-
const clientEndPoint = (await handler.endPoint()).default;
|
|
218
|
-
if (!clientEndPoint?.component) {
|
|
219
|
-
console.log("404");
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
const serverEndPoint = handler.serverEndPoint && (await handler.serverEndPoint())?.default;
|
|
223
|
-
let newLayoutStack = [];
|
|
224
|
-
const data = await loadData(handler, params, path, query, newLayoutStack, clientEndPoint, serverEndPoint);
|
|
225
|
-
let formInput = document.getElementById("t-form-data");
|
|
226
|
-
let form;
|
|
227
|
-
if (formInput) {
|
|
228
|
-
form = JSON.parse(formInput.value);
|
|
229
|
-
$page.form = form;
|
|
230
|
-
formInput.remove();
|
|
231
|
-
}
|
|
232
|
-
let $props = { data };
|
|
233
|
-
$props.form = $page.form;
|
|
234
|
-
const $context = { TorporBuildContext: { onformsubmit: formSubmit } };
|
|
235
|
-
client.layoutStack.push({
|
|
236
|
-
path: route.handler.path,
|
|
237
|
-
data: {},
|
|
238
|
-
reuse: false,
|
|
239
|
-
slotRegion: null
|
|
240
|
-
});
|
|
241
|
-
client.layoutStack = newLayoutStack;
|
|
242
|
-
let layoutStack = client.layoutStack;
|
|
243
|
-
let component = clientEndPoint.component;
|
|
244
|
-
let slots = void 0;
|
|
245
|
-
let reused = false;
|
|
246
|
-
let reusedIndex = -1;
|
|
247
|
-
if (handler.layouts) {
|
|
248
|
-
let slotFunctions = [];
|
|
249
|
-
slotFunctions[handler.layouts.length] = function clientComponent(parent, anchor) {
|
|
250
|
-
let i = layoutStack.length - 1;
|
|
251
|
-
layoutStack[i].slotRegion = fillLayoutSlot(clientEndPoint.component, slotFunctions[i + 1], parent, anchor, $props, $context);
|
|
252
|
-
};
|
|
253
|
-
for (let i = handler.layouts.length - 1; i >= 0; i--) {
|
|
254
|
-
const layoutEndPoint = (await handler.layouts[i].endPoint())?.default;
|
|
255
|
-
if (layoutEndPoint?.component) if (layoutStack[i].reuse) {
|
|
256
|
-
component = slotFunctions[i + 1];
|
|
257
|
-
reusedIndex = i;
|
|
258
|
-
reused = true;
|
|
259
|
-
break;
|
|
260
|
-
} else if (i === 0) {
|
|
261
|
-
component = layoutEndPoint.component;
|
|
262
|
-
slots = { _: slotFunctions[i + 1] };
|
|
263
|
-
} else slotFunctions[i] = function layoutComponent(parent, anchor, _, $context) {
|
|
264
|
-
layoutStack[i - 1].slotRegion = fillLayoutSlot(layoutEndPoint.component, slotFunctions[i + 1], parent, anchor, $props, $context);
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
try {
|
|
269
|
-
if (reused) {
|
|
270
|
-
const slotRegion = layoutStack[reusedIndex].slotRegion;
|
|
271
|
-
parent = slotRegion.startNode.parentNode;
|
|
272
|
-
clearLayoutSlot(slotRegion);
|
|
273
|
-
component(parent, null);
|
|
274
|
-
} else if (withHydration) hydrate(parent, component, $props, slots);
|
|
275
|
-
else {
|
|
276
|
-
unmount(parent);
|
|
277
|
-
mount(parent, component, $props, slots);
|
|
278
|
-
}
|
|
279
|
-
} catch (error) {
|
|
280
|
-
parent.innerHTML = "<span style=\"color: red\">Script syntax error</span><p>" + error + "</p>";
|
|
281
|
-
console.log(error);
|
|
282
|
-
}
|
|
283
|
-
client.prefetchedData = {};
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
286
|
-
//#endregion
|
|
287
|
-
//#region src/nav/load.ts
|
|
288
|
-
/**
|
|
289
|
-
* Loads and renders the page at the supplied path.
|
|
290
|
-
*/
|
|
291
|
-
async function load(path) {
|
|
292
|
-
return await navigate(new URL(path, document.location.href));
|
|
293
|
-
}
|
|
294
|
-
//#endregion
|
|
295
71
|
export { load, makeApi, reload, route };
|
|
296
72
|
|
|
297
73
|
//# sourceMappingURL=nav.mjs.map
|
package/dist/nav.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nav.mjs","names":[],"sources":["../src/nav/route.ts","../src/nav/api.ts","../src/state/client.ts","../src/nav/reload.ts","../src/nav/formSubmit.ts","../src/nav/loadData.ts","../src/nav/navigate.ts","../src/nav/load.ts"],"sourcesContent":["import type { ExactRouteParams, ParseRouteParams } from \"../types/ParseRouteParams\";\n\n/**\n * Builds a route path in a type-safe manner, filling in dynamic segments, e.g.\n * `route(\"/posts/[id]\", { id: 5 })` returns `/posts/5`. The params object is\n * checked for exact keys: missing or unknown params error at compile time.\n *\n * NOTE: The rest args conditional must stay INLINE — routing it through a type\n * alias defeats `Params` inference, and excess keys stop being checked\n * @param path The route path\n * @param params The route params, required if the path has dynamic segments\n * @returns The path with the params filled in, URI-encoded\n */\nexport default function route<\n\tRoute extends string,\n\tParams extends ParseRouteParams<Route> = ParseRouteParams<Route>,\n>(\n\tpath: Route,\n\t...args: string extends Route\n\t\t? [params?: Record<string, string>]\n\t\t: keyof ParseRouteParams<Route> extends never\n\t\t\t? []\n\t\t\t: [params: Params & ExactRouteParams<Params, ParseRouteParams<Route>>]\n): string {\n\tconst params = ((args as unknown[])[0] ?? {}) as Record<string, string>;\n\treturn path.replace(/\\[(\\.\\.\\.)?([^\\]]+)\\]/g, (_, splat: string | undefined, name: string) => {\n\t\tconst value = params[name];\n\t\tif (value === undefined) {\n\t\t\tthrow new Error(`Missing param '${name}' for route '${path}'`);\n\t\t}\n\t\treturn splat\n\t\t\t? value\n\t\t\t\t\t.split(\"/\")\n\t\t\t\t\t.map((segment) => encodeURIComponent(segment))\n\t\t\t\t\t.join(\"/\")\n\t\t\t: encodeURIComponent(String(value));\n\t});\n}\n","import type { ExactRouteParams, ParseRouteParams } from \"../types/ParseRouteParams\";\nimport type TypedResponse from \"../response/TypedResponse\";\nimport route from \"./route\";\n\n/**\n * The value a client call resolves to: the endpoint's typed JSON body when it\n * returns a typed response, or the raw Response when the body isn't JSON.\n * Untyped endpoints resolve to `unknown`.\n */\ntype ApiResult<Handler> = Handler extends (event: any) => any\n\t? Awaited<ReturnType<Handler>> extends TypedResponse<infer Body>\n\t\t? Body | Response\n\t\t: unknown\n\t: never;\n\n/**\n * The body a client call accepts: the endpoint event's annotated JSON body\n * type (from `ServerLoadEvent<Route, Body>`), or a loose value when the\n * handler doesn't annotate one.\n */\ntype ApiRequestBody<Handler> = Handler extends (event: infer Event) => any\n\t? Event extends { json: () => Promise<infer Body> }\n\t\t? unknown extends Body\n\t\t\t? BodyInit | object\n\t\t\t: Body\n\t\t: BodyInit | object\n\t: BodyInit | object;\n\n/** The callable methods of an endpoint, one per handler it defines. */\nexport type ApiMethods<Endpoint extends object> = {\n\t[Method in string & keyof Endpoint as NonNullable<Endpoint[Method]> extends (event: any) => any\n\t\t? Method\n\t\t: never]: (\n\t\tbody?: ApiRequestBody<NonNullable<Endpoint[Method]>>,\n\t\tinit?: RequestInit,\n\t) => Promise<ApiResult<NonNullable<Endpoint[Method]>>>;\n};\n\nconst METHODS = [\"get\", \"post\", \"patch\", \"put\", \"del\", \"options\", \"head\"] as const;\n\n/**\n * Creates typed client callers for a `+server` endpoint, e.g.\n *\n * ```ts\n * import type endpoint from \"@/routes/api/posts/[id]/+server\";\n * const post = makeApi<\"/api/posts/[id]\", typeof endpoint>(\"/api/posts/[id]\");\n * const data = await post.get(); // typed from the endpoint's responses\n * ```\n *\n * Method names match the endpoint's handlers (`del` for DELETE). Params are\n * filled in and URI-encoded from the route path, and JSON responses are\n * parsed; non-JSON responses resolve to the raw Response. Failed requests\n * (non-2xx) throw.\n *\n * @param path The endpoint's route path\n * @param params The route params, required if the path has dynamic segments\n * @returns An object with one caller per handler defined by the endpoint\n */\nexport default function makeApi<\n\tRoute extends string,\n\tEndpoint extends object,\n\tParams extends ParseRouteParams<Route> = ParseRouteParams<Route>,\n>(\n\tpath: Route,\n\t// NOTE: The rest args conditional must stay INLINE — routing it through a\n\t// type alias defeats `Params` inference, and excess keys stop being checked\n\t...args: string extends Route\n\t\t? [params?: Record<string, string>]\n\t\t: keyof ParseRouteParams<Route> extends never\n\t\t\t? []\n\t\t\t: [params: Params & ExactRouteParams<Params, ParseRouteParams<Route>>]\n): ApiMethods<Endpoint> {\n\tconst url = route(path as string, ...(args as [params?: Record<string, string>]));\n\tconst api: Record<string, (body?: unknown, init?: RequestInit) => Promise<unknown>> = {};\n\tfor (const method of METHODS) {\n\t\tapi[method] = (body, init) => request(method, url, body, init);\n\t}\n\treturn api as ApiMethods<Endpoint>;\n}\n\nasync function request(\n\tmethod: string,\n\turl: string,\n\tbody: unknown,\n\tinit?: RequestInit,\n): Promise<unknown> {\n\t// `del` is the endpoint handler name for DELETE requests\n\tconst httpMethod = method === \"del\" ? \"delete\" : method;\n\tconst options: RequestInit = { method: httpMethod.toUpperCase(), ...init };\n\tif (body !== undefined && options.body === undefined && method !== \"get\" && method !== \"head\") {\n\t\tif (typeof body === \"object\") {\n\t\t\toptions.headers ??= { \"Content-Type\": \"application/json\" };\n\t\t\toptions.body = JSON.stringify(body);\n\t\t} else {\n\t\t\toptions.body = body as BodyInit;\n\t\t}\n\t}\n\tconst response = await fetch(url, options);\n\tif (!response.ok) {\n\t\tthrow new Error(`API ${method.toUpperCase()} ${url} failed: ${response.status}`);\n\t}\n\tconst contentType = response.headers.get(\"Content-Type\") ?? \"\";\n\treturn contentType.includes(\"application/json\") ? await response.json() : response;\n}\n","import type ClientState from \"../types/ClientState\";\n\nconst CLIENT_SYMBOL: unique symbol = Symbol.for(\"torp.Client\");\n\nconst client: ClientState =\n\t// @ts-ignore\n\t(globalThis[CLIENT_SYMBOL] ??= {\n\t\t// @ts-ignore This will definitely, 100% get set\n\t\trouter: null,\n\t\tlayoutStack: [],\n\t\t// TODO: Probably expire after 30 seconds or something?\n\t\tprefetchedData: {},\n\t});\n\nexport default client;\n","import navigate from \"./navigate\";\n\n/**\n * Reloads data for the currently loaded page and rerenders it.\n */\nexport default async function reload(): Promise<boolean> {\n\tconst url = new URL(document.location.href);\n\treturn await navigate(url);\n}\n","import $page from \"../state/$page\";\nimport load from \"./load\";\nimport reload from \"./reload\";\n\nexport default async function formSubmit(e: SubmitEvent): Promise<void> {\n\t// We'll take it from here\n\te.preventDefault();\n\n\t// Reset $page on submit and link navigation\n\t// TODO: Should we put data in $page.data??\n\t$page.form = undefined;\n\n\t// Perform a POST fetch to the form's action location\n\tconst form = e.target as HTMLFormElement;\n\tconst button = e.submitter as HTMLButtonElement;\n\t// HACK: submit buttons have their formAction set to the current page if not\n\t// set, so we can't just check whether it's empty and fallback to form action\n\t//const action = button?.formAction ?? form.action;\n\tlet action = form.action || document.URL;\n\tif (button && button.formAction && button.formAction !== document.URL) {\n\t\taction = button.formAction;\n\t}\n\tconst options = {\n\t\tbody: new FormData(form),\n\t\tmethod: \"POST\",\n\t\tmode: \"cors\",\n\t\tcredentials: \"same-origin\",\n\t\t// Add a special header which tells the server that we want the raw\n\t\t// error response (if applicable) rather than the reloaded page that you\n\t\t// would get if submitting a page without javascript\n\t\theaders: {\n\t\t\t\"X-Torpor-Form-Submit\": \"\",\n\t\t},\n\t};\n\tconst response = await fetch(\n\t\taction,\n\t\t// @ts-ignore -- don't know what it's complaining about here\n\t\toptions,\n\t);\n\n\tif (\n\t\tresponse.status === 200 &&\n\t\tresponse.headers.has(\"Content-Disposition\") &&\n\t\t/attachment;\\s*filename=\"(.+?)\"/.test(response.headers.get(\"Content-Disposition\")!)\n\t) {\n\t\t// TODO: Is this enough to check to see if we have a file that should download?\n\t\t// Download the file by creating a dummy <a> and clicking it\n\t\tconst downloadFileName =\n\t\t\tresponse.headers.get(\"Content-Disposition\")?.match(/attachment;\\s*filename=\"(.+?)\"/)![1] ??\n\t\t\t\"file\";\n\t\tconst downloadLink = document.createElement(\"a\");\n\t\tdownloadLink.href = window.URL.createObjectURL(await response.blob());\n\t\tdownloadLink.download = downloadFileName;\n\t\tdownloadLink.click();\n\t} else if (response.status === 200 && response.headers.has(\"X-Torpor-Form-Redirect\")) {\n\t\t// TODO: Should do the correct type of redirect e.g. seeOther etc\n\t\tconst redirect = response.headers.get(\"Location\")!;\n\t\tawait load(redirect);\n\t\twindow.location.href = redirect;\n\t} else if (\n\t\t(response.status >= 200 && response.status <= 299) ||\n\t\t(response.status >= 400 && response.status <= 499)\n\t) {\n\t\t// Put the response into `$page.form`, and it will be put into `$props` in reload\n\t\tif (response.headers.get(\"Content-Type\") === \"application/json\") {\n\t\t\t$page.form = await response.json();\n\t\t}\n\t\tawait reload();\n\t}\n}\n","import client from \"../state/client\";\nimport type LayoutPath from \"../types/LayoutPath\";\nimport type PageEndPoint from \"../types/PageEndPoint\";\nimport type PageServerEndPoint from \"../types/PageServerEndPoint\";\nimport type RouteHandler from \"../types/RouteHandler\";\n\nexport default async function loadData(\n\thandler: RouteHandler,\n\tparams: Record<string, string>,\n\tpath: string,\n\tquery: URLSearchParams,\n\tnewLayoutStack: LayoutPath[],\n\tclientEndPoint: PageEndPoint | undefined,\n\tserverEndPoint: PageServerEndPoint | undefined,\n\tprefetch = false,\n): Promise<Record<string, string> | void> {\n\tlet data = {};\n\tif (handler.layouts) {\n\t\tlet layoutStack = client.layoutStack;\n\t\tfor (let [i, layout] of handler.layouts.entries()) {\n\t\t\tlet layoutPath = layout.path;\n\t\t\tfor (let key in params) {\n\t\t\t\tlayoutPath = layoutPath.replace(`[${key}]`, params[key]);\n\t\t\t}\n\t\t\tif (layoutStack.at(i)?.path === layoutPath) {\n\t\t\t\t// We've already loaded this layout, we can just re-use its data and UI\n\t\t\t\tif (!prefetch) {\n\t\t\t\t\tlayoutStack[i].reuse = true;\n\t\t\t\t}\n\t\t\t\tnewLayoutStack[i] = layoutStack[i];\n\t\t\t\tObject.assign(data, layoutStack[i].data);\n\t\t\t} else {\n\t\t\t\tconst stackLayout = { path: layoutPath, data: {}, reuse: false, slotRegion: null };\n\t\t\t\tconst layoutEndPoint: PageEndPoint | undefined = (await layout.endPoint())?.default;\n\t\t\t\tconst layoutServerEndPoint: PageServerEndPoint | undefined =\n\t\t\t\t\tlayout.serverEndPoint && (await layout.serverEndPoint())?.default;\n\t\t\t\tconst layoutResponse = await loadClientAndServerData(\n\t\t\t\t\tstackLayout.data,\n\t\t\t\t\tdocument.location.origin + layoutPath,\n\t\t\t\t\tquery,\n\t\t\t\t\tparams,\n\t\t\t\t\tlayoutEndPoint,\n\t\t\t\t\tlayoutServerEndPoint,\n\t\t\t\t);\n\t\t\t\tif (layoutResponse?.ok === false) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tObject.assign(data, stackLayout.data);\n\t\t\t\tnewLayoutStack.push(stackLayout);\n\t\t\t}\n\t\t}\n\t}\n\tlet endPointResponse = await loadClientAndServerData(\n\t\tdata,\n\t\tdocument.location.origin + path,\n\t\tquery,\n\t\tparams,\n\t\tclientEndPoint,\n\t\tserverEndPoint,\n\t);\n\tif (endPointResponse?.ok === false) {\n\t\treturn;\n\t}\n\treturn data;\n}\n\nasync function loadClientAndServerData(\n\tdata: Record<string, any>,\n\tlocation: string,\n\tquery: URLSearchParams,\n\tparams: Record<string, string>,\n\tclientEndPoint?: PageEndPoint,\n\tserverEndPoint?: PageServerEndPoint,\n): Promise<Response | undefined | void> {\n\tlet prefetchedData = client.prefetchedData;\n\n\tif (clientEndPoint?.load) {\n\t\tconst clientKey = location + (query.size > 0 ? `?${query}` : \"\");\n\t\tif (prefetchedData[clientKey]) {\n\t\t\tObject.assign(data, prefetchedData[clientKey]);\n\t\t} else {\n\t\t\tconst clientUrl = new URL(document.location.href);\n\t\t\tfor (let [name, value] of query) {\n\t\t\t\tclientUrl.searchParams.append(name, value);\n\t\t\t}\n\t\t\tconst clientParams = buildClientParams(clientUrl, params, data);\n\t\t\tconst clientResponse = await clientEndPoint.load(clientParams);\n\t\t\tif (clientResponse) {\n\t\t\t\tif (clientResponse.ok) {\n\t\t\t\t\tif (clientResponse.headers.get(\"Content-Type\")?.includes(\"application/json\")) {\n\t\t\t\t\t\tconst clientData = await clientResponse.json();\n\t\t\t\t\t\tObject.assign(data, clientData);\n\t\t\t\t\t\tprefetchedData[clientKey] = clientData;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn clientResponse;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tif (serverEndPoint?.load) {\n\t\tconst serverLocation = location.replace(/\\/$/, \"\") + \"/~server\";\n\t\tconst serverKey = serverLocation + (query.size > 0 ? `?${query}` : \"\");\n\t\tif (prefetchedData[serverKey]) {\n\t\t\tObject.assign(data, prefetchedData[serverKey]);\n\t\t} else {\n\t\t\tconst serverUrl = new URL(serverLocation);\n\t\t\tfor (let [name, value] of query) {\n\t\t\t\tserverUrl.searchParams.append(name, value);\n\t\t\t}\n\t\t\tconst serverResponse = await fetch(serverUrl);\n\t\t\tif (serverResponse) {\n\t\t\t\tif (serverResponse.ok) {\n\t\t\t\t\tif (serverResponse.headers.get(\"Content-Type\")?.includes(\"application/json\")) {\n\t\t\t\t\t\tconst serverData = await serverResponse.json();\n\t\t\t\t\t\tObject.assign(data, serverData);\n\t\t\t\t\t\tprefetchedData[serverKey] = serverData;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\treturn serverResponse;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction buildClientParams(url: URL, params: Record<string, string>, data: Record<string, any>) {\n\treturn {\n\t\turl,\n\t\tparams,\n\t\tdata,\n\t};\n}\n","import { clearLayoutSlot, fillLayoutSlot, hydrate } from \"@torpor/view\";\nimport { type Component, type SlotRender } from \"@torpor/view\";\nimport { mount, unmount } from \"@torpor/view\";\nimport $page from \"../state/$page\";\nimport client from \"../state/client\";\nimport type LayoutPath from \"../types/LayoutPath\";\nimport type PageEndPoint from \"../types/PageEndPoint\";\nimport type PageServerEndPoint from \"../types/PageServerEndPoint\";\nimport formSubmit from \"./formSubmit\";\nimport loadData from \"./loadData\";\n\n// @ts-ignore\nexport default async function navigate(url: URL, withHydration = false): Promise<boolean> {\n\tlet parent = document.getElementById(\"app\");\n\tif (!parent) {\n\t\t// TODO: 500\n\t\tconsole.log(\"500\");\n\t\treturn false;\n\t}\n\n\tconst path = url.pathname;\n\tconst query = url.searchParams;\n\n\t//console.log(`navigating to '${path}'${query.size ? ` with ${query}` : \"\"}`);\n\n\tconst route = client.router.match(path, query);\n\tif (!route) {\n\t\t// TODO: 404\n\t\tconsole.log(\"404\");\n\t\treturn false;\n\t}\n\n\t// Update $page before building the components\n\t$page.url = url;\n\tif (path.endsWith(\"/_error\")) {\n\t\t$page.status = parseInt(query.get(\"status\") ?? \"404\");\n\t\t$page.error = { message: query.get(\"message\") ?? \"\" };\n\t\t// Make it look a bit classier by removing the query\n\t\twindow.history.replaceState({}, \"\", url.toString().split(\"?\")[0]);\n\t} else {\n\t\t$page.status = 200;\n\t}\n\tconst handler = route.handler;\n\tconst params = route.params || {};\n\n\t// There must be a client endpoint with a component\n\tconst clientEndPoint: PageEndPoint | undefined = (await handler.endPoint()).default;\n\tif (!clientEndPoint?.component) {\n\t\t// TODO: 404\n\t\tconsole.log(\"404\");\n\t\treturn false;\n\t}\n\n\t// There may be a server endpoint\n\tconst serverEndPoint: PageServerEndPoint | undefined =\n\t\thandler.serverEndPoint && (await handler.serverEndPoint())?.default;\n\n\tlet newLayoutStack: LayoutPath[] = [];\n\n\t// Pass the data into $props\n\t// TODO: Don't load if this is the first time -- it should have been passed\n\t// to us, somehow...\n\tconst data = await loadData(\n\t\thandler,\n\t\tparams,\n\t\tpath,\n\t\tquery,\n\t\tnewLayoutStack,\n\t\tclientEndPoint,\n\t\tserverEndPoint,\n\t);\n\t// We may have form data in a hidden input -- not sure if this is the best\n\t// way to do it\n\tlet formInput = document.getElementById(\"t-form-data\") as HTMLInputElement;\n\tlet form: Record<string, string> | undefined;\n\tif (formInput) {\n\t\tform = JSON.parse(formInput.value);\n\t\t$page.form = form;\n\t\tformInput.remove();\n\t}\n\tlet $props: Record<string, any> = { data };\n\n\t// We may have form data in $page.form, either from the hidden input\n\t// (above), or added via onformsubmit (below)\n\t$props.form = $page.form;\n\n\t// Add some special context for submitting Forms client-side\n\tconst $context = {\n\t\tTorporBuildContext: { onformsubmit: formSubmit },\n\t};\n\n\tclient.layoutStack.push({ path: route.handler.path, data: {}, reuse: false, slotRegion: null });\n\tclient.layoutStack = newLayoutStack;\n\tlet layoutStack = client.layoutStack;\n\n\t// If there are layouts, work our way upwards, pushing each component into\n\t// the default slot of its parent\n\t// TODO: There's probably a nicer way to do this with reducers or something\n\tlet component = clientEndPoint.component as Component;\n\tlet slots: Record<string, SlotRender> | undefined = undefined;\n\tlet reused = false;\n\t// The index of the innermost reused layout. Its slotRegion is the region\n\t// that must be cleared and refilled (it contains the next layout's or the\n\t// page's content). This is usually the last entry of the stack, but not\n\t// when an outer layout is reused while an inner one is new — e.g.\n\t// navigating between sections that share the root layout.\n\tlet reusedIndex = -1;\n\tif (handler.layouts) {\n\t\tlet slotFunctions: SlotRender[] = [];\n\t\t// The last slot function will render the client component\n\t\tslotFunctions[handler.layouts.length] = function clientComponent(parent, anchor) {\n\t\t\tlet i = layoutStack.length - 1;\n\t\t\tlayoutStack[i].slotRegion = fillLayoutSlot(\n\t\t\t\tclientEndPoint.component!,\n\t\t\t\tslotFunctions[i + 1],\n\t\t\t\tparent,\n\t\t\t\tanchor,\n\t\t\t\t$props,\n\t\t\t\t$context,\n\t\t\t);\n\t\t};\n\n\t\t// Each earlier slot function will render a layout\n\t\tfor (let i = handler.layouts.length - 1; i >= 0; i--) {\n\t\t\tconst layoutEndPoint: PageEndPoint | undefined = (await handler.layouts[i].endPoint())\n\t\t\t\t?.default;\n\t\t\tif (layoutEndPoint?.component) {\n\t\t\t\tif (layoutStack[i].reuse) {\n\t\t\t\t\t// Reuse this layout — clear and refill its slot (done in\n\t\t\t\t\t// the try block below so a failure doesn't leave the slot\n\t\t\t\t\t// half-cleared)\n\t\t\t\t\tcomponent = slotFunctions[i + 1] as Component;\n\t\t\t\t\treusedIndex = i;\n\t\t\t\t\treused = true;\n\t\t\t\t\tbreak;\n\t\t\t\t} else if (i === 0) {\n\t\t\t\t\tcomponent = layoutEndPoint.component as Component;\n\t\t\t\t\tslots = { _: slotFunctions[i + 1] };\n\t\t\t\t} else {\n\t\t\t\t\tslotFunctions[i] = function layoutComponent(parent, anchor, _, $context) {\n\t\t\t\t\t\tlayoutStack[i - 1].slotRegion = fillLayoutSlot(\n\t\t\t\t\t\t\tlayoutEndPoint.component!,\n\t\t\t\t\t\t\tslotFunctions[i + 1],\n\t\t\t\t\t\t\tparent,\n\t\t\t\t\t\t\tanchor,\n\t\t\t\t\t\t\t$props,\n\t\t\t\t\t\t\t$context,\n\t\t\t\t\t\t);\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\ttry {\n\t\tif (reused) {\n\t\t\t// The layout is being reused — clear the old slot content, then\n\t\t\t// call the slot function directly to fill it with the new page.\n\t\t\t// We must not go through `mount` here: the slot's container still\n\t\t\t// holds the layout's own children (e.g. a header), which `mount`\n\t\t\t// refuses to mount into. Both the clear and the fill are inside\n\t\t\t// the try so that a failure doesn't leave the slot half-cleared.\n\t\t\tconst slotRegion = layoutStack[reusedIndex].slotRegion;\n\t\t\tparent = slotRegion.startNode!.parentNode as HTMLElement;\n\t\t\tclearLayoutSlot(slotRegion);\n\t\t\tcomponent(parent, null);\n\t\t} else if (withHydration) {\n\t\t\thydrate(parent, component, $props, slots);\n\t\t} else {\n\t\t\t// The layout chain changed (or there was no previous layout to\n\t\t\t// reuse): tear down the previous UI entirely — disposing its\n\t\t\t// region tree and clearing `#app` — so `mount` starts fresh.\n\t\t\t// Without this, `mount` throws because `#app` still holds the\n\t\t\t// previous render's children, and it would reuse a stale root\n\t\t\t// region.\n\t\t\tunmount(parent);\n\t\t\tmount(parent, component, $props, slots);\n\t\t}\n\t} catch (error) {\n\t\t// TODO: Show a proper Error component\n\t\tparent.innerHTML = '<span style=\"color: red\">Script syntax error</span><p>' + error + \"</p>\";\n\t\tconsole.log(error);\n\t}\n\n\t// Reset prefetched data on each navigation\n\tclient.prefetchedData = {};\n\n\treturn true;\n}\n","import navigate from \"./navigate\";\n\n/**\n * Loads and renders the page at the supplied path.\n */\nexport default async function load(path: string): Promise<boolean> {\n\tconst url = new URL(path, document.location.href);\n\treturn await navigate(url);\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAwB,MAIvB,MACA,GAAG,MAKM;CACT,MAAM,SAAW,KAAmB,MAAM,CAAC;CAC3C,OAAO,KAAK,QAAQ,2BAA2B,GAAG,OAA2B,SAAiB;EAC7F,MAAM,QAAQ,OAAO;EACrB,IAAI,UAAU,KAAA,GACb,MAAM,IAAI,MAAM,kBAAkB,KAAK,eAAe,KAAK,EAAE;EAE9D,OAAO,QACJ,MACC,MAAM,GAAG,CAAC,CACV,KAAK,YAAY,mBAAmB,OAAO,CAAC,CAAC,CAC7C,KAAK,GAAG,IACT,mBAAmB,OAAO,KAAK,CAAC;CACpC,CAAC;AACF;;;ACCA,MAAM,UAAU;CAAC;CAAO;CAAQ;CAAS;CAAO;CAAO;CAAW;AAAM;;;;;;;;;;;;;;;;;;;AAoBxE,SAAwB,QAKvB,MAGA,GAAG,MAKoB;CACvB,MAAM,MAAM,MAAM,MAAgB,GAAI,IAA0C;CAChF,MAAM,MAAgF,CAAC;CACvF,KAAK,MAAM,UAAU,SACpB,IAAI,WAAW,MAAM,SAAS,QAAQ,QAAQ,KAAK,MAAM,IAAI;CAE9D,OAAO;AACR;AAEA,eAAe,QACd,QACA,KACA,MACA,MACmB;CAGnB,MAAM,UAAuB;EAAE,SADZ,WAAW,QAAQ,WAAW,OAAA,CACC,YAAY;EAAG,GAAG;CAAK;CACzE,IAAI,SAAS,KAAA,KAAa,QAAQ,SAAS,KAAA,KAAa,WAAW,SAAS,WAAW,QACtF,IAAI,OAAO,SAAS,UAAU;EAC7B,QAAQ,YAAY,EAAE,gBAAgB,mBAAmB;EACzD,QAAQ,OAAO,KAAK,UAAU,IAAI;CACnC,OACC,QAAQ,OAAO;CAGjB,MAAM,WAAW,MAAM,MAAM,KAAK,OAAO;CACzC,IAAI,CAAC,SAAS,IACb,MAAM,IAAI,MAAM,OAAO,OAAO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ON,QAAQ,MAAM;GACd,MAAM,QAAQ,WAAW,QAAQ,KAAK;EACvC;CACD,SAAS,OAAO;EAEf,OAAO,YAAY,6DAA2D,QAAQ;EACtF,QAAQ,IAAI,KAAK;CAClB;CAGA,OAAO,iBAAiB,CAAC;CAEzB,OAAO;AACR;;;;;;ACvLA,eAA8B,KAAK,MAAgC;CAElE,OAAO,MAAM,SAAS,IADN,IAAI,MAAM,SAAS,SAAS,IACpB,CAAC;AAC1B"}
|
|
1
|
+
{"version":3,"file":"nav.mjs","names":[],"sources":["../src/nav/route.ts","../src/nav/api.ts"],"sourcesContent":["import type { ExactRouteParams, ParseRouteParams } from \"../types/ParseRouteParams\";\n\n/**\n * Builds a route path in a type-safe manner, filling in dynamic segments, e.g.\n * `route(\"/posts/[id]\", { id: 5 })` returns `/posts/5`. The params object is\n * checked for exact keys: missing or unknown params error at compile time.\n *\n * NOTE: The rest args conditional must stay INLINE — routing it through a type\n * alias defeats `Params` inference, and excess keys stop being checked\n * @param path The route path\n * @param params The route params, required if the path has dynamic segments\n * @returns The path with the params filled in, URI-encoded\n */\nexport default function route<\n\tRoute extends string,\n\tParams extends ParseRouteParams<Route> = ParseRouteParams<Route>,\n>(\n\tpath: Route,\n\t...args: string extends Route\n\t\t? [params?: Record<string, string>]\n\t\t: keyof ParseRouteParams<Route> extends never\n\t\t\t? []\n\t\t\t: [params: Params & ExactRouteParams<Params, ParseRouteParams<Route>>]\n): string {\n\tconst params = ((args as unknown[])[0] ?? {}) as Record<string, string>;\n\treturn path.replace(/\\[(\\.\\.\\.)?([^\\]]+)\\]/g, (_, splat: string | undefined, name: string) => {\n\t\tconst value = params[name];\n\t\tif (value === undefined) {\n\t\t\tthrow new Error(`Missing param '${name}' for route '${path}'`);\n\t\t}\n\t\treturn splat\n\t\t\t? value\n\t\t\t\t\t.split(\"/\")\n\t\t\t\t\t.map((segment) => encodeURIComponent(segment))\n\t\t\t\t\t.join(\"/\")\n\t\t\t: encodeURIComponent(String(value));\n\t});\n}\n","import type { ExactRouteParams, ParseRouteParams } from \"../types/ParseRouteParams\";\nimport type TypedResponse from \"../response/TypedResponse\";\nimport route from \"./route\";\n\n/**\n * The value a client call resolves to: the endpoint's typed JSON body when it\n * returns a typed response, or the raw Response when the body isn't JSON.\n * Untyped endpoints resolve to `unknown`.\n */\ntype ApiResult<Handler> = Handler extends (event: any) => any\n\t? Awaited<ReturnType<Handler>> extends TypedResponse<infer Body>\n\t\t? Body | Response\n\t\t: unknown\n\t: never;\n\n/**\n * The body a client call accepts: the endpoint event's annotated JSON body\n * type (from `ServerLoadEvent<Route, Body>`), or a loose value when the\n * handler doesn't annotate one.\n */\ntype ApiRequestBody<Handler> = Handler extends (event: infer Event) => any\n\t? Event extends { json: () => Promise<infer Body> }\n\t\t? unknown extends Body\n\t\t\t? BodyInit | object\n\t\t\t: Body\n\t\t: BodyInit | object\n\t: BodyInit | object;\n\n/** The callable methods of an endpoint, one per handler it defines. */\nexport type ApiMethods<Endpoint extends object> = {\n\t[Method in string & keyof Endpoint as NonNullable<Endpoint[Method]> extends (event: any) => any\n\t\t? Method\n\t\t: never]: (\n\t\tbody?: ApiRequestBody<NonNullable<Endpoint[Method]>>,\n\t\tinit?: RequestInit,\n\t) => Promise<ApiResult<NonNullable<Endpoint[Method]>>>;\n};\n\nconst METHODS = [\"get\", \"post\", \"patch\", \"put\", \"del\", \"options\", \"head\"] as const;\n\n/**\n * Creates typed client callers for a `+server` endpoint, e.g.\n *\n * ```ts\n * import type endpoint from \"@/routes/api/posts/[id]/+server\";\n * const post = makeApi<\"/api/posts/[id]\", typeof endpoint>(\"/api/posts/[id]\");\n * const data = await post.get(); // typed from the endpoint's responses\n * ```\n *\n * Method names match the endpoint's handlers (`del` for DELETE). Params are\n * filled in and URI-encoded from the route path, and JSON responses are\n * parsed; non-JSON responses resolve to the raw Response. Failed requests\n * (non-2xx) throw.\n *\n * @param path The endpoint's route path\n * @param params The route params, required if the path has dynamic segments\n * @returns An object with one caller per handler defined by the endpoint\n */\nexport default function makeApi<\n\tRoute extends string,\n\tEndpoint extends object,\n\tParams extends ParseRouteParams<Route> = ParseRouteParams<Route>,\n>(\n\tpath: Route,\n\t// NOTE: The rest args conditional must stay INLINE — routing it through a\n\t// type alias defeats `Params` inference, and excess keys stop being checked\n\t...args: string extends Route\n\t\t? [params?: Record<string, string>]\n\t\t: keyof ParseRouteParams<Route> extends never\n\t\t\t? []\n\t\t\t: [params: Params & ExactRouteParams<Params, ParseRouteParams<Route>>]\n): ApiMethods<Endpoint> {\n\tconst url = route(path as string, ...(args as [params?: Record<string, string>]));\n\tconst api: Record<string, (body?: unknown, init?: RequestInit) => Promise<unknown>> = {};\n\tfor (const method of METHODS) {\n\t\tapi[method] = (body, init) => request(method, url, body, init);\n\t}\n\treturn api as ApiMethods<Endpoint>;\n}\n\nasync function request(\n\tmethod: string,\n\turl: string,\n\tbody: unknown,\n\tinit?: RequestInit,\n): Promise<unknown> {\n\t// `del` is the endpoint handler name for DELETE requests\n\tconst httpMethod = method === \"del\" ? \"delete\" : method;\n\tconst options: RequestInit = { method: httpMethod.toUpperCase(), ...init };\n\tif (body !== undefined && options.body === undefined && method !== \"get\" && method !== \"head\") {\n\t\tif (typeof body === \"object\") {\n\t\t\toptions.headers ??= { \"Content-Type\": \"application/json\" };\n\t\t\toptions.body = JSON.stringify(body);\n\t\t} else {\n\t\t\toptions.body = body as BodyInit;\n\t\t}\n\t}\n\tconst response = await fetch(url, options);\n\tif (!response.ok) {\n\t\tthrow new Error(`API ${method.toUpperCase()} ${url} failed: ${response.status}`);\n\t}\n\tconst contentType = response.headers.get(\"Content-Type\") ?? \"\";\n\treturn contentType.includes(\"application/json\") ? await response.json() : response;\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,SAAwB,MAIvB,MACA,GAAG,MAKM;CACT,MAAM,SAAW,KAAmB,MAAM,CAAC;CAC3C,OAAO,KAAK,QAAQ,2BAA2B,GAAG,OAA2B,SAAiB;EAC7F,MAAM,QAAQ,OAAO;EACrB,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,"file":"run.d.mts","names":[],"sources":["../src/run/run.ts","../src/run/runBuild.ts","../src/run/runDev.ts","../src/run/runOpenApi.ts","../src/run/runPreview.ts"],"mappings":";;;iBAyD8B,IAC7B,gBACA,oCACE;;;
|
|
1
|
+
{"version":3,"file":"run.d.mts","names":[],"sources":["../src/run/run.ts","../src/run/runBuild.ts","../src/run/runDev.ts","../src/run/runOpenApi.ts","../src/run/runPreview.ts"],"mappings":";;;iBAyD8B,IAC7B,gBACA,oCACE;;;iBC5C2B,SAAS,MAAM,OAAO;;;iBCetB,OAAO,MAAM,OAAO;;;;;;;;iBCjBpB,WAC7B,MAAM,MACN,MAAM,eACN,mBACE;;;iBCR2B,WAAW,MAAM,OAAO"}
|
package/dist/run.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as runDev, i as runPreview, o as runBuild, r as run, t as runOpenApi } from "./runOpenApi-
|
|
1
|
+
import { a as runDev, i as runPreview, o as runBuild, r as run, t as runOpenApi } from "./runOpenApi-C8eI2Fh0.mjs";
|
|
2
2
|
export { run, runBuild, runDev, runOpenApi, runPreview };
|