@decodo/sdk-ts 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,7 +97,7 @@ var HttpClient = class {
97
97
  if (config.auth.type === "basic") {
98
98
  this.authHeader = `Basic ${config.auth.token}`;
99
99
  } else {
100
- this.authHeader = config.auth.apiKey;
100
+ this.authHeader = `Bearer ${config.auth.apiKey}`;
101
101
  }
102
102
  }
103
103
  async request(method, path, body) {
@@ -2755,12 +2755,22 @@ var BundledSchema = class _BundledSchema {
2755
2755
  };
2756
2756
 
2757
2757
  // src/api/web-scraping-api.ts
2758
+ var SCRAPER_API_ROUTES = {
2759
+ scrape: "/v2/scrape",
2760
+ task: "/v3/task"
2761
+ };
2762
+ var DATA_API_ROUTES = {
2763
+ scrape: "/v1/scrape",
2764
+ task: "/v1/task"
2765
+ };
2758
2766
  var WebScrapingApi = class {
2759
2767
  http;
2760
2768
  schemas;
2761
- constructor(http, schemas = BundledSchema.shared) {
2769
+ routes;
2770
+ constructor(http, schemas = BundledSchema.shared, routes = SCRAPER_API_ROUTES) {
2762
2771
  this.http = http;
2763
2772
  this.schemas = schemas;
2773
+ this.routes = routes;
2764
2774
  }
2765
2775
  validate(params) {
2766
2776
  const schema = this.schemas.getRequestSchema(params.target);
@@ -2774,21 +2784,21 @@ var WebScrapingApi = class {
2774
2784
  }
2775
2785
  async scrape(params) {
2776
2786
  this.validate(params);
2777
- return this.http.post("/v2/scrape", params);
2787
+ return this.http.post(this.routes.scrape, params);
2778
2788
  }
2779
2789
  async scrapeAsync(params) {
2780
2790
  this.validate(params);
2781
- return this.http.post("/v3/task", params);
2791
+ return this.http.post(this.routes.task, params);
2782
2792
  }
2783
2793
  async scrapeBatch(params) {
2784
- return this.http.post("/v3/task/batch", params);
2794
+ return this.http.post(`${this.routes.task}/batch`, params);
2785
2795
  }
2786
2796
  async getStatus(taskId) {
2787
- return this.http.get(`/v3/task/${taskId}`);
2797
+ return this.http.get(`${this.routes.task}/${taskId}`);
2788
2798
  }
2789
2799
  async getResults(taskId) {
2790
2800
  const res = await this.http.get(
2791
- `/v3/task/${taskId}/results`
2801
+ `${this.routes.task}/${taskId}/results`
2792
2802
  );
2793
2803
  return res ?? null;
2794
2804
  }
@@ -2796,27 +2806,67 @@ var WebScrapingApi = class {
2796
2806
 
2797
2807
  // src/client.ts
2798
2808
  var WEB_API_BASE_URL = "https://scraper-api.decodo.com";
2809
+ var DATA_API_BASE_URL = "https://data.decodo.com";
2799
2810
  var DEFAULT_TIMEOUT_MS = 18e4;
2800
2811
  var notConfigured = (namespace, hint) => {
2801
2812
  throw new Error(`${namespace} is not configured. ${hint}`);
2802
2813
  };
2814
+ var resolveBaseUrl = (baseUrl, fallback) => {
2815
+ if (baseUrl === void 0) {
2816
+ return fallback;
2817
+ }
2818
+ try {
2819
+ new URL(baseUrl);
2820
+ } catch {
2821
+ throw new Error("webScrapingApi baseUrl must be a valid URL.");
2822
+ }
2823
+ return baseUrl;
2824
+ };
2825
+ var resolveTransport = (config) => {
2826
+ const { token, apiKey, baseUrl } = config;
2827
+ if (token !== void 0 && apiKey !== void 0) {
2828
+ throw new Error(
2829
+ "webScrapingApi accepts either token or apiKey, not both. Provide only one."
2830
+ );
2831
+ }
2832
+ if (apiKey !== void 0) {
2833
+ if (!apiKey.trim()) {
2834
+ throw new Error("webScrapingApi apiKey must be a non-empty string.");
2835
+ }
2836
+ return {
2837
+ baseUrl: resolveBaseUrl(baseUrl, DATA_API_BASE_URL),
2838
+ auth: { type: "apiKey", apiKey },
2839
+ routes: DATA_API_ROUTES
2840
+ };
2841
+ }
2842
+ if (token !== void 0) {
2843
+ if (!token.trim()) {
2844
+ throw new Error("webScrapingApi token must be a non-empty string.");
2845
+ }
2846
+ return {
2847
+ baseUrl: resolveBaseUrl(baseUrl, WEB_API_BASE_URL),
2848
+ auth: { type: "basic", token },
2849
+ routes: SCRAPER_API_ROUTES
2850
+ };
2851
+ }
2852
+ throw new Error("webScrapingApi requires token in DecodoConfig.");
2853
+ };
2803
2854
  var DecodoClient = class {
2804
2855
  webScrapingApi;
2805
2856
  constructor(config) {
2806
2857
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
2807
2858
  const schema = config.schema ?? BundledSchema.shared;
2808
2859
  if (config.webScrapingApi) {
2860
+ const { baseUrl, auth, routes } = resolveTransport(config.webScrapingApi);
2809
2861
  this.webScrapingApi = new WebScrapingApi(
2810
2862
  new HttpClient({
2811
- baseUrl: WEB_API_BASE_URL,
2812
- auth: {
2813
- type: "basic",
2814
- token: config.webScrapingApi.token
2815
- },
2863
+ baseUrl,
2864
+ auth,
2816
2865
  timeoutMs,
2817
2866
  integrationHeader: config.webScrapingApi.integrationHeader
2818
2867
  }),
2819
- schema
2868
+ schema,
2869
+ routes
2820
2870
  );
2821
2871
  } else {
2822
2872
  this.webScrapingApi = new Proxy({}, {
@@ -834,10 +834,15 @@ export type TaskMetadata = {
834
834
  export type TaskResultsResponse = {
835
835
  results: ResultEntry[];
836
836
  };
837
+ export type WebScrapingApiRoutes = {
838
+ scrape: string;
839
+ task: string;
840
+ };
837
841
  export declare class WebScrapingApi {
838
842
  private readonly http;
839
843
  private readonly schemas;
840
- constructor(http: HttpClient, schemas?: DecodoSchema);
844
+ private readonly routes;
845
+ constructor(http: HttpClient, schemas?: DecodoSchema, routes?: WebScrapingApiRoutes);
841
846
  private validate;
842
847
  scrape(params: ScrapeRequest): Promise<SyncResponse>;
843
848
  scrapeAsync(params: ScrapeRequest): Promise<AsyncTaskResponse>;
@@ -845,11 +850,19 @@ export declare class WebScrapingApi {
845
850
  getStatus(taskId: string): Promise<TaskMetadata>;
846
851
  getResults(taskId: string): Promise<TaskResultsResponse | null>;
847
852
  }
853
+ export type WebScrapingApiCredentials = {
854
+ token: string;
855
+ apiKey?: never;
856
+ } | {
857
+ apiKey: string;
858
+ token?: never;
859
+ };
860
+ export type WebScrapingApiConfig = WebScrapingApiCredentials & {
861
+ integrationHeader?: string;
862
+ baseUrl?: string;
863
+ };
848
864
  export type DecodoConfig = {
849
- webScrapingApi?: {
850
- token: string;
851
- integrationHeader?: string;
852
- };
865
+ webScrapingApi?: WebScrapingApiConfig;
853
866
  timeoutMs?: number;
854
867
  schema?: DecodoSchema;
855
868
  };
@@ -2,10 +2,17 @@ import { HttpClient } from '../http.js';
2
2
  import type { DecodoSchema } from '../schema/types.js';
3
3
  import type { ScrapeRequest, BatchRequest } from '../generated/targets.js';
4
4
  import type { SyncResponse, AsyncTaskResponse, BatchResponse, TaskMetadata, TaskResultsResponse } from '../types/responses.js';
5
+ export type WebScrapingApiRoutes = {
6
+ scrape: string;
7
+ task: string;
8
+ };
9
+ export declare const SCRAPER_API_ROUTES: WebScrapingApiRoutes;
10
+ export declare const DATA_API_ROUTES: WebScrapingApiRoutes;
5
11
  export declare class WebScrapingApi {
6
12
  private readonly http;
7
13
  private readonly schemas;
8
- constructor(http: HttpClient, schemas?: DecodoSchema);
14
+ private readonly routes;
15
+ constructor(http: HttpClient, schemas?: DecodoSchema, routes?: WebScrapingApiRoutes);
9
16
  private validate;
10
17
  scrape(params: ScrapeRequest): Promise<SyncResponse>;
11
18
  scrapeAsync(params: ScrapeRequest): Promise<AsyncTaskResponse>;
@@ -1,12 +1,22 @@
1
1
  import { prettifyError } from 'zod';
2
2
  import { ValidationError } from '../errors.js';
3
3
  import { BundledSchema } from '../schema/bundled-schema.js';
4
+ export const SCRAPER_API_ROUTES = {
5
+ scrape: '/v2/scrape',
6
+ task: '/v3/task',
7
+ };
8
+ export const DATA_API_ROUTES = {
9
+ scrape: '/v1/scrape',
10
+ task: '/v1/task',
11
+ };
4
12
  export class WebScrapingApi {
5
13
  http;
6
14
  schemas;
7
- constructor(http, schemas = BundledSchema.shared) {
15
+ routes;
16
+ constructor(http, schemas = BundledSchema.shared, routes = SCRAPER_API_ROUTES) {
8
17
  this.http = http;
9
18
  this.schemas = schemas;
19
+ this.routes = routes;
10
20
  }
11
21
  validate(params) {
12
22
  const schema = this.schemas.getRequestSchema(params.target);
@@ -20,20 +30,20 @@ export class WebScrapingApi {
20
30
  }
21
31
  async scrape(params) {
22
32
  this.validate(params);
23
- return this.http.post('/v2/scrape', params);
33
+ return this.http.post(this.routes.scrape, params);
24
34
  }
25
35
  async scrapeAsync(params) {
26
36
  this.validate(params);
27
- return this.http.post('/v3/task', params);
37
+ return this.http.post(this.routes.task, params);
28
38
  }
29
39
  async scrapeBatch(params) {
30
- return this.http.post('/v3/task/batch', params);
40
+ return this.http.post(`${this.routes.task}/batch`, params);
31
41
  }
32
42
  async getStatus(taskId) {
33
- return this.http.get(`/v3/task/${taskId}`);
43
+ return this.http.get(`${this.routes.task}/${taskId}`);
34
44
  }
35
45
  async getResults(taskId) {
36
- const res = await this.http.get(`/v3/task/${taskId}/results`);
46
+ const res = await this.http.get(`${this.routes.task}/${taskId}/results`);
37
47
  return res ?? null;
38
48
  }
39
49
  }
@@ -1,10 +1,18 @@
1
1
  import { WebScrapingApi } from './api/web-scraping-api.js';
2
2
  import type { DecodoSchema } from './schema/types.js';
3
+ type WebScrapingApiCredentials = {
4
+ token: string;
5
+ apiKey?: never;
6
+ } | {
7
+ apiKey: string;
8
+ token?: never;
9
+ };
10
+ export type WebScrapingApiConfig = WebScrapingApiCredentials & {
11
+ integrationHeader?: string;
12
+ baseUrl?: string;
13
+ };
3
14
  export type DecodoConfig = {
4
- webScrapingApi?: {
5
- token: string;
6
- integrationHeader?: string;
7
- };
15
+ webScrapingApi?: WebScrapingApiConfig;
8
16
  timeoutMs?: number;
9
17
  schema?: DecodoSchema;
10
18
  };
@@ -12,3 +20,4 @@ export declare class DecodoClient {
12
20
  readonly webScrapingApi: WebScrapingApi;
13
21
  constructor(config: DecodoConfig);
14
22
  }
23
+ export {};
@@ -1,26 +1,64 @@
1
1
  import { HttpClient } from './http.js';
2
- import { WebScrapingApi } from './api/web-scraping-api.js';
2
+ import { WebScrapingApi, SCRAPER_API_ROUTES, DATA_API_ROUTES, } from './api/web-scraping-api.js';
3
3
  import { BundledSchema } from './schema/bundled-schema.js';
4
4
  const WEB_API_BASE_URL = 'https://scraper-api.decodo.com';
5
+ const DATA_API_BASE_URL = 'https://data.decodo.com';
5
6
  const DEFAULT_TIMEOUT_MS = 180_000;
6
7
  const notConfigured = (namespace, hint) => {
7
8
  throw new Error(`${namespace} is not configured. ${hint}`);
8
9
  };
10
+ const resolveBaseUrl = (baseUrl, fallback) => {
11
+ if (baseUrl === undefined) {
12
+ return fallback;
13
+ }
14
+ try {
15
+ new URL(baseUrl);
16
+ }
17
+ catch {
18
+ throw new Error('webScrapingApi baseUrl must be a valid URL.');
19
+ }
20
+ return baseUrl;
21
+ };
22
+ const resolveTransport = (config) => {
23
+ const { token, apiKey, baseUrl } = config;
24
+ if (token !== undefined && apiKey !== undefined) {
25
+ throw new Error('webScrapingApi accepts either token or apiKey, not both. Provide only one.');
26
+ }
27
+ if (apiKey !== undefined) {
28
+ if (!apiKey.trim()) {
29
+ throw new Error('webScrapingApi apiKey must be a non-empty string.');
30
+ }
31
+ return {
32
+ baseUrl: resolveBaseUrl(baseUrl, DATA_API_BASE_URL),
33
+ auth: { type: 'apiKey', apiKey },
34
+ routes: DATA_API_ROUTES,
35
+ };
36
+ }
37
+ if (token !== undefined) {
38
+ if (!token.trim()) {
39
+ throw new Error('webScrapingApi token must be a non-empty string.');
40
+ }
41
+ return {
42
+ baseUrl: resolveBaseUrl(baseUrl, WEB_API_BASE_URL),
43
+ auth: { type: 'basic', token },
44
+ routes: SCRAPER_API_ROUTES,
45
+ };
46
+ }
47
+ throw new Error('webScrapingApi requires token in DecodoConfig.');
48
+ };
9
49
  export class DecodoClient {
10
50
  webScrapingApi;
11
51
  constructor(config) {
12
52
  const timeoutMs = config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
13
53
  const schema = config.schema ?? BundledSchema.shared;
14
54
  if (config.webScrapingApi) {
55
+ const { baseUrl, auth, routes } = resolveTransport(config.webScrapingApi);
15
56
  this.webScrapingApi = new WebScrapingApi(new HttpClient({
16
- baseUrl: WEB_API_BASE_URL,
17
- auth: {
18
- type: 'basic',
19
- token: config.webScrapingApi.token,
20
- },
57
+ baseUrl,
58
+ auth,
21
59
  timeoutMs,
22
60
  integrationHeader: config.webScrapingApi.integrationHeader,
23
- }), schema);
61
+ }), schema, routes);
24
62
  }
25
63
  else {
26
64
  this.webScrapingApi = new Proxy({}, {
package/build/esm/http.js CHANGED
@@ -12,7 +12,7 @@ export class HttpClient {
12
12
  this.authHeader = `Basic ${config.auth.token}`;
13
13
  }
14
14
  else {
15
- this.authHeader = config.auth.apiKey;
15
+ this.authHeader = `Bearer ${config.auth.apiKey}`;
16
16
  }
17
17
  }
18
18
  async request(method, path, body) {
@@ -1,6 +1,7 @@
1
1
  export { DecodoClient } from './client.js';
2
- export type { DecodoConfig } from './client.js';
2
+ export type { DecodoConfig, WebScrapingApiConfig } from './client.js';
3
3
  export { WebScrapingApi } from './api/web-scraping-api.js';
4
+ export type { WebScrapingApiRoutes } from './api/web-scraping-api.js';
4
5
  export { BundledSchema } from './schema/bundled-schema.js';
5
6
  export { RemoteSchema } from './schema/remote-schema.js';
6
7
  export type { DecodoSchema, RemoteSchemaLoadOptions } from './schema/types.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decodo/sdk-ts",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Official TypeScript SDK for the Decodo APIs",
5
5
  "type": "module",
6
6
  "main": "./build/cjs/index.cjs",
@@ -21,18 +21,6 @@
21
21
  "files": [
22
22
  "build"
23
23
  ],
24
- "scripts": {
25
- "codegen": "tsx src/codegen/codegen.ts",
26
- "build": "pnpm run codegen && pnpm run build:esm && pnpm run build:cjs",
27
- "build:esm": "tsc -p tsconfig.build.json",
28
- "build:cjs": "rm -rf build/cjs && mkdir -p build/cjs && esbuild src/index.ts --bundle --platform=node --format=cjs --external:zod --outfile=build/cjs/index.cjs && dts-bundle-generator -o build/cjs/index.d.cts --project tsconfig.build.json build/esm/index.d.ts",
29
- "typecheck": "tsc --noEmit",
30
- "lint": "eslint .",
31
- "lint:fix": "eslint . --fix",
32
- "test": "vitest run",
33
- "test:watch": "vitest",
34
- "prepack": "pnpm run build:esm && pnpm run build:cjs"
35
- },
36
24
  "engines": {
37
25
  "node": ">=18.0.0"
38
26
  },
@@ -52,5 +40,16 @@
52
40
  "dependencies": {
53
41
  "zod": "^4"
54
42
  },
55
- "license": "MIT"
56
- }
43
+ "license": "MIT",
44
+ "scripts": {
45
+ "codegen": "tsx src/codegen/codegen.ts",
46
+ "build": "pnpm run codegen && pnpm run build:esm && pnpm run build:cjs",
47
+ "build:esm": "tsc -p tsconfig.build.json",
48
+ "build:cjs": "rm -rf build/cjs && mkdir -p build/cjs && esbuild src/index.ts --bundle --platform=node --format=cjs --external:zod --outfile=build/cjs/index.cjs && dts-bundle-generator -o build/cjs/index.d.cts --project tsconfig.build.json build/esm/index.d.ts",
49
+ "typecheck": "tsc --noEmit",
50
+ "lint": "eslint .",
51
+ "lint:fix": "eslint . --fix",
52
+ "test": "vitest run",
53
+ "test:watch": "vitest"
54
+ }
55
+ }