@rpcbase/api 0.15.0 → 0.17.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/api",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "scripts": {
package/src/initApi.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import assert from "assert"
2
+
2
3
  import env from "@rpcbase/env"
3
4
  import BBPromise from "bluebird"
4
5
  import { Application , json } from "express"
5
-
6
6
  import { initApiClient } from "@rpcbase/client"
7
7
 
8
- import { Api, ApiHandler, Payload, Result, Middleware } from "./types"
8
+ import { Api, ApiHandler, Middleware } from "./types"
9
9
 
10
10
 
11
11
  // dotenv setup, merge process env with vite process.env file
@@ -61,18 +61,30 @@ export const initApi = async (app: Application) => {
61
61
  })
62
62
 
63
63
  const routes = {
64
- // @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
64
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
65
65
  ...import.meta.glob("../../auth/src/api/**/handler.ts"),
66
- // @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
66
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
67
67
  ...import.meta.glob("@/api/**/handler.ts")
68
68
  }
69
69
 
70
70
  const routeEntries = Object.entries(routes) as RouteModule[]
71
71
 
72
- const loadRoute = async([routePath, loadFn]: RouteModule) => {
72
+ const loadRoute = async ([routePath, loadFn]: RouteModule) => {
73
73
  const routeModule = await loadFn()
74
74
  routeModule.default(api)
75
- console.log("loaded api route", routePath)
75
+
76
+ const loggedPath = routePath.startsWith("../../")
77
+ ? routePath.replace("../../", "@rpcbase/")
78
+ : routePath
79
+
80
+ // Colorize output: gray for @rpcbase paths, default (white) otherwise
81
+ const GRAY = "\x1b[90m"
82
+ const RESET = "\x1b[0m"
83
+ const coloredPath = loggedPath.startsWith("@rpcbase/")
84
+ ? `${GRAY}${loggedPath}${RESET}`
85
+ : loggedPath
86
+
87
+ console.log("loaded api route", coloredPath)
76
88
  }
77
89
 
78
90
  await BBPromise.map(routeEntries, loadRoute)
package/src/loadModel.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import assert from "assert"
2
+
2
3
  import mongoose from "mongoose"
4
+
3
5
  import { Ctx } from "./types"
4
6
 
5
- const { APP_NAME, DB_NAME } = process.env
7
+
8
+ const { APP_NAME, MONGO_PORT } = process.env
6
9
  const connections: Record<string, mongoose.Connection> = {}
7
10
 
8
- // @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
9
- const modelModules = import.meta.glob('@/models/**/*.ts', { eager: true })
11
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
12
+ const modelModules = import.meta.glob("@/models/**/*.ts", { eager: true })
10
13
 
11
14
  const models = Object.entries(modelModules)
12
15
  .map(([key, exports]) => {
@@ -39,10 +42,10 @@ export const loadModel = async (modelName: string, ctx: Ctx) => {
39
42
  assert(tenantId, "Tenant ID is missing from session")
40
43
 
41
44
  const dbName = ["User", "Tenant"].includes(modelName)
42
- ? `${APP_NAME}-users-${DB_NAME}`
43
- : `${APP_NAME}-${tenantId}-${DB_NAME}`
45
+ ? `${APP_NAME}-users-db`
46
+ : `${APP_NAME}-${tenantId}-db`
44
47
 
45
- const connectionString = `mongodb://localhost:27017/${dbName}`
48
+ const connectionString = `mongodb://localhost:${MONGO_PORT}/${dbName}`
46
49
 
47
50
  if (!connections[dbName]) {
48
51
  const connection = mongoose.createConnection(connectionString, {