@marko/run 0.0.1-beta7 → 0.0.1-beta9

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.
@@ -28,9 +28,17 @@ __export(internal_exports, {
28
28
  noContent: () => noContent,
29
29
  normalize: () => normalize,
30
30
  notHandled: () => notHandled,
31
- notMatched: () => notMatched
31
+ notMatched: () => notMatched,
32
+ pageResponse: () => pageResponse
32
33
  });
33
34
  module.exports = __toCommonJS(internal_exports);
35
+ var pageResponseInit = {
36
+ status: 200,
37
+ headers: { "content-type": "text/html;charset=UTF-8" }
38
+ };
39
+ function pageResponse(template, input) {
40
+ return new Response(template.stream(input), pageResponseInit);
41
+ }
34
42
  var NotHandled = Symbol();
35
43
  var NotMatched = Symbol();
36
44
  globalThis.MarkoRun ?? (globalThis.MarkoRun = {
@@ -147,5 +155,6 @@ function notMatched() {
147
155
  noContent,
148
156
  normalize,
149
157
  notHandled,
150
- notMatched
158
+ notMatched,
159
+ pageResponse
151
160
  });
@@ -1,8 +1,9 @@
1
- import type { InputObject, NextFunction, Route, RouteContext, RouteHandler } from "./types";
2
- export declare const NotHandled: unique symbol;
3
- export declare const NotMatched: unique symbol;
4
- export declare function createInput(context: RouteContext): (data: InputObject) => InputObject;
5
- export declare function call(handler: RouteHandler<Route>, next: NextFunction, context: RouteContext): Promise<Response>;
1
+ import type { InputObject, NextFunction, Route, Context, RouteHandler } from "./types";
2
+ export declare function pageResponse(template: any, input: Record<PropertyKey, unknown>): Response;
3
+ export declare const NotHandled: typeof MarkoRun.NotHandled;
4
+ export declare const NotMatched: typeof MarkoRun.NotMatched;
5
+ export declare function createInput(context: Context): (data: InputObject) => InputObject;
6
+ export declare function call(handler: RouteHandler<Route>, next: NextFunction, context: Context): Promise<Response>;
6
7
  export declare function compose(handlers: RouteHandler[]): RouteHandler;
7
8
  export declare function normalize(obj: RouteHandler | RouteHandler[] | Promise<RouteHandler | RouteHandler[]>): RouteHandler;
8
9
  export declare function noContent(): Response;
@@ -1,4 +1,11 @@
1
1
  // src/runtime/internal.ts
2
+ var pageResponseInit = {
3
+ status: 200,
4
+ headers: { "content-type": "text/html;charset=UTF-8" }
5
+ };
6
+ function pageResponse(template, input) {
7
+ return new Response(template.stream(input), pageResponseInit);
8
+ }
2
9
  var NotHandled = Symbol();
3
10
  var NotMatched = Symbol();
4
11
  globalThis.MarkoRun ?? (globalThis.MarkoRun = {
@@ -114,5 +121,6 @@ export {
114
121
  noContent,
115
122
  normalize,
116
123
  notHandled,
117
- notMatched
124
+ notMatched,
125
+ pageResponse
118
126
  };
@@ -3,11 +3,11 @@ declare type OneOrMany<T> = T | T[];
3
3
  declare type Combine<T> = T extends object ? {
4
4
  [P in keyof T]: T[P];
5
5
  } : T;
6
- export interface RouteContextExtensions {
6
+ export interface ContextExtensions {
7
7
  }
8
8
  export declare type ParamsObject = Record<string, string>;
9
9
  export declare type InputObject = Record<PropertyKey, any>;
10
- export declare type RouteContext<Platform = unknown, TRoute extends Route = Route> = TRoute extends any ? Combine<RouteContextExtensions & Readonly<{
10
+ export declare type Context<Platform = unknown, TRoute extends Route = Route> = TRoute extends any ? Combine<ContextExtensions & Readonly<{
11
11
  url: URL;
12
12
  request: Request;
13
13
  route: TRoute["path"];
@@ -17,7 +17,7 @@ export declare type RouteContext<Platform = unknown, TRoute extends Route = Rout
17
17
  }>> : never;
18
18
  export declare type NextFunction = () => Awaitable<Response>;
19
19
  export declare type HandlerLike<TRoute extends Route = Route> = Awaitable<OneOrMany<RouteHandler<TRoute>>>;
20
- export declare type RouteHandler<TRoute extends Route = Route> = (context: RouteContext<unknown, TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
20
+ export declare type RouteHandler<TRoute extends Route = Route> = (context: Context<unknown, TRoute>, next: NextFunction) => Awaitable<Response | null | void>;
21
21
  export interface Route<Params extends ParamsObject = {}, Meta = unknown, Path extends string = string> {
22
22
  path: Path;
23
23
  params: Params;
@@ -537,6 +537,9 @@ function renderRouteEntry(route) {
537
537
  if (!page || verbs.length > 1) {
538
538
  runtimeImports.push("noContent");
539
539
  }
540
+ if (page) {
541
+ runtimeImports.push("pageResponse");
542
+ }
540
543
  if (runtimeImports.length) {
541
544
  imports.writeLines(
542
545
  `import { ${runtimeImports.join(", ")} } from '${virtualRuntimePrefix}';`
@@ -578,10 +581,8 @@ function renderRouteEntry(route) {
578
581
  return writer.end();
579
582
  }
580
583
  function writePageResponse(writer, wrapFn) {
581
- writer.writeBlock(
582
- `${wrapFn ? `const ${wrapFn} = () =>` : `return`} new Response(page.stream(buildInput()), {`,
583
- ["status: 200,", 'headers: { "content-type": "text/html;charset=UTF-8" }'],
584
- "});"
584
+ writer.writeLines(
585
+ `${wrapFn ? `const ${wrapFn} = () =>` : `return`} pageResponse(page, buildInput());`
585
586
  );
586
587
  }
587
588
  function writeMiddleware(writer, middleware, next, wrapFn) {
@@ -703,8 +704,8 @@ export async function invoke(route, request, platform, url = new URL(request.url
703
704
  request,
704
705
  platform
705
706
  };
707
+ const buildInput = createInput(context);
706
708
  try {
707
- const buildInput = createInput(context);
708
709
  if (route) {
709
710
  context.params = route.params;
710
711
  context.meta = route.meta;
@@ -788,24 +789,12 @@ export async function fetch(request, platform) {
788
789
  const route = match(request.method, pathname);
789
790
  return await invoke(route, request, platform, url);
790
791
  } catch (error) {
791
- const message = import.meta.env.DEV
792
- ? \`Internal Server Error (\${error.message})\`
793
- : "Internal Server Error";
794
-
795
- return new Response(
796
- JSON.stringify({
797
- error: {
798
- message,
799
- stack: import.meta.env.DEV
800
- ? \`This will only be seen in development mode\\n\\n\${error.stack}\`
801
- : ""
802
- }
803
- }),
804
- {
805
- statusText: message,
806
- status: 500,
807
- }
808
- );
792
+ const body = import.meta.env.DEV
793
+ ? error.stack || error.message || "Internal Server Error"
794
+ : null;
795
+ return new Response(body, {
796
+ status: 500
797
+ });
809
798
  }
810
799
  }`);
811
800
  return writer.end();
@@ -1004,7 +993,7 @@ function renderRouteTypeInfo(routes, pathPrefix = ".", adapterTypes = "") {
1004
993
  Do NOT manually edit this file or your changes will be lost.
1005
994
  */
1006
995
  `,
1007
- `import type { HandlerLike, Route, RouteContext, ValidatePath, ValidateHref } from "@marko/run";`,
996
+ `import type { HandlerLike, Route as AnyRoute, Context as AnyContext, ValidatePath, ValidateHref } from "@marko/run";`,
1008
997
  adapterTypes,
1009
998
  `
1010
999
 
@@ -1075,8 +1064,8 @@ declare module '${pathPrefix}/${page.relativePath}' {
1075
1064
  export interface Input {}
1076
1065
 
1077
1066
  namespace MarkoRun {
1078
- type CurrentRoute = ${routeType};
1079
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1067
+ type Route = ${routeType};
1068
+ type Context = AnyContext<AnyContext['platform'], Route>;
1080
1069
  }
1081
1070
  }`);
1082
1071
  }
@@ -1108,7 +1097,7 @@ declare module '${pathPrefix}/${page.relativePath}' {
1108
1097
  }
1109
1098
  }
1110
1099
  routesWriter.writeLines(
1111
- `interface ${routeType} extends Route<${paramsType}, ${metaType}, ${pathType}> {}`
1100
+ `interface ${routeType} extends AnyRoute<${paramsType}, ${metaType}, ${pathType}> {}`
1112
1101
  );
1113
1102
  }
1114
1103
  pathsWriter.write(" type GetPaths =");
@@ -1140,8 +1129,8 @@ declare module '${pathPrefix}/${file.relativePath}' {
1140
1129
  }
1141
1130
 
1142
1131
  namespace MarkoRun {
1143
- type CurrentRoute = ${routeTypes.join(" | ")};
1144
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1132
+ type Route = ${routeTypes.join(" | ")};
1133
+ type Context = AnyContext<AnyContext['platform'], Route>;
1145
1134
  }
1146
1135
  }`);
1147
1136
  }
@@ -1158,8 +1147,8 @@ declare module '${pathPrefix}/${route.page.relativePath}' {
1158
1147
  writer.writeLines(`}
1159
1148
 
1160
1149
  namespace MarkoRun {
1161
- type CurrentRoute = Route;
1162
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1150
+ type Route = AnyRoute;
1151
+ type Context = AnyContext<AnyContext['platform'], Route>;
1163
1152
  }
1164
1153
  }`);
1165
1154
  }
@@ -1171,11 +1160,11 @@ function writeRouteTypeModule(writer, pathPrefix, path3, routeType) {
1171
1160
  writer.writeLines(`
1172
1161
  declare module '${pathPrefix}/${stripTsExtension(path3)}' {
1173
1162
  namespace MarkoRun {
1174
- type CurrentRoute = ${routeType};
1175
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1176
- type Handler<_Params = CurrentRoute['params'], _Meta = CurrentRoute['meta']> = HandlerLike<CurrentRoute>;
1163
+ type Route = ${routeType};
1164
+ type Context = AnyContext<AnyContext['platform'], Route>;
1165
+ type Handler<_Params = Route['params'], _Meta = Route['meta']> = HandlerLike<Route>;
1177
1166
  function route(handler: Handler): typeof handler;
1178
- function route<_Params = CurrentRoute['params'], _Meta = CurrentRoute['meta']>(handler: Handler): typeof handler;
1167
+ function route<_Params = Route['params'], _Meta = Route['meta']>(handler: Handler): typeof handler;
1179
1168
  }
1180
1169
  }`);
1181
1170
  }
@@ -500,6 +500,9 @@ function renderRouteEntry(route) {
500
500
  if (!page || verbs.length > 1) {
501
501
  runtimeImports.push("noContent");
502
502
  }
503
+ if (page) {
504
+ runtimeImports.push("pageResponse");
505
+ }
503
506
  if (runtimeImports.length) {
504
507
  imports.writeLines(
505
508
  `import { ${runtimeImports.join(", ")} } from '${virtualRuntimePrefix}';`
@@ -541,10 +544,8 @@ function renderRouteEntry(route) {
541
544
  return writer.end();
542
545
  }
543
546
  function writePageResponse(writer, wrapFn) {
544
- writer.writeBlock(
545
- `${wrapFn ? `const ${wrapFn} = () =>` : `return`} new Response(page.stream(buildInput()), {`,
546
- ["status: 200,", 'headers: { "content-type": "text/html;charset=UTF-8" }'],
547
- "});"
547
+ writer.writeLines(
548
+ `${wrapFn ? `const ${wrapFn} = () =>` : `return`} pageResponse(page, buildInput());`
548
549
  );
549
550
  }
550
551
  function writeMiddleware(writer, middleware, next, wrapFn) {
@@ -666,8 +667,8 @@ export async function invoke(route, request, platform, url = new URL(request.url
666
667
  request,
667
668
  platform
668
669
  };
670
+ const buildInput = createInput(context);
669
671
  try {
670
- const buildInput = createInput(context);
671
672
  if (route) {
672
673
  context.params = route.params;
673
674
  context.meta = route.meta;
@@ -751,24 +752,12 @@ export async function fetch(request, platform) {
751
752
  const route = match(request.method, pathname);
752
753
  return await invoke(route, request, platform, url);
753
754
  } catch (error) {
754
- const message = import.meta.env.DEV
755
- ? \`Internal Server Error (\${error.message})\`
756
- : "Internal Server Error";
757
-
758
- return new Response(
759
- JSON.stringify({
760
- error: {
761
- message,
762
- stack: import.meta.env.DEV
763
- ? \`This will only be seen in development mode\\n\\n\${error.stack}\`
764
- : ""
765
- }
766
- }),
767
- {
768
- statusText: message,
769
- status: 500,
770
- }
771
- );
755
+ const body = import.meta.env.DEV
756
+ ? error.stack || error.message || "Internal Server Error"
757
+ : null;
758
+ return new Response(body, {
759
+ status: 500
760
+ });
772
761
  }
773
762
  }`);
774
763
  return writer.end();
@@ -967,7 +956,7 @@ function renderRouteTypeInfo(routes, pathPrefix = ".", adapterTypes = "") {
967
956
  Do NOT manually edit this file or your changes will be lost.
968
957
  */
969
958
  `,
970
- `import type { HandlerLike, Route, RouteContext, ValidatePath, ValidateHref } from "@marko/run";`,
959
+ `import type { HandlerLike, Route as AnyRoute, Context as AnyContext, ValidatePath, ValidateHref } from "@marko/run";`,
971
960
  adapterTypes,
972
961
  `
973
962
 
@@ -1038,8 +1027,8 @@ declare module '${pathPrefix}/${page.relativePath}' {
1038
1027
  export interface Input {}
1039
1028
 
1040
1029
  namespace MarkoRun {
1041
- type CurrentRoute = ${routeType};
1042
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1030
+ type Route = ${routeType};
1031
+ type Context = AnyContext<AnyContext['platform'], Route>;
1043
1032
  }
1044
1033
  }`);
1045
1034
  }
@@ -1071,7 +1060,7 @@ declare module '${pathPrefix}/${page.relativePath}' {
1071
1060
  }
1072
1061
  }
1073
1062
  routesWriter.writeLines(
1074
- `interface ${routeType} extends Route<${paramsType}, ${metaType}, ${pathType}> {}`
1063
+ `interface ${routeType} extends AnyRoute<${paramsType}, ${metaType}, ${pathType}> {}`
1075
1064
  );
1076
1065
  }
1077
1066
  pathsWriter.write(" type GetPaths =");
@@ -1103,8 +1092,8 @@ declare module '${pathPrefix}/${file.relativePath}' {
1103
1092
  }
1104
1093
 
1105
1094
  namespace MarkoRun {
1106
- type CurrentRoute = ${routeTypes.join(" | ")};
1107
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1095
+ type Route = ${routeTypes.join(" | ")};
1096
+ type Context = AnyContext<AnyContext['platform'], Route>;
1108
1097
  }
1109
1098
  }`);
1110
1099
  }
@@ -1121,8 +1110,8 @@ declare module '${pathPrefix}/${route.page.relativePath}' {
1121
1110
  writer.writeLines(`}
1122
1111
 
1123
1112
  namespace MarkoRun {
1124
- type CurrentRoute = Route;
1125
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1113
+ type Route = AnyRoute;
1114
+ type Context = AnyContext<AnyContext['platform'], Route>;
1126
1115
  }
1127
1116
  }`);
1128
1117
  }
@@ -1134,11 +1123,11 @@ function writeRouteTypeModule(writer, pathPrefix, path3, routeType) {
1134
1123
  writer.writeLines(`
1135
1124
  declare module '${pathPrefix}/${stripTsExtension(path3)}' {
1136
1125
  namespace MarkoRun {
1137
- type CurrentRoute = ${routeType};
1138
- type CurrentContext = RouteContext<RouteContext['platform'], CurrentRoute>;
1139
- type Handler<_Params = CurrentRoute['params'], _Meta = CurrentRoute['meta']> = HandlerLike<CurrentRoute>;
1126
+ type Route = ${routeType};
1127
+ type Context = AnyContext<AnyContext['platform'], Route>;
1128
+ type Handler<_Params = Route['params'], _Meta = Route['meta']> = HandlerLike<Route>;
1140
1129
  function route(handler: Handler): typeof handler;
1141
- function route<_Params = CurrentRoute['params'], _Meta = CurrentRoute['meta']>(handler: Handler): typeof handler;
1130
+ function route<_Params = Route['params'], _Meta = Route['meta']>(handler: Handler): typeof handler;
1142
1131
  }
1143
1132
  }`);
1144
1133
  }
package/package.json CHANGED
@@ -1,10 +1,20 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.0.1-beta7",
4
- "description": "File-based routing for Marko based on Vite",
5
- "keywords": [],
6
- "author": "Ryan Turnquist <rturnq@gmail.com>",
3
+ "version": "0.0.1-beta9",
4
+ "description": "The Marko application framework.",
7
5
  "license": "MIT",
6
+ "homepage": "https://github.com/marko-js/run/tree/main/packages/run",
7
+ "logo": {
8
+ "url": "https://github.com/marko-js/run/raw/main/assets/marko-run.png"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/marko-js/run.git"
13
+ },
14
+ "author": "Ryan Turnquist <rturnq@gmail.com>",
15
+ "keywords": [
16
+ "marko"
17
+ ],
8
18
  "type": "module",
9
19
  "bin": {
10
20
  "marko-run": "./dist/cli/index.mjs"
@@ -69,11 +79,12 @@
69
79
  "dist"
70
80
  ],
71
81
  "peerDependencies": {
72
- "@marko/vite": "^2.3.9"
82
+ "@marko/vite": "^2.3.12",
83
+ "marko": "^5"
73
84
  },
74
85
  "devDependencies": {
75
86
  "@babel/types": "^7.19.0",
76
- "@marko/compiler": "^5.22.6",
87
+ "@marko/vite": "^2.3.12",
77
88
  "@types/glob": "^8.0.1",
78
89
  "@types/human-format": "^1.0.0",
79
90
  "@types/mocha": "^9.1.1",
@@ -81,7 +92,7 @@
81
92
  "acorn": "^8.8.0",
82
93
  "cross-env": "^7.0.3",
83
94
  "esbuild": "^0.15.7",
84
- "marko": "^5.22.4",
95
+ "marko": "^5.23.0",
85
96
  "mocha": "^10.0.0",
86
97
  "mocha-snap": "^4.3.0",
87
98
  "prettier": "^2.7.1",
@@ -89,7 +100,6 @@
89
100
  "typescript": "^4.7.4"
90
101
  },
91
102
  "dependencies": {
92
- "@marko/vite": "^2.3.9",
93
103
  "cli-table3": "^0.6.3",
94
104
  "compression": "^1.7.4",
95
105
  "dotenv": "^16.0.3",
@@ -100,6 +110,6 @@
100
110
  "sade": "^1.8.1",
101
111
  "serve-static": "^1.15.0",
102
112
  "undici": "^5.20.0",
103
- "vite": "^3.0.8"
113
+ "vite": "^4.1.4"
104
114
  }
105
115
  }