Skip to content
Merged
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
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm check
- run: pnpm test -- --run
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"lineWidth": 100,
"attributePosition": "auto"
},
"linter": {
Expand Down
3 changes: 1 addition & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const testPost = "https://httpbin.org/anything";
try {
const response = await client.get(urlPremium, {
// autoparse: true,
css_extractor:
'{"aproximate_results": "#result-stats", "headers": "#search a > h3"}',
css_extractor: '{"aproximate_results": "#result-stats", "headers": "#search a > h3"}',
// js_render: true,
premium_proxy: true,
proxy_country: "us",
Expand Down
9 changes: 2 additions & 7 deletions examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ const testPost = "https://httpbin.org/anything";
}

try {
const urls = [
"https://httpbin.org/ip",
"https://httpbin.org/anything",
"https://ident.me",
];
const urls = ["https://httpbin.org/ip", "https://httpbin.org/anything", "https://ident.me"];

const promises = urls.map((url) => client.get(url));

Expand All @@ -67,8 +63,7 @@ const testPost = "https://httpbin.org/anything";
(item): item is PromiseRejectedResult => item.status === "rejected",
);
const fulfilled = results.filter(
(item): item is PromiseFulfilledResult<Response> =>
item.status === "fulfilled",
(item): item is PromiseFulfilledResult<Response> => item.status === "fulfilled",
);

console.log(rejected);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
},
"scripts": {
"build": "tsup src/index.ts --dts --format cjs,esm --clean",
"test": "vitest"
"test": "vitest",
"format": "biome format --write .",
"check": "biome check ."
},
"keywords": ["sdk", "zenrows", "scraping"],
"author": "ZenRows",
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ZenRows {
public get(
url: string,
config?: ZenRowsConfig,
{ headers = {} }: { headers?: Headers } = {}
{ headers = {} }: { headers?: Headers } = {},
): Promise<Response> {
return this.queue.push({ url, config, headers });
}
Expand All @@ -79,7 +79,7 @@ export class ZenRows {
config?: ZenRowsConfig,
{ headers = {}, data = {} }: { headers?: Headers; data?: unknown } = {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
}
},
): Promise<Response> {
const normalizedHeaders = Object.keys(headers).reduce(
(acc: { [key: string]: string }, key: string) => {
Expand All @@ -93,7 +93,7 @@ export class ZenRows {
}
return acc;
},
{}
{},
);

return this.queue.push({
Expand Down
28 changes: 14 additions & 14 deletions tests/_setup.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { beforeAll, afterAll, afterEach } from "vitest";
import { setupServer } from "msw/node";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll } from "vitest";

const handlers = [
http.get("https://api.zenrows.com/v1/", () => {
return HttpResponse.json({
firstName: "John",
lastName: "Maverick",
});
}),
http.post("https://api.zenrows.com/v1/", () => {
return new HttpResponse();
}),
http.get("https://api.zenrows.com/v1/", () => {
return HttpResponse.json({
firstName: "John",
lastName: "Maverick",
});
}),
http.post("https://api.zenrows.com/v1/", () => {
return new HttpResponse();
}),
];

export const server = setupServer(...handlers);

beforeAll(() => {
server.listen();
server.listen();
});

afterEach(() => {
server.resetHandlers();
server.resetHandlers();
});

afterAll(() => {
server.close();
server.close();
});
2 changes: 1 addition & 1 deletion tests/concurrency.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, test, expect, beforeEach } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import { beforeEach, describe, expect, test } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import { ZenRows } from "../src";

describe("ZenRows Client with Concurrency", () => {
Expand Down
12 changes: 5 additions & 7 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, test, expect, vi, beforeEach, type Mock } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import { ZenRows } from "../src";
import { http } from "msw";
import { type Mock, beforeEach, describe, expect, test, vi } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import packageJson from "../package.json" assert { type: "json" };
import { ZenRows } from "../src";
import { server } from "./_setup";
import { http } from "msw";

describe("ZenRows Client Get", () => {
const apiKey = "API_KEY";
Expand Down Expand Up @@ -50,9 +50,7 @@ describe("ZenRows Client Get", () => {
const parsedUrl = new URL(requestUrl);

for (const key in optionalParams) {
expect(parsedUrl.searchParams.get(key)).toBe(
optionalParams[key].toString(),
);
expect(parsedUrl.searchParams.get(key)).toBe(optionalParams[key].toString());
}
});

Expand Down Expand Up @@ -98,7 +96,7 @@ describe("ZenRows Client Get", () => {
headers: expect.objectContaining({
"Content-Type": "application/x-www-form-urlencoded",
}),
body: `"${data}"`,
body: data,
}),
);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/retries.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vi, describe, test, expect, beforeEach, type Mock } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import { http, HttpResponse } from "msw";
import { type Mock, beforeEach, describe, expect, test, vi } from "vitest"; //TODO(Nestor): Try to use globals instead of importing
import { ZenRows } from "../src";
import { server } from "./_setup";
import { HttpResponse, http } from "msw";

describe("ZenRows Client with Retries", () => {
const apiKey = "API_KEY";
Expand Down
8 changes: 4 additions & 4 deletions tests/retry-count.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, test, expect } from "vitest";
import { http, HttpResponse } from "msw";
import { describe, expect, test } from "vitest";
import { ZenRows } from "../src";
import { server } from "./_setup";
import { HttpResponse, http } from "msw";

describe("retryOn off-by-one fix", () => {
test.each([
Expand All @@ -17,14 +17,14 @@ describe("retryOn off-by-one fix", () => {
http.get("https://api.zenrows.com/v1/", () => {
fetchCount++;
return new HttpResponse("Could Not Get Content", { status: 422 });
})
}),
);

const client = new ZenRows("API_KEY", { retries });
const response = await client.get("https://example.com");

expect(response.status).toBe(422);
expect(fetchCount).toBe(expectedCalls);
}
},
);
});
Loading