From 69a859ca9dfc2c7fa075d6ff42e09103d95b1349 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Thu, 27 Nov 2025 01:21:20 +0800 Subject: [PATCH] refactor(package): simple type solution --- lib/pkg.ts | 2 +- scripts/workflow/build-routes.ts | 45 ++++---------------------------- 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/lib/pkg.ts b/lib/pkg.ts index f8511ee0d..1a00c17e7 100644 --- a/lib/pkg.ts +++ b/lib/pkg.ts @@ -17,7 +17,7 @@ export const init = async (conf?: ConfigEnv) => { app = (await import('@/app')).default; }; -export const request = async (path: RoutePath) => { +export const request = async (path: RoutePath | (string & {})) => { const res = await app.request(path); return res.json() as Promise; }; diff --git a/scripts/workflow/build-routes.ts b/scripts/workflow/build-routes.ts index ce2648468..9da667529 100644 --- a/scripts/workflow/build-routes.ts +++ b/scripts/workflow/build-routes.ts @@ -29,6 +29,9 @@ const radar: { }; } = {}; +// Generate route paths type +const allRoutePaths = new Set(); + for (const namespace in namespaces) { let defaultCategory = namespaces[namespace].categories?.[0]; if (!defaultCategory) { @@ -44,6 +47,7 @@ for (const namespace in namespaces) { } for (const path in namespaces[namespace].routes) { const realPath = `/${namespace}${path}`; + allRoutePaths.add(realPath); const data = namespaces[namespace].routes[path]; const categories = data.categories || namespaces[namespace].categories || [defaultCategory]; if (foloAnalysisTop100.some(([path]) => path === realPath)) { @@ -88,47 +92,8 @@ for (const namespace in namespaces) { } } -// Generate route paths type -const allRoutePaths: string[] = []; -for (const namespace in namespaces) { - for (const path in namespaces[namespace].routes) { - const fullPath = `/${namespace}${path}`; - - // Split path into segments to handle optional parameters - const segments = fullPath.split('/'); - let hasOptional = false; - - // Process each segment - const processedSegments = segments.map((segment) => { - if (segment.endsWith('?')) { - hasOptional = true; - return '${string}'; - } else if (segment.startsWith(':')) { - return '${string}'; - } - return segment; - }); - - if (hasOptional) { - // Generate both versions: with and without optional parameters - // Find the index where optional parameters start - const firstOptionalIndex = segments.findIndex((s) => s.endsWith('?')); - - // Version without optional parameters - const requiredPath = processedSegments.slice(0, firstOptionalIndex).join('/'); - allRoutePaths.push(requiredPath); - - // Version with optional parameters - const fullProcessedPath = processedSegments.join('/'); - allRoutePaths.push(fullProcessedPath); - } else { - allRoutePaths.push(processedSegments.join('/')); - } - } -} - // Remove duplicates and sort -const uniquePaths = [...new Set(allRoutePaths)].toSorted(); +const uniquePaths = [...allRoutePaths].toSorted(); const routePathsType = `// This file is auto-generated by scripts/workflow/build-routes.ts // Do not edit manually