jbilcke-hf's picture
jbilcke-hf HF staff
a reward for the most friendly users
3411c0f
raw
history blame
582 Bytes
import queryString from 'query-string'
export function getParam<T>(name: string, defaultValue: T): T {
try {
const params = queryString.parseUrl(
typeof window !== "undefined" ? (window.location.href || "") : ""
)
const stringValue = params.query[name]?.toString() || `${defaultValue || ""}`
if (typeof defaultValue === "number") {
return Number(stringValue) as T
} else if (typeof defaultValue === "boolean") {
return Boolean(stringValue) as T
} else {
return stringValue as T
}
} catch (err) {
return defaultValue
}
}