Created
March 2, 2023 21:14
-
-
Save JiveOff/4b5bdb60dc94d4c15b7036a8a7684e1b to your computer and use it in GitHub Desktop.
Table Query Params decorator for Fastify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { FastifyRequest } from "fastify" | |
| export function HasTableParams() { | |
| return function ( | |
| target: object, | |
| propertyKey: string | symbol, | |
| descriptor: PropertyDescriptor | |
| ) { | |
| const originalMethod = descriptor.value | |
| descriptor.value = async function (...args: never[]) { | |
| const req = args[0] as FastifyRequest | |
| req.query = req.query as Record<string, string[]> | |
| const querystring = req.routeSchema.querystring as { | |
| properties: { | |
| [key: string]: { | |
| type: string | |
| } | |
| } | |
| } | |
| const arrayParams = Object.entries(querystring.properties).filter( | |
| ([, value]) => value.type === "array" | |
| ) | |
| for (const [key, value] of Object.entries(req.query)) { | |
| if (arrayParams.some(([param]) => param === key)) { | |
| req.query[key] = value[0].split(",") | |
| } | |
| } | |
| return originalMethod.apply(this, args) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment