From 14db3ce4034a665cf7268c30acdfdb7a697eb3be Mon Sep 17 00:00:00 2001 From: Daniil Date: Thu, 30 Apr 2026 01:13:06 +0300 Subject: [PATCH] feature: add swagger auth --- .scripts/gen-api-types.mjs | 54 ++++++++++++++++++++++++++++++++++++++ package.json | 2 +- redocly.yaml | 6 +++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .scripts/gen-api-types.mjs create mode 100644 redocly.yaml diff --git a/.scripts/gen-api-types.mjs b/.scripts/gen-api-types.mjs new file mode 100644 index 0000000..10a7da4 --- /dev/null +++ b/.scripts/gen-api-types.mjs @@ -0,0 +1,54 @@ +import { spawn } from "node:child_process" + +const schemaUrl = "https://api.trimgu.ru/api/schema/" +const outputPath = "src/shared/api/__generated__/openapi.types.ts" + +const existingHeader = process.env.DOCS_BASIC_AUTH_HEADER +const user = process.env.DOCS_BASIC_AUTH_USER +const password = process.env.DOCS_BASIC_AUTH_PASSWORD + +const authHeader = + existingHeader ?? + (user && password + ? `Basic ${Buffer.from(`${user}:${password}`, "utf8").toString("base64")}` + : undefined) + +if (!authHeader) { + console.error( + "Missing docs auth. Set DOCS_BASIC_AUTH_USER and DOCS_BASIC_AUTH_PASSWORD, or DOCS_BASIC_AUTH_HEADER.", + ) + process.exit(1) +} + +const child = spawn( + "bunx", + [ + "openapi-typescript", + schemaUrl, + "--redocly", + "redocly.yaml", + "--output", + outputPath, + ], + { + env: { + ...process.env, + DOCS_BASIC_AUTH_HEADER: authHeader, + }, + stdio: "inherit", + }, +) + +child.on("exit", (code, signal) => { + if (signal) { + console.error(`openapi-typescript stopped by signal ${signal}`) + process.exit(1) + } + + process.exit(code ?? 1) +}) + +child.on("error", (error) => { + console.error(error.message) + process.exit(1) +}) diff --git a/package.json b/package.json index ed4a2ff..f8e7363 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "create-component": "npx generate-react-cli component", "gc": "bun run .scripts/create-fsd-component.ts", "gicons": "npx @svgr/cli --ext tsx --typescript --no-prettier --icon --ref --no-svgo ./src/shared/assets/raw-icons/ --out-dir ./src/shared/ui/Icons/", - "gen:api-types": "openapi-typescript http://127.0.0.1:8000/api/schema/ --output src/shared/api/__generated__/openapi.types.ts", + "gen:api-types": "bun --env-file=.env.production .scripts/gen-api-types.mjs", "test:e2e": "bunx playwright test --project=chromium --headed", "test:integration": "bunx playwright test --project=integration --headed" }, diff --git a/redocly.yaml b/redocly.yaml new file mode 100644 index 0000000..6534515 --- /dev/null +++ b/redocly.yaml @@ -0,0 +1,6 @@ +resolve: + http: + headers: + - matches: https://api.trimgu.ru/** + name: Authorization + envVariable: DOCS_BASIC_AUTH_HEADER