Skip to content

Commit d1c2712

Browse files
committed
chore(code-gen): fix TS spec tests + enable the routeMatcher specs
1 parent 2f297a6 commit d1c2712

3 files changed

Lines changed: 77 additions & 6 deletions

File tree

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
},
1515
"devDependencies": {
1616
"@lightbase/eslint-config": "3.1.0",
17-
"@types/node": "22.13.1",
18-
"@types/react": "19.0.8",
17+
"@types/lodash": "*",
18+
"@types/node": "*",
19+
"@types/react": "*",
1920
"@tanstack/react-query": "5.66.0",
2021
"axios": "1.7.9",
2122
"fastest-validator": "1.19.0",

packages/code-gen/src/api-client/ts-fetch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export function tsFetchGetApiClientFile(generateContext, route) {
151151
`${route.group}/apiClient.ts`,
152152
{
153153
importCollector: new JavascriptImportCollector(),
154+
typeImportCollector: new JavascriptImportCollector(true),
154155
},
155156
);
156157

packages/code-gen/test/spec-implementations/ts.js

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ async function runGenerate(result, spec) {
153153
includeWrapper: "react-query",
154154
},
155155
},
156+
router: {
157+
target: {
158+
library: "koa",
159+
},
160+
exposeApiStructure: true,
161+
},
156162
},
157163
});
158164

@@ -173,7 +179,6 @@ async function runGenerate(result, spec) {
173179
resolveJsonModule: true,
174180
isolatedModules: true,
175181
jsx: "preserve",
176-
noImplicitAny: false,
177182
},
178183
exclude: ["node_modules"],
179184
include: ["**/*.ts", "**/*.d.ts", "**/*.tsx"],
@@ -268,8 +273,72 @@ async function runValidator(result, spec) {
268273
* @param {SpecResult} result
269274
* @param {import("../spec/specification.js").CodeGenSpecificationRouteMatcher} spec
270275
*/
271-
function runRouteMatcher(result, spec) {
272-
result.skipped++;
276+
async function runRouteMatcher(result, spec) {
277+
try {
278+
const { routeMatcher } = await import(
279+
pathJoin(
280+
process.cwd(),
281+
generateOutputDirectory,
282+
`common/route-matcher.js`,
283+
)
284+
);
285+
286+
const routeMatch = routeMatcher(
287+
spec.matchInput.method,
288+
spec.matchInput.path,
289+
);
273290

274-
return spec;
291+
if (isNil(routeMatch) && isNil(spec.matchOutput)) {
292+
result.passed++;
293+
} else if (isNil(spec.matchOutput)) {
294+
throw AppError.serverError({
295+
message: "Expected no route match, but found a match",
296+
spec,
297+
routeMatch,
298+
});
299+
} else if (isNil(routeMatch)) {
300+
throw AppError.serverError({
301+
message: "Expected a route match, but found no match",
302+
spec,
303+
});
304+
} else {
305+
if (
306+
routeMatch.route.group !== spec.matchOutput.route.group ||
307+
routeMatch.route.name !== spec.matchOutput.route.name
308+
) {
309+
throw AppError.serverError({
310+
message: "Matched an invalid route",
311+
spec,
312+
routeMatch,
313+
});
314+
}
315+
316+
if (
317+
Object.keys(routeMatch.params).length !==
318+
Object.keys(spec.matchOutput.params).length
319+
) {
320+
throw AppError.serverError({
321+
message: "Did not match the appropriate number of params",
322+
spec,
323+
routeMatch,
324+
});
325+
}
326+
327+
for (const [key, value] of Object.entries(spec.matchOutput.params)) {
328+
if (routeMatch.params[key] !== value) {
329+
throw AppError.serverError({
330+
message: "Matched an invalid route param",
331+
spec,
332+
routeMatch,
333+
});
334+
}
335+
}
336+
337+
result.passed++;
338+
}
339+
} catch (e) {
340+
result.failed++;
341+
result.extraLogs.push(`Failed to route match at ${formatSpecPath(result)}`);
342+
result.extraLogs.push(AppError.format(e));
343+
}
275344
}

0 commit comments

Comments
 (0)