Skip to content

Commit 8439097

Browse files
committed
Use native Node.js fetch
Since Node.js 17.5, Node.js has had native `fetch` support. This means we can no longer support Node.js 16.
1 parent 5eccd13 commit 8439097

File tree

10 files changed

+96
-147
lines changed

10 files changed

+96
-147
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
version: [16, 18, latest]
9+
version: [18, latest]
1010
steps:
1111
- uses: actions/checkout@v3
1212
- uses: actions/setup-node@v3

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @type {import('jest').Config} */
22
export default {
33
transform: {},
4+
setupFiles: ["./jest.setup.js"],
45
};

jest.setup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import fetchMock from "jest-fetch-mock";
2+
3+
fetchMock.enableMocks();

lib/Model.test.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { jest } from "@jest/globals";
2-
import { FetchError, Response } from "node-fetch";
3-
4-
jest.unstable_mockModule("node-fetch", () => ({
5-
default: jest.fn(),
6-
FetchError,
7-
}));
8-
92
import { ReplicateResponseError } from "./errors.js";
103
import Model from "./Model.js";
114
import Prediction, { PredictionStatus } from "./Prediction.js";
12-
13-
const { default: ReplicateClient } = await import("./ReplicateClient.js");
5+
import ReplicateClient from "./ReplicateClient.js";
146

157
let client;
168
let model;
@@ -212,22 +204,16 @@ describe("predict()", () => {
212204

213205
const predictionLoadResults = [
214206
() => {
215-
throw new ReplicateResponseError(
216-
"test error",
217-
new Response("{}", {
218-
status: 500,
219-
statusText: "Internal Server Error",
220-
})
221-
);
207+
throw new ReplicateResponseError("test error", {
208+
status: 500,
209+
statusText: "Internal Server Error",
210+
});
222211
},
223212
() => {
224-
throw new ReplicateResponseError(
225-
"test error",
226-
new Response("{}", {
227-
status: 429,
228-
statusText: "Too Many Requests",
229-
})
230-
);
213+
throw new ReplicateResponseError("test error", {
214+
status: 429,
215+
statusText: "Too Many Requests",
216+
});
231217
},
232218
() =>
233219
new Prediction(

lib/Prediction.test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { jest } from "@jest/globals";
2-
import { FetchError } from "node-fetch";
32
import Prediction, { PredictionStatus } from "./Prediction.js";
4-
5-
jest.unstable_mockModule("node-fetch", () => ({
6-
default: jest.fn(),
7-
FetchError,
8-
}));
9-
10-
const { default: ReplicateClient } = await import("./ReplicateClient.js");
3+
import ReplicateClient from "./ReplicateClient.js";
114

125
let client;
136
let prediction;

lib/ReplicateClient.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fetch, { FetchError } from "node-fetch";
21
import pkg from "../package.json" assert { type: "json" };
32
import {
43
ReplicateError,
@@ -111,11 +110,7 @@ export default class ReplicateClient {
111110
try {
112111
resp = await fetch(url, requestInit);
113112
} catch (err) {
114-
if (!err instanceof FetchError) {
115-
throw err;
116-
}
117-
118-
throw new ReplicateRequestError(url, requestInit);
113+
throw new ReplicateRequestError(url, requestInit, err);
119114
}
120115

121116
const respText = await resp.text();

lib/ReplicateClient.test.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { jest } from "@jest/globals";
2-
import { FetchError, Response } from "node-fetch";
3-
4-
jest.unstable_mockModule("node-fetch", () => ({
5-
default: jest.fn(),
6-
FetchError,
7-
}));
8-
92
import {
103
ReplicateError,
114
ReplicateRequestError,
125
ReplicateResponseError,
136
} from "./errors.js";
147
import Model from "./Model.js";
158
import Prediction from "./Prediction.js";
16-
17-
const { default: fetch } = await import("node-fetch");
18-
const { default: ReplicateClient } = await import("./ReplicateClient.js");
9+
import ReplicateClient from "./ReplicateClient.js";
1910

2011
let client;
2112

@@ -170,7 +161,7 @@ describe("prediction()", () => {
170161
describe("request()", () => {
171162
it("throws ReplicateRequestError on failed fetch", async () => {
172163
fetch.mockImplementation(async () => {
173-
throw new FetchError("Something went wrong");
164+
throw new Error("Something went wrong");
174165
});
175166

176167
await expect(
@@ -179,18 +170,15 @@ describe("request()", () => {
179170
await expect(
180171
async () => await client.request("GET /v1/predictions/testprediction")
181172
).rejects.toThrowError(
182-
"Failed to make request: method=GET, url=https://api.replicate.com/v1/predictions/testprediction, body=undefined"
173+
"Failed to make request: method=GET, url=https://api.replicate.com/v1/predictions/testprediction, body=undefined, error=Error: Something went wrong"
183174
);
184175
});
185176

186177
it("throws ReplicateResponseError on error status", async () => {
187-
fetch.mockImplementation(
188-
async () =>
189-
new Response('{"status":403,"details":"Something went wrong"}', {
190-
status: 403,
191-
statusText: "Unauthorized",
192-
})
193-
);
178+
fetch.mockResponse('{"status":403,"details":"Something went wrong"}', {
179+
status: 403,
180+
statusText: "Unauthorized",
181+
});
194182

195183
await expect(
196184
async () => await client.request("GET /v1/predictions/testprediction")

lib/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ export class ReplicateRequestError extends ReplicateError {
44
method;
55
body;
66

7-
constructor(url, requestInit) {
7+
constructor(url, requestInit, err) {
88
super(
9-
`Failed to make request: method=${requestInit.method}, url=${url}, body=${requestInit.body}`
9+
`Failed to make request: method=${requestInit.method}, url=${url}, body=${requestInit.body}, error=${err}`
1010
);
1111

1212
this.method = requestInit.method;

package-lock.json

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

0 commit comments

Comments
 (0)