screenshotone-api-sdk 1.1.22 → 1.1.24

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.
@@ -1,5 +1,15 @@
1
1
  import APIError from "./errors";
2
2
  export { APIError };
3
+ export interface ClientOptions {
4
+ /** API base URL. Defaults to https://api.screenshotone.com. */
5
+ apiBaseUrl?: string;
6
+ /**
7
+ * Additional attempts for ECONNRESET errors, including response download
8
+ * failures. Defaults to 0 (disabled); set a positive value to enable retries.
9
+ * Retries use exponential backoff with jitter and may start another render.
10
+ */
11
+ maxRetries?: number;
12
+ }
3
13
  /**
4
14
  * Represents an API client for the ScreenshotOne.com API.
5
15
  */
@@ -7,7 +17,13 @@ export declare class Client {
7
17
  private readonly accessKey;
8
18
  private readonly secretKey;
9
19
  private readonly apiBaseUrl;
10
- constructor(accessKey: string, secretKey: string, apiBaseUrl?: string);
20
+ private readonly maxRetries;
21
+ /**
22
+ * @param options Client configuration. An API base URL string is also
23
+ * accepted for compatibility with existing constructor calls.
24
+ */
25
+ constructor(accessKey: string, secretKey: string, options?: ClientOptions | string);
26
+ private withConnectionResetRetries;
11
27
  /**
12
28
  * Generates an URL that can be used to send GET requests for taking screenshots.
13
29
  *
@@ -119,9 +135,15 @@ export declare class TakeOptions {
119
135
  */
120
136
  externalIdentifier(identifier: string): TakeOptions;
121
137
  /**
122
- * When reduced_motion set to `true`,
123
- * the API will request the site to minimize the amount of non-essential motion it uses.
124
- * When the site supports it, it should use animations as least as possible.
138
+ * Actively reduces supported motion by finishing animations and pausing media and sliders.
139
+ * Also enables the reduced-motion preference. The API default is false.
140
+ * Unlike reducedMotion(), this does not rely only on the website honoring that preference.
141
+ */
142
+ reduceMotion(reduceMotion: boolean): TakeOptions;
143
+ /**
144
+ * Requests reduced motion through the prefers-reduced-motion preference.
145
+ * Websites may ignore this preference; it does not actively freeze motion.
146
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
125
147
  */
126
148
  reducedMotion(reducedMotion: boolean): TakeOptions;
127
149
  /**
@@ -629,11 +651,17 @@ export declare class AnimateOptions {
629
651
  */
630
652
  responseType(responseType: string): this;
631
653
  /**
632
- * When reduced_motion set to `true`,
633
- * the API will request the site to minimize the amount of non-essential motion it uses.
634
- * When the site supports it, it should use animations as least as possible.
654
+ * Actively reduces supported page motion during recording by finishing animations
655
+ * and pausing media and sliders. Also enables the reduced-motion preference.
656
+ * The API default is false. Unlike reducedMotion(), this actively freezes motion.
657
+ */
658
+ reduceMotion(reduceMotion: boolean): AnimateOptions;
659
+ /**
660
+ * Requests reduced motion through the prefers-reduced-motion preference.
661
+ * Websites may ignore this preference; it does not actively freeze motion.
662
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
635
663
  */
636
- reducedMotion(darkMode: boolean): AnimateOptions;
664
+ reducedMotion(reducedMotion: boolean): AnimateOptions;
637
665
  /**
638
666
  * If you want to request the page and it is supported to be rendered for printers, specify `print`.
639
667
  * If the page is by default rendered for printers and you want it to be rendered for screens,
package/dist/main.js CHANGED
@@ -12,17 +12,45 @@ exports.APIError = errors_1.default;
12
12
  const API_BASE_URL = "https://api.screenshotone.com";
13
13
  const API_TAKE_PATH = "/take";
14
14
  const API_ANIMATE_PATH = "/animate";
15
+ function isConnectionReset(error) {
16
+ return (typeof error === "object" &&
17
+ error !== null &&
18
+ error.code === "ECONNRESET");
19
+ }
15
20
  /**
16
21
  * Represents an API client for the ScreenshotOne.com API.
17
22
  */
18
23
  class Client {
19
- constructor(accessKey, secretKey, apiBaseUrl) {
24
+ /**
25
+ * @param options Client configuration. An API base URL string is also
26
+ * accepted for compatibility with existing constructor calls.
27
+ */
28
+ constructor(accessKey, secretKey, options = {}) {
20
29
  if (!accessKey || !secretKey) {
21
30
  throw new Error("Both non-empty access and secret keys are required");
22
31
  }
32
+ const { apiBaseUrl, maxRetries = 0 } = typeof options === "string" ? { apiBaseUrl: options } : options;
33
+ if (!Number.isSafeInteger(maxRetries) || maxRetries < 0) {
34
+ throw new RangeError("maxRetries must be a non-negative safe integer");
35
+ }
23
36
  this.accessKey = accessKey;
24
37
  this.secretKey = secretKey;
25
38
  this.apiBaseUrl = apiBaseUrl ? apiBaseUrl : API_BASE_URL;
39
+ this.maxRetries = maxRetries;
40
+ }
41
+ async withConnectionResetRetries(operation) {
42
+ for (let retry = 0;; retry++) {
43
+ try {
44
+ return await operation();
45
+ }
46
+ catch (error) {
47
+ if (!isConnectionReset(error) || retry >= this.maxRetries) {
48
+ throw error;
49
+ }
50
+ const delay = Math.min(1000 * 2 ** retry, 30000) + Math.random() * 250;
51
+ await new Promise((resolve) => setTimeout(resolve, delay));
52
+ }
53
+ }
26
54
  }
27
55
  /**
28
56
  * Generates an URL that can be used to send GET requests for taking screenshots.
@@ -76,20 +104,22 @@ class Client {
76
104
  */
77
105
  async take(options) {
78
106
  const url = await this.generateSignedTakeURL(options);
79
- const response = await (0, cross_fetch_1.default)(url);
80
- if (response.ok) {
81
- return await response.blob();
82
- }
83
- try {
84
- const data = await response.json();
85
- throw new errors_1.default(`Failed to generate screenshot, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
86
- }
87
- catch (e) {
88
- if (e instanceof errors_1.default) {
89
- throw e;
107
+ return this.withConnectionResetRetries(async () => {
108
+ const response = await (0, cross_fetch_1.default)(url);
109
+ if (response.ok) {
110
+ return await response.blob();
90
111
  }
91
- throw new Error(`failed to take screenshot, response returned ${response.status} ${response.statusText}`);
92
- }
112
+ try {
113
+ const data = await response.json();
114
+ throw new errors_1.default(`Failed to generate screenshot, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
115
+ }
116
+ catch (e) {
117
+ if (e instanceof errors_1.default || isConnectionReset(e)) {
118
+ throw e;
119
+ }
120
+ throw new Error(`failed to take screenshot, response returned ${response.status} ${response.statusText}`);
121
+ }
122
+ });
93
123
  }
94
124
  /**
95
125
  * Generates an animated screenshot and return the image blob.
@@ -99,20 +129,22 @@ class Client {
99
129
  */
100
130
  async animate(options) {
101
131
  const url = await this.generateSignedAnimateURL(options);
102
- const response = await (0, cross_fetch_1.default)(url);
103
- if (response.ok) {
104
- return await response.blob();
105
- }
106
- try {
107
- const data = await response.json();
108
- throw new errors_1.default(`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
109
- }
110
- catch (e) {
111
- if (e instanceof errors_1.default) {
112
- throw e;
132
+ return this.withConnectionResetRetries(async () => {
133
+ const response = await (0, cross_fetch_1.default)(url);
134
+ if (response.ok) {
135
+ return await response.blob();
113
136
  }
114
- throw new Error(`Failed to generate animation, response returned ${response.status} (${response.statusText})`);
115
- }
137
+ try {
138
+ const data = await response.json();
139
+ throw new errors_1.default(`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
140
+ }
141
+ catch (e) {
142
+ if (e instanceof errors_1.default || isConnectionReset(e)) {
143
+ throw e;
144
+ }
145
+ throw new Error(`Failed to generate animation, response returned ${response.status} (${response.statusText})`);
146
+ }
147
+ });
116
148
  }
117
149
  /**
118
150
  * Generates an animated screenshot and return the image blob.
@@ -140,15 +172,17 @@ class Client {
140
172
  const url = options instanceof TakeOptions
141
173
  ? await this.generateSignedTakeURL(options)
142
174
  : await this.generateSignedAnimateURL(options);
143
- const response = await (0, cross_fetch_1.default)(url);
144
- if (response.ok) {
145
- return {
146
- key: response.headers.get("X-ScreenshotOne-Store-Key"),
147
- bucket: response.headers.get("X-ScreenshotOne-Store-Bucket"),
148
- };
149
- }
150
- const data = await response.json();
151
- throw new errors_1.default(`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
175
+ return this.withConnectionResetRetries(async () => {
176
+ const response = await (0, cross_fetch_1.default)(url);
177
+ if (response.ok) {
178
+ return {
179
+ key: response.headers.get("X-ScreenshotOne-Store-Key"),
180
+ bucket: response.headers.get("X-ScreenshotOne-Store-Bucket"),
181
+ };
182
+ }
183
+ const data = await response.json();
184
+ throw new errors_1.default(`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
185
+ });
152
186
  }
153
187
  }
154
188
  exports.Client = Client;
@@ -263,9 +297,18 @@ class TakeOptions {
263
297
  return this;
264
298
  }
265
299
  /**
266
- * When reduced_motion set to `true`,
267
- * the API will request the site to minimize the amount of non-essential motion it uses.
268
- * When the site supports it, it should use animations as least as possible.
300
+ * Actively reduces supported motion by finishing animations and pausing media and sliders.
301
+ * Also enables the reduced-motion preference. The API default is false.
302
+ * Unlike reducedMotion(), this does not rely only on the website honoring that preference.
303
+ */
304
+ reduceMotion(reduceMotion) {
305
+ this.put("reduce_motion", reduceMotion ? "true" : "false");
306
+ return this;
307
+ }
308
+ /**
309
+ * Requests reduced motion through the prefers-reduced-motion preference.
310
+ * Websites may ignore this preference; it does not actively freeze motion.
311
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
269
312
  */
270
313
  reducedMotion(reducedMotion) {
271
314
  this.put("reduced_motion", reducedMotion ? "true" : "false");
@@ -1126,12 +1169,21 @@ class AnimateOptions {
1126
1169
  return this;
1127
1170
  }
1128
1171
  /**
1129
- * When reduced_motion set to `true`,
1130
- * the API will request the site to minimize the amount of non-essential motion it uses.
1131
- * When the site supports it, it should use animations as least as possible.
1172
+ * Actively reduces supported page motion during recording by finishing animations
1173
+ * and pausing media and sliders. Also enables the reduced-motion preference.
1174
+ * The API default is false. Unlike reducedMotion(), this actively freezes motion.
1175
+ */
1176
+ reduceMotion(reduceMotion) {
1177
+ this.put("reduce_motion", reduceMotion ? "true" : "false");
1178
+ return this;
1179
+ }
1180
+ /**
1181
+ * Requests reduced motion through the prefers-reduced-motion preference.
1182
+ * Websites may ignore this preference; it does not actively freeze motion.
1183
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
1132
1184
  */
1133
- reducedMotion(darkMode) {
1134
- this.put("reduced_motion", darkMode ? "true" : "false");
1185
+ reducedMotion(reducedMotion) {
1186
+ this.put("reduced_motion", reducedMotion ? "true" : "false");
1135
1187
  return this;
1136
1188
  }
1137
1189
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "screenshotone-api-sdk",
3
3
  "homepage": "https://screenshotone.com",
4
- "version": "1.1.22",
4
+ "version": "1.1.24",
5
5
  "description": "Use ScreenshotOne.com API to generate screenshots of any website.",
6
6
  "repository": {
7
7
  "type": "git",
package/src/main.ts CHANGED
@@ -9,6 +9,25 @@ const API_ANIMATE_PATH = "/animate";
9
9
 
10
10
  export { APIError };
11
11
 
12
+ export interface ClientOptions {
13
+ /** API base URL. Defaults to https://api.screenshotone.com. */
14
+ apiBaseUrl?: string;
15
+ /**
16
+ * Additional attempts for ECONNRESET errors, including response download
17
+ * failures. Defaults to 0 (disabled); set a positive value to enable retries.
18
+ * Retries use exponential backoff with jitter and may start another render.
19
+ */
20
+ maxRetries?: number;
21
+ }
22
+
23
+ function isConnectionReset(error: unknown): boolean {
24
+ return (
25
+ typeof error === "object" &&
26
+ error !== null &&
27
+ (error as { code?: unknown }).code === "ECONNRESET"
28
+ );
29
+ }
30
+
12
31
  /**
13
32
  * Represents an API client for the ScreenshotOne.com API.
14
33
  */
@@ -16,17 +35,52 @@ export class Client {
16
35
  private readonly accessKey: string;
17
36
  private readonly secretKey: string;
18
37
  private readonly apiBaseUrl: string;
38
+ private readonly maxRetries: number;
19
39
 
20
- constructor(accessKey: string, secretKey: string, apiBaseUrl?: string) {
40
+ /**
41
+ * @param options Client configuration. An API base URL string is also
42
+ * accepted for compatibility with existing constructor calls.
43
+ */
44
+ constructor(
45
+ accessKey: string,
46
+ secretKey: string,
47
+ options: ClientOptions | string = {}
48
+ ) {
21
49
  if (!accessKey || !secretKey) {
22
50
  throw new Error(
23
51
  "Both non-empty access and secret keys are required"
24
52
  );
25
53
  }
26
54
 
55
+ const { apiBaseUrl, maxRetries = 0 } =
56
+ typeof options === "string" ? { apiBaseUrl: options } : options;
57
+
58
+ if (!Number.isSafeInteger(maxRetries) || maxRetries < 0) {
59
+ throw new RangeError("maxRetries must be a non-negative safe integer");
60
+ }
61
+
27
62
  this.accessKey = accessKey;
28
63
  this.secretKey = secretKey;
29
64
  this.apiBaseUrl = apiBaseUrl ? apiBaseUrl : API_BASE_URL;
65
+ this.maxRetries = maxRetries;
66
+ }
67
+
68
+ private async withConnectionResetRetries<T>(
69
+ operation: () => Promise<T>
70
+ ): Promise<T> {
71
+ for (let retry = 0; ; retry++) {
72
+ try {
73
+ return await operation();
74
+ } catch (error) {
75
+ if (!isConnectionReset(error) || retry >= this.maxRetries) {
76
+ throw error;
77
+ }
78
+
79
+ const delay =
80
+ Math.min(1000 * 2 ** retry, 30000) + Math.random() * 250;
81
+ await new Promise((resolve) => setTimeout(resolve, delay));
82
+ }
83
+ }
30
84
  }
31
85
 
32
86
  /**
@@ -89,30 +143,32 @@ export class Client {
89
143
  */
90
144
  async take(options: TakeOptions): Promise<Blob> {
91
145
  const url = await this.generateSignedTakeURL(options);
92
- const response = await fetch(url);
93
- if (response.ok) {
94
- return await response.blob();
95
- }
96
-
97
- try {
98
- const data = await response.json();
99
-
100
- throw new APIError(
101
- `Failed to generate screenshot, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
102
- response.status,
103
- data?.error_code,
104
- data?.error_message,
105
- data?.documentation_url
106
- );
107
- } catch (e) {
108
- if (e instanceof APIError) {
109
- throw e;
146
+ return this.withConnectionResetRetries(async () => {
147
+ const response = await fetch(url);
148
+ if (response.ok) {
149
+ return await response.blob();
110
150
  }
111
151
 
112
- throw new Error(
113
- `failed to take screenshot, response returned ${response.status} ${response.statusText}`
114
- );
115
- }
152
+ try {
153
+ const data = await response.json();
154
+
155
+ throw new APIError(
156
+ `Failed to generate screenshot, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
157
+ response.status,
158
+ data?.error_code,
159
+ data?.error_message,
160
+ data?.documentation_url
161
+ );
162
+ } catch (e) {
163
+ if (e instanceof APIError || isConnectionReset(e)) {
164
+ throw e;
165
+ }
166
+
167
+ throw new Error(
168
+ `failed to take screenshot, response returned ${response.status} ${response.statusText}`
169
+ );
170
+ }
171
+ });
116
172
  }
117
173
 
118
174
  /**
@@ -123,30 +179,32 @@ export class Client {
123
179
  */
124
180
  async animate(options: AnimateOptions): Promise<Blob> {
125
181
  const url = await this.generateSignedAnimateURL(options);
126
- const response = await fetch(url);
127
- if (response.ok) {
128
- return await response.blob();
129
- }
130
-
131
- try {
132
- const data = await response.json();
133
-
134
- throw new APIError(
135
- `Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
136
- response.status,
137
- data?.error_code,
138
- data?.error_message,
139
- data?.documentation_url
140
- );
141
- } catch (e) {
142
- if (e instanceof APIError) {
143
- throw e;
182
+ return this.withConnectionResetRetries(async () => {
183
+ const response = await fetch(url);
184
+ if (response.ok) {
185
+ return await response.blob();
144
186
  }
145
187
 
146
- throw new Error(
147
- `Failed to generate animation, response returned ${response.status} (${response.statusText})`
148
- );
149
- }
188
+ try {
189
+ const data = await response.json();
190
+
191
+ throw new APIError(
192
+ `Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
193
+ response.status,
194
+ data?.error_code,
195
+ data?.error_message,
196
+ data?.documentation_url
197
+ );
198
+ } catch (e) {
199
+ if (e instanceof APIError || isConnectionReset(e)) {
200
+ throw e;
201
+ }
202
+
203
+ throw new Error(
204
+ `Failed to generate animation, response returned ${response.status} (${response.statusText})`
205
+ );
206
+ }
207
+ });
150
208
  }
151
209
 
152
210
  /**
@@ -187,23 +245,25 @@ export class Client {
187
245
  ? await this.generateSignedTakeURL(options)
188
246
  : await this.generateSignedAnimateURL(options);
189
247
 
190
- const response = await fetch(url);
191
- if (response.ok) {
192
- return {
193
- key: response.headers.get("X-ScreenshotOne-Store-Key"),
194
- bucket: response.headers.get("X-ScreenshotOne-Store-Bucket"),
195
- };
196
- }
248
+ return this.withConnectionResetRetries(async () => {
249
+ const response = await fetch(url);
250
+ if (response.ok) {
251
+ return {
252
+ key: response.headers.get("X-ScreenshotOne-Store-Key"),
253
+ bucket: response.headers.get("X-ScreenshotOne-Store-Bucket"),
254
+ };
255
+ }
197
256
 
198
- const data = await response.json();
257
+ const data = await response.json();
199
258
 
200
- throw new APIError(
201
- `Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
202
- response.status,
203
- data?.error_code,
204
- data?.error_message,
205
- data?.documentation_url
206
- );
259
+ throw new APIError(
260
+ `Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
261
+ response.status,
262
+ data?.error_code,
263
+ data?.error_message,
264
+ data?.documentation_url
265
+ );
266
+ });
207
267
  }
208
268
  }
209
269
 
@@ -346,9 +406,20 @@ export class TakeOptions {
346
406
  }
347
407
 
348
408
  /**
349
- * When reduced_motion set to `true`,
350
- * the API will request the site to minimize the amount of non-essential motion it uses.
351
- * When the site supports it, it should use animations as least as possible.
409
+ * Actively reduces supported motion by finishing animations and pausing media and sliders.
410
+ * Also enables the reduced-motion preference. The API default is false.
411
+ * Unlike reducedMotion(), this does not rely only on the website honoring that preference.
412
+ */
413
+ reduceMotion(reduceMotion: boolean): TakeOptions {
414
+ this.put("reduce_motion", reduceMotion ? "true" : "false");
415
+
416
+ return this;
417
+ }
418
+
419
+ /**
420
+ * Requests reduced motion through the prefers-reduced-motion preference.
421
+ * Websites may ignore this preference; it does not actively freeze motion.
422
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
352
423
  */
353
424
  reducedMotion(reducedMotion: boolean): TakeOptions {
354
425
  this.put("reduced_motion", reducedMotion ? "true" : "false");
@@ -1452,12 +1523,23 @@ export class AnimateOptions {
1452
1523
  }
1453
1524
 
1454
1525
  /**
1455
- * When reduced_motion set to `true`,
1456
- * the API will request the site to minimize the amount of non-essential motion it uses.
1457
- * When the site supports it, it should use animations as least as possible.
1526
+ * Actively reduces supported page motion during recording by finishing animations
1527
+ * and pausing media and sliders. Also enables the reduced-motion preference.
1528
+ * The API default is false. Unlike reducedMotion(), this actively freezes motion.
1529
+ */
1530
+ reduceMotion(reduceMotion: boolean): AnimateOptions {
1531
+ this.put("reduce_motion", reduceMotion ? "true" : "false");
1532
+
1533
+ return this;
1534
+ }
1535
+
1536
+ /**
1537
+ * Requests reduced motion through the prefers-reduced-motion preference.
1538
+ * Websites may ignore this preference; it does not actively freeze motion.
1539
+ * Use reduceMotion(true) to actively reduce motion and enable this preference.
1458
1540
  */
1459
- reducedMotion(darkMode: boolean): AnimateOptions {
1460
- this.put("reduced_motion", darkMode ? "true" : "false");
1541
+ reducedMotion(reducedMotion: boolean): AnimateOptions {
1542
+ this.put("reduced_motion", reducedMotion ? "true" : "false");
1461
1543
 
1462
1544
  return this;
1463
1545
  }