Skip to content

Commit

Permalink
chore: add api-gen generate benchmark command
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Jan 31, 2024
1 parent abe1f7c commit 34abf5a
Show file tree
Hide file tree
Showing 7 changed files with 14,816 additions and 37 deletions.
63 changes: 39 additions & 24 deletions packages/api-client-next/src/tests/transform.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@ import { describe, bench } from "vitest";
import { transformPathToQuery } from "../transformPathToQuery";

describe("transforming requests", () => {
bench("transformPathToQuery - transform without any params", () => {
transformPathToQuery("readCart get /checkout/cart", {});
});
bench(
"[api-client][transformPathToQuery] - transform without any params",
() => {
transformPathToQuery("readCart get /checkout/cart", {});
},
);

bench("transformPathToQuery - transform with query params", () => {
transformPathToQuery("readCart get /checkout/cart?name", {
name: "myId123",
});
});
bench(
"[api-client][transformPathToQuery] - transform with query params",
() => {
transformPathToQuery("readCart get /checkout/cart?name", {
name: "myId123",
});
},
);

bench("transformPathToQuery - transform with body params", () => {
transformPathToQuery("readCart get /checkout/cart", {
name: "myId123",
});
});
bench(
"[api-client][transformPathToQuery] - transform with body params",
() => {
transformPathToQuery("readCart get /checkout/cart", {
name: "myId123",
});
},
);

bench("transformPathToQuery - transform with path params", () => {
transformPathToQuery("readCart get /checkout/cart/{name}", {
name: "myId123",
});
});
bench(
"[api-client][transformPathToQuery] - transform with path params",
() => {
transformPathToQuery("readCart get /checkout/cart/{name}", {
name: "myId123",
});
},
);

bench("transformPathToQuery - transform with header params", () => {
transformPathToQuery("readCart get /checkout/cart name", {
name: "myId123",
});
});
bench(
"[api-client][transformPathToQuery] - transform with header params",
() => {
transformPathToQuery("readCart get /checkout/cart name", {
name: "myId123",
});
},
);

bench(
"transformPathToQuery - transform with path,query,body,header params",
"[api-client][transformPathToQuery] - transform with path,query,body,header params",
() => {
transformPathToQuery("readCart get /checkout/cart/{id}?name surname", {
id: "myId123",
Expand Down
1 change: 1 addition & 0 deletions packages/api-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lint": "eslint src/**/*.ts* --fix --max-warnings=0 && pnpm run typecheck",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:bench": "vitest bench",
"test:watch": "vitest"
},
"devDependencies": {
Expand Down
21 changes: 12 additions & 9 deletions packages/api-gen/src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";
import { resolve, join } from "node:path";
import openapiTS from "openapi-typescript";
import type { OpenAPI3 } from "openapi-typescript";
import * as dotenv from "dotenv";
Expand All @@ -14,14 +14,17 @@ export async function generate(args: { cwd: string; filename: string }) {
try {
const outputFilename = args.filename.replace(".json", ".d.ts");

const fullInputFilePath = join(args.cwd, args.filename);
const fullOutputFilePath = join(args.cwd, outputFilename);

//check if file exist
const fileExist = existsSync(args.filename);
const fileExist = existsSync(fullInputFilePath);

if (!fileExist) {
console.log(
c.yellow(
`Schema file ${c.bold(
args.filename,
fullInputFilePath,
)} does not exist. Check whether the file is created (use ${c.bold(
"loadSchema",
)} command first).`,
Expand All @@ -31,7 +34,7 @@ export async function generate(args: { cwd: string; filename: string }) {
}

// Apply patches
const schemaFile = readFileSync(args.filename, {
const schemaFile = readFileSync(fullInputFilePath, {
encoding: "utf-8",
});
let schemaForPatching = JSON.parse(schemaFile) as OpenAPI3;
Expand All @@ -53,19 +56,19 @@ export async function generate(args: { cwd: string; filename: string }) {
parser: "json",
});
const content = formatted.trim();
writeFileSync(args.filename, content, {
writeFileSync(fullInputFilePath, content, {
encoding: "utf-8",
});

const readedContentFromFile = readFileSync(args.filename, {
const readedContentFromFile = readFileSync(fullInputFilePath, {
encoding: "utf-8",
});

const originalSchema = JSON.parse(readedContentFromFile);
const { paths } = originalSchema;
console.log("schema", originalSchema.info);

const address = resolve(process.cwd(), args.filename);
const address = resolve(fullInputFilePath);
let schema = await openapiTS(
// new URL(SCHEMA_FILENAME, import.meta.url),
address,
Expand Down Expand Up @@ -232,13 +235,13 @@ export async function generate(args: { cwd: string; filename: string }) {
schema = schema.trim();

if (typeof schema === "string") {
writeFileSync(outputFilename, schema, {
writeFileSync(fullOutputFilePath, schema, {
encoding: "utf-8",
});
} else {
throw new Error("Schema is not a string");
}
console.log(c.green(`Types generated in ${c.bold(outputFilename)}`));
console.log(c.green(`Types generated in ${c.bold(fullOutputFilePath)}`));
} catch (error) {
console.error(
c.red(
Expand Down
1 change: 1 addition & 0 deletions packages/api-gen/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testSchema.d.ts
11 changes: 11 additions & 0 deletions packages/api-gen/tests/generate.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, bench } from "vitest";
import { generate } from "../src/commands/generate";

describe("api-gen - generate", () => {
bench("[api-gen][generate] - generate schema command", async () => {
await generate({
cwd: __dirname,
filename: "testSchema.json",
});
});
});
Loading

0 comments on commit 34abf5a

Please sign in to comment.