Skip to content

Commit 2516d1d

Browse files
committed
Drop cross-fetch
1 parent baa4978 commit 2516d1d

40 files changed

+186
-152
lines changed

karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ module.exports = function(config) {
3131
"portfinder": require.resolve('./test/empty-stub.js'),
3232
"dns2": require.resolve('./test/empty-stub.js'),
3333
"ws": require.resolve('./test/empty-stub.js'),
34-
"tmp-promise": require.resolve('./test/empty-stub.js')
34+
"tmp-promise": require.resolve('./test/empty-stub.js'),
35+
"undici": require.resolve('./test/empty-stub.js')
3536
},
3637
fallback: {
3738
// With Webpack 5, we need explicit mocks for all node modules. Because the

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"https": false,
4646
"http2": false,
4747
"http2-wrapper": false,
48-
"cross-fetch": false,
4948
"cacheable-lookup": false,
5049
"@httptoolkit/proxy-agent": false,
5150
"http-encoding": false
@@ -148,6 +147,7 @@
148147
"ts-node": "^10.9.2",
149148
"typedoc": "^0.26.7",
150149
"typescript": "5.6.2",
150+
"undici": "^7.22.0",
151151
"url": "^0.11.0",
152152
"util": "^0.12.4",
153153
"webpack": "^5.72.0",
@@ -177,7 +177,6 @@
177177
"connect": "^3.7.0",
178178
"cors": "^2.8.4",
179179
"cors-gate": "^1.1.3",
180-
"cross-fetch": "^3.1.5",
181180
"destroyable-server": "^1.1.1",
182181
"express": "^4.14.0",
183182
"fast-json-patch": "^3.1.1",

src/client/admin-client.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import { EventEmitter } from 'events';
33
import { Duplex } from 'stream';
44
import DuplexPair = require('native-duplexpair');
55
import { TypedError } from 'typed-error';
6-
import * as CrossFetch from 'cross-fetch';
76
import * as WebSocket from 'isomorphic-ws';
87
import connectWebSocketStream = require('@httptoolkit/websocket-stream');
98
import { SubscriptionClient } from '@httptoolkit/subscriptions-transport-ws';
10-
import { MaybePromise, getDeferred } from '@httptoolkit/util';
9+
import { ErrorLike, MaybePromise, getDeferred } from '@httptoolkit/util';
1110
import { print } from 'graphql';
1211

1312
import { DEFAULT_ADMIN_SERVER_PORT } from "../types";
1413

1514
import { RequireProps } from '../util/type-utils';
16-
import { isNode } from '../util/util';
1715
import { delay, isErrorLike } from '@httptoolkit/util';
1816

1917
import { introspectionQuery } from './schema-introspection';
@@ -22,10 +20,6 @@ import { AdminPlugin, PluginClientResponsesMap, PluginStartParamsMap } from '../
2220
import { SchemaIntrospector } from './schema-introspection';
2321
import { AdminQuery, getSingleSelectedFieldName } from './admin-query';
2422

25-
const { fetch, Headers } = isNode || typeof globalThis.fetch === 'undefined'
26-
? CrossFetch
27-
: globalThis;
28-
2923
export class ConnectionError extends TypedError { }
3024

3125
// The Response type requires lib.dom. We include an empty placeholder here to
@@ -135,9 +129,14 @@ async function requestFromAdminServer<T>(serverUrl: string, path: string, option
135129
try {
136130
response = await fetch(url, options);
137131
} catch (e) {
138-
if (isErrorLike(e) && e.code === 'ECONNREFUSED') {
132+
if (isErrorLike(e) && (
133+
e.code === 'ECONNREFUSED' ||
134+
(e.cause as ErrorLike)?.code === 'ECONNREFUSED')
135+
) {
139136
throw new ConnectionError(`Failed to connect to admin server at ${serverUrl}`);
140-
} else throw e;
137+
} else {
138+
throw e;
139+
}
141140
}
142141

143142
if (response.status >= 400) {
@@ -160,7 +159,7 @@ async function requestFromAdminServer<T>(serverUrl: string, path: string, option
160159
);
161160
}
162161
} else {
163-
return response.json();
162+
return response.json() as Promise<T>;
164163
}
165164
}
166165

@@ -397,7 +396,7 @@ export class AdminClient<Plugins extends { [key: string]: AdminPlugin<any, any>
397396
body: JSON.stringify({ query, variables })
398397
}));
399398

400-
const { data, errors }: { data?: T, errors?: Error[] } = await response.json();
399+
const { data, errors } = await response.json() as { data?: T, errors?: Error[] };
401400

402401
if (errors && errors.length) {
403402
throw new GraphQLError(response, errors);
@@ -411,7 +410,7 @@ export class AdminClient<Plugins extends { [key: string]: AdminPlugin<any, any>
411410

412411
let graphQLErrors: Error[] | undefined = undefined;
413412
try {
414-
graphQLErrors = (await e.response.json()).errors;
413+
graphQLErrors = (await e.response.json() as { errors?: Error[] }).errors;
415414
} catch (e2) {}
416415

417416
if (graphQLErrors) {

test/certificates.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
DestroyableServer,
88
makeDestroyable,
99
expect,
10-
fetch,
1110
ignoreNetworkError,
1211
nodeOnly
1312
} from "./test-utils";

test/integration/completion.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getLocal } from "../..";
2-
import { expect, fetch } from "../test-utils";
2+
import { expect } from "../test-utils";
33
import * as _ from "lodash";
44

55
describe("HTTP mock rule completion", function () {

test/integration/explanations.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getLocal } from "../..";
2-
import { expect, fetch, URLSearchParams, Headers, isNode } from "../test-utils";
2+
import { expect, URLSearchParams, isNode } from "../test-utils";
33
import * as _ from "lodash";
44
import { Readable } from 'stream';
55

test/integration/form-data.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ import { Buffer } from 'buffer';
33
import { getLocal } from "../..";
44
import {
55
expect,
6-
File,
7-
fetch as fetchPolyfill
6+
File
87
} from "../test-utils";
98

10-
const fetch = globalThis.fetch ?? fetchPolyfill;
11-
129
describe("Body getXFormData methods", () => {
1310
let server = getLocal();
1411

test/integration/handlers/broken-response.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as http from 'http';
33
import { getLocal } from "../../..";
44
import {
55
expect,
6-
fetch,
76
isNode,
87
nodeOnly,
98
delay,
@@ -28,7 +27,7 @@ describe("Broken response handlers", function () {
2827
let result = await fetch(server.urlFor('/mocked-endpoint')).catch(e => e);
2928

3029
expect(result).to.be.instanceof(Error);
31-
expect(result.message).to.contain(isNode ? 'socket hang up' : 'Failed to fetch');
30+
expect(result.message).to.contain(isNode ? 'fetch failed' : 'Failed to fetch');
3231
});
3332

3433
it("should allow forcibly resetting the connection", async function () {
@@ -37,7 +36,7 @@ describe("Broken response handlers", function () {
3736
let result = await fetch(server.urlFor('/mocked-endpoint')).catch(e => e);
3837

3938
expect(result).to.be.instanceof(Error);
40-
expect(result.message).to.contain(isNode ? 'read ECONNRESET' : 'Failed to fetch');
39+
expect(result.message).to.contain(isNode ? 'fetch failed' : 'Failed to fetch');
4140
});
4241

4342

test/integration/handlers/callback-response.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as http from 'http';
44
import { getLocal } from "../../..";
55
import {
66
expect,
7-
fetch,
87
isNode,
98
isWeb,
109
headersToObject,
@@ -167,7 +166,7 @@ describe("Callback response handlers", function () {
167166

168167
expect(response).to.be.instanceOf(Error);
169168
if (isNode) {
170-
expect((response as any).code).to.equal('ECONNRESET');
169+
expect((response as any).cause?.code).to.equal('UND_ERR_SOCKET');
171170
} else {
172171
expect((response as Error).message).to.include('Failed to fetch');
173172
}

test/integration/handlers/delay.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as WebSocket from 'ws';
33
import { getLocal } from "../../..";
44
import {
55
expect,
6-
fetch,
76
nodeOnly
87
} from "../../test-utils";
98

0 commit comments

Comments
 (0)