screenshotone-api-sdk 1.1.23 → 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.
- package/dist/@types/main.d.ts +17 -1
- package/dist/main.js +70 -36
- package/package.json +1 -1
- package/src/main.ts +120 -60
package/dist/@types/main.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
*
|
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
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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;
|
package/package.json
CHANGED
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
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
113
|
-
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
147
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
257
|
+
const data = await response.json();
|
|
199
258
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
|