|
1 | 1 | import type Datastore from "@seald-io/nedb"; |
2 | | -import type { Request, RequestHandler, Response, Router } from "express"; |
| 2 | +import type { Request, Response, Router } from "express"; |
3 | 3 | import { Router as createRouter, json } from "express"; |
4 | 4 | import { nanoid } from "nanoid"; |
5 | | -import type { Logger } from "../../createDevLogger.js"; |
6 | | -import type { Database } from "../database.js"; |
7 | | -import type { BroadcastEntityEvent, EntityEventType } from "../realtime.js"; |
| 5 | +import type { Logger } from "../../../createDevLogger.js"; |
| 6 | +import type { Database } from "../../database.js"; |
| 7 | +import type { BroadcastEntityEvent, EntityEventType } from "../../realtime.js"; |
| 8 | +import { createUserRouter } from "./entities-user-router.js"; |
| 9 | +import { stripInternalFields } from "./utils.js"; |
8 | 10 |
|
9 | 11 | interface EntityParams { |
10 | 12 | appId: string; |
@@ -51,28 +53,11 @@ function parseFields( |
51 | 53 | return Object.keys(projection).length > 0 ? projection : undefined; |
52 | 54 | } |
53 | 55 |
|
54 | | -function stripInternalFields<T extends Record<string, unknown>>( |
55 | | - doc: T[], |
56 | | -): Omit<T, "_id">[]; |
57 | | -function stripInternalFields<T extends Record<string, unknown>>( |
58 | | - doc: T, |
59 | | -): Omit<T, "_id">; |
60 | | -function stripInternalFields<T extends Record<string, unknown>>( |
61 | | - doc: T | T[], |
62 | | -): Omit<T, "_id"> | Omit<T, "_id">[] { |
63 | | - if (Array.isArray(doc)) { |
64 | | - return doc.map((d) => stripInternalFields(d)); |
65 | | - } |
66 | | - const { _id, ...rest } = doc; |
67 | | - return rest; |
68 | | -} |
69 | | - |
70 | | -export function createEntityRoutes( |
| 56 | +export async function createEntityRoutes( |
71 | 57 | db: Database, |
72 | 58 | logger: Logger, |
73 | | - remoteProxy: RequestHandler, |
74 | 59 | broadcast: BroadcastEntityEvent, |
75 | | -): Router { |
| 60 | +): Promise<Router> { |
76 | 61 | const router = createRouter({ mergeParams: true }); |
77 | 62 | const parseBody = json(); |
78 | 63 |
|
@@ -116,15 +101,8 @@ export function createEntityRoutes( |
116 | 101 | broadcast(appId, entityName, createData(data)); |
117 | 102 | } |
118 | 103 |
|
119 | | - router.get("/User/:id", (req, res, next) => { |
120 | | - logger.warn( |
121 | | - `"${req.originalUrl}" is not supported in local development, passing call to production`, |
122 | | - ); |
123 | | - // This is necessary because Express strips the router prefix from req.url, |
124 | | - // so without this the proxy would send just `/User/:id` instead of the full path. |
125 | | - req.url = req.originalUrl; |
126 | | - remoteProxy(req, res, next); |
127 | | - }); |
| 104 | + const userRouter = await createUserRouter(db); |
| 105 | + router.use("/User", userRouter); |
128 | 106 |
|
129 | 107 | router.get( |
130 | 108 | "/:entityName/:id", |
|
0 commit comments