1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-10-16 15:07:27 +00:00
festa/utils/querystring.ts

21 lines
No EOL
730 B
TypeScript

import { ParsedUrlQuery } from "querystring"
/**
* Ensure that the passed {@link ParsedUrlQuery} object has **one and only one** key with the specified name, and get its value.
*
* @param queryObj The object to read the value from.
* @param key The name of the value to read.
* @returns The resulting string.
*/
export function getSingle(queryObj: ParsedUrlQuery, key: string): string {
const value = queryObj[key]
switch(typeof value) {
case "undefined":
throw new Error(`No "${key}" parameter found in the query string.`)
case "object":
throw new Error(`Multiple "${key}" parameters specified in the query string.`)
case "string":
return value
}
}