@torpor/build 0.4.14 → 1.0.0
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/README.md +20 -2
- package/dist/Jsonify-Dg6gChd3.d.mts +10 -0
- package/dist/Jsonify-Dg6gChd3.d.mts.map +1 -0
- package/dist/ParseRouteParams-DBmqlmLh.d.mts +42 -0
- package/dist/ParseRouteParams-DBmqlmLh.d.mts.map +1 -0
- package/dist/{RouteType-B6BMOyYw.mjs → Router-C4ADUfhv.mjs} +30 -42
- package/dist/Router-C4ADUfhv.mjs.map +1 -0
- package/dist/{Server-CF5wDJp6.d.mts → Server-C_IKav_e.d.mts} +44 -16
- package/dist/Server-C_IKav_e.d.mts.map +1 -0
- package/dist/{ServerEvent-CxPwA4lH.mjs → ServerEvent-Dljn5i7A.mjs} +37 -11
- package/dist/ServerEvent-Dljn5i7A.mjs.map +1 -0
- package/dist/Site-CMa153FA.d.mts +565 -0
- package/dist/Site-CMa153FA.d.mts.map +1 -0
- package/dist/StandardSchema-D28L8_zZ.d.mts +100 -0
- package/dist/StandardSchema-D28L8_zZ.d.mts.map +1 -0
- package/dist/TypedResponse-6lznqN4U.d.mts +22 -0
- package/dist/TypedResponse-6lznqN4U.d.mts.map +1 -0
- package/dist/{_page-BQs4WvCO.mjs → _page-4cQKs_Dn.mjs} +3 -5
- package/dist/_page-4cQKs_Dn.mjs.map +1 -0
- package/dist/bin/index.d.mts +1 -1
- package/dist/bin/index.js +15 -11
- package/dist/bin/index.js.map +1 -1
- package/dist/flattenHeaders-C_YYLdOq.mjs +114 -0
- package/dist/flattenHeaders-C_YYLdOq.mjs.map +1 -0
- package/dist/form.d.mts +35 -0
- package/dist/form.d.mts.map +1 -0
- package/dist/form.mjs +48 -0
- package/dist/form.mjs.map +1 -0
- package/dist/index.d.mts +72 -156
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +158 -26
- package/dist/index.mjs.map +1 -1
- package/dist/nav.d.mts +58 -5
- package/dist/nav.d.mts.map +1 -1
- package/dist/nav.mjs +116 -38
- package/dist/nav.mjs.map +1 -1
- package/dist/openapi.d.mts +144 -0
- package/dist/openapi.d.mts.map +1 -0
- package/dist/openapi.mjs +2 -0
- package/dist/pathTrie-D4Ax2Mu9.mjs +160 -0
- package/dist/pathTrie-D4Ax2Mu9.mjs.map +1 -0
- package/dist/plugin-D8Www11B.mjs +202 -0
- package/dist/plugin-D8Www11B.mjs.map +1 -0
- package/dist/{response-C5TtAsh1.mjs → response-Bg4w2S1q.mjs} +16 -39
- package/dist/response-Bg4w2S1q.mjs.map +1 -0
- package/dist/response.d.mts +309 -289
- package/dist/response.d.mts.map +1 -1
- package/dist/response.mjs +3 -4
- package/dist/run.d.mts +11 -4
- package/dist/run.d.mts.map +1 -1
- package/dist/run.mjs +2 -8
- package/dist/runOpenApi-BUi1RH1l.mjs +1381 -0
- package/dist/runOpenApi-BUi1RH1l.mjs.map +1 -0
- package/dist/schema.d.mts +32 -0
- package/dist/schema.d.mts.map +1 -0
- package/dist/schema.mjs +2 -0
- package/dist/server.d.mts +10 -8
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +132 -4
- package/dist/server.mjs.map +1 -0
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs +2 -3
- package/dist/test.d.mts +8 -10
- package/dist/test.d.mts.map +1 -1
- package/dist/test.mjs +283 -79
- package/dist/test.mjs.map +1 -1
- package/dist/{seeOther-B4Yhu9iq.mjs → unprocessable-DAZbrDeR.mjs} +34 -13
- package/dist/unprocessable-DAZbrDeR.mjs.map +1 -0
- package/dist/validate-Ok4krlkX.mjs +40 -0
- package/dist/validate-Ok4krlkX.mjs.map +1 -0
- package/package.json +28 -17
- package/src/bin/index.ts +13 -2
- package/src/dev.ts +10 -0
- package/src/form/formDataToRecord.ts +23 -0
- package/src/form/readForm.ts +71 -0
- package/src/form.ts +4 -0
- package/src/index.ts +53 -1
- package/src/nav/api.test-d.ts +79 -0
- package/src/nav/api.ts +104 -0
- package/src/nav/formSubmit.ts +18 -4
- package/src/nav/navigate.ts +40 -15
- package/src/nav/route.ts +38 -0
- package/src/nav.ts +3 -1
- package/src/openapi/docsHtml.ts +25 -0
- package/src/openapi/document.ts +154 -0
- package/src/openapi/plugin.ts +78 -0
- package/src/openapi/types.ts +82 -0
- package/src/openapi.ts +14 -0
- package/src/response/TypedResponse.ts +17 -0
- package/src/response/badRequest.ts +14 -2
- package/src/response/created.ts +13 -2
- package/src/response/found.ts +2 -2
- package/src/response/movedPermanently.ts +2 -2
- package/src/response/notModified.ts +2 -2
- package/src/response/ok.ts +13 -4
- package/src/response/response.ts +4 -4
- package/src/response/unprocessable.ts +14 -2
- package/src/run/depCache.ts +175 -0
- package/src/run/devPlugin.ts +128 -0
- package/src/run/prepareTemplate.ts +6 -3
- package/src/run/run.ts +63 -39
- package/src/run/runBuild.ts +63 -12
- package/src/run/runDev.ts +146 -58
- package/src/run/runOpenApi.ts +52 -0
- package/src/run/runPreview.ts +17 -26
- package/src/run/staleTorpCopies.ts +100 -0
- package/src/run.ts +2 -1
- package/src/schema.ts +7 -0
- package/src/server/CookieHelper.ts +17 -7
- package/src/server/Server.ts +54 -30
- package/src/server/ServerEvent.ts +24 -1
- package/src/server/connect/connectMiddleware.ts +44 -40
- package/src/server/connect/flattenHeaders.ts +1 -23
- package/src/server/connect/requestToNodeMessage.ts +6 -3
- package/src/server/types/MiddlewareFunction.ts +18 -4
- package/src/site/Router.ts +52 -41
- package/src/site/Site.ts +184 -10
- package/src/site/checkLayoutSlots.ts +114 -0
- package/src/site/checkRoutes.ts +435 -0
- package/src/site/clientEntry.ts +22 -9
- package/src/site/layoutSlots.ts +53 -0
- package/src/site/manifest.ts +125 -6
- package/src/site/serverEntry.ts +341 -132
- package/src/state/$page.ts +2 -1
- package/src/state/$serverPage.ts +22 -0
- package/src/test/runTest.ts +306 -118
- package/src/types/Adapter.ts +12 -0
- package/src/types/Jsonify.ts +20 -0
- package/src/types/PageData.test-d.ts +136 -0
- package/src/types/PageData.ts +35 -0
- package/src/types/PageEndPoint.ts +25 -9
- package/src/types/PageForm.test-d.ts +78 -0
- package/src/types/PageForm.ts +29 -0
- package/src/types/PageLoadEvent.ts +16 -4
- package/src/types/PageLoadReturn.ts +13 -0
- package/src/types/PageProps.ts +14 -0
- package/src/types/PageServerAction.ts +8 -2
- package/src/types/PageServerEndPoint.test-d.ts +105 -0
- package/src/types/PageServerEndPoint.ts +99 -5
- package/src/types/PageServerLoad.ts +11 -3
- package/src/types/ParseRouteParams.test-d.ts +84 -0
- package/src/types/ParseRouteParams.ts +64 -0
- package/src/types/Route.ts +20 -1
- package/src/types/RouteHandler.ts +8 -1
- package/src/types/ServerEndPoint.test-d.ts +77 -0
- package/src/types/ServerEndPoint.ts +148 -16
- package/src/types/ServerHook.ts +10 -3
- package/src/types/ServerLoadEvent.ts +48 -3
- package/src/types/ServerRequest.ts +10 -2
- package/src/types/SitePlugin.ts +24 -0
- package/src/types/StandardSchema.ts +109 -0
- package/src/utils/pathToRegex.ts +4 -2
- package/src/utils/pathTrie.ts +182 -0
- package/src/utils/searchParamsToRecord.ts +18 -0
- package/src/utils/torporPackages.ts +150 -0
- package/src/utils/tsconfigAliases.ts +90 -0
- package/src/validation/ValidationError.ts +18 -0
- package/src/validation/endpoint.ts +83 -0
- package/src/validation/validate.ts +26 -0
- package/dist/RouteType-B6BMOyYw.mjs.map +0 -1
- package/dist/Server-CF5wDJp6.d.mts.map +0 -1
- package/dist/ServerEvent-CxPwA4lH.mjs.map +0 -1
- package/dist/Site-DgC6WWn1.d.mts +0 -78
- package/dist/Site-DgC6WWn1.d.mts.map +0 -1
- package/dist/_page-BQs4WvCO.mjs.map +0 -1
- package/dist/connectMiddleware-D7nehjj3.mjs +0 -252
- package/dist/connectMiddleware-D7nehjj3.mjs.map +0 -1
- package/dist/pathToRegex-TUAMc3zS.mjs +0 -11
- package/dist/pathToRegex-TUAMc3zS.mjs.map +0 -1
- package/dist/response-C5TtAsh1.mjs.map +0 -1
- package/dist/run-BPFtSYA3.mjs +0 -332
- package/dist/run-BPFtSYA3.mjs.map +0 -1
- package/dist/seeOther-B4Yhu9iq.mjs.map +0 -1
- package/src/server/Routerx.ts +0 -72
- package/src/server/connect/bufferToArrayBuffer.ts +0 -8
- package/src/server/connect/readableToBuffer.ts +0 -16
package/dist/nav.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nav.mjs","names":["CLIENT_SYMBOL: unique symbol","client: ClientState","client","layoutEndPoint: PageEndPoint | undefined","layoutServerEndPoint: PageServerEndPoint | undefined","client","clientEndPoint: PageEndPoint | undefined","serverEndPoint: PageServerEndPoint | undefined","newLayoutStack: LayoutPath[]","form: Record<string, string> | undefined","$props: Record<string, any>","$page","slots: Record<string, SlotRender> | undefined","slotFunctions: SlotRender[]","parent","layoutEndPoint: PageEndPoint | undefined","$context"],"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 (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 } 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\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// Set the parent to add the new content to (from the old\n\t\t\t\t\t// content), clear the range under this point, and set the\n\t\t\t\t\t// component to the slot function within this layout\n\t\t\t\t\tparent = layoutStack[i].slotRegion.startNode.parentNode as HTMLElement;\n\t\t\t\t\tclearLayoutSlot(layoutStack[i].slotRegion);\n\t\t\t\t\tcomponent = slotFunctions[i + 1];\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\tif (withHydration) {\n\t\thydrate(parent, component, $props, slots);\n\t} else {\n\t\ttry {\n\t\t\tmount(parent, component, $props, slots);\n\t\t} catch (error) {\n\t\t\t// TODO: Show a proper Error component\n\t\t\tparent.innerHTML = '<span style=\"color: red\">Script syntax error</span><p>' + error + \"</p>\";\n\t\t\tconsole.log(error);\n\t\t}\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":";;;;AAEA,MAAMA,gBAA+B,OAAO,IAAI,cAAc;AAE9D,MAAMC,SAEJ,WAAW,mBAAmB;CAE9B,QAAQ;CACR,aAAa,EAAE;CAEf,gBAAgB,EAAE;CAClB;AAEF,qBAAe;;;;;;;ACTf,eAA8B,SAA2B;AAExD,QAAO,MAAM,SADD,IAAI,IAAI,SAAS,SAAS,KAAK,CACjB;;;;;ACH3B,eAA8B,WAAW,GAA+B;AAEvE,GAAE,gBAAgB;AAIlB,eAAM,OAAO;CAGb,MAAM,OAAO,EAAE;CACf,MAAM,SAAS,EAAE;CAIjB,IAAI,SAAS,KAAK,UAAU,SAAS;AACrC,KAAI,UAAU,OAAO,cAAc,OAAO,eAAe,SAAS,IACjE,UAAS,OAAO;CAEjB,MAAM,UAAU;EACf,MAAM,IAAI,SAAS,KAAK;EACxB,QAAQ;EACR,MAAM;EACN,aAAa;EAIb,SAAS,EACR,wBAAwB,IACxB;EACD;CACD,MAAM,WAAW,MAAM,MACtB,QAEA,QACA;AAED,KAAI,SAAS,WAAW,OAAO,SAAS,QAAQ,IAAI,yBAAyB,EAAE;EAE9E,MAAM,WAAW,SAAS,QAAQ,IAAI,WAAW;AACjD,QAAM,KAAK,SAAS;AACpB,SAAO,SAAS,OAAO;YAEtB,SAAS,UAAU,OAAO,SAAS,UAAU,OAC7C,SAAS,UAAU,OAAO,SAAS,UAAU,KAC7C;AAED,MAAI,SAAS,QAAQ,IAAI,eAAe,KAAK,mBAC5C,eAAM,OAAO,MAAM,SAAS,MAAM;AAEnC,QAAM,QAAQ;;;;;;AC/ChB,eAA8B,SAC7B,SACA,QACA,MACA,OACA,gBACA,gBACA,gBACA,WAAW,OAC8B;CACzC,IAAI,OAAO,EAAE;AACb,KAAI,QAAQ,SAAS;EACpB,IAAI,cAAcC,eAAO;AACzB,OAAK,IAAI,CAAC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GAAG,KAAK;GACrD,MAAMC,kBAA4C,MAAM,QAAQ,QAAQ,GAAG,UAAU,GAClF;AACH,OAAI,gBAAgB,UACnB,KAAI,YAAY,GAAG,OAAO;AAIzB,aAAS,YAAY,GAAG,WAAW,UAAU;AAC7C,oBAAgB,YAAY,GAAG,WAAW;AAC1C,gBAAY,cAAc,IAAI;AAC9B;cACU,MAAM,GAAG;AACnB,gBAAY,eAAe;AAC3B,YAAQ,EAAE,GAAG,cAAc,IAAI,IAAI;SAEnC,eAAc,KAAK,SAAS,gBAAgB,UAAQ,QAAQ,GAAG,YAAU;AACxE,gBAAY,IAAI,GAAG,aAAa,eAC/B,eAAe,WACf,cAAc,IAAI,IAClBD,UACA,QACA,QACAE,WACA;;;;AAON,KAAI,cACH,SAAQ,QAAQ,WAAW,QAAQ,MAAM;KAEzC,KAAI;AACH,QAAM,QAAQ,WAAW,QAAQ,MAAM;UAC/B,OAAO;AAEf,SAAO,YAAY,6DAA2D,QAAQ;AACtF,UAAQ,IAAI,MAAM;;AAKpB,gBAAO,iBAAiB,EAAE;AAE1B,QAAO;;;;;;;;AC7JR,eAA8B,KAAK,MAAgC;AAElE,QAAO,MAAM,SADD,IAAI,IAAI,MAAM,SAAS,SAAS,KAAK,CACvB"}
|
|
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,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|
+
import { t as StandardSchemaV1 } from "./StandardSchema-D28L8_zZ.mjs";
|
|
3
|
+
//#region src/openapi/types.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A JSON Schema object, as produced by a schema library's converter (e.g.
|
|
6
|
+
* `z.toJSONSchema`, valibot's `toJsonSchema`, arktype's `toJsonSchema`).
|
|
7
|
+
*/
|
|
8
|
+
type JsonSchema = Record<string, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Standard Schema (zod, valibot, arktype, ...) into a JSON Schema
|
|
11
|
+
* object, for inclusion in an OpenAPI document. Supplied to the openApi
|
|
12
|
+
* plugin, since each schema library has its own conversion function:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* // zod
|
|
16
|
+
* openApi({ toJsonSchema: (s) => z.toJSONSchema(s as z.ZodType, { io: "input" }) })
|
|
17
|
+
* // arktype
|
|
18
|
+
* openApi({ toJsonSchema: (s) => (s as Type).toJsonSchema() })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
type ToJsonSchema = (schema: StandardSchemaV1) => JsonSchema;
|
|
22
|
+
/**
|
|
23
|
+
* Options for the openApi plugin.
|
|
24
|
+
*/
|
|
25
|
+
type OpenApiPluginOptions = {
|
|
26
|
+
/**
|
|
27
|
+
* The path to serve the OpenAPI document at. Defaults to `/openapi.json`.
|
|
28
|
+
*/
|
|
29
|
+
path?: string;
|
|
30
|
+
/**
|
|
31
|
+
* The path to serve interactive API docs at, or `false` to disable the
|
|
32
|
+
* docs page. Defaults to `/docs`.
|
|
33
|
+
*/
|
|
34
|
+
docs?: string | false;
|
|
35
|
+
/**
|
|
36
|
+
* The API title for the document's `info` section. Defaults to `"API"`.
|
|
37
|
+
*/
|
|
38
|
+
title?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The API version for the document's `info` section. Defaults to
|
|
41
|
+
* `"1.0.0"`.
|
|
42
|
+
*/
|
|
43
|
+
version?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Converts an endpoint's schemas into JSON Schema. Required for endpoints
|
|
46
|
+
* that declare schemas; endpoints without schemas are documented with
|
|
47
|
+
* their paths and methods only.
|
|
48
|
+
*/
|
|
49
|
+
toJsonSchema?: ToJsonSchema;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* OpenApi options with defaults applied. Created by the openApi plugin and
|
|
53
|
+
* consumed by the document builder.
|
|
54
|
+
*/
|
|
55
|
+
type ResolvedOpenApiOptions = {
|
|
56
|
+
path: string;
|
|
57
|
+
/** Undefined when the docs page is disabled */
|
|
58
|
+
docs: string | undefined;
|
|
59
|
+
title: string;
|
|
60
|
+
version: string;
|
|
61
|
+
toJsonSchema: ToJsonSchema | undefined;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* A ServerEndPoint shape, loosely typed so that endpoint modules loaded from
|
|
65
|
+
* anywhere can be passed to the document builder.
|
|
66
|
+
*/
|
|
67
|
+
type OpenApiEndPoint = {
|
|
68
|
+
schema?: Record<string, StandardSchemaV1 | undefined> | undefined;
|
|
69
|
+
[method: string]: unknown;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* A single API endpoint to include in an OpenAPI document: its route path
|
|
73
|
+
* (with `[param]` segments) and its default-exported endpoint object.
|
|
74
|
+
*/
|
|
75
|
+
type OpenApiRouteEntry = {
|
|
76
|
+
path: string;
|
|
77
|
+
endPoint: OpenApiEndPoint;
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/openapi/document.d.ts
|
|
81
|
+
/**
|
|
82
|
+
* Builds an OpenAPI 3.1 document from a list of +server endpoints.
|
|
83
|
+
*
|
|
84
|
+
* Each endpoint's route path (`/api/posts/[id]`) is converted to OpenAPI
|
|
85
|
+
* syntax (`/api/posts/{id}`); its `params` schema becomes path parameters,
|
|
86
|
+
* its `get`/`head` schema becomes query parameters, and its `post`/`patch`/
|
|
87
|
+
* `put`/`del`/`options` schema becomes a json request body. Responses are
|
|
88
|
+
* stubbed: a `200` for every handler, plus a `422` when the handler declares
|
|
89
|
+
* an input schema (validation errors are returned automatically).
|
|
90
|
+
*
|
|
91
|
+
* Throws if any endpoint declares a schema but the options have no
|
|
92
|
+
* `toJsonSchema` converter.
|
|
93
|
+
*/
|
|
94
|
+
declare function buildOpenApiDocument(entries: OpenApiRouteEntry[], options: OpenApiPluginOptions): Record<string, unknown>;
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/openapi/docsHtml.d.ts
|
|
97
|
+
/**
|
|
98
|
+
* A standalone HTML page loading Swagger UI from a CDN, pointed at the
|
|
99
|
+
* OpenAPI document endpoint.
|
|
100
|
+
*/
|
|
101
|
+
declare function openApiDocsHtml(docPath: string): string;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/openapi/plugin.d.ts
|
|
104
|
+
/**
|
|
105
|
+
* The `site.pluginState` key the openApi plugin stores its resolved options
|
|
106
|
+
* under. A registry symbol (`Symbol.for`), so that the user's config, the
|
|
107
|
+
* manifest plugin and the generated runtime code all resolve to the same
|
|
108
|
+
* key even when they are loaded as separate module instances.
|
|
109
|
+
*/
|
|
110
|
+
declare const OPEN_API_STATE_KEY: symbol;
|
|
111
|
+
/**
|
|
112
|
+
* Serves an OpenAPI 3.1 document for the site's `+server` endpoints, plus an
|
|
113
|
+
* optional interactive docs page.
|
|
114
|
+
*
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { Site } from "@torpor/build";
|
|
117
|
+
* import { openApi } from "@torpor/build/openapi";
|
|
118
|
+
* import { z } from "zod";
|
|
119
|
+
*
|
|
120
|
+
* const site = new Site();
|
|
121
|
+
* site.addRouteFolder("src/routes");
|
|
122
|
+
* site.plugins = [
|
|
123
|
+
* openApi({
|
|
124
|
+
* title: "My API",
|
|
125
|
+
* toJsonSchema: (s) => z.toJSONSchema(s as z.ZodType, { io: "input" }),
|
|
126
|
+
* }),
|
|
127
|
+
* ];
|
|
128
|
+
* export default site;
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* The document is served at `/openapi.json` (configurable with `path`) and
|
|
132
|
+
* the docs page at `/docs` (set `docs: false` to disable). Endpoints are
|
|
133
|
+
* picked up from every `+server.ts` route: its `params` schema becomes path
|
|
134
|
+
* parameters, `get`/`head` schemas become query parameters, and
|
|
135
|
+
* `post`/`patch`/`put`/`del`/`options` schemas become request bodies.
|
|
136
|
+
*
|
|
137
|
+
* The resolved options are stored in `site.pluginState` under
|
|
138
|
+
* `OPEN_API_STATE_KEY`, which the manifest plugin reads at build time to
|
|
139
|
+
* generate the document endpoint.
|
|
140
|
+
*/
|
|
141
|
+
declare function openApi(options?: OpenApiPluginOptions): SitePlugin;
|
|
142
|
+
//#endregion
|
|
143
|
+
export { type JsonSchema, OPEN_API_STATE_KEY, type OpenApiEndPoint, type OpenApiPluginOptions, type OpenApiRouteEntry, type ResolvedOpenApiOptions, type ToJsonSchema, buildOpenApiDocument, openApi, openApiDocsHtml };
|
|
144
|
+
//# sourceMappingURL=openapi.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapi.d.mts","names":[],"sources":["../src/openapi/types.ts","../src/openapi/document.ts","../src/openapi/docsHtml.ts","../src/openapi/plugin.ts"],"mappings":";;;;;;;KAMY,aAAa;;;;;;;;;;;;;KAcb,gBAAgB,QAAQ,qBAAqB;;;;KAK7C;;;;EAIX;;;;;EAKA;;;;EAIA;;;;;EAKA;;;;;;EAMA,eAAe;;;;;;KAOJ;EACX;;EAEA;EACA;EACA;EACA,cAAc;;;;;;KAOH;EACX,SAAS,eAAe;;;;;;;KAQb;EACX;EACA,UAAU;;;;;;;;;;;;;;;;;iBCvDa,qBACvB,SAAS,qBACT,SAAS,uBACP;;;;;;;iBCxBqB,gBAAgB;;;;;;;;;cCM3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgCG,QAAQ,UAAS,uBAA4B"}
|
package/dist/openapi.mjs
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//#region src/utils/pathTrie.ts
|
|
2
|
+
/**
|
|
3
|
+
* A simple trie router for path patterns.
|
|
4
|
+
*
|
|
5
|
+
* Pattern segments:
|
|
6
|
+
* - `posts` — static segment
|
|
7
|
+
* - `[id]` — matches one non-empty path segment, captured as `params.id`
|
|
8
|
+
* - `[...path]` — matches the rest of the path (at least one segment),
|
|
9
|
+
* captured as `params.path`
|
|
10
|
+
* - `*` — matches the rest of the path (possibly nothing), not captured
|
|
11
|
+
* - `*.css` — a segment with an embedded `*` matches one path segment
|
|
12
|
+
*
|
|
13
|
+
* `*` and `[...]` must be the last segment of a pattern. Matching priority at
|
|
14
|
+
* each level is static, then param, then glob, then rest. Trailing slashes are
|
|
15
|
+
* ignored, so `/posts` and `/posts/` match the same route.
|
|
16
|
+
*/
|
|
17
|
+
var PathTrie = class {
|
|
18
|
+
#root = new TrieNode();
|
|
19
|
+
/**
|
|
20
|
+
* Registers a value at a path pattern. The first registration of an exact
|
|
21
|
+
* pattern wins, matching the previous regex-scanning router.
|
|
22
|
+
*/
|
|
23
|
+
insert(pattern, value) {
|
|
24
|
+
let node = this.#root;
|
|
25
|
+
const segments = splitPath(pattern);
|
|
26
|
+
for (let i = 0; i < segments.length; i++) {
|
|
27
|
+
const segment = segments[i];
|
|
28
|
+
if (segment === "*") {
|
|
29
|
+
if (i !== segments.length - 1) throw new Error(`The '*' wildcard must be the last segment of a path: ${pattern}`);
|
|
30
|
+
node.rest ??= {
|
|
31
|
+
name: void 0,
|
|
32
|
+
allowEmpty: true,
|
|
33
|
+
child: new TrieNode()
|
|
34
|
+
};
|
|
35
|
+
node = node.rest.child;
|
|
36
|
+
} else if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
37
|
+
if (i !== segments.length - 1) throw new Error(`The '[...x]' splat must be the last segment of a path: ${pattern}`);
|
|
38
|
+
node.rest ??= {
|
|
39
|
+
name: segment.slice(4, -1),
|
|
40
|
+
allowEmpty: false,
|
|
41
|
+
child: new TrieNode()
|
|
42
|
+
};
|
|
43
|
+
node = node.rest.child;
|
|
44
|
+
} else if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
45
|
+
node.param ??= {
|
|
46
|
+
name: segment.slice(1, -1),
|
|
47
|
+
child: new TrieNode()
|
|
48
|
+
};
|
|
49
|
+
node = node.param.child;
|
|
50
|
+
} else if (segment.includes("*")) {
|
|
51
|
+
node.glob ??= {
|
|
52
|
+
regex: globToRegex(segment),
|
|
53
|
+
child: new TrieNode()
|
|
54
|
+
};
|
|
55
|
+
node = node.glob.child;
|
|
56
|
+
} else {
|
|
57
|
+
node.static ??= /* @__PURE__ */ new Map();
|
|
58
|
+
let child = node.static.get(segment);
|
|
59
|
+
if (!child) {
|
|
60
|
+
child = new TrieNode();
|
|
61
|
+
node.static.set(segment, child);
|
|
62
|
+
}
|
|
63
|
+
node = child;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
node.value ??= value;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Matches a request path against the trie. Returns the registered value
|
|
70
|
+
* and any captured params, or undefined when nothing matches.
|
|
71
|
+
*/
|
|
72
|
+
match(path) {
|
|
73
|
+
let trailingSlash = path === "/";
|
|
74
|
+
while (path.length > 1 && path.endsWith("/")) {
|
|
75
|
+
path = path.slice(0, -1);
|
|
76
|
+
trailingSlash = true;
|
|
77
|
+
}
|
|
78
|
+
const segments = path === "/" ? [] : splitPath(path);
|
|
79
|
+
return this.#matchNode(this.#root, segments, 0, {}, trailingSlash);
|
|
80
|
+
}
|
|
81
|
+
#matchNode(node, segments, i, params, trailingSlash) {
|
|
82
|
+
if (i === segments.length) {
|
|
83
|
+
if (node.value !== void 0) return {
|
|
84
|
+
value: node.value,
|
|
85
|
+
params: orUndefined(params)
|
|
86
|
+
};
|
|
87
|
+
const rest = node.rest;
|
|
88
|
+
if (rest?.allowEmpty && trailingSlash && rest.child.value !== void 0) return {
|
|
89
|
+
value: rest.child.value,
|
|
90
|
+
params: orUndefined(params)
|
|
91
|
+
};
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const segment = segments[i];
|
|
95
|
+
const stat = node.static?.get(segment);
|
|
96
|
+
if (stat) {
|
|
97
|
+
const match = this.#matchNode(stat, segments, i + 1, params, trailingSlash);
|
|
98
|
+
if (match) return match;
|
|
99
|
+
}
|
|
100
|
+
if (node.param && segment !== "") {
|
|
101
|
+
const match = this.#matchNode(node.param.child, segments, i + 1, {
|
|
102
|
+
...params,
|
|
103
|
+
[node.param.name]: segment
|
|
104
|
+
}, trailingSlash);
|
|
105
|
+
if (match) return match;
|
|
106
|
+
}
|
|
107
|
+
if (node.glob?.regex.test(segment)) {
|
|
108
|
+
const glob = node.glob;
|
|
109
|
+
const match = this.#matchNode(glob.child, segments, i + 1, params, trailingSlash);
|
|
110
|
+
if (match) return match;
|
|
111
|
+
}
|
|
112
|
+
const rest = node.rest;
|
|
113
|
+
if (rest?.child.value !== void 0) {
|
|
114
|
+
if (rest.name !== void 0) return {
|
|
115
|
+
value: rest.child.value,
|
|
116
|
+
params: orUndefined({
|
|
117
|
+
...params,
|
|
118
|
+
[rest.name]: segments.slice(i).join("/")
|
|
119
|
+
})
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
value: rest.child.value,
|
|
123
|
+
params: orUndefined(params)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var TrieNode = class {
|
|
129
|
+
value = void 0;
|
|
130
|
+
static;
|
|
131
|
+
param;
|
|
132
|
+
glob;
|
|
133
|
+
rest;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Splits a path or pattern into segments, dropping the leading empty segment
|
|
137
|
+
* and any trailing empty (from a trailing slash). `/posts/5` becomes
|
|
138
|
+
* `["posts", "5"]` and `/` becomes `[]`.
|
|
139
|
+
*/
|
|
140
|
+
function splitPath(path) {
|
|
141
|
+
const segments = path.split("/");
|
|
142
|
+
if (segments[0] === "") segments.shift();
|
|
143
|
+
if (segments.length && segments[segments.length - 1] === "") segments.pop();
|
|
144
|
+
return segments;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Compiles a pattern segment with an embedded `*` (e.g. `*.css`) into a
|
|
148
|
+
* regex that matches a single path segment.
|
|
149
|
+
*/
|
|
150
|
+
function globToRegex(segment) {
|
|
151
|
+
const source = segment.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*");
|
|
152
|
+
return new RegExp(`^${source}$`);
|
|
153
|
+
}
|
|
154
|
+
function orUndefined(params) {
|
|
155
|
+
return Object.keys(params).length ? params : void 0;
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { PathTrie as t };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=pathTrie-D4Ax2Mu9.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pathTrie-D4Ax2Mu9.mjs","names":["#root","#matchNode"],"sources":["../src/utils/pathTrie.ts"],"sourcesContent":["/**\n * A simple trie router for path patterns.\n *\n * Pattern segments:\n * - `posts` — static segment\n * - `[id]` — matches one non-empty path segment, captured as `params.id`\n * - `[...path]` — matches the rest of the path (at least one segment),\n * captured as `params.path`\n * - `*` — matches the rest of the path (possibly nothing), not captured\n * - `*.css` — a segment with an embedded `*` matches one path segment\n *\n * `*` and `[...]` must be the last segment of a pattern. Matching priority at\n * each level is static, then param, then glob, then rest. Trailing slashes are\n * ignored, so `/posts` and `/posts/` match the same route.\n */\nexport default class PathTrie<T> {\n\t#root = new TrieNode<T>();\n\n\t/**\n\t * Registers a value at a path pattern. The first registration of an exact\n\t * pattern wins, matching the previous regex-scanning router.\n\t */\n\tinsert(pattern: string, value: T): void {\n\t\tlet node = this.#root;\n\t\tconst segments = splitPath(pattern);\n\t\tfor (let i = 0; i < segments.length; i++) {\n\t\t\tconst segment = segments[i];\n\t\t\tif (segment === \"*\") {\n\t\t\t\tif (i !== segments.length - 1) {\n\t\t\t\t\tthrow new Error(`The '*' wildcard must be the last segment of a path: ${pattern}`);\n\t\t\t\t}\n\t\t\t\tnode.rest ??= { name: undefined, allowEmpty: true, child: new TrieNode() };\n\t\t\t\tnode = node.rest.child;\n\t\t\t} else if (segment.startsWith(\"[...\") && segment.endsWith(\"]\")) {\n\t\t\t\tif (i !== segments.length - 1) {\n\t\t\t\t\tthrow new Error(`The '[...x]' splat must be the last segment of a path: ${pattern}`);\n\t\t\t\t}\n\t\t\t\tnode.rest ??= {\n\t\t\t\t\tname: segment.slice(4, -1),\n\t\t\t\t\tallowEmpty: false,\n\t\t\t\t\tchild: new TrieNode(),\n\t\t\t\t};\n\t\t\t\tnode = node.rest.child;\n\t\t\t} else if (segment.startsWith(\"[\") && segment.endsWith(\"]\")) {\n\t\t\t\tnode.param ??= { name: segment.slice(1, -1), child: new TrieNode() };\n\t\t\t\tnode = node.param.child;\n\t\t\t} else if (segment.includes(\"*\")) {\n\t\t\t\tnode.glob ??= { regex: globToRegex(segment), child: new TrieNode() };\n\t\t\t\tnode = node.glob.child;\n\t\t\t} else {\n\t\t\t\tnode.static ??= new Map();\n\t\t\t\tlet child = node.static.get(segment);\n\t\t\t\tif (!child) {\n\t\t\t\t\tchild = new TrieNode();\n\t\t\t\t\tnode.static.set(segment, child);\n\t\t\t\t}\n\t\t\t\tnode = child;\n\t\t\t}\n\t\t}\n\t\tnode.value ??= value;\n\t}\n\n\t/**\n\t * Matches a request path against the trie. Returns the registered value\n\t * and any captured params, or undefined when nothing matches.\n\t */\n\tmatch(path: string): PathMatch<T> | undefined {\n\t\t// Trailing slashes are optional: `/posts/` matches `/posts`. Whether\n\t\t// there WAS a trailing separator still matters for a trailing bare\n\t\t// `*`, which requires one (e.g. `/assets/*` matches `/assets/` but\n\t\t// not `/assets`)\n\t\tlet trailingSlash = path === \"/\";\n\t\twhile (path.length > 1 && path.endsWith(\"/\")) {\n\t\t\tpath = path.slice(0, -1);\n\t\t\ttrailingSlash = true;\n\t\t}\n\t\tconst segments = path === \"/\" ? [] : splitPath(path);\n\t\treturn this.#matchNode(this.#root, segments, 0, {}, trailingSlash);\n\t}\n\n\t#matchNode(\n\t\tnode: TrieNode<T>,\n\t\tsegments: string[],\n\t\ti: number,\n\t\tparams: Record<string, string>,\n\t\ttrailingSlash: boolean,\n\t): PathMatch<T> | undefined {\n\t\tif (i === segments.length) {\n\t\t\tif (node.value !== undefined) {\n\t\t\t\treturn { value: node.value, params: orUndefined(params) };\n\t\t\t}\n\t\t\t// A trailing bare `*` also matches nothing, when the path ended\n\t\t\t// with its separator (e.g. `/assets/*` matches `/assets/`)\n\t\t\tconst rest = node.rest;\n\t\t\tif (rest?.allowEmpty && trailingSlash && rest.child.value !== undefined) {\n\t\t\t\treturn { value: rest.child.value, params: orUndefined(params) };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst segment = segments[i];\n\n\t\t// Static segments win over params\n\t\tconst stat = node.static?.get(segment);\n\t\tif (stat) {\n\t\t\tconst match = this.#matchNode(stat, segments, i + 1, params, trailingSlash);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\tif (node.param && segment !== \"\") {\n\t\t\tconst match = this.#matchNode(node.param.child, segments, i + 1, {\n\t\t\t\t...params,\n\t\t\t\t[node.param.name]: segment,\n\t\t\t}, trailingSlash);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\tif (node.glob?.regex.test(segment)) {\n\t\t\tconst glob = node.glob;\n\t\t\tconst match = this.#matchNode(glob.child, segments, i + 1, params, trailingSlash);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\t// The rest matcher consumes all remaining segments\n\t\tconst rest = node.rest;\n\t\tif (rest?.child.value !== undefined) {\n\t\t\t// Only build the captured value (and a new params object) when the\n\t\t\t// pattern actually names it — a bare `*` catch-all shouldn't pay\n\t\t\t// for a slice+join on every request\n\t\t\tif (rest.name !== undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tvalue: rest.child.value,\n\t\t\t\t\tparams: orUndefined({ ...params, [rest.name]: segments.slice(i).join(\"/\") }),\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { value: rest.child.value, params: orUndefined(params) };\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n\nexport interface PathMatch<T> {\n\tvalue: T;\n\tparams: Record<string, string> | undefined;\n}\n\nclass TrieNode<T> {\n\tvalue: T | undefined = undefined;\n\tstatic: Map<string, TrieNode<T>> | undefined;\n\tparam: { name: string; child: TrieNode<T> } | undefined;\n\tglob: { regex: RegExp; child: TrieNode<T> } | undefined;\n\trest: { name: string | undefined; allowEmpty: boolean; child: TrieNode<T> } | undefined;\n}\n\n/**\n * Splits a path or pattern into segments, dropping the leading empty segment\n * and any trailing empty (from a trailing slash). `/posts/5` becomes\n * `[\"posts\", \"5\"]` and `/` becomes `[]`.\n */\nfunction splitPath(path: string): string[] {\n\tconst segments = path.split(\"/\");\n\tif (segments[0] === \"\") segments.shift();\n\tif (segments.length && segments[segments.length - 1] === \"\") segments.pop();\n\treturn segments;\n}\n\n/**\n * Compiles a pattern segment with an embedded `*` (e.g. `*.css`) into a\n * regex that matches a single path segment.\n */\nfunction globToRegex(segment: string): RegExp {\n\tconst source = segment\n\t\t.split(\"*\")\n\t\t.map((part) => part.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"))\n\t\t.join(\".*\");\n\treturn new RegExp(`^${source}$`);\n}\n\nfunction orUndefined(params: Record<string, string>): Record<string, string> | undefined {\n\treturn Object.keys(params).length ? params : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAeA,IAAqB,WAArB,MAAiC;CAChC,QAAQ,IAAI,SAAY;;;;;CAMxB,OAAO,SAAiB,OAAgB;EACvC,IAAI,OAAO,KAAKA;EAChB,MAAM,WAAW,UAAU,OAAO;EAClC,KAAK,IAAI,IAAI,GAAG,IAAI,SAAS,QAAQ,KAAK;GACzC,MAAM,UAAU,SAAS;GACzB,IAAI,YAAY,KAAK;IACpB,IAAI,MAAM,SAAS,SAAS,GAC3B,MAAM,IAAI,M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vB;CACA;CACA;CACA;AACD;;;;;;AAOA,SAAS,UAAU,MAAwB;CAC1C,MAAM,WAAW,KAAK,MAAM,GAAG;CAC/B,IAAI,SAAS,OAAO,IAAI,SAAS,MAAM;CACvC,IAAI,SAAS,UAAU,SAAS,SAAS,SAAS,OAAO,IAAI,SAAS,IAAI;CAC1E,OAAO;AACR;;;;;AAMA,SAAS,YAAY,SAAyB;CAC7C,MAAM,SAAS,QACb,MAAM,GAAG,CAAC,CACV,KAAK,SAAS,KAAK,QAAQ,uBAAuB,MAAM,CAAC,CAAC,CAC1D,KAAK,IAAI;CACX,OAAO,IAAI,OAAO,IAAI,OAAO,EAAE;AAChC;AAEA,SAAS,YAAY,QAAoE;CACxF,OAAO,OAAO,KAAK,MAAM,CAAC,CAAC,SAAS,SAAS,KAAA;AAC9C"}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
//#region src/openapi/document.ts
|
|
2
|
+
const METHODS = [
|
|
3
|
+
"get",
|
|
4
|
+
"post",
|
|
5
|
+
"patch",
|
|
6
|
+
"put",
|
|
7
|
+
"del",
|
|
8
|
+
"options",
|
|
9
|
+
"head"
|
|
10
|
+
];
|
|
11
|
+
const QUERY_METHODS = ["get", "head"];
|
|
12
|
+
const BODY_METHODS = [
|
|
13
|
+
"post",
|
|
14
|
+
"patch",
|
|
15
|
+
"put",
|
|
16
|
+
"del",
|
|
17
|
+
"options"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Builds an OpenAPI 3.1 document from a list of +server endpoints.
|
|
21
|
+
*
|
|
22
|
+
* Each endpoint's route path (`/api/posts/[id]`) is converted to OpenAPI
|
|
23
|
+
* syntax (`/api/posts/{id}`); its `params` schema becomes path parameters,
|
|
24
|
+
* its `get`/`head` schema becomes query parameters, and its `post`/`patch`/
|
|
25
|
+
* `put`/`del`/`options` schema becomes a json request body. Responses are
|
|
26
|
+
* stubbed: a `200` for every handler, plus a `422` when the handler declares
|
|
27
|
+
* an input schema (validation errors are returned automatically).
|
|
28
|
+
*
|
|
29
|
+
* Throws if any endpoint declares a schema but the options have no
|
|
30
|
+
* `toJsonSchema` converter.
|
|
31
|
+
*/
|
|
32
|
+
function buildOpenApiDocument(entries, options) {
|
|
33
|
+
if (!options.toJsonSchema) {
|
|
34
|
+
const withSchema = entries.find(hasSchemas);
|
|
35
|
+
if (withSchema) throw new Error(`The endpoint at ${withSchema.path} declares schema(s), but the openApi() plugin was not given a "toJsonSchema" converter. Pass one, e.g. openApi({ toJsonSchema: (s) => z.toJSONSchema(s) })`);
|
|
36
|
+
}
|
|
37
|
+
const paths = {};
|
|
38
|
+
for (const entry of entries) {
|
|
39
|
+
const pathItem = buildPathItem(entry, options);
|
|
40
|
+
if (pathItem) paths[openApiPath(entry.path)] = pathItem;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
openapi: "3.1.0",
|
|
44
|
+
info: {
|
|
45
|
+
title: options.title ?? "API",
|
|
46
|
+
version: options.version ?? "1.0.0"
|
|
47
|
+
},
|
|
48
|
+
paths
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function hasSchemas(entry) {
|
|
52
|
+
const schema = entry.endPoint?.schema;
|
|
53
|
+
if (!schema) return false;
|
|
54
|
+
return METHODS.some((method) => schema[method]) || !!schema.params;
|
|
55
|
+
}
|
|
56
|
+
function buildPathItem(entry, options) {
|
|
57
|
+
const endPoint = entry.endPoint;
|
|
58
|
+
const pathItem = {};
|
|
59
|
+
let count = 0;
|
|
60
|
+
for (const method of METHODS) {
|
|
61
|
+
if (typeof endPoint[method] !== "function") continue;
|
|
62
|
+
count++;
|
|
63
|
+
const schema = endPoint.schema?.[method];
|
|
64
|
+
const paramsSchema = endPoint.schema?.params;
|
|
65
|
+
const parameters = [];
|
|
66
|
+
if (paramsSchema) parameters.push(...flattenObject(convert(paramsSchema, options), "path"));
|
|
67
|
+
if (schema && QUERY_METHODS.includes(method)) parameters.push(...flattenObject(convert(schema, options), "query"));
|
|
68
|
+
const responses = { "200": { description: "OK" } };
|
|
69
|
+
if (schema) responses["422"] = { description: "Validation failed" };
|
|
70
|
+
pathItem[method === "del" ? "delete" : method] = {
|
|
71
|
+
operationId: operationId(method, entry.path),
|
|
72
|
+
...parameters.length ? { parameters } : {},
|
|
73
|
+
...schema && BODY_METHODS.includes(method) ? { requestBody: {
|
|
74
|
+
required: true,
|
|
75
|
+
content: { "application/json": { schema: convert(schema, options) } }
|
|
76
|
+
} } : {},
|
|
77
|
+
responses
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return count > 0 ? pathItem : void 0;
|
|
81
|
+
}
|
|
82
|
+
function convert(schema, options) {
|
|
83
|
+
return options.toJsonSchema(schema);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Flattens a converted object schema into OpenAPI parameters. Path
|
|
87
|
+
* parameters are always required; query parameters take their `required`
|
|
88
|
+
* flag from the schema's `required` array.
|
|
89
|
+
*/
|
|
90
|
+
function flattenObject(jsonSchema, location) {
|
|
91
|
+
const properties = jsonSchema.properties;
|
|
92
|
+
if (!properties || typeof properties !== "object") return [];
|
|
93
|
+
const required = Array.isArray(jsonSchema.required) ? jsonSchema.required : [];
|
|
94
|
+
return Object.entries(properties).map(([name, propSchema]) => ({
|
|
95
|
+
name,
|
|
96
|
+
in: location,
|
|
97
|
+
required: location === "path" ? true : required.includes(name),
|
|
98
|
+
schema: propSchema
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Converts a route path to OpenAPI syntax: `/api/posts/[id]` becomes
|
|
103
|
+
* `/api/posts/{id}`.
|
|
104
|
+
*/
|
|
105
|
+
function openApiPath(routePath) {
|
|
106
|
+
return routePath.replace(/\[\.\.\.([^\]]+)\]/g, "{$1}").replace(/\[([^\]]+)\]/g, "{$1}");
|
|
107
|
+
}
|
|
108
|
+
function operationId(method, routePath) {
|
|
109
|
+
const segments = openApiPath(routePath).split(/[^a-zA-Z0-9]+/).filter((s) => s.length > 0).map((s) => s.charAt(0).toUpperCase() + s.slice(1));
|
|
110
|
+
return (method === "del" ? "delete" : method) + (segments.join("") || "Root");
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/openapi/docsHtml.ts
|
|
114
|
+
/**
|
|
115
|
+
* A standalone HTML page loading Swagger UI from a CDN, pointed at the
|
|
116
|
+
* OpenAPI document endpoint.
|
|
117
|
+
*/
|
|
118
|
+
function openApiDocsHtml(docPath) {
|
|
119
|
+
return `<!doctype html>
|
|
120
|
+
<html lang="en">
|
|
121
|
+
<head>
|
|
122
|
+
<meta charset="utf-8" />
|
|
123
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
124
|
+
<title>API Docs</title>
|
|
125
|
+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
|
|
126
|
+
</head>
|
|
127
|
+
<body>
|
|
128
|
+
<div id="swagger-ui"></div>
|
|
129
|
+
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js" crossorigin><\/script>
|
|
130
|
+
<script>
|
|
131
|
+
window.addEventListener("DOMContentLoaded", () => {
|
|
132
|
+
SwaggerUIBundle({ url: ${JSON.stringify(docPath)}, dom_id: "#swagger-ui" });
|
|
133
|
+
});
|
|
134
|
+
<\/script>
|
|
135
|
+
</body>
|
|
136
|
+
</html>
|
|
137
|
+
`;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/openapi/plugin.ts
|
|
141
|
+
/**
|
|
142
|
+
* The `site.pluginState` key the openApi plugin stores its resolved options
|
|
143
|
+
* under. A registry symbol (`Symbol.for`), so that the user's config, the
|
|
144
|
+
* manifest plugin and the generated runtime code all resolve to the same
|
|
145
|
+
* key even when they are loaded as separate module instances.
|
|
146
|
+
*/
|
|
147
|
+
const OPEN_API_STATE_KEY = Symbol.for("@torpor/build/openapi");
|
|
148
|
+
/**
|
|
149
|
+
* Serves an OpenAPI 3.1 document for the site's `+server` endpoints, plus an
|
|
150
|
+
* optional interactive docs page.
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { Site } from "@torpor/build";
|
|
154
|
+
* import { openApi } from "@torpor/build/openapi";
|
|
155
|
+
* import { z } from "zod";
|
|
156
|
+
*
|
|
157
|
+
* const site = new Site();
|
|
158
|
+
* site.addRouteFolder("src/routes");
|
|
159
|
+
* site.plugins = [
|
|
160
|
+
* openApi({
|
|
161
|
+
* title: "My API",
|
|
162
|
+
* toJsonSchema: (s) => z.toJSONSchema(s as z.ZodType, { io: "input" }),
|
|
163
|
+
* }),
|
|
164
|
+
* ];
|
|
165
|
+
* export default site;
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* The document is served at `/openapi.json` (configurable with `path`) and
|
|
169
|
+
* the docs page at `/docs` (set `docs: false` to disable). Endpoints are
|
|
170
|
+
* picked up from every `+server.ts` route: its `params` schema becomes path
|
|
171
|
+
* parameters, `get`/`head` schemas become query parameters, and
|
|
172
|
+
* `post`/`patch`/`put`/`del`/`options` schemas become request bodies.
|
|
173
|
+
*
|
|
174
|
+
* The resolved options are stored in `site.pluginState` under
|
|
175
|
+
* `OPEN_API_STATE_KEY`, which the manifest plugin reads at build time to
|
|
176
|
+
* generate the document endpoint.
|
|
177
|
+
*/
|
|
178
|
+
function openApi(options = {}) {
|
|
179
|
+
return (site) => {
|
|
180
|
+
const docPath = withLeadingSlash(options.path ?? "/openapi.json");
|
|
181
|
+
if (site.routes.some((route) => route.path === docPath)) throw new Error(`There is already a route registered at ${docPath}, so the OpenAPI document can't be served there. Pass a different "path" to the openApi() plugin`);
|
|
182
|
+
const resolved = {
|
|
183
|
+
path: docPath,
|
|
184
|
+
docs: options.docs === false ? void 0 : withLeadingSlash(options.docs ?? "/docs"),
|
|
185
|
+
title: options.title ?? "API",
|
|
186
|
+
version: options.version ?? "1.0.0",
|
|
187
|
+
toJsonSchema: options.toJsonSchema
|
|
188
|
+
};
|
|
189
|
+
site.pluginState.set(OPEN_API_STATE_KEY, resolved);
|
|
190
|
+
if (resolved.docs) {
|
|
191
|
+
const html = openApiDocsHtml(docPath);
|
|
192
|
+
site.addRoute(resolved.docs, { server: { get: () => new Response(html, { headers: { "Content-Type": "text/html; charset=utf-8" } }) } });
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function withLeadingSlash(pathName) {
|
|
197
|
+
return pathName.startsWith("/") ? pathName : "/" + pathName;
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
export { buildOpenApiDocument as i, openApi as n, openApiDocsHtml as r, OPEN_API_STATE_KEY as t };
|
|
201
|
+
|
|
202
|
+
//# sourceMappingURL=plugin-D8Www11B.mjs.map
|