Skip to content
Open
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
5 changes: 5 additions & 0 deletions .env_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SPEC_URL=
SPEC_OUT=
SPEC_STRATEGY=
SPEC_USERNAME=
SPEC_PASSWORD=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ count.txt
src/gen
# Bun
.bun-cache
.bun-version
.bun-version
spec/
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export default antfu({
"style/indent-binary-ops": "off",
"style/indent": "off",
"style/comma-dangle": "off",
"style/quote-props": "off",
},
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"postgenerate:kubb": "find src/gen/types -name '*.ts' -exec sed -i '' 's/\\[key: string\\]: string\\[\\] | null;/[key: string]: string[] | undefined;/g' {} \\;",
"generate": "bun run generate:kubb && bun run generate:routes",
"check:routes": "bun run generate:routes && git diff --exit-code src/routes/routes.gen.ts",
"download-spec": "bun scripts/dow-spec.mjs --url https://petstore3.swagger.io/api/v3/openapi.json --out ./spec/openapi.yaml"
"download-spec": "bun scripts/dow-spec.mjs --out ./spec/openapi.yaml"
},
"dependencies": {
"@hookform/resolvers": "^5.2.1",
Expand Down Expand Up @@ -59,6 +59,7 @@
"@vitejs/plugin-react": "^4.3.4",
"ajv": "^8.17.1",
"ajv-draft-04": "^1.0.0",
"cheerio": "^1.1.2",
"eslint": "^9.34.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.20",
Expand Down
49 changes: 49 additions & 0 deletions scripts/create-spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Buffer } from "node:buffer";
import fs from "node:fs/promises";
import yaml from "js-yaml";

export class CreateSpec {
_url = "";
_strategy = null;
_outPath = "";
_outDir = "";

constructor({ url, strategy, outDir, outPath }) {
this._url = url;
this._outDir = outDir;
this._outPath = outPath;
this._strategy = strategy;
}

async generate() {
const text = await this._strategy.download(this._url);

await this._createFile(text);
}

async _createFile(text) {
let yamlText;
const trimmed = text.trim();
const looksLikeJson = trimmed.startsWith("{") || trimmed.startsWith("[");

if (looksLikeJson) {
try {
const obj = JSON.parse(text);
yamlText = yaml.dump(obj, { noRefs: true });
console.log("✅ JSON -> converted to YAML");
} catch {
yamlText = text;
console.log("⚠️ JSON parse failed -> saving raw");
}
} else {
yamlText = text;
console.log("✅ YAML -> saving as-is");
}

await fs.mkdir(this._outDir, { recursive: true });
await fs.writeFile(this._outPath, yamlText, "utf8");
console.log(
`💾 saved ${Buffer.byteLength(yamlText, "utf8")} bytes to ${this._outPath} (overwritten if existed)`,
);
}
}
62 changes: 32 additions & 30 deletions scripts/dow-spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@
* bun scripts/download-spec.mjs --url https://example.com/openapi.json --out ./spec/my.yaml
*/

import { Buffer } from "node:buffer";
import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import yaml from "js-yaml";
import minimist from "minimist";
import { CreateSpec } from "./create-spec.mjs";
import { DjangoStrategy } from "./strategies/djangoStrategy.mjs";
import { PublicStrategy } from "./strategies/publicStrategy.mjs";

const strategies = {
Public: PublicStrategy,
Django: DjangoStrategy,
};

const args = minimist(process.argv.slice(2), {
string: ["url", "out"],
alias: { u: "url", o: "out" },
});

const url = args.url || process.env.SPEC_URL;

if (!url) {
console.error(
"❌ SPEC URL not provided. Use --url, SPEC_URL env, or package.json specUrl",
Expand All @@ -34,33 +40,29 @@ const outDir = path.dirname(outPath);
console.log(`[spec] URL: ${url}`);
console.log(`[spec] output: ${outPath}`);

// 1️⃣ Скачиваем spec
const res = await fetch(url);
if (!res.ok)
throw new Error(`Failed to fetch: ${res.status} ${res.statusText}`);
const text = await res.text();
const strategyArg = args.strategy || process.env.SPEC_STRATEGY;

if (!strategyArg) {
console.error(
"❌ SPEC STRATEGY not provided. Use --strategy, or SPEC_STRATEGY env",
);
process.exit(1);
}

const strategy = new strategies[strategyArg]();

// 2️⃣ Конвертируем JSON -> YAML если нужно
let yamlText;
const trimmed = text.trim();
const looksLikeJson = trimmed.startsWith("{") || trimmed.startsWith("[");
if (looksLikeJson) {
try {
const obj = JSON.parse(text);
yamlText = yaml.dump(obj, { noRefs: true });
console.log("✅ JSON -> converted to YAML");
} catch {
yamlText = text;
console.log("⚠️ JSON parse failed -> saving raw");
}
} else {
yamlText = text;
console.log("✅ YAML -> saving as-is");
if (!strategy) {
console.error("❌ SPEC STRATEGY is incorrect.");
process.exit(1);
}

// 3️⃣ Создаём директорию (если нет) и перезаписываем файл
await fs.mkdir(outDir, { recursive: true });
await fs.writeFile(outPath, yamlText, "utf8");
console.log(
`💾 saved ${Buffer.byteLength(yamlText, "utf8")} bytes to ${outPath} (overwritten if existed)`,
);
console.log(`[spec] using strategy: ${strategyArg}`);

const constructorSpec = new CreateSpec({
url,
outDir,
outPath,
strategy,
});

await constructorSpec.generate();
4 changes: 4 additions & 0 deletions scripts/strategies/baseStrategy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class BaseStrategy {
// url
async download(_) {}
}
88 changes: 88 additions & 0 deletions scripts/strategies/djangoStrategy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import process from "node:process";
import * as cheerio from "cheerio";
import { BaseStrategy } from "./baseStrategy.mjs";

export class DjangoStrategy extends BaseStrategy {
_getSetCookie(response, names) {
const setCookie = response.headers.get("set-cookie");
const cookies = [];

for (let i = 0; i < names.length; i++) {
const name = names[i];
let cookie = null;

if (setCookie) {
const regex = new RegExp(`${name}=([^;]+)`);
const match = setCookie.match(regex);
if (match) cookie = match[1];
}

if (!cookie) {
console.error(`❌ Не удалось достать ${name} из Set-Cookie`);
process.exit(1);
}

cookies.push(cookie);
}

return cookies;
}

async download(url) {
await super.download(url);

const response = await fetch(url);

if (!response.ok) {
throw new Error(
`Failed to fetch: ${response.status} ${response.statusText}`,
);
}

const html = await response.text();
const $ = cheerio.load(html);

const csrfmiddlewaretoken = $("input[name=csrfmiddlewaretoken]").val();
const next = $("input[name=next]").val();
const submit = $("input[name=submit]").val();

const csrftoken = this._getSetCookie(response, ["csrftoken"])[0];

const urlObj = new URL(response.url);
urlObj.search = "";

const urlWithoutSearch = urlObj.toString();
const payload = new URLSearchParams({
username: process.env.SPEC_USERNAME,
password: process.env.SPEC_PASSWORD,
next,
submit,
csrfmiddlewaretoken,
});

const loginResponse = await fetch(urlWithoutSearch, {
body: payload,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: `csrftoken=${csrftoken}`,
"X-CSRFToken": csrftoken,
Referer: response.url,
Origin: urlObj.origin,
},
method: "POST",
redirect: "manual",
});

const sessionid = this._getSetCookie(loginResponse, ["sessionid"]);
const redirectURL = loginResponse.headers.get("location");
const nextUrl = new URL(redirectURL, urlObj.origin);

const docResponse = await fetch(nextUrl, {
headers: {
Cookie: `csrftoken=${csrftoken}; sessionid=${sessionid}`,
},
});

return await docResponse.text();
}
}
14 changes: 14 additions & 0 deletions scripts/strategies/publicStrategy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { BaseStrategy } from "./baseStrategy.mjs";

export class PublicStrategy extends BaseStrategy {
async download(url) {
await super.download(url);

const res = await fetch(url);

if (!res.ok)
throw new Error(`Failed to fetch: ${res.status} ${res.statusText}`);

return await res.text();
}
}
Loading