Skip to content

Commit c7e96e4

Browse files
committed
fix: Deduplicate additional headers
1 parent 4b8901b commit c7e96e4

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
npm/
22
examples/
33
typefetch.d.ts
4-
tests/*/schemas/*.ts
4+
tests/*/typefetch.ts
5+
tests/*/schemas/*.ts

deno.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@denosaurs/typefetch",
3-
"version": "0.0.29",
3+
"version": "0.0.30",
44
"exports": {
55
".": "./main.ts"
66
},
@@ -13,7 +13,7 @@
1313
"openapi-types": "npm:[email protected]",
1414
"ts-morph": "npm:[email protected]"
1515
},
16-
"exclude": ["tests/*/schemas/*.ts", "npm/*"],
16+
"exclude": ["tests/*/schemas/*.ts", "tests/*/typefetch.ts", "npm/*"],
1717
"test": {
1818
"include": ["tests/test.ts"]
1919
},

mod.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export function escapeObjectKey(key: string): string {
4848
*/
4949
function toSafeUnionString(
5050
type: string | undefined,
51-
_: number,
5251
types: (string | undefined)[],
5352
): string | undefined {
5453
if (type === "string" && types.length > 1) {
@@ -110,7 +109,7 @@ export function toSchemaType(
110109
if (schema.oneOf) {
111110
return schema.oneOf
112111
.map((schema) => toSchemaType(document, schema))
113-
.map(toSafeUnionString)
112+
.map((type, _, types) => toSafeUnionString(type, types))
114113
.filter(Boolean)
115114
.join("|");
116115
}
@@ -131,7 +130,7 @@ export function toSchemaType(
131130

132131
return schema.anyOf
133132
.map((schema) => toSchemaType(document, schema))
134-
.map(toSafeUnionString)
133+
.map((type, _, types) => toSafeUnionString(type, types))
135134
.filter(Boolean)
136135
.join("|");
137136
}
@@ -431,17 +430,25 @@ export function toHeadersInitType(
431430
parameters: ParameterObjectMap,
432431
additionalHeaders: string[] = [],
433432
): string | undefined {
434-
const headersInitProperties = [...additionalHeaders];
433+
const headersInitProperties = [];
435434

436435
for (const parameter of parameters.values()) {
437436
if (parameter.in !== "header") continue;
437+
438+
// If the header has a predefined default in the additionalHeaders argument discard it
439+
additionalHeaders = additionalHeaders.filter((header) =>
440+
!header.startsWith(`"${parameter.name}"`)
441+
);
442+
438443
headersInitProperties.push(
439444
`"${parameter.name}"${parameter.required ? "" : "?"}: ${
440445
toSchemaType(document, parameter.schema) ?? "string"
441446
}`,
442447
);
443448
}
444449

450+
headersInitProperties.unshift(...additionalHeaders);
451+
445452
if (headersInitProperties.length === 0) return undefined;
446453
return `TypedHeadersInit<{ ${headersInitProperties.join("; ")} }>`;
447454
}

tests/test.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { walk } from "jsr:@std/fs";
22
import { assert } from "jsr:@std/assert";
33

4-
import { globToRegExp } from "@std/path";
4+
import { dirname, globToRegExp } from "@std/path";
55

66
const pwd = import.meta.resolve("../");
77
const main = import.meta.resolve("../main.ts");
@@ -22,6 +22,30 @@ Deno.test("Generate schemas", async ({ step }) => {
2222
assert(output.success);
2323
});
2424
}
25+
26+
for await (
27+
const schema of walk("./tests/", {
28+
match: [globToRegExp("**/schema.ts")],
29+
})
30+
) {
31+
const { default: path } = await import(`../${schema.path}`);
32+
const dir = dirname(schema.path);
33+
await step(`Generating schema for ${path}`, async () => {
34+
const output = await new Deno.Command("deno", {
35+
args: [
36+
"run",
37+
"-A",
38+
main,
39+
`-o=${dir}/typefetch.ts`,
40+
`--import=${pwd}`,
41+
path,
42+
],
43+
stdout: "inherit",
44+
stderr: "inherit",
45+
}).output();
46+
assert(output.success);
47+
});
48+
}
2549
});
2650

2751
Deno.test("Check types", async ({ step }) => {

tests/tmdb3/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "https://developer.themoviedb.org/openapi/64542913e1f86100738e227f";

tests/tmdb4/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "https://developer.themoviedb.org/openapi/6453cc549c91cf004cd2a015";

0 commit comments

Comments
 (0)