feat: Add base to project

Signed-off-by: Avior <git@avior.me>
This commit is contained in:
2024-05-16 16:45:50 +02:00
parent 9b2d412a9e
commit f50ec828fb
36 changed files with 5248 additions and 1698 deletions

View File

@ -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)