@rpcbase/api 0.29.0 → 0.30.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/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Request, Response, NextFunction } from "express";
1
+ import { Request, Response, NextFunction } from 'express';
2
2
  export declare const MOCK_TYPE = "MOCK_TYPE";
3
3
  export type Ctx = {
4
4
  req: Request;
@@ -1 +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"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/api",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -13,12 +13,14 @@
13
13
  },
14
14
  "wireit": {
15
15
  "build": {
16
- "command": "node ../../scripts/build-package.js api",
16
+ "command": "../../node_modules/.bin/vite build",
17
17
  "files": [
18
18
  "src/**/*",
19
19
  "../../tsconfig.json",
20
- "../../tsconfig.base.json",
21
- "../../scripts/build-package.js"
20
+ "../../scripts/tsconfig.pkg.json",
21
+ "./tsconfig.json",
22
+ "./vite.config.ts",
23
+ "../../scripts/vite.config-pkg.js"
22
24
  ],
23
25
  "output": [
24
26
  "dist/"
package/dist/initApi.js DELETED
@@ -1,72 +0,0 @@
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
- };
package/dist/loadModel.js DELETED
@@ -1,64 +0,0 @@
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
- };
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export const MOCK_TYPE = "MOCK_TYPE";