Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/scrapegraphai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
} from "./types/index.js";

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

export async function getCredits(apiKey: string): Promise<ApiResult<CreditsResponse>> {
try {
const { data, elapsedMs } = await request<CreditsResponse>("GET", "/credits", apiKey);
const { data, elapsedMs } = await request<CreditsResponse>(
"GET",
"/v2/credits",
apiKey,
undefined,
ROOT_URL,
);
return ok(data, elapsedMs);
} catch (err) {
return fail(err);
Expand All @@ -312,7 +318,7 @@ export async function checkHealth(apiKey: string): Promise<ApiResult<HealthRespo
"/healthz",
apiKey,
undefined,
HEALTH_URL,
ROOT_URL,
);
return ok(data, elapsedMs);
} catch (err) {
Expand Down
11 changes: 5 additions & 6 deletions tests/scrapegraphai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as scrapegraphai from "../src/scrapegraphai.js";

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

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

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

expect(res.status).toBe("success");
expect(res.data).toEqual(body);
expectGet(0, "/credits");
expectGet(0, "/v2/credits", ROOT);
});
});

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

expect(res.status).toBe("success");
expect(res.data).toEqual(body);
const [url, init] = fetchSpy.mock.calls[0] as [string, RequestInit];
expect(url).toBe("https://api.scrapegraphai.com/healthz");
expect(init.method).toBe("GET");
expectGet(0, "/healthz", ROOT);
});
});
Loading