@rpcbase/api 0.0.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 +30 -0
- package/src/index.ts +3 -0
- package/src/initApi.ts +77 -0
- package/src/loadModel.ts +85 -0
- package/src/types.ts +25 -0
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rpcbase/api",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc --watch",
|
|
8
|
+
"release": "wireit"
|
|
9
|
+
},
|
|
10
|
+
"wireit": {
|
|
11
|
+
"release": {
|
|
12
|
+
"command": "../../scripts/publish.js",
|
|
13
|
+
"dependencies": [],
|
|
14
|
+
"files": [
|
|
15
|
+
"package.json"
|
|
16
|
+
],
|
|
17
|
+
"output": [],
|
|
18
|
+
"env": {
|
|
19
|
+
"NPM_RELEASE_CHANNEL": {
|
|
20
|
+
"external": true
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"bluebird": "3.7.2",
|
|
28
|
+
"express": "5.0.1"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
ADDED
package/src/initApi.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import assert from "assert"
|
|
2
|
+
|
|
3
|
+
import BBPromise from "bluebird"
|
|
4
|
+
import { Application , json } from "express"
|
|
5
|
+
|
|
6
|
+
import { initApiClient } from "@rpcbase/client"
|
|
7
|
+
|
|
8
|
+
import { Api, ApiHandler, Payload, Result, Middleware } from "./types"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
type RouteModule = [string, () => Promise<{default: (api: Api) => void}>]
|
|
12
|
+
|
|
13
|
+
export const initApi = async (app: Application) => {
|
|
14
|
+
|
|
15
|
+
await initApiClient({app})
|
|
16
|
+
|
|
17
|
+
const registerHandler = (method: "get" | "post" | "put" | "delete", path: string, handler: ApiHandler) => {
|
|
18
|
+
assert(path.startsWith("/api"), "API routes must start with /api")
|
|
19
|
+
|
|
20
|
+
app[method](path, async(req, res, next) => {
|
|
21
|
+
const payload = req.body || {}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const result = await handler(payload, {req, res})
|
|
25
|
+
res.json(result)
|
|
26
|
+
} catch (e) {
|
|
27
|
+
next(e)
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const api = {
|
|
33
|
+
use: (path: string, middleware: Middleware): void => {
|
|
34
|
+
app.use(path, middleware)
|
|
35
|
+
},
|
|
36
|
+
get: (path: string, handler: ApiHandler): void => {
|
|
37
|
+
registerHandler("get", path, handler)
|
|
38
|
+
},
|
|
39
|
+
post: (path: string, handler: ApiHandler): void => {
|
|
40
|
+
registerHandler("post", path, handler)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// body parser
|
|
45
|
+
app.use("/api", json({limit: "8mb"}))
|
|
46
|
+
|
|
47
|
+
// WARNING:
|
|
48
|
+
// This is used to mark the start of the api middleware chain
|
|
49
|
+
// keep this middleware named like this, the serer api client will start calling middlewares starting from here
|
|
50
|
+
app.use("/api", function __FIRST_API_MIDDLEWARE__(_req, _res, next) {
|
|
51
|
+
next()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const routes = {
|
|
55
|
+
// @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
|
|
56
|
+
...import.meta.glob("../../auth/src/api/**/handler.ts"),
|
|
57
|
+
// @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
|
|
58
|
+
...import.meta.glob("@/api/**/handler.ts")
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const routeEntries = Object.entries(routes) as RouteModule[]
|
|
62
|
+
|
|
63
|
+
const loadRoute = async([routePath, loadFn]: RouteModule) => {
|
|
64
|
+
const routeModule = await loadFn()
|
|
65
|
+
routeModule.default(api)
|
|
66
|
+
console.log("loaded api route", routePath)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
await BBPromise.map(routeEntries, loadRoute)
|
|
70
|
+
console.log("Done loading routes")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
app.use("/api", (req, res) => {
|
|
74
|
+
res.status(404).json({ error: `Not Found: ${req.method}:${req.originalUrl}` })
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
}
|
package/src/loadModel.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import assert from "assert"
|
|
2
|
+
import mongoose from "mongoose"
|
|
3
|
+
import { Ctx } from "./types"
|
|
4
|
+
|
|
5
|
+
const { APP_NAME, DB_NAME } = process.env
|
|
6
|
+
const connections: Record<string, mongoose.Connection> = {}
|
|
7
|
+
|
|
8
|
+
// @ts-ignore: Property 'glob' does not exist on type 'ImportMeta'.
|
|
9
|
+
const modelModules = import.meta.glob('@/models/**/*.ts', { eager: true })
|
|
10
|
+
|
|
11
|
+
const models = Object.entries(modelModules)
|
|
12
|
+
.map(([key, exports]) => {
|
|
13
|
+
assert(key.startsWith("/src/models/"), "Expected Model path to start with /src/models/")
|
|
14
|
+
|
|
15
|
+
const modelName = key.replace("/src/models/", "").replace(".ts", "").split("/").pop()
|
|
16
|
+
assert(modelName, "Model name not found")
|
|
17
|
+
|
|
18
|
+
const schema = Object.entries(exports as Record<string, unknown>)
|
|
19
|
+
.find(([key]) => {
|
|
20
|
+
return key === `${modelName}Schema`
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
assert(schema, `Schema not found for model ${modelName}`)
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
name: modelName,
|
|
27
|
+
model: mongoose.model(modelName, schema[1] as mongoose.Schema)
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.reduce((acc, { name, model }) => {
|
|
31
|
+
acc[name] = model
|
|
32
|
+
return acc
|
|
33
|
+
}, {} as Record<string, mongoose.Model<any>>)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export const loadModel = async (modelName: string, ctx: Ctx) => {
|
|
37
|
+
|
|
38
|
+
const tenantId = ctx.req.session.user?.current_tenant_id || "00000000"
|
|
39
|
+
assert(tenantId, "Tenant ID is missing from session")
|
|
40
|
+
|
|
41
|
+
const dbName = ["User", "Tenant"].includes(modelName)
|
|
42
|
+
? `${APP_NAME}-users-${DB_NAME}`
|
|
43
|
+
: `${APP_NAME}-${tenantId}-${DB_NAME}`
|
|
44
|
+
|
|
45
|
+
const connectionString = `mongodb://localhost:27017/${dbName}`
|
|
46
|
+
|
|
47
|
+
if (!connections[dbName]) {
|
|
48
|
+
const connection = mongoose.createConnection(connectionString, {
|
|
49
|
+
sanitizeFilter: true,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
await new Promise((resolve, reject) => {
|
|
53
|
+
connection.once("open", resolve)
|
|
54
|
+
connection.on("error", reject)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
connections[dbName] = connection
|
|
58
|
+
} else {
|
|
59
|
+
const connection = connections[dbName]
|
|
60
|
+
if (connection.readyState !== 1) {
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
connection.once("open", resolve)
|
|
63
|
+
connection.on("error", reject)
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const modelConnection = connections[dbName]
|
|
69
|
+
|
|
70
|
+
// const modelModule = Object.entries(models).find(([_, module]) =>
|
|
71
|
+
// (module as { [key: string]: any })[modelName] !== undefined
|
|
72
|
+
// )?.[1] as { [key: string]: any } | undefined
|
|
73
|
+
|
|
74
|
+
// if (!modelModule) {
|
|
75
|
+
// throw new Error(`Model ${modelName} not found in any directory`)
|
|
76
|
+
// }
|
|
77
|
+
|
|
78
|
+
const model = models[modelName]
|
|
79
|
+
|
|
80
|
+
if (!modelConnection.models[modelName]) {
|
|
81
|
+
modelConnection.model(modelName, model.schema)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return modelConnection.models[modelName]
|
|
85
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type {Request, Response, NextFunction} from "express"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const MOCK_TYPE = "MOCK_TYPE"
|
|
5
|
+
|
|
6
|
+
export type Ctx = {
|
|
7
|
+
req: Request;
|
|
8
|
+
res: Response;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// export type Payload = Record<string, unknown>
|
|
13
|
+
export type Payload = any
|
|
14
|
+
export type Result = Record<string, unknown>
|
|
15
|
+
|
|
16
|
+
export type ApiHandler = (payload: Payload, ctx: Ctx) => Promise<Result>
|
|
17
|
+
|
|
18
|
+
export type Middleware = (req: Request, res: Response, next: NextFunction) => void;
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export type Api = {
|
|
22
|
+
use: (path: string, middleware: Middleware) => void;
|
|
23
|
+
get: (path: string, handler: ApiHandler) => void;
|
|
24
|
+
post: (path: string, handler: ApiHandler) => void;
|
|
25
|
+
}
|