Skip to content

Commit ff4197e

Browse files
committed
feat: add api route handler and server manifest
1 parent 0a5c95f commit ff4197e

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
export const GET = () => {
2-
return true;
3-
};
1+
import { defineHandler } from "pranx/server";
42

5-
export const POST = () => {
6-
return true;
7-
};
3+
export const GET = defineHandler(() => {
4+
return { ok: true };
5+
});
86

9-
export const PUT = () => {
10-
return true;
11-
};
7+
export const POST = defineHandler(() => {
8+
return { ok: true };
9+
});
1210

13-
export const DELETE = () => {
14-
return true;
15-
};
11+
export const PUT = defineHandler(() => {
12+
return { ok: true };
13+
});
14+
15+
export const DELETE = defineHandler(() => {
16+
return { ok: true };
17+
});

packages/pranx/src/cmd/build.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,53 @@ export async function build() {
3838
await fse.emptyDir(OUTPUT_PRANX_DIR);
3939

4040
// Bundling
41-
await bundle_server({
42-
optimize: true,
41+
const optimize_output = false;
42+
43+
const server_bundle_result = await bundle_server({
44+
optimize: optimize_output,
4345
user_config: await get_user_pranx_config(),
4446
});
4547

4648
const browser_bundle_result = await bundle_browser({
47-
optimize: true,
49+
optimize: optimize_output,
4850
user_config: await get_user_pranx_config(),
4951
});
5052

5153
// Manifests
5254
const server_site_manifest: SERVER_MANIFEST = {
5355
entry_server: join(OUTPUT_BUNDLE_SERVER_DIR, "entry-server.js"),
5456
routes: [],
57+
api: [],
5558
};
5659

5760
let server_entry_module: ServerEntryModule | null = null;
5861

5962
server_entry_module = (await import(server_site_manifest.entry_server)) as ServerEntryModule;
6063

64+
// Generating api routes Manifest
65+
const pranx_server_base_path = join(".pranx", "server");
66+
67+
for (const [file, _output] of Object.entries(server_bundle_result.metafile.outputs)) {
68+
if (!file.endsWith("route.js")) continue;
69+
70+
const pages_relative_path = file.replace(pranx_server_base_path, "");
71+
72+
const final_path_normalized = `/${pages_relative_path
73+
.replace("route.js", "")
74+
.replace("pages", "")
75+
.split("/")
76+
.filter(Boolean)
77+
.join("/")}`;
78+
79+
const module_path = join(OUTPUT_BUNDLE_SERVER_DIR, "pages", pages_relative_path);
80+
81+
server_site_manifest.api.push({
82+
path: final_path_normalized,
83+
module: pages_relative_path,
84+
absolute_module_path: module_path,
85+
});
86+
}
87+
6188
const pranx_browser_base_path = join(".pranx", "browser");
6289

6390
type CSS_OUTPUT = {
@@ -266,7 +293,7 @@ export async function build() {
266293
const html = generate_html_template({
267294
page_prerendered,
268295
hydrate_data_as_string,
269-
minify: true,
296+
minify: optimize_output,
270297
css: route.css,
271298
});
272299

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export { Meta } from "./Meta.js";
22
export { Scripts } from "./Scripts.js";
3+
4+
export { defineHandler } from "h3";

packages/pranx/types/index.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BuildOptions } from "esbuild";
2-
import { EventHandlerRequest, H3Event } from "h3";
2+
import { EventHandlerRequest, H3Event, defineHandler } from "h3";
33
import { VNode } from "preact";
44
import { PropsWithChildren } from "preact/compat";
55
import { useHead } from "unhead";
@@ -74,9 +74,27 @@ export type ServerManifestRoute = {
7474
css: string[];
7575
};
7676

77+
export type PranxApiHandler = typeof defineHandler;
78+
79+
export type PranxRouteModule = {
80+
GET?: PranxApiHandler;
81+
POST?: PranxApiHandler;
82+
PUT?: PranxApiHandler;
83+
DELETE?: PranxApiHandler;
84+
PATCH?: PranxApiHandler;
85+
HEAD?: PranxApiHandler;
86+
};
87+
88+
export type ServerManifestApiHandler = {
89+
path: string;
90+
module: string;
91+
absolute_module_path: string;
92+
};
93+
7794
export type SERVER_MANIFEST = {
7895
entry_server: string;
7996
routes: ServerManifestRoute[];
97+
api: ServerManifestApiHandler[];
8098
};
8199

82100
export type HydrateDataRoute = {

packages/pranx/types/server.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ import { VNode } from "preact";
22

33
export declare function Scripts(): VNode<any>;
44
export declare function Meta(): VNode<any>;
5+
6+
export { defineHandler } from "h3";

0 commit comments

Comments
 (0)