mirror of
https://github.com/Steffo99/festa.git
synced 2024-12-23 07:04:22 +00:00
21 lines
730 B
TypeScript
21 lines
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
|
||
|
}
|
||
|
}
|