From c7e96e4b03085a0810341ad82b18e1395c1aae6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20Sj=C3=B6green?= Date: Wed, 25 Sep 2024 14:02:25 +0200 Subject: [PATCH] fix: Deduplicate additional headers --- .gitignore | 3 ++- deno.json | 4 ++-- mod.ts | 15 +++++++++++---- tests/test.ts | 26 +++++++++++++++++++++++++- tests/tmdb3/schema.ts | 1 + tests/tmdb4/schema.ts | 1 + 6 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 tests/tmdb3/schema.ts create mode 100644 tests/tmdb4/schema.ts diff --git a/.gitignore b/.gitignore index 3502f11..22268c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ npm/ examples/ typefetch.d.ts -tests/*/schemas/*.ts \ No newline at end of file +tests/*/typefetch.ts +tests/*/schemas/*.ts diff --git a/deno.json b/deno.json index 5fc71c9..01d476d 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@denosaurs/typefetch", - "version": "0.0.29", + "version": "0.0.30", "exports": { ".": "./main.ts" }, @@ -13,7 +13,7 @@ "openapi-types": "npm:openapi-types@12.1", "ts-morph": "npm:ts-morph@22.0" }, - "exclude": ["tests/*/schemas/*.ts", "npm/*"], + "exclude": ["tests/*/schemas/*.ts", "tests/*/typefetch.ts", "npm/*"], "test": { "include": ["tests/test.ts"] }, diff --git a/mod.ts b/mod.ts index fce4f9e..1d6d8f0 100644 --- a/mod.ts +++ b/mod.ts @@ -48,7 +48,6 @@ export function escapeObjectKey(key: string): string { */ function toSafeUnionString( type: string | undefined, - _: number, types: (string | undefined)[], ): string | undefined { if (type === "string" && types.length > 1) { @@ -110,7 +109,7 @@ export function toSchemaType( if (schema.oneOf) { return schema.oneOf .map((schema) => toSchemaType(document, schema)) - .map(toSafeUnionString) + .map((type, _, types) => toSafeUnionString(type, types)) .filter(Boolean) .join("|"); } @@ -131,7 +130,7 @@ export function toSchemaType( return schema.anyOf .map((schema) => toSchemaType(document, schema)) - .map(toSafeUnionString) + .map((type, _, types) => toSafeUnionString(type, types)) .filter(Boolean) .join("|"); } @@ -431,10 +430,16 @@ export function toHeadersInitType( parameters: ParameterObjectMap, additionalHeaders: string[] = [], ): string | undefined { - const headersInitProperties = [...additionalHeaders]; + const headersInitProperties = []; for (const parameter of parameters.values()) { if (parameter.in !== "header") continue; + + // If the header has a predefined default in the additionalHeaders argument discard it + additionalHeaders = additionalHeaders.filter((header) => + !header.startsWith(`"${parameter.name}"`) + ); + headersInitProperties.push( `"${parameter.name}"${parameter.required ? "" : "?"}: ${ toSchemaType(document, parameter.schema) ?? "string" @@ -442,6 +447,8 @@ export function toHeadersInitType( ); } + headersInitProperties.unshift(...additionalHeaders); + if (headersInitProperties.length === 0) return undefined; return `TypedHeadersInit<{ ${headersInitProperties.join("; ")} }>`; } diff --git a/tests/test.ts b/tests/test.ts index 0dc9b36..9a7e9a5 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -1,7 +1,7 @@ import { walk } from "jsr:@std/fs"; import { assert } from "jsr:@std/assert"; -import { globToRegExp } from "@std/path"; +import { dirname, globToRegExp } from "@std/path"; const pwd = import.meta.resolve("../"); const main = import.meta.resolve("../main.ts"); @@ -22,6 +22,30 @@ Deno.test("Generate schemas", async ({ step }) => { assert(output.success); }); } + + for await ( + const schema of walk("./tests/", { + match: [globToRegExp("**/schema.ts")], + }) + ) { + const { default: path } = await import(`../${schema.path}`); + const dir = dirname(schema.path); + await step(`Generating schema for ${path}`, async () => { + const output = await new Deno.Command("deno", { + args: [ + "run", + "-A", + main, + `-o=${dir}/typefetch.ts`, + `--import=${pwd}`, + path, + ], + stdout: "inherit", + stderr: "inherit", + }).output(); + assert(output.success); + }); + } }); Deno.test("Check types", async ({ step }) => { diff --git a/tests/tmdb3/schema.ts b/tests/tmdb3/schema.ts new file mode 100644 index 0000000..d866899 --- /dev/null +++ b/tests/tmdb3/schema.ts @@ -0,0 +1 @@ +export default "https://developer.themoviedb.org/openapi/64542913e1f86100738e227f"; diff --git a/tests/tmdb4/schema.ts b/tests/tmdb4/schema.ts new file mode 100644 index 0000000..912a6d1 --- /dev/null +++ b/tests/tmdb4/schema.ts @@ -0,0 +1 @@ +export default "https://developer.themoviedb.org/openapi/6453cc549c91cf004cd2a015";