(fastify)
| 18 | const idParam = z.object({ id: z.string() }); |
| 19 | |
| 20 | const manageRouter: FastifyPluginAsyncZodOpenApi = async (fastify) => { |
| 21 | await activateRateLimiter({ |
| 22 | fastify, |
| 23 | max: 20, |
| 24 | timeWindow: '10 seconds', |
| 25 | }); |
| 26 | |
| 27 | fastify.addHook('preHandler', async (req: FastifyRequest, reply) => { |
| 28 | try { |
| 29 | const client = await validateManageRequest(req.headers); |
| 30 | req.client = client; |
| 31 | } catch (e) { |
| 32 | if (e instanceof Prisma.PrismaClientKnownRequestError) { |
| 33 | return reply.status(401).send({ |
| 34 | error: 'Unauthorized', |
| 35 | message: 'Client ID seems to be malformed', |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | if (e instanceof Error) { |
| 40 | return reply |
| 41 | .status(401) |
| 42 | .send({ error: 'Unauthorized', message: e.message }); |
| 43 | } |
| 44 | |
| 45 | return reply |
| 46 | .status(401) |
| 47 | .send({ error: 'Unauthorized', message: 'Unexpected error' }); |
| 48 | } |
| 49 | |
| 50 | // Validate :projectId URL param belongs to this client's organization. |
| 51 | const client = req.client!; |
| 52 | const params = req.params as { projectId?: string }; |
| 53 | if (params.projectId) { |
| 54 | try { |
| 55 | await resolveClientProjectId({ |
| 56 | clientType: 'root', |
| 57 | clientProjectId: null, |
| 58 | organizationId: client.organizationId, |
| 59 | inputProjectId: params.projectId, |
| 60 | }); |
| 61 | } catch { |
| 62 | return reply.status(403).send({ error: 'Forbidden', message: 'Project does not belong to your organization' }); |
| 63 | } |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | // Projects routes |
| 68 | fastify.route({ |
| 69 | method: 'GET', |
| 70 | url: '/projects', |
| 71 | schema: { tags: ['Manage'], description: 'List all projects for the organization.' }, |
| 72 | handler: controller.listProjects, |
| 73 | }); |
| 74 | |
| 75 | fastify.route({ |
| 76 | method: 'GET', |
| 77 | url: '/projects/:id', |
nothing calls this directly
no test coverage detected