Skip to content

Commit 839e79a

Browse files
committed
fix: use v2 credits endpoint
1 parent 70b75d5 commit 839e79a

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/scrapegraphai.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {
2424
} from "./types/index.js";
2525

2626
const BASE_URL = process.env.SGAI_API_URL || "https://api.scrapegraphai.com/v1";
27-
const HEALTH_URL = process.env.SGAI_API_URL
27+
const ROOT_URL = process.env.SGAI_API_URL
2828
? `${process.env.SGAI_API_URL.replace(/\/v\d+$/, "")}`
2929
: "https://api.scrapegraphai.com";
3030
const POLL_INTERVAL_MS = 3000;
@@ -298,7 +298,13 @@ export async function sitemap(
298298

299299
export async function getCredits(apiKey: string): Promise<ApiResult<CreditsResponse>> {
300300
try {
301-
const { data, elapsedMs } = await request<CreditsResponse>("GET", "/credits", apiKey);
301+
const { data, elapsedMs } = await request<CreditsResponse>(
302+
"GET",
303+
"/v2/credits",
304+
apiKey,
305+
undefined,
306+
ROOT_URL,
307+
);
302308
return ok(data, elapsedMs);
303309
} catch (err) {
304310
return fail(err);
@@ -312,7 +318,7 @@ export async function checkHealth(apiKey: string): Promise<ApiResult<HealthRespo
312318
"/healthz",
313319
apiKey,
314320
undefined,
315-
HEALTH_URL,
321+
ROOT_URL,
316322
);
317323
return ok(data, elapsedMs);
318324
} catch (err) {

tests/scrapegraphai.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as scrapegraphai from "../src/scrapegraphai.js";
88

99
const API_KEY = "test-sgai-key-abc123";
1010
const BASE = "https://api.scrapegraphai.com/v1";
11+
const ROOT = "https://api.scrapegraphai.com";
1112

1213
function json(body: unknown, status = 200): Response {
1314
return new Response(JSON.stringify(body), {
@@ -31,9 +32,9 @@ function expectPost(callIndex: number, path: string, body?: object) {
3132
if (body) expect(JSON.parse(init.body as string)).toEqual(body);
3233
}
3334

34-
function expectGet(callIndex: number, path: string) {
35+
function expectGet(callIndex: number, path: string, base = BASE) {
3536
const [url, init] = fetchSpy.mock.calls[callIndex] as [string, RequestInit];
36-
expect(url).toBe(`${BASE}${path}`);
37+
expect(url).toBe(`${base}${path}`);
3738
expect(init.method).toBe("GET");
3839
expect((init.headers as Record<string, string>)["SGAI-APIKEY"]).toBe(API_KEY);
3940
}
@@ -315,7 +316,7 @@ describe("getCredits", () => {
315316

316317
expect(res.status).toBe("success");
317318
expect(res.data).toEqual(body);
318-
expectGet(0, "/credits");
319+
expectGet(0, "/v2/credits", ROOT);
319320
});
320321
});
321322

@@ -328,8 +329,6 @@ describe("checkHealth", () => {
328329

329330
expect(res.status).toBe("success");
330331
expect(res.data).toEqual(body);
331-
const [url, init] = fetchSpy.mock.calls[0] as [string, RequestInit];
332-
expect(url).toBe("https://api.scrapegraphai.com/healthz");
333-
expect(init.method).toBe("GET");
332+
expectGet(0, "/healthz", ROOT);
334333
});
335334
});

0 commit comments

Comments
 (0)