Skip to content

Commit 0ce19ac

Browse files
fix: allow to load dynamic rest endpoint (#35731)
Co-authored-by: Guilherme Gazzo <[email protected]>
1 parent 2daf2c0 commit 0ce19ac

File tree

2 files changed

+53
-62
lines changed

2 files changed

+53
-62
lines changed

.changeset/serious-grapes-smell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
---
4+
5+
Fixes an issue with dynamic API routes requiring a server restart to be operable.

apps/meteor/app/api/server/router.ts

+48-62
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ export class Router<
99
[x: string]: unknown;
1010
} = NonNullable<unknown>,
1111
> {
12-
private middleware: (router: express.Router) => void = () => void 0;
12+
public router;
1313

14-
constructor(readonly base: TBasePath) {}
14+
private innerRouter: express.Router;
15+
16+
constructor(readonly base: TBasePath) {
17+
// eslint-disable-next-line new-cap
18+
this.router = express.Router();
19+
// eslint-disable-next-line new-cap
20+
this.innerRouter = express.Router();
21+
this.router.use(this.base, this.innerRouter);
22+
}
1523

1624
public typedRoutes: Record<string, Record<string, unknown>> = {};
1725

@@ -82,46 +90,42 @@ export class Router<
8290
path: TPathPattern;
8391
} & Omit<TOptions, 'response'>)
8492
> {
85-
const prev = this.middleware;
86-
this.middleware = (router: express.Router) => {
87-
prev(router);
88-
router[method.toLowerCase() as Lowercase<Method>](`/${subpath}`.replace('//', '/'), async (req, res) => {
89-
const {
90-
body,
91-
statusCode = 200,
92-
headers = {},
93-
} = await action.apply(
94-
{
95-
urlParams: req.params,
96-
queryParams: req.query,
97-
bodyParams: (req as any).bodyParams || req.body,
98-
request: req,
99-
response: res,
100-
} as any,
101-
[req],
102-
);
103-
104-
const responseHeaders = Object.fromEntries(
105-
Object.entries({
106-
...res.header,
107-
'Content-Type': 'application/json',
108-
'Cache-Control': 'no-store',
109-
'Pragma': 'no-cache',
110-
...headers,
111-
}).map(([key, value]) => [key.toLowerCase(), value]),
112-
);
113-
114-
res.writeHead(statusCode, responseHeaders);
115-
116-
if (responseHeaders['content-type']?.match(/json|javascript/) !== null) {
117-
body !== undefined && res.write(JSON.stringify(body));
118-
} else {
119-
body !== undefined && res.write(body);
120-
}
121-
122-
res.end();
123-
});
124-
};
93+
this.innerRouter[method.toLowerCase() as Lowercase<Method>](`/${subpath}`.replace('//', '/'), async (req, res) => {
94+
const {
95+
body,
96+
statusCode = 200,
97+
headers = {},
98+
} = await action.apply(
99+
{
100+
urlParams: req.params,
101+
queryParams: req.query,
102+
bodyParams: (req as any).bodyParams || req.body,
103+
request: req,
104+
response: res,
105+
} as any,
106+
[req],
107+
);
108+
109+
const responseHeaders = Object.fromEntries(
110+
Object.entries({
111+
...res.header,
112+
'Content-Type': 'application/json',
113+
'Cache-Control': 'no-store',
114+
'Pragma': 'no-cache',
115+
...headers,
116+
}).map(([key, value]) => [key.toLowerCase(), value]),
117+
);
118+
119+
res.writeHead(statusCode, responseHeaders);
120+
121+
if (responseHeaders['content-type']?.match(/json|javascript/) !== null) {
122+
body !== undefined && res.write(JSON.stringify(body));
123+
} else {
124+
body !== undefined && res.write(body);
125+
}
126+
127+
res.end();
128+
});
125129
this.registerTypedRoutes(method, subpath, options);
126130
return this;
127131
}
@@ -199,31 +203,13 @@ export class Router<
199203
...Object.fromEntries(Object.entries(innerRouter.typedRoutes).map(([path, routes]) => [`${this.base}${path}`, routes])),
200204
};
201205

202-
const prev = this.middleware;
203-
this.middleware = (router: express.Router) => {
204-
prev(router);
205-
router.use(innerRouter.router);
206-
};
206+
this.innerRouter.use(innerRouter.router);
207207
}
208208
if (typeof innerRouter === 'function') {
209-
const prev = this.middleware;
210-
this.middleware = (router: express.Router) => {
211-
prev(router);
212-
router.use(innerRouter);
213-
};
209+
this.innerRouter.use(innerRouter);
214210
}
215211
return this as any;
216212
}
217-
218-
get router(): express.Router {
219-
// eslint-disable-next-line new-cap
220-
const router = express.Router();
221-
// eslint-disable-next-line new-cap
222-
const innerRouter = express.Router();
223-
this.middleware(innerRouter);
224-
router.use(this.base, innerRouter);
225-
return router;
226-
}
227213
}
228214

229215
type Prettify<T> = {

0 commit comments

Comments
 (0)