Skip to content

Commit 3b96de8

Browse files
Fix fetch with build-time substitution (#2744)
* split fetch into build-time files * fix lint * test sh * add grep replace for ci * bump types node version * add info log * add override for node-fetch whatwg
1 parent 7cd667e commit 3b96de8

File tree

10 files changed

+83
-31
lines changed

10 files changed

+83
-31
lines changed

package-lock.json

Lines changed: 36 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
"ts-node": "^10.9.2",
6767
"watch": "^1.0.2"
6868
},
69+
"overrides": {
70+
"cross-fetch": {
71+
"node-fetch": {
72+
"whatwg-url": "^13.0.0"
73+
}
74+
}
75+
},
6976
"files": ["dist"],
7077
"bin": "dist/index.js"
7178
}

packages/quicktype-core/env.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ $CI ]]
4+
then
5+
grep -rl '$fetch' src | xargs sed -i '' -e 's/$fetch/$fetch.ci/g'
6+
fi
7+
8+
exit 0

packages/quicktype-core/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"repository": "https://github.com/quicktype/quicktype",
99
"scripts": {
1010
"clean": "rm -rf dist node_modules *~",
11-
"build": "tsc"
11+
"build": "./env.sh && tsc"
1212
},
1313
"dependencies": {
1414
"@glideapps/ts-necessities": "2.2.3",
@@ -29,7 +29,7 @@
2929
"devDependencies": {
3030
"@types/browser-or-node": "^1.3.2",
3131
"@types/is-url": "^1.2.32",
32-
"@types/node": "18.19.31",
32+
"@types/node": "~22.14.0",
3333
"@types/pako": "^1.0.0",
3434
"@types/pluralize": "0.0.30",
3535
"@types/readable-stream": "4.0.10",
@@ -38,6 +38,13 @@
3838
"@types/wordwrap": "^1.0.3",
3939
"typescript": "~5.8.3"
4040
},
41+
"overrides": {
42+
"cross-fetch": {
43+
"node-fetch": {
44+
"whatwg-url": "^13.0.0"
45+
}
46+
}
47+
},
4148
"files": ["dist"],
4249
"browser": {
4350
"fs": false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
console.info("=== RUNNING IN CI, USE FETCH.CI ===");
2+
3+
export const fetch = require("cross-fetch").default;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type CrossFetch from "cross-fetch";
2+
3+
let fetch: typeof CrossFetch;
4+
5+
try {
6+
fetch = global.fetch ?? require("cross-fetch").default;
7+
} catch {
8+
fetch = require("cross-fetch").default;
9+
}
10+
11+
export { fetch };

packages/quicktype-core/src/input/io/NodeIO.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from "fs";
1+
import * as fs from "node:fs";
22

33
import { defined, exceptionToString } from "@glideapps/ts-necessities";
44
import { isNode } from "browser-or-node";
@@ -10,11 +10,7 @@ import { panic } from "../../support/Support";
1010

1111
import { getStream } from "./get-stream";
1212

13-
// We need to use cross-fetch in CI or if fetch is not available in the global scope
14-
// We use a dynamic import to avoid punycode deprecated dependency warning on node > 20
15-
const fetch = process.env.CI
16-
? require("cross-fetch").default
17-
: ((global as any).fetch ?? require("cross-fetch").default);
13+
import { fetch } from "./$fetch";
1814

1915
interface HttpHeaders {
2016
[key: string]: string;
@@ -25,10 +21,7 @@ function parseHeaders(httpHeaders?: string[]): HttpHeaders {
2521
return {};
2622
}
2723

28-
return httpHeaders.reduce((
29-
result: HttpHeaders,
30-
httpHeader: string,
31-
) => {
24+
return httpHeaders.reduce((result: HttpHeaders, httpHeader: string) => {
3225
if (httpHeader !== undefined && httpHeader.length > 0) {
3326
const split = httpHeader.indexOf(":");
3427

@@ -56,7 +49,9 @@ export async function readableFromFileOrURL(
5649
});
5750

5851
return defined(response.body) as unknown as Readable;
59-
} else if (isNode) {
52+
}
53+
54+
if (isNode) {
6055
if (fileOrURL === "-") {
6156
// Cast node readable to isomorphic readable from readable-stream
6257
return process.stdin as unknown as Readable;

packages/quicktype-graphql-input/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"graphql": "^0.11.7"
1717
},
1818
"devDependencies": {
19-
"@types/node": "18.19.31",
19+
"@types/node": "~22.14.0",
2020
"@types/graphql": "^0.11.7",
2121
"typescript": "~5.8.3"
2222
},

packages/quicktype-typescript-input/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@mark.probst/typescript-json-schema": "0.55.0"
1717
},
1818
"devDependencies": {
19-
"@types/node": "18.19.31"
19+
"@types/node": "~22.14.0"
2020
},
2121
"files": ["dist"]
2222
}

packages/quicktype-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
},
140140
"devDependencies": {
141141
"@types/mocha": "^10.0.6",
142-
"@types/node": "18.x",
142+
"@types/node": "~22.14.0",
143143
"@types/node-persist": "^3.1.8",
144144
"@types/readable-stream": "^4.0.10",
145145
"@types/vscode": "^1.87.0",

0 commit comments

Comments
 (0)