screenshotone-api-sdk 1.1.12 → 1.1.14

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.
@@ -0,0 +1,8 @@
1
+ declare class APIError extends Error {
2
+ readonly httpStatusCode?: number;
3
+ readonly errorCode?: string;
4
+ readonly errorMessage?: string;
5
+ readonly documentationUrl?: string;
6
+ constructor(message: string, httpStatusCode?: number, errorCode?: string, errorMessage?: string, documentationUrl?: string);
7
+ }
8
+ export default APIError;
@@ -82,7 +82,7 @@ export declare class TakeOptions {
82
82
  /**
83
83
  * Available response types:
84
84
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
85
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
85
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
86
86
  * The default value is by_format.
87
87
  */
88
88
  responseType(responseType: string): this;
@@ -109,7 +109,7 @@ export declare class TakeOptions {
109
109
  /**
110
110
  * Set `true` to request site rendering, if supported, in the dark mode.
111
111
  * Set `false` to request site rendering in the light mode if supported.
112
- * If you don’t set the parameter. The site is rendered in the default mode.
112
+ * If you don't set the parameter. The site is rendered in the default mode.
113
113
  */
114
114
  darkMode(darkMode: boolean): TakeOptions;
115
115
  /**
@@ -299,7 +299,7 @@ export declare class TakeOptions {
299
299
  */
300
300
  scrollIntoView(selector: string): TakeOptions;
301
301
  /**
302
- * After the given element appears in the viewport and its top coordinate is aligned with the viewport’s top,
302
+ * After the given element appears in the viewport and its top coordinate is aligned with the viewport's top,
303
303
  * you can adjust the position a bit before taking a screenshot.
304
304
  */
305
305
  scrollIntoViewAdjustTop(adjustTop: number): TakeOptions;
@@ -345,9 +345,9 @@ export declare class TakeOptions {
345
345
  store(store: boolean): TakeOptions;
346
346
  /**
347
347
  * The parameter is required if you set store=true.
348
- * You must specify the key for the file, but don’t specify an extension,
348
+ * You must specify the key for the file, but don't specify an extension,
349
349
  * it will be added automatically based on the format you specified.
350
- * You can also specify “subdirectories” in the path part.
350
+ * You can also specify "subdirectories" in the path part.
351
351
  */
352
352
  storagePath(path: string): TakeOptions;
353
353
  storageACL(acl: string): TakeOptions;
@@ -391,7 +391,7 @@ export declare class AnimateOptions {
391
391
  /**
392
392
  * Available response types:
393
393
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
394
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
394
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
395
395
  * The default value is by_format.
396
396
  */
397
397
  responseType(responseType: string): this;
@@ -410,7 +410,7 @@ export declare class AnimateOptions {
410
410
  /**
411
411
  * Set `true` to request site rendering, if supported, in the dark mode.
412
412
  * Set `false` to request site rendering in the light mode if supported.
413
- * If you don’t set the parameter. The site is rendered in the default mode.
413
+ * If you don't set the parameter. The site is rendered in the default mode.
414
414
  */
415
415
  darkMode(darkMode: boolean): AnimateOptions;
416
416
  /**
@@ -590,9 +590,9 @@ export declare class AnimateOptions {
590
590
  storageSecretAccessKey(secretAccessKey: string): AnimateOptions;
591
591
  /**
592
592
  * The parameter is required if you set store=true.
593
- * You must specify the key for the file, but don’t specify an extension,
593
+ * You must specify the key for the file, but don't specify an extension,
594
594
  * it will be added automatically based on the format you specified.
595
- * You can also specify “subdirectories” in the path part.
595
+ * You can also specify "subdirectories" in the path part.
596
596
  */
597
597
  storagePath(path: string): AnimateOptions;
598
598
  /**
@@ -620,5 +620,15 @@ export declare class AnimateOptions {
620
620
  scrollStartDelay(delay: number): AnimateOptions;
621
621
  scrollComplete(complete: boolean): AnimateOptions;
622
622
  scrollBy(pixels: number): AnimateOptions;
623
+ /**
624
+ * When the site responds out of the range of 200-299 status code,
625
+ * you can ignore errors and take a screenshot of the error page anyway.
626
+ * To do that, set the option "ignore_host_errors" to true.
627
+ * It is false by default.
628
+ *
629
+ * It is helpful when you want to create a gallery of error pages or,
630
+ * for some reason, you need to render error pages.
631
+ */
632
+ ignoreHostErrors(ignore: boolean): AnimateOptions;
623
633
  toQuery(): URLSearchParams;
624
634
  }
package/dist/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class APIError extends Error {
4
+ constructor(message, httpStatusCode, errorCode, errorMessage, documentationUrl) {
5
+ super(message);
6
+ this.httpStatusCode = httpStatusCode;
7
+ this.errorCode = errorCode;
8
+ this.errorMessage = errorMessage;
9
+ this.documentationUrl = documentationUrl;
10
+ }
11
+ }
12
+ exports.default = APIError;
package/dist/main.js CHANGED
@@ -7,6 +7,7 @@ exports.AnimateOptions = exports.TakeOptions = exports.Client = void 0;
7
7
  const big_js_1 = __importDefault(require("big.js"));
8
8
  const cross_fetch_1 = __importDefault(require("cross-fetch"));
9
9
  const signature_1 = require("./signature");
10
+ const errors_1 = __importDefault(require("./errors"));
10
11
  const API_BASE_URL = "https://api.screenshotone.com";
11
12
  const API_TAKE_PATH = "/take";
12
13
  const API_ANIMATE_PATH = "/animate";
@@ -100,10 +101,13 @@ class Client {
100
101
  }
101
102
  try {
102
103
  const data = await response.json();
103
- throw new Error(`failed to generate animation, response returned ${response.status} ${response.statusText}: ${data?.error_message}`);
104
+ 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);
104
105
  }
105
106
  catch (e) {
106
- throw new Error(`failed to generate animation, response returned ${response.status} ${response.statusText}`);
107
+ if (e instanceof errors_1.default) {
108
+ throw e;
109
+ }
110
+ throw new Error(`Failed to generate animation, response returned ${response.status} (${response.statusText})`);
107
111
  }
108
112
  }
109
113
  /**
@@ -196,7 +200,7 @@ class TakeOptions {
196
200
  /**
197
201
  * Available response types:
198
202
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
199
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
203
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
200
204
  * The default value is by_format.
201
205
  */
202
206
  responseType(responseType) {
@@ -238,7 +242,7 @@ class TakeOptions {
238
242
  /**
239
243
  * Set `true` to request site rendering, if supported, in the dark mode.
240
244
  * Set `false` to request site rendering in the light mode if supported.
241
- * If you don’t set the parameter. The site is rendered in the default mode.
245
+ * If you don't set the parameter. The site is rendered in the default mode.
242
246
  */
243
247
  darkMode(darkMode) {
244
248
  this.put("dark_mode", darkMode ? "true" : "false");
@@ -548,7 +552,7 @@ class TakeOptions {
548
552
  return this;
549
553
  }
550
554
  /**
551
- * After the given element appears in the viewport and its top coordinate is aligned with the viewport’s top,
555
+ * After the given element appears in the viewport and its top coordinate is aligned with the viewport's top,
552
556
  * you can adjust the position a bit before taking a screenshot.
553
557
  */
554
558
  scrollIntoViewAdjustTop(adjustTop) {
@@ -621,9 +625,9 @@ class TakeOptions {
621
625
  }
622
626
  /**
623
627
  * The parameter is required if you set store=true.
624
- * You must specify the key for the file, but don’t specify an extension,
628
+ * You must specify the key for the file, but don't specify an extension,
625
629
  * it will be added automatically based on the format you specified.
626
- * You can also specify “subdirectories” in the path part.
630
+ * You can also specify "subdirectories" in the path part.
627
631
  */
628
632
  storagePath(path) {
629
633
  this.put("storage_path", path);
@@ -711,7 +715,7 @@ class AnimateOptions {
711
715
  /**
712
716
  * Available response types:
713
717
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
714
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
718
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
715
719
  * The default value is by_format.
716
720
  */
717
721
  responseType(responseType) {
@@ -739,7 +743,7 @@ class AnimateOptions {
739
743
  /**
740
744
  * Set `true` to request site rendering, if supported, in the dark mode.
741
745
  * Set `false` to request site rendering in the light mode if supported.
742
- * If you don’t set the parameter. The site is rendered in the default mode.
746
+ * If you don't set the parameter. The site is rendered in the default mode.
743
747
  */
744
748
  darkMode(darkMode) {
745
749
  this.put("dark_mode", darkMode ? "true" : "false");
@@ -1036,9 +1040,9 @@ class AnimateOptions {
1036
1040
  }
1037
1041
  /**
1038
1042
  * The parameter is required if you set store=true.
1039
- * You must specify the key for the file, but don’t specify an extension,
1043
+ * You must specify the key for the file, but don't specify an extension,
1040
1044
  * it will be added automatically based on the format you specified.
1041
- * You can also specify “subdirectories” in the path part.
1045
+ * You can also specify "subdirectories" in the path part.
1042
1046
  */
1043
1047
  storagePath(path) {
1044
1048
  this.put("storage_path", path);
@@ -1108,6 +1112,19 @@ class AnimateOptions {
1108
1112
  this.put("scroll_by", pixels.toString());
1109
1113
  return this;
1110
1114
  }
1115
+ /**
1116
+ * When the site responds out of the range of 200-299 status code,
1117
+ * you can ignore errors and take a screenshot of the error page anyway.
1118
+ * To do that, set the option "ignore_host_errors" to true.
1119
+ * It is false by default.
1120
+ *
1121
+ * It is helpful when you want to create a gallery of error pages or,
1122
+ * for some reason, you need to render error pages.
1123
+ */
1124
+ ignoreHostErrors(ignore) {
1125
+ this.put("ignore_host_errors", ignore ? "true" : "false");
1126
+ return this;
1127
+ }
1111
1128
  toQuery() {
1112
1129
  return new URLSearchParams(this.query.toString());
1113
1130
  }
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.12",
4
+ "version": "1.1.14",
5
5
  "description": "Use ScreenshotOne.com API to generate screenshots of any website.",
6
6
  "repository": {
7
7
  "type": "git",
package/src/errors.ts ADDED
@@ -0,0 +1,18 @@
1
+
2
+ class APIError extends Error {
3
+ public readonly httpStatusCode?: number;
4
+ public readonly errorCode?: string;
5
+ public readonly errorMessage?: string;
6
+ public readonly documentationUrl?: string;
7
+
8
+ constructor(message: string, httpStatusCode?: number, errorCode?: string, errorMessage?: string, documentationUrl?: string) {
9
+ super(message);
10
+
11
+ this.httpStatusCode = httpStatusCode;
12
+ this.errorCode = errorCode;
13
+ this.errorMessage = errorMessage;
14
+ this.documentationUrl = documentationUrl;
15
+ }
16
+ }
17
+
18
+ export default APIError;
package/src/main.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import Big from "big.js";
2
2
  import fetch from "cross-fetch";
3
3
  import { signQueryString } from "./signature";
4
+ import APIError from "./errors";
4
5
 
5
6
  const API_BASE_URL = "https://api.screenshotone.com";
6
7
  const API_TAKE_PATH = "/take";
@@ -120,12 +121,20 @@ export class Client {
120
121
  try {
121
122
  const data = await response.json();
122
123
 
123
- throw new Error(
124
- `failed to generate animation, response returned ${response.status} ${response.statusText}: ${data?.error_message}`
125
- );
124
+ throw new APIError(
125
+ `Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
126
+ response.status,
127
+ data?.error_code,
128
+ data?.error_message,
129
+ data?.documentation_url
130
+ );
126
131
  } catch (e) {
132
+ if (e instanceof APIError) {
133
+ throw e;
134
+ }
135
+
127
136
  throw new Error(
128
- `failed to generate animation, response returned ${response.status} ${response.statusText}`
137
+ `Failed to generate animation, response returned ${response.status} (${response.statusText})`
129
138
  );
130
139
  }
131
140
  }
@@ -248,7 +257,7 @@ export class TakeOptions {
248
257
  /**
249
258
  * Available response types:
250
259
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
251
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
260
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
252
261
  * The default value is by_format.
253
262
  */
254
263
  responseType(responseType: string) {
@@ -300,7 +309,7 @@ export class TakeOptions {
300
309
  /**
301
310
  * Set `true` to request site rendering, if supported, in the dark mode.
302
311
  * Set `false` to request site rendering in the light mode if supported.
303
- * If you don’t set the parameter. The site is rendered in the default mode.
312
+ * If you don't set the parameter. The site is rendered in the default mode.
304
313
  */
305
314
  darkMode(darkMode: boolean): TakeOptions {
306
315
  this.put("dark_mode", darkMode ? "true" : "false");
@@ -696,7 +705,7 @@ export class TakeOptions {
696
705
  }
697
706
 
698
707
  /**
699
- * After the given element appears in the viewport and its top coordinate is aligned with the viewport’s top,
708
+ * After the given element appears in the viewport and its top coordinate is aligned with the viewport's top,
700
709
  * you can adjust the position a bit before taking a screenshot.
701
710
  */
702
711
  scrollIntoViewAdjustTop(adjustTop: number): TakeOptions {
@@ -787,9 +796,9 @@ export class TakeOptions {
787
796
 
788
797
  /**
789
798
  * The parameter is required if you set store=true.
790
- * You must specify the key for the file, but don’t specify an extension,
799
+ * You must specify the key for the file, but don't specify an extension,
791
800
  * it will be added automatically based on the format you specified.
792
- * You can also specify “subdirectories” in the path part.
801
+ * You can also specify "subdirectories" in the path part.
793
802
  */
794
803
  storagePath(path: string): TakeOptions {
795
804
  this.put("storage_path", path);
@@ -899,7 +908,7 @@ export class AnimateOptions {
899
908
  /**
900
909
  * Available response types:
901
910
  * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
902
- * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
911
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don't care about the results. It also speeds up the response since there are no networking costs involved.
903
912
  * The default value is by_format.
904
913
  */
905
914
  responseType(responseType: string) {
@@ -933,7 +942,7 @@ export class AnimateOptions {
933
942
  /**
934
943
  * Set `true` to request site rendering, if supported, in the dark mode.
935
944
  * Set `false` to request site rendering in the light mode if supported.
936
- * If you don’t set the parameter. The site is rendered in the default mode.
945
+ * If you don't set the parameter. The site is rendered in the default mode.
937
946
  */
938
947
  darkMode(darkMode: boolean): AnimateOptions {
939
948
  this.put("dark_mode", darkMode ? "true" : "false");
@@ -1314,9 +1323,9 @@ export class AnimateOptions {
1314
1323
 
1315
1324
  /**
1316
1325
  * The parameter is required if you set store=true.
1317
- * You must specify the key for the file, but don’t specify an extension,
1326
+ * You must specify the key for the file, but don't specify an extension,
1318
1327
  * it will be added automatically based on the format you specified.
1319
- * You can also specify “subdirectories” in the path part.
1328
+ * You can also specify "subdirectories" in the path part.
1320
1329
  */
1321
1330
  storagePath(path: string): AnimateOptions {
1322
1331
  this.put("storage_path", path);
@@ -1415,6 +1424,21 @@ export class AnimateOptions {
1415
1424
  return this;
1416
1425
  }
1417
1426
 
1427
+ /**
1428
+ * When the site responds out of the range of 200-299 status code,
1429
+ * you can ignore errors and take a screenshot of the error page anyway.
1430
+ * To do that, set the option "ignore_host_errors" to true.
1431
+ * It is false by default.
1432
+ *
1433
+ * It is helpful when you want to create a gallery of error pages or,
1434
+ * for some reason, you need to render error pages.
1435
+ */
1436
+ ignoreHostErrors(ignore: boolean): AnimateOptions {
1437
+ this.put("ignore_host_errors", ignore ? "true" : "false");
1438
+
1439
+ return this;
1440
+ }
1441
+
1418
1442
  toQuery(): URLSearchParams {
1419
1443
  return new URLSearchParams(this.query.toString());
1420
1444
  }
@@ -1,3 +1,4 @@
1
+ import APIError from "../src/errors";
1
2
  import { TakeOptions, Client, AnimateOptions } from "../src/main";
2
3
 
3
4
  describe("testing client", () => {
@@ -52,4 +53,25 @@ describe("testing client", () => {
52
53
 
53
54
  expect(options.toQuery().getAll("block_ads").length).toBe(1);
54
55
  });
56
+
57
+ test("returning an API error when the response is not successful", async () => {
58
+ jest.mock('cross-fetch', () => ({
59
+ __esModule: true,
60
+ default: jest.fn().mockImplementationOnce(() =>
61
+ Promise.resolve({
62
+ ok: false,
63
+ status: 400,
64
+ statusText: "Bad Request",
65
+ json: () => Promise.resolve({
66
+ error_code: "name_not_resolved",
67
+ error_message: "Usually, the error happens when the domain name of the requested URL is not resolved. If you are trying to take a screenshot of the new site, please, wait a bit until the DNS records are refreshed.",
68
+ documentation_url: "https://screenshotone.com/docs/errors/"
69
+ })
70
+ })
71
+ )
72
+ }));
73
+
74
+ const options = AnimateOptions.url("https://example.com");
75
+ await expect(client.animate(options)).rejects.toThrow(APIError);
76
+ });
55
77
  });