-
Notifications
You must be signed in to change notification settings - Fork 0
fix: validate generated REST contracts #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a258d1e
fix: validate generated REST contracts
kang-heewon 47b43cd
fix: enforce generated contract diagnostics
kang-heewon c4ce87b
fix: pack generated contract smoke dependencies
kang-heewon c5417ed
fix: pack rpc codegen published dependencies
kang-heewon 544d5cb
fix: normalize generated catch-all paths
kang-heewon eeb9bfd
fix: align contract graph route metadata
kang-heewon 7fba88f
fix: stabilize contract graph access refs
kang-heewon e866e3d
fix: identify contract graph guard refs
kang-heewon 4ab2805
fix: disambiguate contract graph guard refs
kang-heewon 7fadac8
fix: align catch-all route runtime
kang-heewon 64df88d
fix: preserve generated path param boundaries
kang-heewon fa1058e
fix: preserve contract graph edge metadata
kang-heewon 01c60a8
fix: stabilize generated client typecheck tests
kang-heewon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@croco/protocols-core": patch | ||
| "@croco/transports-http": patch | ||
| "@croco/openapi-spec": patch | ||
| "@croco/rpc-codegen": patch | ||
| "@croco/cli": patch | ||
| "create-croco-app": patch | ||
| --- | ||
|
|
||
| Expose a canonical REST contract graph with route diagnostics and add a contract check path before OpenAPI and RPC client generation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| "devDependencies": { | ||
| "@types/node": "^22.0.0", | ||
| "tsup": "^8.3.5", | ||
| "typedi": "0.10.0", | ||
| "vitest": "^4.0.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { defineCommand } from "citty"; | ||
| import { contractsCheck } from "./contractsCheck.js"; | ||
| import { GLOBAL_OPTIONS } from "./options.js"; | ||
|
|
||
| export const contracts = defineCommand({ | ||
| meta: { | ||
| name: "contracts", | ||
| description: "Validate Croco contract graph artifacts", | ||
| }, | ||
| args: { | ||
| ...GLOBAL_OPTIONS, | ||
| }, | ||
| subCommands: { | ||
| check: contractsCheck, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { type ChildProcess, type SpawnOptions, spawn } from "node:child_process"; | ||
| import { createRequire } from "node:module"; | ||
| import { basename, dirname, join } from "node:path"; | ||
| import { defineCommand } from "citty"; | ||
| import { GLOBAL_OPTIONS } from "./options.js"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| export type ContractsCheckSpawn = ( | ||
| command: string, | ||
| args: string[], | ||
| options: SpawnOptions, | ||
| ) => ChildProcess; | ||
|
|
||
| export const contractsCheck = defineCommand({ | ||
| meta: { | ||
| name: "check", | ||
| description: "Validate the canonical contract graph without generating artifacts", | ||
| }, | ||
| args: { | ||
| ...GLOBAL_OPTIONS, | ||
| }, | ||
| run({ rawArgs }) { | ||
| runContractsCheck(rawArgs); | ||
| }, | ||
| }); | ||
|
|
||
| export function runContractsCheck( | ||
| args: string[], | ||
| options: { | ||
| readonly resolveBin?: () => string; | ||
| readonly spawn?: ContractsCheckSpawn; | ||
| readonly setExitCode?: (code: number) => void; | ||
| readonly writeError?: (message: string) => void; | ||
| } = {}, | ||
| ): void { | ||
| const resolveBin = options.resolveBin ?? resolveRpcCodegenBin; | ||
| const spawnChild = options.spawn ?? spawn; | ||
| const setExitCode = | ||
| options.setExitCode ?? | ||
| ((code: number) => { | ||
| process.exitCode = code; | ||
| }); | ||
| const writeError = options.writeError ?? ((message: string) => console.error(message)); | ||
| const child = spawnChild(process.execPath, [resolveBin(), "--check", ...args], { | ||
| stdio: "inherit", | ||
| }); | ||
|
|
||
| child.on("exit", (code) => { | ||
| setExitCode(code ?? 1); | ||
| }); | ||
|
|
||
| child.on("error", (error) => { | ||
| writeError(error.message); | ||
| setExitCode(1); | ||
| }); | ||
| } | ||
|
|
||
| export function resolveRpcCodegenBin(): string { | ||
| return resolveRpcCodegenBinFromEntry(require.resolve("@croco/rpc-codegen")); | ||
| } | ||
|
|
||
| export function resolveRpcCodegenBinFromEntry(entry: string): string { | ||
| const entryDir = dirname(entry); | ||
|
|
||
| return basename(entryDir) === "src" | ||
| ? join(dirname(entryDir), "dist", "cli.js") | ||
| : join(entryDir, "cli.js"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import type { ChildProcess, SpawnOptions } from "node:child_process"; | ||
| import { EventEmitter } from "node:events"; | ||
| import { join } from "node:path"; | ||
| import { Container } from "typedi"; | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { | ||
| type ContractsCheckSpawn, | ||
| resolveRpcCodegenBinFromEntry, | ||
| runContractsCheck, | ||
| } from "../commands/contractsCheck.js"; | ||
|
|
||
| describe("contractsCheck", () => { | ||
| beforeEach(() => { | ||
| Container.reset(); | ||
| }); | ||
|
|
||
| it("should resolve a workspace source RPC package entry to the built RPC CLI", () => { | ||
| const root = join("workspace", "packages", "rpc-codegen"); | ||
|
|
||
| expect(resolveRpcCodegenBinFromEntry(join(root, "src", "index.ts"))).toBe( | ||
| join(root, "dist", "cli.js"), | ||
| ); | ||
| }); | ||
|
|
||
| it("should spawn the RPC check mode with forwarded args and preserve its exit code", () => { | ||
| const child = new EventEmitter() as unknown as ChildProcess; | ||
| const calls: SpawnCall[] = []; | ||
| const exitCodes: number[] = []; | ||
| const spawnCheck: ContractsCheckSpawn = (command, args, options) => { | ||
| calls.push({ command, args, options }); | ||
| return child; | ||
| }; | ||
|
|
||
| runContractsCheck(["--controllers", "src/**/*.ts"], { | ||
| resolveBin: () => "/pkg/dist/cli.js", | ||
| spawn: spawnCheck, | ||
| setExitCode: (code) => exitCodes.push(code), | ||
| }); | ||
| child.emit("exit", 7); | ||
|
|
||
| expect(calls).toEqual([ | ||
| { | ||
| command: process.execPath, | ||
| args: ["/pkg/dist/cli.js", "--check", "--controllers", "src/**/*.ts"], | ||
| options: { stdio: "inherit" }, | ||
| }, | ||
| ]); | ||
| expect(exitCodes).toEqual([7]); | ||
| }); | ||
|
|
||
| it("should report spawn errors as command failures", () => { | ||
| const child = new EventEmitter() as unknown as ChildProcess; | ||
| const errors: string[] = []; | ||
| const exitCodes: number[] = []; | ||
| const spawnCheck: ContractsCheckSpawn = () => child; | ||
|
|
||
| runContractsCheck([], { | ||
| resolveBin: () => "/pkg/dist/cli.js", | ||
| spawn: spawnCheck, | ||
| setExitCode: (code) => exitCodes.push(code), | ||
| writeError: (message) => errors.push(message), | ||
| }); | ||
| child.emit("error", new Error("spawn failed")); | ||
|
|
||
| expect(errors).toEqual(["spawn failed"]); | ||
| expect(exitCodes).toEqual([1]); | ||
| }); | ||
| }); | ||
|
|
||
| type SpawnCall = { | ||
| readonly command: string; | ||
| readonly args: string[]; | ||
| readonly options: SpawnOptions; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export { | ||
| emitOpenAPI, | ||
| emitOpenAPIFromContractGraph, | ||
| type EmitOpenAPIOptions, | ||
| type ProblemResponseConfig, | ||
| } from "./libs/emitOpenAPI"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.