bini-router 1.0.18 → 1.0.20

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/dist/index.cjs CHANGED
@@ -655,9 +655,74 @@ async function handleApiRequest(req, res, next, apiDir, enableCors, getCache, se
655
655
  next(e);
656
656
  }
657
657
  }
658
- function buildRouteImports(routes, outFile, enableCors) {
658
+ var ADAPTERS = {
659
+ netlify: {
660
+ // Netlify Edge Functions — Deno runtime, use deno.land URL imports (zero npm)
661
+ // Entry file uses URL imports so Netlify bundles with no npm module errors.
662
+ // User route files import `from 'hono'` (npm) but Netlify resolves them fine
663
+ // since the entry provides Hono via URL import at bundle time.
664
+ importLine: `import { Hono } from 'https://deno.land/x/hono@v4.3.11/mod.ts';
665
+ import { handle } from 'https://deno.land/x/hono@v4.3.11/adapter/netlify/index.ts';`,
666
+ exportLine: `export default handle(app);`,
667
+ outFile: (cwd) => import_path.default.join(cwd, "netlify", "edge-functions", "api.ts"),
668
+ stripsApiPrefix: false
669
+ },
670
+ cloudflare: {
671
+ exportLine: `export default app;`,
672
+ outFile: (cwd) => import_path.default.join(cwd, "worker.ts"),
673
+ stripsApiPrefix: false
674
+ },
675
+ node: {
676
+ pkg: "@hono/node-server",
677
+ importLine: `import { serve } from '@hono/node-server';
678
+ import { serveStatic } from '@hono/node-server/serve-static';`,
679
+ exportLine: `app.use('/*', serveStatic({ root: './dist' }));
680
+ app.use('/*', serveStatic({ path: './dist/index.html' }));
681
+
682
+ serve({ fetch: app.fetch, port: Number(process.env.PORT ?? 3000) }, (i) => {
683
+ console.log(\`Server running on http://localhost:\${i.port}\`);
684
+ });`,
685
+ outFile: (cwd) => import_path.default.join(cwd, "server", "index.ts"),
686
+ stripsApiPrefix: false
687
+ },
688
+ deno: {
689
+ importLine: `import { serve } from 'hono/deno';`,
690
+ exportLine: `serve(app.fetch);`,
691
+ outFile: (cwd) => import_path.default.join(cwd, "server", "index.ts"),
692
+ stripsApiPrefix: false
693
+ },
694
+ bun: {
695
+ exportLine: `export default app;`,
696
+ outFile: (cwd) => import_path.default.join(cwd, "server", "index.ts"),
697
+ stripsApiPrefix: false
698
+ },
699
+ aws: {
700
+ // Requires separate install: npm install @hono/aws-lambda
701
+ pkg: "@hono/aws-lambda",
702
+ importLine: `import { handle } from '@hono/aws-lambda';`,
703
+ exportLine: `export const handler = handle(app);`,
704
+ outFile: (cwd) => import_path.default.join(cwd, "handler.ts"),
705
+ stripsApiPrefix: false
706
+ }
707
+ };
708
+ function checkAdapter(platform) {
709
+ const adapter = ADAPTERS[platform];
710
+ if (!adapter.pkg) return;
711
+ try {
712
+ require.resolve(adapter.pkg, { paths: [process.cwd()] });
713
+ } catch {
714
+ console.error(`
715
+ [bini-router] \u2717 Missing required package for platform '${platform}'.
716
+ Run: npm install ${adapter.pkg}
717
+ `);
718
+ process.exit(1);
719
+ }
720
+ }
721
+ function buildRouteImports(routes, outFile, enableCors, platform) {
659
722
  const imports = [];
660
723
  const mountings = [];
724
+ const adapter = ADAPTERS[platform];
725
+ const stripPrefix = adapter.stripsApiPrefix;
661
726
  for (let i = 0; i < routes.length; i++) {
662
727
  const route = routes[i];
663
728
  const rel = norm(import_path.default.relative(import_path.default.dirname(outFile), route.filePath)).replace(/\.(ts|tsx)$/, "");
@@ -673,10 +738,13 @@ function buildRouteImports(routes, outFile, enableCors) {
673
738
  if (isHonoApp) {
674
739
  mountings.push(`app.route('/', ${name});`);
675
740
  } else {
676
- mountings.push(`app.all('/api${route.routePath}', async (c) => { const r = await ${name}(c.req.raw); return r instanceof Response ? r : c.json(r); });`);
741
+ const mountPath = stripPrefix ? route.routePath : `/api${route.routePath}`;
742
+ mountings.push(`app.all('${mountPath}', async (c) => { const r = await ${name}(c.req.raw); return r instanceof Response ? r : c.json(r); });`);
677
743
  }
678
744
  }
679
- const corsLine = enableCors ? `app.use('/api/*', cors({ origin: '*', allowMethods: ['GET','POST','PUT','PATCH','DELETE','OPTIONS'], allowHeaders: ['Content-Type','Authorization'] }));` : null;
745
+ const corsPattern = stripPrefix ? "/*" : "/api/*";
746
+ const isNetlifyPlatform = platform === "netlify";
747
+ const corsLine = enableCors ? isNetlifyPlatform ? `app.use('${corsPattern}', async (c, next) => { await next(); c.res.headers.set('Access-Control-Allow-Origin', '*'); c.res.headers.set('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS'); c.res.headers.set('Access-Control-Allow-Headers', 'Content-Type,Authorization'); if (c.req.method === 'OPTIONS') return new Response(null, { status: 204, headers: c.res.headers }); });` : `app.use('${corsPattern}', cors({ origin: '*', allowMethods: ['GET','POST','PUT','PATCH','DELETE','OPTIONS'], allowHeaders: ['Content-Type','Authorization'] }));` : null;
680
748
  const corsImport = enableCors ? `import { cors } from 'hono/cors';` : null;
681
749
  return { imports, mountings, corsLine, corsImport };
682
750
  }
@@ -688,137 +756,24 @@ function buildProductionEntry(srcApiDir, platform, enableCors) {
688
756
  `// \u26A0\uFE0F Auto-generated by bini-router on every build \u2014 do not edit.`,
689
757
  `// Add routes by creating files in src/app/api/ only.`
690
758
  ];
691
- if (platform === "vercel") {
692
- const funcDir = import_path.default.join(cwd, ".vercel", "output", "functions", "api.func");
693
- const staticDir = import_path.default.join(cwd, ".vercel", "output", "static");
694
- const outFile2 = import_path.default.join(funcDir, "index.ts");
695
- const { imports: imports2, mountings: mountings2, corsLine: corsLine2, corsImport: corsImport2 } = buildRouteImports(routes, outFile2, enableCors);
696
- const lines2 = [
697
- ...header,
698
- `import { Hono } from 'hono';`,
699
- ...corsImport2 ? [corsImport2] : [],
700
- ...imports2,
701
- ``,
702
- `const app = new Hono();`,
703
- ...corsLine2 ? [corsLine2] : [],
704
- ...mountings2,
705
- ``,
706
- `export const runtime = 'edge';`,
707
- `export default { fetch: app.fetch };`
708
- ];
709
- import_fs.default.mkdirSync(funcDir, { recursive: true });
710
- import_fs.default.writeFileSync(outFile2, lines2.join("\n") + "\n", "utf8");
711
- import_fs.default.writeFileSync(import_path.default.join(funcDir, ".vc-config.json"), JSON.stringify({
712
- runtime: "edge",
713
- entrypoint: "index.ts"
714
- }, null, 2));
715
- import_fs.default.mkdirSync(import_path.default.join(cwd, ".vercel", "output"), { recursive: true });
716
- import_fs.default.writeFileSync(import_path.default.join(cwd, ".vercel", "output", "config.json"), JSON.stringify({
717
- version: 3,
718
- routes: [
719
- { src: "/api/(.*)", dest: "/api" },
720
- { src: "/assets/(.*)", dest: "/assets/$1" },
721
- { src: "/css/(.*)", dest: "/css/$1" },
722
- { src: "/js/(.*)", dest: "/js/$1" },
723
- { handle: "filesystem" },
724
- { src: "/(.*)", dest: "/index.html" }
725
- ]
726
- }, null, 2));
727
- const distDir = import_path.default.join(cwd, "dist");
728
- if (import_fs.default.existsSync(distDir)) {
729
- import_fs.default.mkdirSync(staticDir, { recursive: true });
730
- import_fs.default.cpSync(distDir, staticDir, { recursive: true });
731
- }
732
- console.log(`[bini-router] \u2713 Generated .vercel/output/ (Vercel Build Output API)`);
733
- return;
734
- }
735
- let outFile;
736
- if (platform === "netlify") {
737
- outFile = import_path.default.join(cwd, "netlify", "functions", "api.ts");
738
- } else if (platform === "cloudflare") {
739
- outFile = import_path.default.join(cwd, "worker.ts");
740
- } else if (platform === "aws") {
741
- outFile = import_path.default.join(cwd, "handler.ts");
742
- } else {
743
- outFile = import_path.default.join(cwd, "server", "index.ts");
744
- }
745
- const { imports, mountings, corsLine, corsImport } = buildRouteImports(routes, outFile, enableCors);
746
- const appSetup = [
759
+ checkAdapter(platform);
760
+ const adapter = ADAPTERS[platform];
761
+ const outFile = adapter.outFile(cwd);
762
+ const { imports, mountings, corsLine, corsImport } = buildRouteImports(routes, outFile, enableCors, platform);
763
+ const isNetlify = platform === "netlify";
764
+ const lines = [
765
+ ...header,
766
+ ...isNetlify ? [] : [`import { Hono } from 'hono';`],
767
+ ...adapter.importLine ? adapter.importLine.split("\n") : [],
768
+ ...corsImport && !isNetlify ? [corsImport] : [],
769
+ ...imports,
747
770
  ``,
748
771
  `const app = new Hono();`,
749
772
  ...corsLine ? [corsLine] : [],
750
- ...mountings
773
+ ...mountings,
774
+ ``,
775
+ ...adapter.exportLine.split("\n")
751
776
  ];
752
- let lines;
753
- if (platform === "netlify") {
754
- lines = [
755
- ...header,
756
- `import { Hono } from 'hono';`,
757
- `import { handle } from 'hono/netlify';`,
758
- ...corsImport ? [corsImport] : [],
759
- ...imports,
760
- ...appSetup,
761
- ``,
762
- `export default handle(app);`
763
- ];
764
- } else if (platform === "cloudflare") {
765
- lines = [
766
- ...header,
767
- `import { Hono } from 'hono';`,
768
- ...corsImport ? [corsImport] : [],
769
- ...imports,
770
- ...appSetup,
771
- ``,
772
- `export default app;`
773
- ];
774
- } else if (platform === "aws") {
775
- lines = [
776
- ...header,
777
- `import { Hono } from 'hono';`,
778
- `import { handle } from 'hono/aws-lambda';`,
779
- ...corsImport ? [corsImport] : [],
780
- ...imports,
781
- ...appSetup,
782
- ``,
783
- `export const handler = handle(app);`
784
- ];
785
- } else if (platform === "deno") {
786
- lines = [
787
- ...header,
788
- `import { Hono } from 'hono';`,
789
- ...corsImport ? [corsImport] : [],
790
- ...imports,
791
- ...appSetup,
792
- ``,
793
- `Deno.serve(app.fetch);`
794
- ];
795
- } else if (platform === "bun") {
796
- lines = [
797
- ...header,
798
- `import { Hono } from 'hono';`,
799
- ...corsImport ? [corsImport] : [],
800
- ...imports,
801
- ...appSetup,
802
- ``,
803
- `export default app;`
804
- ];
805
- } else {
806
- lines = [
807
- ...header,
808
- `import { Hono } from 'hono';`,
809
- `import { serve } from '@hono/node-server';`,
810
- `import { serveStatic } from '@hono/node-server/serve-static';`,
811
- ...corsImport ? [corsImport] : [],
812
- ...imports,
813
- ...appSetup,
814
- `app.use('/*', serveStatic({ root: './dist' }));`,
815
- `app.use('/*', serveStatic({ path: './dist/index.html' }));`,
816
- ``,
817
- `serve({ fetch: app.fetch, port: Number(process.env.PORT ?? 3000) }, (i) => {`,
818
- ` console.log(\`Server running on http://localhost:\${i.port}\`);`,
819
- `});`
820
- ];
821
- }
822
777
  import_fs.default.mkdirSync(import_path.default.dirname(outFile), { recursive: true });
823
778
  import_fs.default.writeFileSync(outFile, lines.join("\n") + "\n", "utf8");
824
779
  console.log(`[bini-router] \u2713 Generated ${norm(import_path.default.relative(cwd, outFile))}`);