Skip to content

Commit

Permalink
fix: Deduplicate additional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Sep 25, 2024
1 parent 4b8901b commit c7e96e4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
npm/
examples/
typefetch.d.ts
tests/*/schemas/*.ts
tests/*/typefetch.ts
tests/*/schemas/*.ts
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@denosaurs/typefetch",
"version": "0.0.29",
"version": "0.0.30",
"exports": {
".": "./main.ts"
},
Expand All @@ -13,7 +13,7 @@
"openapi-types": "npm:[email protected]",
"ts-morph": "npm:[email protected]"
},
"exclude": ["tests/*/schemas/*.ts", "npm/*"],
"exclude": ["tests/*/schemas/*.ts", "tests/*/typefetch.ts", "npm/*"],
"test": {
"include": ["tests/test.ts"]
},
Expand Down
15 changes: 11 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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("|");
}
Expand All @@ -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("|");
}
Expand Down Expand Up @@ -431,17 +430,25 @@ 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"
}`,
);
}

headersInitProperties.unshift(...additionalHeaders);

if (headersInitProperties.length === 0) return undefined;
return `TypedHeadersInit<{ ${headersInitProperties.join("; ")} }>`;
}
Expand Down
26 changes: 25 additions & 1 deletion tests/test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 }) => {
Expand Down
1 change: 1 addition & 0 deletions tests/tmdb3/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "https://developer.themoviedb.org/openapi/64542913e1f86100738e227f";
1 change: 1 addition & 0 deletions tests/tmdb4/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "https://developer.themoviedb.org/openapi/6453cc549c91cf004cd2a015";

0 comments on commit c7e96e4

Please sign in to comment.