generated from avior/template-web-astro
@ -14,7 +14,7 @@ import { objectLoop } from '@dzeio/object-util'
|
||||
*
|
||||
* @returns the URL formatted with the params
|
||||
*/
|
||||
export function formatRoute<T extends string>(url: T, params?: Record<string, string | number>): string {
|
||||
export function formatRoute<T extends string>(url: T, params?: Record<string, string | number>, skipEncoding = false): string {
|
||||
let result: string = url
|
||||
|
||||
// early return if there are no params
|
||||
@ -28,9 +28,14 @@ export function formatRoute<T extends string>(url: T, params?: Record<string, st
|
||||
// loop through the parameters
|
||||
objectLoop(params, (value, key) => {
|
||||
const search = \`[\${key}]\`
|
||||
value = encodeURI(value.toString())
|
||||
if (!skipEncoding) {
|
||||
value = encodeURI(value.toString())
|
||||
key = encodeURI(key)
|
||||
} else {
|
||||
value = value.toString()
|
||||
}
|
||||
if (!result.includes(search)) {
|
||||
externalQueries += \`\${encodeURI(key)}=\${value}&\`
|
||||
externalQueries += \`\${key}=\${value}&\`
|
||||
} else {
|
||||
result = result.replace(search, value)
|
||||
}
|
||||
@ -53,8 +58,8 @@ async function updateRoutes(output: string, routes: Array<string>) {
|
||||
let file = baseFile
|
||||
file += `\n\nexport type Routes = ${routes.map((it) => `'${it}'`).join(' | ')}`
|
||||
|
||||
file += '\n\nexport default function route(route: Routes, query?: Record<string, string | number>) {'
|
||||
file += '\n\treturn formatRoute(route, query)'
|
||||
file += '\n\nexport default function route(route: Routes, query?: Record<string, string | number>, skipEncoding = false) {'
|
||||
file += '\n\treturn formatRoute(route, query, skipEncoding)'
|
||||
file += '\n}\n'
|
||||
|
||||
await fs.writeFile(output, file)
|
||||
|
Reference in New Issue
Block a user