@crawlbrulee/sdk 1.0.1 → 1.2.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.
package/dist/index.d.cts CHANGED
@@ -91,19 +91,52 @@ type ResolvedProxyTier = 'basic' | 'advanced';
91
91
  */
92
92
  type BillingEngine = 'http' | 'browser' | 'screenshot' | 'cache';
93
93
  /**
94
- * Usage accounting for a single billable scrape operation, returned on the
95
- * response envelope of scrape and async-status (when terminal), and on
96
- * completion webhooks.
94
+ * Usage accounting for a single scrape, returned on `response_meta.usage` of
95
+ * scrape and async-status (when terminal) responses, and on completion
96
+ * webhooks.
97
+ *
98
+ * The response explains its own price:
99
+ * `total_credit_cost = engine_credit_cost × proxy_multiplier + screenshot_slicing_credit_cost + zero_data_retention_credit_cost`.
100
+ *
101
+ * The `*_credit_cost` / `proxy_multiplier` fields are optional in this type
102
+ * because older api versions do not send them.
97
103
  */
98
104
  interface Usage {
99
105
  /**
100
- * Credits charged for this operation. This equals the engine base multiplied
101
- * by the resolved proxy multiplier, plus {@link screenshot_slices}.
106
+ * Credits charged for this request. Always equals
107
+ * `engine_credit_cost × proxy_multiplier + screenshot_slicing_credit_cost + zero_data_retention_credit_cost`.
108
+ * `0` when nothing is billed: a cache hit, or a page whose status is not
109
+ * billed (see `page_status_code` on the scrape result).
110
+ *
111
+ * Optional only because older api versions do not send it.
112
+ */
113
+ total_credit_cost?: number;
114
+ /**
115
+ * The engine base charged, before the proxy multiplier: `1` for `http`, `3`
116
+ * for `browser`, `5` for `screenshot`, `0` for `cache`. Also `0` when the
117
+ * page is not billed.
118
+ */
119
+ engine_credit_cost?: number;
120
+ /**
121
+ * The multiplier of the proxy tier the request ran on: `1` for `basic`, `5`
122
+ * for `advanced`. Always reported, even when the engine cost is `0`.
123
+ */
124
+ proxy_multiplier?: number;
125
+ /**
126
+ * The screenshot slicing add-on: `1` when the screenshot was split into
127
+ * slices on this request (a flat +1 credit outside the proxy multiplier,
128
+ * however many slices it made), otherwise `0`.
129
+ */
130
+ screenshot_slicing_credit_cost?: number;
131
+ /**
132
+ * Credits added by zero data retention: `1` on a billed, fresh result,
133
+ * otherwise `0`. Absent on an older api version; read it as
134
+ * `usage.zero_data_retention_credit_cost ?? 0`.
102
135
  */
103
- credits: number;
136
+ zero_data_retention_credit_cost?: number;
104
137
  /**
105
- * Engine base billed for the delivered result: `http` (1), `browser` (3),
106
- * `screenshot` (5), or `cache` (0).
138
+ * Engine the delivered result was billed at: `http`, `browser`,
139
+ * `screenshot`, or `cache` (served from cache).
107
140
  */
108
141
  engine: BillingEngine;
109
142
  /**
@@ -111,11 +144,6 @@ interface Usage {
111
144
  * multiplies the engine base by 5.
112
145
  */
113
146
  proxy: ResolvedProxyTier;
114
- /**
115
- * Screenshot-slice add-on billed for this request: `1` when slices were
116
- * produced during this request, otherwise `0`.
117
- */
118
- screenshot_slices: number;
119
147
  }
120
148
  /**
121
149
  * Response envelope `response_meta` carried by scrape responses and async-status
@@ -186,7 +214,7 @@ interface ScreenshotRequest {
186
214
  * Machine-readable error names returned by the crawlbrulee API. Stable
187
215
  * identifiers — clients can switch on them.
188
216
  */
189
- type ApiErrorName = 'usage_allocation_error' | 'request_timeout' | 'invalid_url' | 'url_too_long' | 'client_closed_request' | 'reset_password_token_expired' | 'user_not_found' | 'unsupported_url_schema' | 'url_credentials_not_supported' | 'blocked_url' | 'scrape_error' | 'job_failed' | 'incorrect_login_method_used' | 'not_found' | 'invalid_credentials' | 'resource_already_exists' | 'access_denied' | 'internal_server_error' | 'service_unavailable' | 'too_many_requests' | 'unsupported_content' | 'unsupported_screenshot_output' | 'validation_error' | 'antibot_blocked' | 'too_many_redirects' | 'page_too_large';
217
+ type ApiErrorName = 'usage_allocation_error' | 'request_timeout' | 'invalid_url' | 'url_too_long' | 'client_closed_request' | 'reset_password_token_expired' | 'user_not_found' | 'unsupported_url_schema' | 'url_credentials_not_supported' | 'blocked_url' | 'scrape_error' | 'job_failed' | 'incorrect_login_method_used' | 'not_found' | 'invalid_credentials' | 'resource_already_exists' | 'access_denied' | 'internal_server_error' | 'service_unavailable' | 'too_many_requests' | 'unsupported_content' | 'unsupported_screenshot_output' | 'validation_error' | 'antibot_blocked' | 'too_many_redirects' | 'page_too_large' | 'target_unreachable' | 'zero_data_retention_not_enabled';
190
218
  /** Reason a usage allocation was denied (when `error_name = usage_allocation_error`). */
191
219
  type UsageAllocationReason = 'credit_limit' | 'concurrency_limit' | 'duplicate_reservation' | 'internal_error';
192
220
  /** Snapshot of the org's current usage at the moment the error was raised. */
@@ -351,6 +379,13 @@ interface ScrapeRequest {
351
379
  proxy?: ProxyTier;
352
380
  /** Optional locale + country emulation. */
353
381
  location?: ScrapeLocation;
382
+ /**
383
+ * Keeps the result out of the shared cache; anything stored to deliver it is
384
+ * kept for 24 hours, then deleted. Adds 1 credit. Must be enabled for your
385
+ * organization, or the request fails with {@link ZeroDataRetentionNotEnabledError}.
386
+ * See https://crawlbrulee.com/docs/zero-data-retention.
387
+ */
388
+ zero_data_retention?: boolean;
354
389
  }
355
390
  /**
356
391
  * Per-job completion webhook, attached when submitting an ASYNC scrape via
@@ -503,7 +538,14 @@ interface ScrapeMetadata {
503
538
  * outright rather than returning a hollow `200`, and isn't billed.
504
539
  */
505
540
  type ScrapeWarningCode = 'screenshot_truncated' | 'links_truncated' | 'inline_images_truncated' | 'raw_html_truncated' | 'metadata_truncated' | 'links_unavailable' | 'inline_images_unavailable' | 'metadata_unavailable';
506
- /** Successful response from `POST /api/scrape` and `GET /api/scrape/result/:jobId`. */
541
+ /**
542
+ * Successful response from `POST /api/scrape` and `GET /api/scrape/result/:jobId`.
543
+ *
544
+ * A successful response means we reached the site and got its final page —
545
+ * whatever status the site answered with. A 404, 410, 401 or 503 page comes
546
+ * back here with its content, and the site's own status is in
547
+ * {@link ScrapeResponse.page_status_code}. It is not thrown as an error.
548
+ */
507
549
  interface ScrapeResponse {
508
550
  /**
509
551
  * The URL that was actually scraped, after any redirects, in cleaned
@@ -513,6 +555,22 @@ interface ScrapeResponse {
513
555
  url: string;
514
556
  /** The URL you requested, echoed verbatim — before any redirects. */
515
557
  requested_url: string;
558
+ /**
559
+ * The HTTP status the target site answered with for the final page, after
560
+ * redirects. When the page is rendered in a browser, it is the status of the
561
+ * main document, not of images or scripts.
562
+ *
563
+ * A page the site really served is a successful result, whatever its
564
+ * status: a 404 page comes back here as `404` with its content — it is not
565
+ * thrown as an error. Check this field when the page's status matters to
566
+ * you, for example `if (page.page_status_code === 404)`.
567
+ *
568
+ * Billing follows this status: 2xx and 4xx pages are billed, except 403,
569
+ * 407, 408, 429 and 451; 5xx pages are never billed.
570
+ *
571
+ * Optional only because older api versions do not send it.
572
+ */
573
+ page_status_code?: number;
516
574
  /** `Content-Type` header returned by the origin. */
517
575
  content_type?: string;
518
576
  /**
@@ -559,8 +617,10 @@ interface ScrapeResponse {
559
617
  */
560
618
  warnings?: (ScrapeWarningCode | (string & {}))[];
561
619
  /**
562
- * Response envelope metadata. Carries `usage` (credits charged, billing
563
- * engine, resolved proxy tier, and billed screenshot slices).
620
+ * Response metadata. Carries `usage`: the credits charged
621
+ * (`total_credit_cost`) and how they add up (`engine_credit_cost`,
622
+ * `proxy_multiplier`, `screenshot_slicing_credit_cost`), plus the billed
623
+ * `engine` and the resolved `proxy` tier. See `Usage`.
564
624
  */
565
625
  response_meta: ResponseMeta;
566
626
  }
@@ -628,6 +688,13 @@ interface MapRequest {
628
688
  limit?: number;
629
689
  /** Optional country emulation. */
630
690
  location?: MapLocation;
691
+ /**
692
+ * Keeps the result out of the shared cache; anything stored to deliver it is
693
+ * kept for 24 hours, then deleted. Adds 1 credit. Must be enabled for your
694
+ * organization, or the request fails with {@link ZeroDataRetentionNotEnabledError}.
695
+ * See https://crawlbrulee.com/docs/zero-data-retention.
696
+ */
697
+ zero_data_retention?: boolean;
631
698
  }
632
699
  /** Single discovered URL in a map result. */
633
700
  interface MapLinkItem {
@@ -699,10 +766,40 @@ interface MapTruncation {
699
766
  }
700
767
  /** Billing engine reported by map: fresh discovery or a cached result. */
701
768
  type MapBillingEngine = 'http' | 'cache';
702
- /** Usage accounting returned by the map endpoint. Map operations do not produce screenshot slices. */
769
+ /**
770
+ * Usage accounting returned by the map endpoint. Map requests make no
771
+ * screenshots, so there is no slicing add-on:
772
+ * `total_credit_cost = engine_credit_cost × proxy_multiplier + zero_data_retention_credit_cost`.
773
+ *
774
+ * The `*_credit_cost` / `proxy_multiplier` fields are optional in this type
775
+ * because older api versions do not send them.
776
+ */
703
777
  interface MapUsage {
704
- /** Credits charged for this operation. */
705
- credits: number;
778
+ /**
779
+ * Credits charged for this map request. Always equals
780
+ * `engine_credit_cost × proxy_multiplier + zero_data_retention_credit_cost`. `0` for a cache hit, and for an
781
+ * empty map when the site answered only with statuses we don't bill (a `5xx`,
782
+ * for example) or not at all.
783
+ *
784
+ * Optional only because older api versions do not send it.
785
+ */
786
+ total_credit_cost?: number;
787
+ /**
788
+ * The engine base charged, before the proxy multiplier: `1` for `http`, `0`
789
+ * for `cache`. Also `0` for an empty map that is not billed.
790
+ */
791
+ engine_credit_cost?: number;
792
+ /**
793
+ * The multiplier of the proxy tier the request ran on: `1` for `basic`, `5`
794
+ * for `advanced`. Always reported, even when the engine cost is `0`.
795
+ */
796
+ proxy_multiplier?: number;
797
+ /**
798
+ * Credits added by zero data retention: `1` on a billed, fresh result,
799
+ * otherwise `0`. Absent on an older api version; read it as
800
+ * `usage.zero_data_retention_credit_cost ?? 0`.
801
+ */
802
+ zero_data_retention_credit_cost?: number;
706
803
  /** `http` for fresh discovery or `cache` for a cached result. */
707
804
  engine: MapBillingEngine;
708
805
  /** The proxy tier the server resolved and used (never `auto`). */
@@ -749,8 +846,8 @@ interface AsyncJobStatusResponse {
749
846
  error?: string;
750
847
  /**
751
848
  * Response envelope metadata — present only when the job has reached the
752
- * terminal `done` state. Carries `usage` (credits charged, billing engine,
753
- * resolved proxy tier, and billed screenshot slices).
849
+ * terminal `done` state. Carries `usage`: the credits charged and how they
850
+ * add up. See `Usage`.
754
851
  */
755
852
  response_meta?: ResponseMeta;
756
853
  }
@@ -810,6 +907,15 @@ interface ScrapeCompleteWebhookData {
810
907
  status: ScrapeWebhookStatus;
811
908
  /** The URL that was scraped. */
812
909
  url: string;
910
+ /**
911
+ * The HTTP status the target site answered with for the final page, after
912
+ * redirects. Present only on `status: 'success'` deliveries. A 404 page is a
913
+ * `success` delivery with `page_status_code: 404` — see
914
+ * `ScrapeResponse.page_status_code`.
915
+ *
916
+ * Optional also because older api versions do not send it.
917
+ */
918
+ page_status_code?: number;
813
919
  /** ISO-8601 UTC timestamp when the job reached its terminal state. */
814
920
  completed_at: string;
815
921
  /** Failure message — present only when `status === 'failed'`. */
@@ -820,9 +926,9 @@ interface ScrapeCompleteWebhookData {
820
926
  */
821
927
  metadata?: Record<string, unknown>;
822
928
  /**
823
- * Response envelope metadata — `usage` (credits charged, billing engine,
824
- * resolved proxy tier, and billed screenshot slices). Present only on `status: 'success'`
825
- * deliveries; omitted for `failed` / `cancelled` (no usage was charged).
929
+ * Response metadata — `usage`: the credits charged and how they add up
930
+ * (see `Usage`). Present only on `status: 'success'` deliveries;
931
+ * omitted for `failed` / `cancelled` (nothing was charged).
826
932
  */
827
933
  response_meta?: ResponseMeta;
828
934
  }
@@ -927,6 +1033,13 @@ declare class Crawlbrulee {
927
1033
  * (heavy JS rendering, screenshots of long pages) prefer
928
1034
  * {@link Crawlbrulee.scrapeAsync} so the connection isn't held open.
929
1035
  *
1036
+ * Any page the site really served is returned, whatever its status: a 404,
1037
+ * 410, 401 or 503 page resolves normally, with the site's status in
1038
+ * `page_status_code` — it does not throw. Check that field when the page's
1039
+ * status matters to you. The call throws when we could not return the page,
1040
+ * for example `TargetUnreachableError` when the site could not be reached,
1041
+ * `AntibotBlockedError`, or `PageTooLargeError`.
1042
+ *
930
1043
  * @param request — body for `POST /api/scrape`.
931
1044
  * @param options — per-call timeout and abort signal.
932
1045
  */
@@ -947,6 +1060,10 @@ declare class Crawlbrulee {
947
1060
  * Fetch the result of a completed async scrape job. Throws if the job is
948
1061
  * still pending/running — call {@link Crawlbrulee.getScrapeStatus}
949
1062
  * first, or use {@link Crawlbrulee.waitForScrape} to poll-then-fetch.
1063
+ *
1064
+ * Like {@link Crawlbrulee.scrape}, a 404 (or any other) page the site served
1065
+ * is a completed job: the result carries the site's status in
1066
+ * `page_status_code`.
950
1067
  */
951
1068
  getScrapeResult(jobId: string, options?: RequestOptions): Promise<ScrapeResponse>;
952
1069
  /**
@@ -1038,7 +1155,9 @@ declare class CrawlbruleeError extends Error {
1038
1155
  *
1039
1156
  * Not every 403 is a key problem: a 403 carrying `antibot_blocked` is the
1040
1157
  * *target site* blocking us and raises {@link AntibotBlockedError} instead.
1041
- * Only an unrecognized 403 name falls back to this class.
1158
+ * A 403 carrying `zero_data_retention_not_enabled` raises
1159
+ * {@link ZeroDataRetentionNotEnabledError}. Only an unrecognized 403 name falls
1160
+ * back to this class.
1042
1161
  */
1043
1162
  declare class AuthenticationError extends CrawlbruleeError {
1044
1163
  constructor(message: string, options: {
@@ -1087,6 +1206,42 @@ declare class PageTooLargeError extends CrawlbruleeError {
1087
1206
  response?: ApiErrorResponse;
1088
1207
  });
1089
1208
  }
1209
+ /**
1210
+ * Raised when we could not reach the target site at all (HTTP 502,
1211
+ * `target_unreachable`) — for example the site did not answer in time or its
1212
+ * TLS certificate was not valid. We never got a page, so there is nothing to
1213
+ * return and you are not charged. The message is always "Could not reach the
1214
+ * target site." and there are no `details`.
1215
+ *
1216
+ * This is about the site, not your key or your request, so it is neither an
1217
+ * `AuthenticationError` nor a `ValidationError`. It is also not our own
1218
+ * outage ({@link ServiceUnavailableError}). Retrying later may help, since
1219
+ * the site may come back. The SDK does not retry for you.
1220
+ *
1221
+ * Not the same as a page with an error status: when the site answers with a
1222
+ * 404 or 503 page, the scrape succeeds and the site's status is in
1223
+ * `page_status_code`. Returned by both `scrape` and `map`.
1224
+ */
1225
+ declare class TargetUnreachableError extends CrawlbruleeError {
1226
+ constructor(message: string, options: {
1227
+ status: number;
1228
+ errorName: ApiErrorName;
1229
+ response?: ApiErrorResponse;
1230
+ });
1231
+ }
1232
+ /**
1233
+ * Raised when you send `zero_data_retention: true` but the option is not
1234
+ * enabled for your organization (HTTP 403, `zero_data_retention_not_enabled`).
1235
+ * The request is not billed. Not an API-key problem, so it is not an
1236
+ * {@link AuthenticationError}. See https://crawlbrulee.com/docs/zero-data-retention.
1237
+ */
1238
+ declare class ZeroDataRetentionNotEnabledError extends CrawlbruleeError {
1239
+ constructor(message: string, options: {
1240
+ status: number;
1241
+ errorName: ApiErrorName;
1242
+ response?: ApiErrorResponse;
1243
+ });
1244
+ }
1090
1245
  /**
1091
1246
  * Raised for HTTP 429 responses. When the server included a `retry_after_ms`
1092
1247
  * hint in `details` it is surfaced directly on the instance.
@@ -1133,7 +1288,14 @@ declare class ValidationError extends CrawlbruleeError {
1133
1288
  response?: ApiErrorResponse;
1134
1289
  });
1135
1290
  }
1136
- /** Raised for 404 responses (e.g. unknown async job ID). */
1291
+ /**
1292
+ * Raised for 404 responses from the crawlbrulee api — something on our side
1293
+ * was not found, for example an unknown or expired async job ID.
1294
+ *
1295
+ * It never means the target page was not found. When the site you scrape
1296
+ * answers with a 404 page, the scrape succeeds and returns that page with
1297
+ * `page_status_code: 404`.
1298
+ */
1137
1299
  declare class NotFoundError extends CrawlbruleeError {
1138
1300
  constructor(message: string, options: {
1139
1301
  status: number;
@@ -1280,5 +1442,5 @@ interface VerifyWebhookSignatureOptions {
1280
1442
  */
1281
1443
  declare function verifyWebhookSignature(options: VerifyWebhookSignatureOptions): Promise<WebhookVerificationResult>;
1282
1444
  //#endregion
1283
- export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, isCrawlbruleeError, verifyWebhookSignature };
1445
+ export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TargetUnreachableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, ZeroDataRetentionNotEnabledError, isCrawlbruleeError, verifyWebhookSignature };
1284
1446
  //# sourceMappingURL=index.d.cts.map