@rpcbase/api 0.27.0 → 0.28.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.
@@ -0,0 +1,4 @@
1
+ export * from "./types";
2
+ export * from "./initApi";
3
+ export * from "./loadModel";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../pkg/api/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./initApi";
3
+ export * from "./loadModel";
@@ -0,0 +1,3 @@
1
+ import { Application } from "express";
2
+ export declare const initApi: (app: Application) => Promise<void>;
3
+ //# sourceMappingURL=initApi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initApi.d.ts","sourceRoot":"","sources":["../../../../pkg/api/src/initApi.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAS,MAAM,SAAS,CAAA;AAiB5C,eAAO,MAAM,OAAO,GAAU,KAAK,WAAW,kBA4E7C,CAAA"}
@@ -0,0 +1,72 @@
1
+ import assert from "assert";
2
+ import env from "@rpcbase/env";
3
+ import BBPromise from "bluebird";
4
+ import { json } from "express";
5
+ import { initApiClient } from "@rpcbase/client";
6
+ // dotenv setup, merge process env with vite process.env file
7
+ process.env = {
8
+ ...__vite_env__,
9
+ ...process.env,
10
+ ...env.config().parsed
11
+ };
12
+ export const initApi = async (app) => {
13
+ await initApiClient({ app });
14
+ const registerHandler = (method, path, handler) => {
15
+ assert(path.startsWith("/api"), "API routes must start with /api");
16
+ app[method](path, async (req, res, next) => {
17
+ const payload = req.body || {};
18
+ try {
19
+ const result = await handler(payload, { req, res });
20
+ res.json(result);
21
+ }
22
+ catch (e) {
23
+ next(e);
24
+ }
25
+ });
26
+ };
27
+ const api = {
28
+ use: (path, middleware) => {
29
+ app.use(path, middleware);
30
+ },
31
+ get: (path, handler) => {
32
+ registerHandler("get", path, handler);
33
+ },
34
+ post: (path, handler) => {
35
+ registerHandler("post", path, handler);
36
+ }
37
+ };
38
+ // body parser
39
+ app.use("/api", json({ limit: "8mb" }));
40
+ // WARNING:
41
+ // This is used to mark the start of the api middleware chain
42
+ // keep this middleware named like this, the serer api client will start calling middlewares starting from here
43
+ app.use("/api", function __FIRST_API_MIDDLEWARE__(_req, _res, next) {
44
+ next();
45
+ });
46
+ const routes = {
47
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
48
+ ...import.meta.glob("../../auth/src/api/**/handler.ts"),
49
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
50
+ ...import.meta.glob("@/api/**/handler.ts")
51
+ };
52
+ const routeEntries = Object.entries(routes);
53
+ const loadRoute = async ([routePath, loadFn]) => {
54
+ const routeModule = await loadFn();
55
+ routeModule.default(api);
56
+ const loggedPath = routePath.startsWith("../../")
57
+ ? routePath.replace("../../", "@rpcbase/")
58
+ : routePath;
59
+ // Colorize output: gray for @rpcbase paths, default (white) otherwise
60
+ const GRAY = "\x1b[90m";
61
+ const RESET = "\x1b[0m";
62
+ const coloredPath = loggedPath.startsWith("@rpcbase/")
63
+ ? `${GRAY}${loggedPath}${RESET}`
64
+ : loggedPath;
65
+ console.log("loaded api route", coloredPath);
66
+ };
67
+ await BBPromise.map(routeEntries, loadRoute);
68
+ console.log("Done loading routes");
69
+ app.use("/api", (req, res) => {
70
+ res.status(404).json({ error: `Not Found: ${req.method}:${req.originalUrl}` });
71
+ });
72
+ };
@@ -0,0 +1,4 @@
1
+ import mongoose from "mongoose";
2
+ import { Ctx } from "./types";
3
+ export declare const loadModel: (modelName: string, ctx: Ctx) => Promise<mongoose.Model<any, {}, {}, {}, any, any>>;
4
+ //# sourceMappingURL=loadModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadModel.d.ts","sourceRoot":"","sources":["../../../../pkg/api/src/loadModel.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,UAAU,CAAA;AAE/B,OAAO,EAAE,GAAG,EAAE,MAAM,SAAS,CAAA;AAkC7B,eAAO,MAAM,SAAS,GAAU,WAAW,MAAM,EAAE,KAAK,GAAG,uDAiD1D,CAAA"}
@@ -0,0 +1,64 @@
1
+ import assert from "assert";
2
+ import mongoose from "mongoose";
3
+ const { APP_NAME, DB_PORT } = process.env;
4
+ const connections = {};
5
+ // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
6
+ const modelModules = import.meta.glob("@/models/**/*.ts", { eager: true });
7
+ const models = Object.entries(modelModules)
8
+ .map(([key, exports]) => {
9
+ assert(key.startsWith("/src/models/"), "Expected Model path to start with /src/models/");
10
+ const modelName = key.replace("/src/models/", "").replace(".ts", "").split("/").pop();
11
+ assert(modelName, "Model name not found");
12
+ const schema = Object.entries(exports)
13
+ .find(([key]) => {
14
+ return key === `${modelName}Schema`;
15
+ });
16
+ assert(schema, `Schema not found for model ${modelName}`);
17
+ return {
18
+ name: modelName,
19
+ model: mongoose.model(modelName, schema[1])
20
+ };
21
+ })
22
+ .reduce((acc, { name, model }) => {
23
+ acc[name] = model;
24
+ return acc;
25
+ }, {});
26
+ export const loadModel = async (modelName, ctx) => {
27
+ const tenantId = ctx.req.session.user?.current_tenant_id || "00000000";
28
+ assert(tenantId, "Tenant ID is missing from session");
29
+ const dbName = ["User", "Tenant"].includes(modelName)
30
+ ? `${APP_NAME}-users-db`
31
+ : `${APP_NAME}-${tenantId}-db`;
32
+ const connectionString = `mongodb://localhost:${DB_PORT}/${dbName}`;
33
+ if (!connections[dbName]) {
34
+ const connection = mongoose.createConnection(connectionString, {
35
+ sanitizeFilter: true,
36
+ });
37
+ await new Promise((resolve, reject) => {
38
+ connection.once("open", resolve);
39
+ connection.on("error", reject);
40
+ });
41
+ connections[dbName] = connection;
42
+ }
43
+ else {
44
+ const connection = connections[dbName];
45
+ if (connection.readyState !== 1) {
46
+ await new Promise((resolve, reject) => {
47
+ connection.once("open", resolve);
48
+ connection.on("error", reject);
49
+ });
50
+ }
51
+ }
52
+ const modelConnection = connections[dbName];
53
+ // const modelModule = Object.entries(models).find(([_, module]) =>
54
+ // (module as { [key: string]: any })[modelName] !== undefined
55
+ // )?.[1] as { [key: string]: any } | undefined
56
+ // if (!modelModule) {
57
+ // throw new Error(`Model ${modelName} not found in any directory`)
58
+ // }
59
+ const model = models[modelName];
60
+ if (!modelConnection.models[modelName]) {
61
+ modelConnection.model(modelName, model.schema);
62
+ }
63
+ return modelConnection.models[modelName];
64
+ };
@@ -0,0 +1,16 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ export declare const MOCK_TYPE = "MOCK_TYPE";
3
+ export type Ctx = {
4
+ req: Request;
5
+ res: Response;
6
+ };
7
+ export type Payload = any;
8
+ export type Result = Record<string, unknown>;
9
+ export type ApiHandler = (payload: Payload, ctx: Ctx) => Promise<Result>;
10
+ export type Middleware = (req: Request, res: Response, next: NextFunction) => void;
11
+ export type Api = {
12
+ use: (path: string, middleware: Middleware) => void;
13
+ get: (path: string, handler: ApiHandler) => void;
14
+ post: (path: string, handler: ApiHandler) => void;
15
+ };
16
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../pkg/api/src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAC,MAAM,SAAS,CAAA;AAG5D,eAAO,MAAM,SAAS,cAAc,CAAA;AAEpC,MAAM,MAAM,GAAG,GAAG;IAChB,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,CAAC;AAIF,MAAM,MAAM,OAAO,GAAG,GAAG,CAAA;AACzB,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAE5C,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;AAExE,MAAM,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;AAGnF,MAAM,MAAM,GAAG,GAAG;IAChB,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,KAAK,IAAI,CAAC;IACpD,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;IACjD,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,KAAK,IAAI,CAAC;CACnD,CAAA"}
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export const MOCK_TYPE = "MOCK_TYPE";
package/package.json CHANGED
@@ -1,16 +1,34 @@
1
1
  {
2
2
  "name": "@rpcbase/api",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "type": "module",
5
- "main": "./src/index.ts",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
6
10
  "scripts": {
7
- "build": "tsc --watch",
11
+ "build": "wireit",
8
12
  "release": "wireit"
9
13
  },
10
14
  "wireit": {
15
+ "build": {
16
+ "command": "node ../../scripts/build-package.js api",
17
+ "files": [
18
+ "src/**/*",
19
+ "../../tsconfig.json",
20
+ "../../tsconfig.base.json",
21
+ "../../scripts/build-package.js"
22
+ ],
23
+ "output": [
24
+ "dist/"
25
+ ]
26
+ },
11
27
  "release": {
12
28
  "command": "../../scripts/publish.js",
13
- "dependencies": [],
29
+ "dependencies": [
30
+ "build"
31
+ ],
14
32
  "files": [
15
33
  "package.json",
16
34
  "src/**/*"
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./types"
2
- export * from "./initApi"
3
- export * from "./loadModel"
package/src/initApi.ts DELETED
@@ -1,98 +0,0 @@
1
- import assert from "assert"
2
-
3
- import env from "@rpcbase/env"
4
- import BBPromise from "bluebird"
5
- import { Application , json } from "express"
6
- import { initApiClient } from "@rpcbase/client"
7
-
8
- import { Api, ApiHandler, Middleware } from "./types"
9
-
10
-
11
- // dotenv setup, merge process env with vite process.env file
12
-
13
- process.env = {
14
- ...__vite_env__,
15
- ...process.env,
16
- ...env.config().parsed
17
- }
18
-
19
-
20
- type RouteModule = [string, () => Promise<{default: (api: Api) => void}>]
21
-
22
- export const initApi = async (app: Application) => {
23
-
24
- await initApiClient({app})
25
-
26
- const registerHandler = (method: "get" | "post" | "put" | "delete", path: string, handler: ApiHandler) => {
27
- assert(path.startsWith("/api"), "API routes must start with /api")
28
-
29
- app[method](path, async(req, res, next) => {
30
- const payload = req.body || {}
31
-
32
- try {
33
- const result = await handler(payload, {req, res})
34
- res.json(result)
35
- } catch (e) {
36
- next(e)
37
- }
38
- })
39
- }
40
-
41
- const api = {
42
- use: (path: string, middleware: Middleware): void => {
43
- app.use(path, middleware)
44
- },
45
- get: (path: string, handler: ApiHandler): void => {
46
- registerHandler("get", path, handler)
47
- },
48
- post: (path: string, handler: ApiHandler): void => {
49
- registerHandler("post", path, handler)
50
- }
51
- }
52
-
53
- // body parser
54
- app.use("/api", json({limit: "8mb"}))
55
-
56
- // WARNING:
57
- // This is used to mark the start of the api middleware chain
58
- // keep this middleware named like this, the serer api client will start calling middlewares starting from here
59
- app.use("/api", function __FIRST_API_MIDDLEWARE__(_req, _res, next) {
60
- next()
61
- })
62
-
63
- const routes = {
64
- // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
65
- ...import.meta.glob("../../auth/src/api/**/handler.ts"),
66
- // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
67
- ...import.meta.glob("@/api/**/handler.ts")
68
- }
69
-
70
- const routeEntries = Object.entries(routes) as RouteModule[]
71
-
72
- const loadRoute = async ([routePath, loadFn]: RouteModule) => {
73
- const routeModule = await loadFn()
74
- routeModule.default(api)
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)
88
- }
89
-
90
- await BBPromise.map(routeEntries, loadRoute)
91
- console.log("Done loading routes")
92
-
93
-
94
- app.use("/api", (req, res) => {
95
- res.status(404).json({ error: `Not Found: ${req.method}:${req.originalUrl}` })
96
- })
97
-
98
- }
package/src/loadModel.ts DELETED
@@ -1,88 +0,0 @@
1
- import assert from "assert"
2
-
3
- import mongoose from "mongoose"
4
-
5
- import { Ctx } from "./types"
6
-
7
-
8
- const { APP_NAME, DB_PORT } = process.env
9
- const connections: Record<string, mongoose.Connection> = {}
10
-
11
- // @ts-expect-error: Property 'glob' does not exist on type 'ImportMeta'.
12
- const modelModules = import.meta.glob("@/models/**/*.ts", { eager: true })
13
-
14
- const models = Object.entries(modelModules)
15
- .map(([key, exports]) => {
16
- assert(key.startsWith("/src/models/"), "Expected Model path to start with /src/models/")
17
-
18
- const modelName = key.replace("/src/models/", "").replace(".ts", "").split("/").pop()
19
- assert(modelName, "Model name not found")
20
-
21
- const schema = Object.entries(exports as Record<string, unknown>)
22
- .find(([key]) => {
23
- return key === `${modelName}Schema`
24
- })
25
-
26
- assert(schema, `Schema not found for model ${modelName}`)
27
-
28
- return {
29
- name: modelName,
30
- model: mongoose.model(modelName, schema[1] as mongoose.Schema)
31
- }
32
- })
33
- .reduce((acc, { name, model }) => {
34
- acc[name] = model
35
- return acc
36
- }, {} as Record<string, mongoose.Model<any>>)
37
-
38
-
39
- export const loadModel = async (modelName: string, ctx: Ctx) => {
40
-
41
- const tenantId = ctx.req.session.user?.current_tenant_id || "00000000"
42
- assert(tenantId, "Tenant ID is missing from session")
43
-
44
- const dbName = ["User", "Tenant"].includes(modelName)
45
- ? `${APP_NAME}-users-db`
46
- : `${APP_NAME}-${tenantId}-db`
47
-
48
- const connectionString = `mongodb://localhost:${DB_PORT}/${dbName}`
49
-
50
- if (!connections[dbName]) {
51
- const connection = mongoose.createConnection(connectionString, {
52
- sanitizeFilter: true,
53
- })
54
-
55
- await new Promise((resolve, reject) => {
56
- connection.once("open", resolve)
57
- connection.on("error", reject)
58
- })
59
-
60
- connections[dbName] = connection
61
- } else {
62
- const connection = connections[dbName]
63
- if (connection.readyState !== 1) {
64
- await new Promise((resolve, reject) => {
65
- connection.once("open", resolve)
66
- connection.on("error", reject)
67
- })
68
- }
69
- }
70
-
71
- const modelConnection = connections[dbName]
72
-
73
- // const modelModule = Object.entries(models).find(([_, module]) =>
74
- // (module as { [key: string]: any })[modelName] !== undefined
75
- // )?.[1] as { [key: string]: any } | undefined
76
-
77
- // if (!modelModule) {
78
- // throw new Error(`Model ${modelName} not found in any directory`)
79
- // }
80
-
81
- const model = models[modelName]
82
-
83
- if (!modelConnection.models[modelName]) {
84
- modelConnection.model(modelName, model.schema)
85
- }
86
-
87
- return modelConnection.models[modelName]
88
- }
package/src/types.ts DELETED
@@ -1,25 +0,0 @@
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
- }