| 8 | |
| 9 | @Controller('api') |
| 10 | export class DynamicController { |
| 11 | // Handle all dynamic endpoint requests |
| 12 | @All('*') |
| 13 | async handleDynamic(@Req() req: Request, @Res() res: Response) { |
| 14 | const normalizedPath = req.path.replace(/^\/api/, ''); |
| 15 | |
| 16 | const match = await prisma.endpoint.findFirst({ |
| 17 | where: { |
| 18 | path: normalizedPath, |
| 19 | method: req.method, |
| 20 | }, |
| 21 | }); |
| 22 | |
| 23 | if (!match) { |
| 24 | throw new HttpException('Not Found', HttpStatus.NOT_FOUND); |
| 25 | } |
| 26 | |
| 27 | try { |
| 28 | let safeBody = req.body; |
| 29 | if (typeof safeBody === 'string') { |
| 30 | try { |
| 31 | safeBody = JSON.parse(safeBody); |
| 32 | } catch { |
| 33 | throw new HttpException('Invalid JSON body', HttpStatus.BAD_REQUEST); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if (match.language === 'javascript') { |
| 38 | const sandbox: any = { |
| 39 | req: { |
| 40 | path: req.path, |
| 41 | method: req.method, |
| 42 | headers: req.headers, |
| 43 | query: req.query, |
| 44 | body: safeBody || {}, |
| 45 | }, |
| 46 | res, |
| 47 | console, |
| 48 | setTimeout, |
| 49 | fetch: globalThis.fetch, |
| 50 | uuid: uuidv4, |
| 51 | helpers: { |
| 52 | sayHello: () => 'Hello from helper!', |
| 53 | sum: (a: number, b: number) => a + b, |
| 54 | }, |
| 55 | db: { |
| 56 | find: () => [{ id: 1, name: 'Test Record' }], |
| 57 | insert: (doc: any) => ({ insertedId: uuidv4(), ...doc }), |
| 58 | update: (id: string, changes: any) => ({ id, ...changes }), |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | const vm = new VM({ timeout: 1500, sandbox }); |
| 63 | |
| 64 | const wrappedCode = ` |
| 65 | const handler = ${match.code}; |
| 66 | handler(req); |
| 67 | `; |
nothing calls this directly
no outgoing calls
no test coverage detected