screenshotone-api-sdk 1.1.13 → 1.1.15
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/README.md +18 -5
- package/dist/@types/errors.d.ts +7 -0
- package/dist/@types/main.d.ts +2 -0
- package/dist/errors.js +12 -0
- package/dist/main.js +8 -3
- package/package.json +1 -1
- package/src/errors.ts +16 -0
- package/src/main.ts +15 -4
- package/tests/main.test.ts +22 -0
package/README.md
CHANGED
|
@@ -36,11 +36,24 @@ const url = client.generateTakeURL(options);
|
|
|
36
36
|
console.log(url);
|
|
37
37
|
// expected output: https://api.screenshotone.com/take?url=https%3A%2F%2Fexample.com&delay=3&block_ads=true&access_key=%3Caccess+key%3E&signature=7f3419ece2c53ed2c7923c7d5deef290d662c3643822bf69ec8259ce10b3ea61
|
|
38
38
|
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
// download the screenshot and handle potential API errors
|
|
40
|
+
try {
|
|
41
|
+
const imageBlob = await client.take(options);
|
|
42
|
+
|
|
43
|
+
const buffer = Buffer.from(await imageBlob.arrayBuffer());
|
|
44
|
+
fs.writeFileSync("example.png", buffer);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error instanceof screenshotone.APIError) {
|
|
47
|
+
// An API error occurred with
|
|
48
|
+
// HTTP Status Code: error.httpStatusCode
|
|
49
|
+
// Error Code: error.errorCode
|
|
50
|
+
// Error Message: error.errorMessage
|
|
51
|
+
// Documentation URL: error.documentationUrl
|
|
52
|
+
} else {
|
|
53
|
+
// An unexpected error occurred
|
|
54
|
+
console.error("An unexpected error occurred:", error.message);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
44
57
|
```
|
|
45
58
|
|
|
46
59
|
## Build and publish (a manual for SDK developers)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export default 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
|
+
}
|
package/dist/@types/main.d.ts
CHANGED
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
|
@@ -3,10 +3,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.AnimateOptions = exports.TakeOptions = exports.Client = void 0;
|
|
6
|
+
exports.AnimateOptions = exports.TakeOptions = exports.Client = exports.APIError = 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"));
|
|
11
|
+
exports.APIError = errors_1.default;
|
|
10
12
|
const API_BASE_URL = "https://api.screenshotone.com";
|
|
11
13
|
const API_TAKE_PATH = "/take";
|
|
12
14
|
const API_ANIMATE_PATH = "/animate";
|
|
@@ -100,10 +102,13 @@ class Client {
|
|
|
100
102
|
}
|
|
101
103
|
try {
|
|
102
104
|
const data = await response.json();
|
|
103
|
-
throw new
|
|
105
|
+
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
106
|
}
|
|
105
107
|
catch (e) {
|
|
106
|
-
|
|
108
|
+
if (e instanceof errors_1.default) {
|
|
109
|
+
throw e;
|
|
110
|
+
}
|
|
111
|
+
throw new Error(`Failed to generate animation, response returned ${response.status} (${response.statusText})`);
|
|
107
112
|
}
|
|
108
113
|
}
|
|
109
114
|
/**
|
package/package.json
CHANGED
package/src/errors.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
export default 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
|
+
}
|
package/src/main.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
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";
|
|
7
8
|
const API_ANIMATE_PATH = "/animate";
|
|
8
9
|
|
|
10
|
+
export { APIError };
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* Represents an API client for the ScreenshotOne.com API.
|
|
11
14
|
*/
|
|
@@ -120,12 +123,20 @@ export class Client {
|
|
|
120
123
|
try {
|
|
121
124
|
const data = await response.json();
|
|
122
125
|
|
|
123
|
-
throw new
|
|
124
|
-
`
|
|
125
|
-
|
|
126
|
+
throw new APIError(
|
|
127
|
+
`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
|
|
128
|
+
response.status,
|
|
129
|
+
data?.error_code,
|
|
130
|
+
data?.error_message,
|
|
131
|
+
data?.documentation_url
|
|
132
|
+
);
|
|
126
133
|
} catch (e) {
|
|
134
|
+
if (e instanceof APIError) {
|
|
135
|
+
throw e;
|
|
136
|
+
}
|
|
137
|
+
|
|
127
138
|
throw new Error(
|
|
128
|
-
`
|
|
139
|
+
`Failed to generate animation, response returned ${response.status} (${response.statusText})`
|
|
129
140
|
);
|
|
130
141
|
}
|
|
131
142
|
}
|
package/tests/main.test.ts
CHANGED
|
@@ -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
|
});
|