Skip to content

Fix Redocly automatic config loading #1453

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 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@
"devDependencies": {
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"del-cli": "^5.1.0",
"eslint": "^8.50.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-vitest": "^0.2.8",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"typescript": "^5.2.2"
"prettier": "^3.1.0",
"typescript": "^5.3.2"
}
}
2 changes: 1 addition & 1 deletion packages/openapi-fetch/examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"@types/react": "18.2.20",
"@types/react-dom": "18.2.7",
"openapi-typescript": "workspace:^",
"typescript": "5.2.2"
"typescript": "5.3.2"
}
}
2 changes: 1 addition & 1 deletion packages/openapi-fetch/examples/react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"@vitejs/plugin-react-swc": "^3.3.2",
"typescript": "^5.2.2",
"typescript": "^5.3.2",
"vite": "^4.4.9"
}
}
2 changes: 1 addition & 1 deletion packages/openapi-fetch/examples/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"svelte": "^4.2.0",
"svelte-check": "^3.5.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"typescript": "^5.3.2",
"vite": "^4.4.9"
}
}
2 changes: 1 addition & 1 deletion packages/openapi-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"openapi-typescript-codegen": "^0.25.0",
"openapi-typescript-fetch": "^1.1.3",
"superagent": "^8.1.2",
"typescript": "^5.2.2",
"typescript": "^5.3.2",
"vitest": "^0.34.6",
"vitest-fetch-mock": "^0.2.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-typescript-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"test": "tsc --noEmit"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.3.2"
}
}
14 changes: 9 additions & 5 deletions packages/openapi-typescript/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async function main() {
);
const redoc = maybeRedoc
? await loadConfig({ configPath: maybeRedoc })
: createConfig({}, { extends: ["minimal"] });
: await createConfig({}, { extends: ["minimal"] });

// handle Redoc APIs
const hasRedoclyApis = Object.keys(redoc?.apis ?? {}).length > 0;
Expand All @@ -161,9 +161,13 @@ async function main() {
}
await Promise.all(
Object.entries(redoc.apis).map(async ([name, api]) => {
const configRoot = redoc?.configFile
? new URL(`file://${redoc.configFile}`)
: CWD;
let configRoot = CWD;
if (redoc.configFile) {
// note: this will be absolute if --redoc is passed; otherwise, relative
configRoot = path.isAbsolute(redoc.configFile)
? new URL(`file://${redoc.configFile}`)
: new URL(redoc.configFile, `file://${process.cwd()}/`);
}
if (!api["openapi-ts"]?.output) {
errorAndExit(
`API ${name} is missing an \`openapi-ts.output\` key. See https://openapi-ts.pages.dev/cli/#multiple-schemas.`,
Expand All @@ -175,7 +179,7 @@ async function main() {
const outFile = new URL(api["openapi-ts"].output, configRoot);
fs.mkdirSync(new URL(".", outFile), { recursive: true });
fs.writeFileSync(outFile, result, "utf8");
done(name, api.root, performance.now() - timeStart);
done(name, api["openapi-ts"].output, performance.now() - timeStart);
}),
);
}
Expand Down
13 changes: 6 additions & 7 deletions packages/openapi-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,20 @@
"version": "pnpm run build"
},
"dependencies": {
"@redocly/openapi-core": "^1.2.0",
"@redocly/openapi-core": "^1.4.1",
"ansi-colors": "^4.1.3",
"supports-color": "^9.4.0",
"typescript": "^5.2.2",
"typescript": "^5.3.2",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"@types/degit": "^2.8.4",
"@types/js-yaml": "^4.0.6",
"@types/node": "^20.8.0",
"@types/degit": "^2.8.6",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.9.4",
"degit": "^2.8.4",
"del-cli": "^5.1.0",
"esbuild": "^0.19.5",
"esbuild": "^0.19.7",
"execa": "^7.2.0",
"vite": "^4.4.9",
"vite-node": "^0.34.6",
"vitest": "^0.34.6"
}
Expand Down
13 changes: 12 additions & 1 deletion packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ export default async function openapiTS(
}

const redoc =
options.redocly ?? (await createConfig({}, { extends: ["minimal"] }));
options.redocly ??
(await createConfig(
{
// @ts-expect-error This is OK
styleguide: {
rules: {
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
},
},
},
{ extends: ["minimal"] },
));

const schema = await validateAndBundle(source, {
redoc,
Expand Down
25 changes: 22 additions & 3 deletions packages/openapi-typescript/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,33 @@ describe("CLI", () => {
});

describe("Redocly config", () => {
test("accepts multiple APIs", async () => {
await execa(cmd, ["--redoc", "test/fixtures/redocly/redocly.yaml"], {
// TODO: why won’t this run on Windows?
test.skipIf(os.platform() === "win32")("automatic config", async () => {
/* eslint-disable @typescript-eslint/no-shadow */

// note: we have to change the cwd here for the automatic config to pick up properly
const root = new URL("./fixtures/redocly/", import.meta.url);
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root;

await execa("../../../bin/cli.js", { cwd });
for (const schema of ["a", "b", "c"]) {
expect(
fs.readFileSync(new URL(`./output/${schema}.ts`, root), "utf8"),
).toMatchFileSnapshot(
fileURLToPath(new URL("../../../examples/simple-example.ts", root)),
);
}
/* eslint-enable @typescript-eslint/no-shadow */
});

test("--redoc config", async () => {
await execa(cmd, ["--redoc", "test/fixtures/redocly-flag/redocly.yaml"], {
cwd,
});
for (const schema of ["a", "b", "c"]) {
expect(
fs.readFileSync(
new URL(`./test/fixtures/redocly/output/${schema}.ts`, root),
new URL(`./test/fixtures/redocly-flag/output/${schema}.ts`, root),
"utf8",
),
).toMatchFileSnapshot(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output
Loading