apify-client 3.0.0-beta.1 → 3.0.0-beta.11

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.
Files changed (94) hide show
  1. package/README.md +5 -3
  2. package/dist/apify_api_error.d.ts +59 -3
  3. package/dist/apify_api_error.js +71 -11
  4. package/dist/apify_client.d.ts +27 -27
  5. package/dist/apify_client.js +86 -91
  6. package/dist/base/api_client.d.ts +3 -3
  7. package/dist/base/api_client.js +6 -10
  8. package/dist/base/resource_client.d.ts +13 -4
  9. package/dist/base/resource_client.js +25 -21
  10. package/dist/base/resource_collection_client.d.ts +7 -6
  11. package/dist/base/resource_collection_client.js +11 -15
  12. package/dist/body_parser.js +6 -11
  13. package/dist/bundle.js +38 -48
  14. package/dist/bundle.js.map +1 -1
  15. package/dist/generated/api.d.ts +287 -214
  16. package/dist/generated/api.js +1 -2
  17. package/dist/generated/schemas.d.ts +6699 -0
  18. package/dist/generated/schemas.js +1521 -0
  19. package/dist/http_client.d.ts +2 -2
  20. package/dist/http_client.js +23 -28
  21. package/dist/index.d.ts +33 -32
  22. package/dist/index.js +32 -36
  23. package/dist/interceptors.d.ts +1 -1
  24. package/dist/interceptors.js +14 -19
  25. package/dist/lazy_schema.d.ts +8 -0
  26. package/dist/lazy_schema.js +11 -0
  27. package/dist/models.d.ts +33 -52
  28. package/dist/models.js +8 -11
  29. package/dist/resource_clients/actor.d.ts +13 -13
  30. package/dist/resource_clients/actor.js +70 -73
  31. package/dist/resource_clients/actor_collection.d.ts +10 -10
  32. package/dist/resource_clients/actor_collection.js +19 -20
  33. package/dist/resource_clients/actor_env_var.d.ts +3 -3
  34. package/dist/resource_clients/actor_env_var.js +7 -10
  35. package/dist/resource_clients/actor_env_var_collection.d.ts +11 -20
  36. package/dist/resource_clients/actor_env_var_collection.js +15 -19
  37. package/dist/resource_clients/actor_version.d.ts +16 -8
  38. package/dist/resource_clients/actor_version.js +15 -19
  39. package/dist/resource_clients/actor_version_collection.d.ts +13 -19
  40. package/dist/resource_clients/actor_version_collection.js +17 -19
  41. package/dist/resource_clients/build.d.ts +7 -5
  42. package/dist/resource_clients/build.js +16 -17
  43. package/dist/resource_clients/build_collection.d.ts +5 -5
  44. package/dist/resource_clients/build_collection.js +10 -13
  45. package/dist/resource_clients/dataset.d.ts +8 -8
  46. package/dist/resource_clients/dataset.js +75 -85
  47. package/dist/resource_clients/dataset_collection.d.ts +4 -4
  48. package/dist/resource_clients/dataset_collection.js +18 -21
  49. package/dist/resource_clients/key_value_store.d.ts +16 -8
  50. package/dist/resource_clients/key_value_store.js +61 -65
  51. package/dist/resource_clients/key_value_store_collection.d.ts +4 -4
  52. package/dist/resource_clients/key_value_store_collection.js +18 -21
  53. package/dist/resource_clients/log.d.ts +6 -4
  54. package/dist/resource_clients/log.js +23 -28
  55. package/dist/resource_clients/request_queue.d.ts +4 -11
  56. package/dist/resource_clients/request_queue.js +100 -106
  57. package/dist/resource_clients/request_queue_collection.d.ts +4 -4
  58. package/dist/resource_clients/request_queue_collection.js +16 -19
  59. package/dist/resource_clients/run.d.ts +18 -8
  60. package/dist/resource_clients/run.js +62 -54
  61. package/dist/resource_clients/run_collection.d.ts +4 -4
  62. package/dist/resource_clients/run_collection.js +15 -18
  63. package/dist/resource_clients/schedule.d.ts +8 -8
  64. package/dist/resource_clients/schedule.js +14 -23
  65. package/dist/resource_clients/schedule_collection.d.ts +4 -4
  66. package/dist/resource_clients/schedule_collection.js +13 -16
  67. package/dist/resource_clients/store_collection.d.ts +5 -5
  68. package/dist/resource_clients/store_collection.js +15 -18
  69. package/dist/resource_clients/task.d.ts +11 -11
  70. package/dist/resource_clients/task.js +49 -59
  71. package/dist/resource_clients/task_collection.d.ts +6 -6
  72. package/dist/resource_clients/task_collection.js +12 -15
  73. package/dist/resource_clients/user.d.ts +11 -11
  74. package/dist/resource_clients/user.js +16 -35
  75. package/dist/resource_clients/webhook.d.ts +8 -8
  76. package/dist/resource_clients/webhook.js +13 -23
  77. package/dist/resource_clients/webhook_collection.d.ts +4 -4
  78. package/dist/resource_clients/webhook_collection.js +13 -16
  79. package/dist/resource_clients/webhook_dispatch.d.ts +5 -5
  80. package/dist/resource_clients/webhook_dispatch.js +5 -9
  81. package/dist/resource_clients/webhook_dispatch_collection.d.ts +4 -4
  82. package/dist/resource_clients/webhook_dispatch_collection.js +10 -13
  83. package/dist/response_validation_error.d.ts +26 -0
  84. package/dist/response_validation_error.js +37 -0
  85. package/dist/schemas.d.ts +15 -0
  86. package/dist/schemas.js +15 -0
  87. package/dist/statistics.js +5 -9
  88. package/dist/timezones.js +1 -2
  89. package/dist/utils.d.ts +27 -24
  90. package/dist/utils.js +69 -93
  91. package/package.json +38 -40
  92. package/dist/argument_validation_error.d.ts +0 -17
  93. package/dist/argument_validation_error.js +0 -157
  94. package/dist/index.mjs +0 -42
package/README.md CHANGED
@@ -26,7 +26,7 @@ automatic retries and convenience functions that improve the experience of using
26
26
  ## Quick Start
27
27
 
28
28
  ```js
29
- const { ApifyClient } = require('apify-client');
29
+ import { ApifyClient } from 'apify-client';
30
30
 
31
31
  const client = new ApifyClient({
32
32
  token: 'MY-APIFY-TOKEN',
@@ -47,7 +47,9 @@ Besides greatly simplifying the process of querying the Apify API, the client pr
47
47
  Based on the endpoint, the client automatically extracts the relevant data and returns it in the
48
48
  expected format. Date strings are automatically converted to `Date` objects. For exceptions,
49
49
  we throw an `ApifyApiError`, which wraps the plain JSON errors returned by API and enriches
50
- them with other context for easier debugging.
50
+ them with other context for easier debugging. The error is an instance of the subclass matching the
51
+ HTTP status code, such as `NotFoundError` or `RateLimitError`, so a `catch` block can tell them apart
52
+ with `instanceof`.
51
53
 
52
54
  ### Retries with exponential backoff
53
55
 
@@ -72,7 +74,7 @@ resources are created. There are two types of those clients. A client for manage
72
74
  resource and a client for a collection of resources.
73
75
 
74
76
  ```js
75
- const { ApifyClient } = require('apify-client');
77
+ import { ApifyClient } from 'apify-client';
76
78
  const apifyClient = new ApifyClient({ token: 'my-token' });
77
79
 
78
80
  // Collection clients do not require a parameter.
@@ -1,10 +1,20 @@
1
1
  import type { AxiosResponse } from 'axios';
2
+ import type { LiteralUnion } from 'type-fest';
3
+ import type { ApifyApiErrorType } from './models.js';
4
+ export type { ApifyApiErrorType } from './models.js';
2
5
  /**
3
6
  * An `ApifyApiError` is thrown for successful HTTP requests that reach the API,
4
7
  * but the API responds with an error response. Typically, those are rate limit
5
8
  * errors and internal errors, which are automatically retried, or validation
6
9
  * errors, which are thrown immediately, because a correction by the user is
7
10
  * needed.
11
+ *
12
+ * The thrown error is an instance of the subclass matching the HTTP status code of the response:
13
+ * {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
14
+ * {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
15
+ * {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
16
+ * subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
17
+ * share a status code are told apart by their `type`.
8
18
  */
9
19
  export declare class ApifyApiError extends Error {
10
20
  name: string;
@@ -18,9 +28,10 @@ export declare class ApifyApiError extends Error {
18
28
  */
19
29
  statusCode: number;
20
30
  /**
21
- * The type of the error, as returned by the API.
31
+ * The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
32
+ * values for autocompletion, while still accepting any string the API may return.
22
33
  */
23
- type?: string;
34
+ type?: LiteralUnion<ApifyApiErrorType, string>;
24
35
  /**
25
36
  * Number of the API call attempt.
26
37
  */
@@ -46,6 +57,11 @@ export declare class ApifyApiError extends Error {
46
57
  * @hidden
47
58
  */
48
59
  constructor(response: AxiosResponse, attempt: number);
60
+ /**
61
+ * Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
62
+ * @hidden
63
+ */
64
+ static fromResponse(response: AxiosResponse, attempt: number): ApifyApiError;
49
65
  private _safelyParsePathFromResponse;
50
66
  private _extractClientAndMethodFromStack;
51
67
  /**
@@ -54,7 +70,7 @@ export declare class ApifyApiError extends Error {
54
70
  *
55
71
  * Example:
56
72
  *
57
- * ApifyApiError: Actor task was not found
73
+ * NotFoundError: Actor task was not found
58
74
  * clientMethod: TaskClient.start
59
75
  * statusCode: 404
60
76
  * type: record-not-found
@@ -64,3 +80,43 @@ export declare class ApifyApiError extends Error {
64
80
  */
65
81
  private _createApiStack;
66
82
  }
83
+ /**
84
+ * Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
85
+ * failed validation.
86
+ */
87
+ export declare class InvalidRequestError extends ApifyApiError {
88
+ }
89
+ /**
90
+ * Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
91
+ * or invalid.
92
+ */
93
+ export declare class UnauthorizedError extends ApifyApiError {
94
+ }
95
+ /**
96
+ * Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
97
+ * permission for the operation.
98
+ */
99
+ export declare class ForbiddenError extends ApifyApiError {
100
+ }
101
+ /**
102
+ * Thrown when the Apify API responds with HTTP 404 Not Found.
103
+ */
104
+ export declare class NotFoundError extends ApifyApiError {
105
+ }
106
+ /**
107
+ * Thrown when the Apify API responds with HTTP 409 Conflict.
108
+ */
109
+ export declare class ConflictError extends ApifyApiError {
110
+ }
111
+ /**
112
+ * Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
113
+ * requests, so the error surfaces once the retries are exhausted.
114
+ */
115
+ export declare class RateLimitError extends ApifyApiError {
116
+ }
117
+ /**
118
+ * Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
119
+ * so the error surfaces once the retries are exhausted.
120
+ */
121
+ export declare class ServerError extends ApifyApiError {
122
+ }
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApifyApiError = void 0;
4
- const body_parser_1 = require("./body_parser");
5
- const utils_1 = require("./utils");
1
+ import { isomorphicBufferToString } from './body_parser.js';
2
+ import { isBuffer } from './utils.js';
6
3
  /**
7
4
  * Examples of capturing groups for "...at ActorCollectionClient._list (/Users/..."
8
5
  * 0: "at ActorCollectionClient._list ("
@@ -19,8 +16,15 @@ const CLIENT_METHOD_REGEX = /at( async)? ([A-Za-z]+(Collection)?Client)\._?([A-Z
19
16
  * errors and internal errors, which are automatically retried, or validation
20
17
  * errors, which are thrown immediately, because a correction by the user is
21
18
  * needed.
19
+ *
20
+ * The thrown error is an instance of the subclass matching the HTTP status code of the response:
21
+ * {@link InvalidRequestError} (400), {@link UnauthorizedError} (401), {@link ForbiddenError} (403),
22
+ * {@link NotFoundError} (404), {@link ConflictError} (409), {@link RateLimitError} (429) or
23
+ * {@link ServerError} (5xx). Any other status code is thrown as a plain `ApifyApiError`. Every
24
+ * subclass extends `ApifyApiError`, so `instanceof ApifyApiError` matches all of them. Errors that
25
+ * share a status code are told apart by their `type`.
22
26
  */
23
- class ApifyApiError extends Error {
27
+ export class ApifyApiError extends Error {
24
28
  name;
25
29
  /**
26
30
  * The invoked resource client and the method. Known issue: Sometimes it displays
@@ -32,7 +36,8 @@ class ApifyApiError extends Error {
32
36
  */
33
37
  statusCode;
34
38
  /**
35
- * The type of the error, as returned by the API.
39
+ * The type of the error, as returned by the API. Typed as the known {@link ApifyApiErrorType}
40
+ * values for autocompletion, while still accepting any string the API may return.
36
41
  */
37
42
  type;
38
43
  /**
@@ -66,9 +71,9 @@ class ApifyApiError extends Error {
66
71
  let errorData;
67
72
  // Some methods (e.g. downloadItems) set up forceBuffer on request response. If this request failed
68
73
  // the body buffer needs to parse to get the correct error.
69
- if ((0, utils_1.isBuffer)(responseData)) {
74
+ if (isBuffer(responseData)) {
70
75
  try {
71
- responseData = JSON.parse((0, body_parser_1.isomorphicBufferToString)(response.data, 'utf-8'));
76
+ responseData = JSON.parse(isomorphicBufferToString(response.data, 'utf-8'));
72
77
  }
73
78
  catch {
74
79
  // This can happen. The data in the response body are malformed.
@@ -103,6 +108,14 @@ class ApifyApiError extends Error {
103
108
  this.stack = this._createApiStack();
104
109
  this.data = errorData;
105
110
  }
111
+ /**
112
+ * Creates the error for a failed response as an instance of the subclass matching its HTTP status code.
113
+ * @hidden
114
+ */
115
+ static fromResponse(response, attempt) {
116
+ const ErrorClass = ERROR_CLASS_BY_STATUS[response.status] ?? (response.status >= 500 ? ServerError : ApifyApiError);
117
+ return new ErrorClass(response, attempt);
118
+ }
106
119
  _safelyParsePathFromResponse(response) {
107
120
  const urlString = response.config?.url;
108
121
  let url;
@@ -126,7 +139,7 @@ class ApifyApiError extends Error {
126
139
  *
127
140
  * Example:
128
141
  *
129
- * ApifyApiError: Actor task was not found
142
+ * NotFoundError: Actor task was not found
130
143
  * clientMethod: TaskClient.start
131
144
  * statusCode: 404
132
145
  * type: record-not-found
@@ -148,4 +161,51 @@ class ApifyApiError extends Error {
148
161
  return `${name}: ${this.message}\n${stack}`;
149
162
  }
150
163
  }
151
- exports.ApifyApiError = ApifyApiError;
164
+ /**
165
+ * Thrown when the Apify API responds with HTTP 400 Bad Request, typically because the request
166
+ * failed validation.
167
+ */
168
+ export class InvalidRequestError extends ApifyApiError {
169
+ }
170
+ /**
171
+ * Thrown when the Apify API responds with HTTP 401 Unauthorized, because the token is missing
172
+ * or invalid.
173
+ */
174
+ export class UnauthorizedError extends ApifyApiError {
175
+ }
176
+ /**
177
+ * Thrown when the Apify API responds with HTTP 403 Forbidden, because the token lacks the
178
+ * permission for the operation.
179
+ */
180
+ export class ForbiddenError extends ApifyApiError {
181
+ }
182
+ /**
183
+ * Thrown when the Apify API responds with HTTP 404 Not Found.
184
+ */
185
+ export class NotFoundError extends ApifyApiError {
186
+ }
187
+ /**
188
+ * Thrown when the Apify API responds with HTTP 409 Conflict.
189
+ */
190
+ export class ConflictError extends ApifyApiError {
191
+ }
192
+ /**
193
+ * Thrown when the Apify API responds with HTTP 429 Too Many Requests. The client retries such
194
+ * requests, so the error surfaces once the retries are exhausted.
195
+ */
196
+ export class RateLimitError extends ApifyApiError {
197
+ }
198
+ /**
199
+ * Thrown when the Apify API responds with an HTTP 5xx status. The client retries such requests,
200
+ * so the error surfaces once the retries are exhausted.
201
+ */
202
+ export class ServerError extends ApifyApiError {
203
+ }
204
+ const ERROR_CLASS_BY_STATUS = {
205
+ 400: InvalidRequestError,
206
+ 401: UnauthorizedError,
207
+ 403: ForbiddenError,
208
+ 404: NotFoundError,
209
+ 409: ConflictError,
210
+ 429: RateLimitError,
211
+ };
@@ -1,32 +1,32 @@
1
1
  import type { SetStatusMessageOptions } from '@crawlee/types';
2
2
  import type { Log } from '@apify/log';
3
- import { HttpClient } from './http_client';
4
- import type { RequestInterceptorFunction } from './interceptors';
5
- import { ActorClient } from './resource_clients/actor';
6
- import { ActorCollectionClient } from './resource_clients/actor_collection';
7
- import { BuildClient } from './resource_clients/build';
8
- import { BuildCollectionClient } from './resource_clients/build_collection';
9
- import { DatasetClient } from './resource_clients/dataset';
10
- import { DatasetCollectionClient } from './resource_clients/dataset_collection';
11
- import { KeyValueStoreClient } from './resource_clients/key_value_store';
12
- import { KeyValueStoreCollectionClient } from './resource_clients/key_value_store_collection';
13
- import { LogClient } from './resource_clients/log';
14
- import type { RequestQueueUserOptions } from './resource_clients/request_queue';
15
- import { RequestQueueClient } from './resource_clients/request_queue';
16
- import { RequestQueueCollectionClient } from './resource_clients/request_queue_collection';
17
- import { RunClient } from './resource_clients/run';
18
- import { RunCollectionClient } from './resource_clients/run_collection';
19
- import { ScheduleClient } from './resource_clients/schedule';
20
- import { ScheduleCollectionClient } from './resource_clients/schedule_collection';
21
- import { StoreCollectionClient } from './resource_clients/store_collection';
22
- import { TaskClient } from './resource_clients/task';
23
- import { TaskCollectionClient } from './resource_clients/task_collection';
24
- import { UserClient } from './resource_clients/user';
25
- import { WebhookClient } from './resource_clients/webhook';
26
- import { WebhookCollectionClient } from './resource_clients/webhook_collection';
27
- import { WebhookDispatchClient } from './resource_clients/webhook_dispatch';
28
- import { WebhookDispatchCollectionClient } from './resource_clients/webhook_dispatch_collection';
29
- import { Statistics } from './statistics';
3
+ import { HttpClient } from './http_client.js';
4
+ import type { RequestInterceptorFunction } from './interceptors.js';
5
+ import { ActorClient } from './resource_clients/actor.js';
6
+ import { ActorCollectionClient } from './resource_clients/actor_collection.js';
7
+ import { BuildClient } from './resource_clients/build.js';
8
+ import { BuildCollectionClient } from './resource_clients/build_collection.js';
9
+ import { DatasetClient } from './resource_clients/dataset.js';
10
+ import { DatasetCollectionClient } from './resource_clients/dataset_collection.js';
11
+ import { KeyValueStoreClient } from './resource_clients/key_value_store.js';
12
+ import { KeyValueStoreCollectionClient } from './resource_clients/key_value_store_collection.js';
13
+ import { LogClient } from './resource_clients/log.js';
14
+ import type { RequestQueueUserOptions } from './resource_clients/request_queue.js';
15
+ import { RequestQueueClient } from './resource_clients/request_queue.js';
16
+ import { RequestQueueCollectionClient } from './resource_clients/request_queue_collection.js';
17
+ import { RunClient } from './resource_clients/run.js';
18
+ import { RunCollectionClient } from './resource_clients/run_collection.js';
19
+ import { ScheduleClient } from './resource_clients/schedule.js';
20
+ import { ScheduleCollectionClient } from './resource_clients/schedule_collection.js';
21
+ import { StoreCollectionClient } from './resource_clients/store_collection.js';
22
+ import { TaskClient } from './resource_clients/task.js';
23
+ import { TaskCollectionClient } from './resource_clients/task_collection.js';
24
+ import { UserClient } from './resource_clients/user.js';
25
+ import { WebhookClient } from './resource_clients/webhook.js';
26
+ import { WebhookCollectionClient } from './resource_clients/webhook_collection.js';
27
+ import { WebhookDispatchClient } from './resource_clients/webhook_dispatch.js';
28
+ import { WebhookDispatchCollectionClient } from './resource_clients/webhook_dispatch_collection.js';
29
+ import { Statistics } from './statistics.js';
30
30
  /**
31
31
  * The official JavaScript client for the Apify API.
32
32
  *