change some defaults

This commit is contained in:
2025-04-22 13:43:33 +02:00
parent de3a4fb281
commit ac0977b84b
28 changed files with 1465 additions and 8922 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 | undefined>, skipEncoding = false): string {
let result: string = url
// early return if there are no params
@ -26,11 +26,16 @@ export function formatRoute<T extends string>(url: T, params?: Record<string, st
let externalQueries = ''
// loop through the parameters
objectLoop(params, (value, key) => {
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)
}
@ -51,10 +56,11 @@ export function formatRoute<T extends string>(url: T, params?: Record<string, st
*/
async function updateRoutes(output: string, routes: Array<string>) {
let file = baseFile
file += `\n\nexport type Routes = ${routes.map((it) => `'${it}'`).join(' | ')}`
const routesStr = routes.map((it) => `'${it}'`).join(' | ')
file += `\n\nexport type Routes = ${routesStr}`
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 | undefined>, skipEncoding = false) {'
file += '\n\treturn formatRoute(route, query, skipEncoding)'
file += '\n}\n'
await fs.writeFile(output, file)
@ -100,7 +106,7 @@ async function getFiles(path: string): Promise<Array<string>> {
const dir = await fs.readdir(path)
let files: Array<string> = []
for (const file of dir) {
if (file.startsWith('_')) continue
if (file.startsWith('_')) { continue }
const filePath = path + '/' + file
if ((await fs.stat(filePath)).isDirectory()) {
files = files.concat(await getFiles(filePath))
@ -154,11 +160,11 @@ const integration: () => AstroIntegration = () => ({
// ignore files starting with '_'
const filename = path.slice(path.lastIndexOf('/') + 1)
if (filename.startsWith('_')) return
if (filename.startsWith('_')) { return }
let removeExtension = true
let folder = pagesFolder
if(path.startsWith(publicFolder)) {
if (path.startsWith(publicFolder)) {
removeExtension = false
folder = publicFolder
} else if (!path.startsWith(folder)) {
@ -178,7 +184,7 @@ const integration: () => AstroIntegration = () => ({
path = path.replace(/\\/g, '/')
let removeExtension = true
let folder = pagesFolder
if(path.startsWith(publicFolder)) {
if (path.startsWith(publicFolder)) {
removeExtension = false
folder = publicFolder
}