@pracht/core 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pracht/core",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/JoviDeCroock/pracht/tree/main/packages/framework",
6
6
  "bugs": {
@@ -1,241 +0,0 @@
1
- //#region \0rolldown/runtime.js
2
- var __defProp = Object.defineProperty;
3
- var __exportAll = (all, no_symbols) => {
4
- let target = {};
5
- for (var name in all) __defProp(target, name, {
6
- get: all[name],
7
- enumerable: true
8
- });
9
- if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
- return target;
11
- };
12
- //#endregion
13
- //#region src/app.ts
14
- var app_exports = /* @__PURE__ */ __exportAll({
15
- buildPathFromSegments: () => buildPathFromSegments,
16
- defineApp: () => defineApp,
17
- group: () => group,
18
- matchApiRoute: () => matchApiRoute,
19
- matchAppRoute: () => matchAppRoute,
20
- resolveApiRoutes: () => resolveApiRoutes,
21
- resolveApp: () => resolveApp,
22
- route: () => route,
23
- timeRevalidate: () => timeRevalidate
24
- });
25
- function timeRevalidate(seconds) {
26
- if (!Number.isInteger(seconds) || seconds <= 0) throw new Error("timeRevalidate expects a positive integer number of seconds.");
27
- return {
28
- kind: "time",
29
- seconds
30
- };
31
- }
32
- function route(path, fileOrConfig, meta = {}) {
33
- if (typeof fileOrConfig === "string" || typeof fileOrConfig === "function") return {
34
- kind: "route",
35
- path: normalizeRoutePath(path),
36
- file: resolveModuleRef(fileOrConfig),
37
- ...meta
38
- };
39
- const { component, loader, ...routeMeta } = fileOrConfig;
40
- return {
41
- kind: "route",
42
- path: normalizeRoutePath(path),
43
- file: resolveModuleRef(component),
44
- loaderFile: resolveModuleRef(loader),
45
- ...routeMeta
46
- };
47
- }
48
- function resolveModuleRef(ref) {
49
- if (ref === void 0) return void 0;
50
- if (typeof ref === "string") return ref;
51
- return "";
52
- }
53
- function group(meta, routes) {
54
- return {
55
- kind: "group",
56
- meta,
57
- routes
58
- };
59
- }
60
- function defineApp(config) {
61
- return {
62
- shells: resolveModuleRefRecord(config.shells ?? {}),
63
- middleware: resolveModuleRefRecord(config.middleware ?? {}),
64
- api: config.api ?? {},
65
- routes: config.routes
66
- };
67
- }
68
- function resolveModuleRefRecord(record) {
69
- const result = {};
70
- for (const [key, value] of Object.entries(record)) result[key] = resolveModuleRef(value);
71
- return result;
72
- }
73
- function resolveApp(app) {
74
- const routes = [];
75
- const inherited = {
76
- pathPrefix: "/",
77
- middleware: []
78
- };
79
- for (const node of app.routes) flattenRouteNode(app, node, inherited, routes);
80
- return {
81
- shells: app.shells,
82
- middleware: app.middleware,
83
- api: app.api,
84
- routes,
85
- apiRoutes: []
86
- };
87
- }
88
- function matchAppRoute(app, pathname) {
89
- const resolved = isResolvedApp(app) ? app : resolveApp(app);
90
- const normalizedPathname = normalizeRoutePath(pathname);
91
- const targetSegments = splitPathSegments(normalizedPathname);
92
- for (const currentRoute of resolved.routes) {
93
- const params = matchRouteSegments(currentRoute.segments, targetSegments);
94
- if (params) return {
95
- route: currentRoute,
96
- params,
97
- pathname: normalizedPathname
98
- };
99
- }
100
- }
101
- function flattenRouteNode(app, node, inherited, routes) {
102
- if (node.kind === "group") {
103
- const nextInherited = {
104
- pathPrefix: mergeRoutePaths(inherited.pathPrefix, node.meta.pathPrefix),
105
- shell: node.meta.shell ?? inherited.shell,
106
- render: node.meta.render ?? inherited.render,
107
- middleware: [...inherited.middleware, ...node.meta.middleware ?? []]
108
- };
109
- for (const child of node.routes) flattenRouteNode(app, child, nextInherited, routes);
110
- return;
111
- }
112
- const fullPath = mergeRoutePaths(inherited.pathPrefix, node.path);
113
- const shell = node.shell ?? inherited.shell;
114
- const middleware = [...inherited.middleware, ...node.middleware ?? []];
115
- routes.push({
116
- id: node.id ?? createRouteId(fullPath),
117
- path: fullPath,
118
- file: node.file,
119
- loaderFile: node.loaderFile,
120
- shell,
121
- shellFile: shell ? app.shells[shell] : void 0,
122
- render: node.render ?? inherited.render,
123
- middleware,
124
- middlewareFiles: middleware.flatMap((name) => {
125
- const middlewareFile = app.middleware[name];
126
- return middlewareFile ? [middlewareFile] : [];
127
- }),
128
- revalidate: node.revalidate,
129
- segments: parseRouteSegments(fullPath)
130
- });
131
- }
132
- function isResolvedApp(app) {
133
- return app.routes.length === 0 || "segments" in app.routes[0];
134
- }
135
- function matchRouteSegments(routeSegments, targetSegments) {
136
- const params = {};
137
- let routeIndex = 0;
138
- let targetIndex = 0;
139
- while (routeIndex < routeSegments.length) {
140
- const currentSegment = routeSegments[routeIndex];
141
- if (currentSegment.type === "catchall") {
142
- params[currentSegment.name] = targetSegments.slice(targetIndex).join("/");
143
- return params;
144
- }
145
- const targetSegment = targetSegments[targetIndex];
146
- if (typeof targetSegment === "undefined") return null;
147
- if (currentSegment.type === "static") {
148
- if (currentSegment.value !== targetSegment) return null;
149
- } else try {
150
- params[currentSegment.name] = decodeURIComponent(targetSegment);
151
- } catch {
152
- return null;
153
- }
154
- routeIndex += 1;
155
- targetIndex += 1;
156
- }
157
- return targetIndex === targetSegments.length ? params : null;
158
- }
159
- function parseRouteSegments(path) {
160
- return splitPathSegments(path).map((segment) => {
161
- if (segment === "*") return {
162
- type: "catchall",
163
- name: "*"
164
- };
165
- if (segment.startsWith(":")) return {
166
- type: "param",
167
- name: segment.slice(1)
168
- };
169
- return {
170
- type: "static",
171
- value: segment
172
- };
173
- });
174
- }
175
- function splitPathSegments(path) {
176
- return normalizeRoutePath(path).split("/").filter(Boolean);
177
- }
178
- function mergeRoutePaths(prefix, path) {
179
- if (!path) return normalizeRoutePath(prefix);
180
- const normalizedPrefix = normalizeRoutePath(prefix);
181
- const normalizedPath = normalizeRoutePath(path);
182
- if (normalizedPrefix === "/") return normalizedPath;
183
- if (normalizedPath === "/") return normalizedPrefix;
184
- return normalizeRoutePath(`${normalizedPrefix}/${normalizedPath.slice(1)}`);
185
- }
186
- function normalizeRoutePath(path) {
187
- if (!path || path === "/") return "/";
188
- const collapsed = (path.startsWith("/") ? path : `/${path}`).replace(/\/{2,}/g, "/");
189
- return collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
190
- }
191
- function buildPathFromSegments(segments, params) {
192
- return normalizeRoutePath("/" + segments.map((segment) => {
193
- if (segment.type === "static") return segment.value;
194
- if (segment.type === "param") return encodeURIComponent(params[segment.name] ?? "");
195
- return params["*"] ?? "";
196
- }).join("/"));
197
- }
198
- /**
199
- * Convert a list of file paths from `import.meta.glob` into resolved API routes.
200
- *
201
- * Example: `"/src/api/health.ts"` → path `/api/health`
202
- * `"/src/api/users/[id].ts"` → path `/api/users/:id`
203
- * `"/src/api/index.ts"` → path `/api`
204
- */
205
- function resolveApiRoutes(files, apiDir = "/src/api") {
206
- const normalizedDir = apiDir.replace(/\/$/, "");
207
- return files.map((file) => {
208
- let relative = file;
209
- if (relative.startsWith(normalizedDir)) relative = relative.slice(normalizedDir.length);
210
- relative = relative.replace(/\.(ts|tsx|js|jsx)$/, "");
211
- if (relative.endsWith("/index")) relative = relative.slice(0, -6) || "/";
212
- relative = relative.replace(/\[([^\]]+)\]/g, ":$1");
213
- const path = normalizeRoutePath(`/api${relative}`);
214
- return {
215
- path,
216
- file,
217
- segments: parseRouteSegments(path)
218
- };
219
- });
220
- }
221
- function matchApiRoute(apiRoutes, pathname) {
222
- const normalizedPathname = normalizeRoutePath(pathname);
223
- const targetSegments = splitPathSegments(normalizedPathname);
224
- for (const route of apiRoutes) {
225
- const params = matchRouteSegments(route.segments, targetSegments);
226
- if (params) return {
227
- route,
228
- params,
229
- pathname: normalizedPathname
230
- };
231
- }
232
- }
233
- function createRouteId(path) {
234
- if (path === "/") return "index";
235
- return path.slice(1).split("/").map((segment) => {
236
- if (segment === "*") return "splat";
237
- return segment.startsWith(":") ? segment.slice(1) : segment;
238
- }).join("-").replace(/[^a-zA-Z0-9-]/g, "-");
239
- }
240
- //#endregion
241
- export { matchApiRoute as a, resolveApp as c, group as i, route as l, buildPathFromSegments as n, matchAppRoute as o, defineApp as r, resolveApiRoutes as s, app_exports as t, timeRevalidate as u };