Update API routes function. Add middleware. Update README.md
This commit is contained in:
@@ -41,7 +41,7 @@ type Params = {
|
||||
export default async function allPagesBundler(params?: Params) {
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
const { ClientRootElementIDName, ClientRootComponentWindowName } =
|
||||
await grabConstants();
|
||||
grabConstants();
|
||||
|
||||
const virtualEntries: Record<string, string> = {};
|
||||
const dev = isDevelopment();
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import type { Server } from "bun";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextServerRouteConfig,
|
||||
BunxRouteParams,
|
||||
} from "../../types";
|
||||
import type { BunextServerRouteConfig, BunxRouteParams } from "../../types";
|
||||
import grabRouteParams from "../../utils/grab-route-params";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
import grabRouter from "../../utils/grab-router";
|
||||
@@ -13,14 +9,10 @@ type Params = {
|
||||
server: Server;
|
||||
};
|
||||
|
||||
export default async function ({
|
||||
req,
|
||||
server,
|
||||
}: Params): Promise<APIResponseObject | undefined> {
|
||||
export default async function ({ req, server }: Params): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
|
||||
const { MBInBytes, ServerDefaultRequestBodyLimitBytes } =
|
||||
await grabConstants();
|
||||
const { MBInBytes, ServerDefaultRequestBodyLimitBytes } = grabConstants();
|
||||
|
||||
const router = grabRouter();
|
||||
|
||||
@@ -28,13 +20,19 @@ export default async function ({
|
||||
|
||||
if (!match?.filePath) {
|
||||
const errMsg = `Route ${url.pathname} not found`;
|
||||
// console.error(errMsg);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
status: 401,
|
||||
msg: errMsg,
|
||||
};
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
msg: errMsg,
|
||||
},
|
||||
{
|
||||
status: 401,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const routeParams: BunxRouteParams = await grabRouteParams({ req });
|
||||
@@ -52,17 +50,25 @@ export default async function ({
|
||||
size > config.maxRequestBodyMB * MBInBytes) ||
|
||||
size > ServerDefaultRequestBodyLimitBytes
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
status: 413,
|
||||
msg: "Request Body Too Large!",
|
||||
};
|
||||
return Response.json(
|
||||
{
|
||||
success: false,
|
||||
msg: "Request Body Too Large!",
|
||||
},
|
||||
{
|
||||
status: 413,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const res: APIResponseObject = await module["default"](
|
||||
routeParams as BunxRouteParams,
|
||||
);
|
||||
const res: Response = await module["default"]({
|
||||
...routeParams,
|
||||
server,
|
||||
} as BunxRouteParams);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
||||
import handleWebPages from "./web-pages/handle-web-pages";
|
||||
import handleRoutes from "./handle-routes";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
|
||||
type Params = {
|
||||
dev?: boolean;
|
||||
@@ -19,6 +20,20 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
try {
|
||||
const url = new URL(req.url);
|
||||
|
||||
const { config } = grabConstants();
|
||||
|
||||
if (config?.middleware) {
|
||||
const middleware_res = await config.middleware({
|
||||
req,
|
||||
url,
|
||||
server,
|
||||
});
|
||||
|
||||
if (typeof middleware_res == "object") {
|
||||
return middleware_res;
|
||||
}
|
||||
}
|
||||
|
||||
if (url.pathname === "/__hmr" && isDevelopment()) {
|
||||
const referer_url = new URL(
|
||||
req.headers.get("referer") || "",
|
||||
@@ -69,14 +84,7 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/api/")) {
|
||||
const res = await handleRoutes({ req, server });
|
||||
|
||||
return new Response(JSON.stringify(res), {
|
||||
status: res?.status,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return await handleRoutes({ req, server });
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/public/")) {
|
||||
|
||||
@@ -5,8 +5,6 @@ import rebuildBundler from "./rebuild-bundler";
|
||||
|
||||
const { SRC_DIR } = grabDirNames();
|
||||
|
||||
const PAGE_FILE_RE = /\.(tsx?|jsx?|css)$/;
|
||||
|
||||
export default function watcher() {
|
||||
watch(
|
||||
SRC_DIR,
|
||||
@@ -16,12 +14,7 @@ export default function watcher() {
|
||||
},
|
||||
async (event, filename) => {
|
||||
if (!filename) return;
|
||||
const file_path = path.join(SRC_DIR, filename);
|
||||
// if (!PAGE_FILE_RE.test(filename)) return;
|
||||
|
||||
// "change" events (file content modified) are already handled by
|
||||
// esbuild's internal ctx.watch(). Only "rename" (create or delete)
|
||||
// requires a full rebuild because entry points have changed.
|
||||
if (event !== "rename") return;
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
|
||||
@@ -16,7 +16,7 @@ export default async function genWebHTML({
|
||||
routeParams,
|
||||
}: LivePageDistGenParams) {
|
||||
const { ClientRootElementIDName, ClientWindowPagePropsName } =
|
||||
await grabContants();
|
||||
grabContants();
|
||||
|
||||
const { renderToString } = await import(
|
||||
path.join(process.cwd(), "node_modules", "react-dom", "server")
|
||||
|
||||
@@ -125,7 +125,7 @@ export default async function grabPageComponent({
|
||||
: undefined;
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
const Head = module.head as FC<any>;
|
||||
const Head = module.Head as FC<any>;
|
||||
|
||||
const component = RootComponent ? (
|
||||
<RootComponent {...serverRes}>
|
||||
|
||||
Reference in New Issue
Block a user