(fastify)
| 5 | import { Prisma } from '@openpanel/db'; |
| 6 | |
| 7 | const importRouter: FastifyPluginCallback = async (fastify) => { |
| 8 | fastify.addHook('preHandler', async (req: FastifyRequest, reply) => { |
| 9 | try { |
| 10 | const client = await validateImportRequest(req.headers); |
| 11 | req.client = client; |
| 12 | } catch (e) { |
| 13 | if (e instanceof Prisma.PrismaClientKnownRequestError) { |
| 14 | return reply.status(401).send({ |
| 15 | error: 'Unauthorized', |
| 16 | message: 'Client ID seems to be malformed', |
| 17 | }); |
| 18 | } |
| 19 | |
| 20 | if (e instanceof Error) { |
| 21 | return reply |
| 22 | .status(401) |
| 23 | .send({ error: 'Unauthorized', message: e.message }); |
| 24 | } |
| 25 | |
| 26 | return reply |
| 27 | .status(401) |
| 28 | .send({ error: 'Unauthorized', message: 'Unexpected error' }); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | fastify.route({ |
| 33 | method: 'POST', |
| 34 | url: '/events', |
| 35 | schema: { |
| 36 | tags: ['Import'], |
| 37 | description: 'Bulk import historical events.', |
| 38 | }, |
| 39 | handler: controller.importEvents, |
| 40 | }); |
| 41 | }; |
| 42 | |
| 43 | export default importRouter; |
nothing calls this directly
no test coverage detected