Skip to content

Instantly share code, notes, and snippets.

@TheLucifurry
Created March 26, 2024 05:16
Show Gist options
  • Select an option

  • Save TheLucifurry/364529aefa9b9b79d60eca89cade1a47 to your computer and use it in GitHub Desktop.

Select an option

Save TheLucifurry/364529aefa9b9b79d60eca89cade1a47 to your computer and use it in GitHub Desktop.
import type { LocationQueryValue } from 'vue-router'
type TQueryParams = Record<string, LocationQueryValue | LocationQueryValue[]>
type TQueryParamsValues = string | Record<string, unknown> | Array<unknown>
export function getEncodedQueryParams(params: Record<string, TQueryParamsValues>): TQueryParams {
return Object.entries(params)
.reduce((acc, [key, value]) => {
if (typeof value === 'string') {
acc[key] = encodeURIComponent(decodeURIComponent(value))
} else {
try {
acc[key] = encodeURIComponent(JSON.stringify(value))
} catch (error) {}
}
return acc
}, {} as TQueryParams)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment