Skip to content

Instantly share code, notes, and snippets.

@JiveOff
Created March 2, 2023 21:14
Show Gist options
  • Select an option

  • Save JiveOff/4b5bdb60dc94d4c15b7036a8a7684e1b to your computer and use it in GitHub Desktop.

Select an option

Save JiveOff/4b5bdb60dc94d4c15b7036a8a7684e1b to your computer and use it in GitHub Desktop.
Table Query Params decorator for Fastify
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