refactor(package): simple type solution

This commit is contained in:
Stephen Zhou 2025-11-27 01:21:20 +08:00
parent bc86bda10d
commit 69a859ca9d
No known key found for this signature in database
2 changed files with 6 additions and 41 deletions

View File

@ -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<Data>;
};

View File

@ -29,6 +29,9 @@ const radar: {
};
} = {};
// Generate route paths type
const allRoutePaths = new Set<string>();
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