Skip to content

Commit 8228ec8

Browse files
committed
Throw if no API token provided
1 parent 27fbf74 commit 8228ec8

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/ReplicateClient.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fetch from "node-fetch";
22
import pkg from "../package.json" assert { type: "json" };
3+
import { ReplicateError } from "./errors.js";
34
import Prediction from "./Prediction.js";
45
import { sleep } from "./utils.js";
56

@@ -10,6 +11,10 @@ export default class ReplicateClient {
1011
constructor({ baseURL, token }) {
1112
this.baseURL = baseURL || "https://api.replicate.com";
1213
this.token = token || process.env.REPLICATE_API_TOKEN;
14+
15+
if (!this.token) {
16+
throw new ReplicateError("Missing API token");
17+
}
1318
}
1419

1520
// TODO: Optionally autocancel prediction on exception.

lib/ReplicateClient.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { jest } from "@jest/globals";
2+
import { ReplicateError } from "./errors.js";
23
import { PredictionStatus } from "./Prediction.js";
34
import ReplicateClient from "./ReplicateClient.js";
45

@@ -10,6 +11,46 @@ beforeEach(() => {
1011
client = new ReplicateClient({});
1112
});
1213

14+
describe("baseURL", () => {
15+
it("is baseURL provided to constructor", () => {
16+
const client = new ReplicateClient({
17+
baseURL: "http://test.host",
18+
});
19+
20+
expect(client.baseURL).toEqual("http://test.host");
21+
});
22+
23+
it("defaults to https://api.replicate.com", () => {
24+
const client = new ReplicateClient({});
25+
26+
expect(client.baseURL).toEqual("https://api.replicate.com");
27+
});
28+
});
29+
30+
describe("token", () => {
31+
it("is token provided to constructor", () => {
32+
const client = new ReplicateClient({
33+
token: "test-token-from-constructor",
34+
});
35+
36+
expect(client.token).toEqual("test-token-from-constructor");
37+
});
38+
39+
it("defaults to REPLICATE_API_TOKEN env var", () => {
40+
const client = new ReplicateClient({});
41+
42+
expect(client.token).toEqual("test-token-from-env");
43+
});
44+
});
45+
46+
describe("constructor()", () => {
47+
it("throws when no token is provided or set in env", () => {
48+
delete process.env.REPLICATE_API_TOKEN;
49+
50+
expect(() => new ReplicateClient({})).toThrowError(ReplicateError);
51+
});
52+
});
53+
1354
describe("predict()", () => {
1455
it("makes request to create prediction", async () => {
1556
jest

lib/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export class ReplicateError extends Error {}

0 commit comments

Comments
 (0)