(fastify, options, done)
| 26 | * Fastify plugin for handling CRUD requests. |
| 27 | */ |
| 28 | const pluginHandler: FastifyPluginCallback<FastifyPluginOptions<SchemaDef>> = (fastify, options, done) => { |
| 29 | const prefix = options.prefix ?? ''; |
| 30 | |
| 31 | fastify.all(`${prefix}/*`, async (request, reply) => { |
| 32 | const client = await options.getClient(request, reply); |
| 33 | if (!client) { |
| 34 | reply.status(500).send({ message: 'unable to get ZenStackClient from request context' }); |
| 35 | return reply; |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | const response = await options.apiHandler.handleRequest({ |
| 40 | method: request.method, |
| 41 | path: (request.params as any)['*'], |
| 42 | query: request.query as Record<string, string | string[]>, |
| 43 | requestBody: request.body, |
| 44 | client, |
| 45 | }); |
| 46 | reply.status(response.status).send(response.body); |
| 47 | } catch (err) { |
| 48 | logInternalError(options.apiHandler.log, err); |
| 49 | reply.status(500).send({ message: `An internal server error occurred` }); |
| 50 | } |
| 51 | |
| 52 | return reply; |
| 53 | }); |
| 54 | |
| 55 | done(); |
| 56 | }; |
| 57 | |
| 58 | const plugin = fp(pluginHandler); |
| 59 |
nothing calls this directly
no test coverage detected