@dnax/core 0.11.0 → 0.12.0

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/app/hono.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Q, Tenant, sessionCtx } from "../types/";
2
2
  import fs from "fs-extra";
3
+ import { cleanDoubleSlashes } from "ufo";
3
4
  import sharp from "sharp";
4
5
  import path from "path";
5
6
  import mime from "mime-types";
@@ -39,6 +40,15 @@ const cache = bentoCache.namespace("DNAX_API");
39
40
  const app = new Hono();
40
41
  const API_PATH = "/api";
41
42
  function HonoInstance(): typeof app {
43
+ // Public assets
44
+ app.get(
45
+ "/assets/*",
46
+ serveStatic({
47
+ root: "uploads",
48
+ //rewriteRequestPath: (path) => path?.replace(/^\/assets/, ""),
49
+ })
50
+ );
51
+
42
52
  if (Cfg?.server?.logger) {
43
53
  if (typeof Cfg?.server?.logger == "function" && Cfg?.server?.logger) {
44
54
  app.use(async (c, next) => {
@@ -650,16 +660,13 @@ function HonoInstance(): typeof app {
650
660
  }
651
661
  });
652
662
 
663
+ if (Cfg.routes?.length) {
664
+ Cfg.routes.map((r) => {
665
+ app.route(cleanDoubleSlashes("/" + r.base), r.router);
666
+ });
667
+ }
668
+
653
669
  return app;
654
670
  }
655
671
 
656
- // Public assets
657
- app.get(
658
- "/assets/*",
659
- serveStatic({
660
- root: "uploads",
661
- //rewriteRequestPath: (path) => path?.replace(/^\/assets/, ""),
662
- })
663
- );
664
-
665
672
  export { HonoInstance };
package/define/index.ts CHANGED
@@ -10,6 +10,7 @@ import type {
10
10
  ctxApi,
11
11
  middlewareCtx,
12
12
  permissionSchema,
13
+ routeCtx,
13
14
  } from "../types";
14
15
  import { Cfg } from "../config/";
15
16
  import { deepMerge, freeze } from "../utils";
@@ -29,6 +30,11 @@ function Endpoint(config: Endpoint) {
29
30
  return config;
30
31
  }
31
32
 
33
+ function Route(config: routeCtx) {
34
+ config.ok = true;
35
+ return config;
36
+ }
37
+
32
38
  function Service(config: Service) {
33
39
  return config;
34
40
  }
@@ -93,6 +99,7 @@ const define = {
93
99
  Permission,
94
100
  Api,
95
101
  Access,
102
+ Route,
96
103
  };
97
104
 
98
105
  export default define;
package/lib/index.ts CHANGED
@@ -5,6 +5,7 @@ import { loadServices } from "./service";
5
5
  import { initCron } from "./cron";
6
6
  import { loadPermissions } from "./permissions";
7
7
  import { loadSocket } from "./socket";
8
+ import { loadAutoRoutes } from "./routes";
8
9
  // load all ressource
9
10
  async function init(cf = { app: null }) {
10
11
  // load all tenants database
@@ -27,6 +28,9 @@ async function init(cf = { app: null }) {
27
28
  // load all loadEndpoints
28
29
  await loadEndpoints();
29
30
 
31
+ // load auto routes
32
+ await loadAutoRoutes();
33
+
30
34
  await initCron();
31
35
  }
32
36
 
package/lib/routes.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { omit } from "radash";
2
+ import type { Collection, Endpoint, Field } from "../types/index";
3
+ import { Glob } from "bun";
4
+ import { Cfg } from "../config";
5
+ import { cleanPath, resolvePath } from "../utils";
6
+ import path from "path";
7
+ import { useRest } from "../driver/mongo/rest";
8
+ import { Hono } from "hono";
9
+ async function loadAutoRoutes() {
10
+ let endpoints = [] as any;
11
+ if (Cfg?.tenants) {
12
+ for await (let t of Cfg.tenants) {
13
+ let tenantPath = `${t.dir}/routes/**/*.{ts,js}`;
14
+ const glob = new Glob(tenantPath);
15
+ for await (let file of glob.scan({
16
+ cwd: Cfg.cwd,
17
+ })) {
18
+ let fullPathFile = path.join(Cfg.cwd || "", file);
19
+ const regex = /^src\/routes\/(.*)/;
20
+ const matchPath = file.match(regex);
21
+ let baseURL: string | null = null;
22
+
23
+ if (matchPath) {
24
+ baseURL = matchPath[1];
25
+ baseURL = path.dirname(baseURL);
26
+ }
27
+
28
+ await import(fullPathFile)
29
+ .then((inject) => {
30
+ let instance = {
31
+ router: new Hono(),
32
+ };
33
+ inject?.default?.handler({
34
+ io: Cfg.io,
35
+ router: instance.router,
36
+ });
37
+ if (baseURL) {
38
+ endpoints.push({
39
+ router: instance.router,
40
+ base: baseURL,
41
+ });
42
+ }
43
+ })
44
+ .catch((err) => {
45
+ console.error(err);
46
+ });
47
+ }
48
+ }
49
+ }
50
+ Cfg.routes = endpoints;
51
+ }
52
+
53
+ export { loadAutoRoutes };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/types/index.ts CHANGED
@@ -5,7 +5,7 @@ import * as v from "valibot";
5
5
  import type { Db, MongoClient } from "mongodb";
6
6
  import { useRest } from "../driver/mongo/rest";
7
7
  import { sessionStorage } from "../lib/asyncLocalStorage";
8
- import type { Context } from "hono";
8
+ import type { Context, Hono } from "hono";
9
9
  import type { Server as ServerIO, Socket as SocketType } from "socket.io";
10
10
 
11
11
  type Io = InstanceType<typeof ServerIO>;
@@ -383,6 +383,7 @@ export type Config = {
383
383
  * Tenants database for API
384
384
  */
385
385
  tenants: Tenant[];
386
+ routes?: { base: string; router: InstanceType<typeof Hono> }[];
386
387
  permissions?: permissionSchema[];
387
388
  /**
388
389
  * Dont touch it automatically generated
@@ -426,6 +427,20 @@ export type Q = {
426
427
  | "batch";
427
428
  };
428
429
 
430
+ export type routeOption = {
431
+ /**
432
+ * Base URL
433
+ */
434
+ base: string; // base URL
435
+ handler: (ctx: {
436
+ io: Io;
437
+ router: {
438
+ post: (path: string, clb: (c: Context) => void) => {};
439
+ get: (path: string, clb: (c: Context) => void) => {};
440
+ };
441
+ }) => any;
442
+ };
443
+
429
444
  export type endpointCtx = {
430
445
  enabled: boolean;
431
446
  handler: (ctx: {
@@ -437,6 +452,17 @@ export type endpointCtx = {
437
452
  }) => any;
438
453
  };
439
454
 
455
+ export type routeCtx = {
456
+ enabled: boolean;
457
+ handler: (ctx: {
458
+ io: Io;
459
+ router: {
460
+ post: (path: string, clb: (c: Context) => void) => {};
461
+ get: (path: string, clb: (c: Context) => void) => {};
462
+ };
463
+ }) => any;
464
+ };
465
+
440
466
  export type Endpoint = endpointCtx;
441
467
 
442
468
  export type Service = {