screenshotone-api-sdk 1.0.0

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,25 @@
1
+ name: Build
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ matrix:
16
+ node-version: [16.x, 17.x, 18.x]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v3
20
+ - name: Use Node.js ${{ matrix.node-version }}
21
+ uses: actions/setup-node@v3
22
+ with:
23
+ node-version: ${{ matrix.node-version }}
24
+ - run: npm install
25
+ - run: npm run build --if-present
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 ScreenshotOne.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21
+ OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # tssdk
2
+
3
+ [![Build](https://github.com/screenshotone/tssdk/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/screenshotone/tssdk/actions/workflows/build.yml)
4
+
5
+ An official [Screenshot API](https://screenshotone.com/) client for JavaScript and TypeScript.
6
+
7
+ It takes minutes to start taking screenshots. Just [sign up](https://screenshotone.com/) to get access and secret keys, import the client, and you are ready to go.
8
+
9
+ ## Installation
10
+
11
+ ```shell
12
+
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Import the library:
18
+ ```go
19
+
20
+ ```
21
+
22
+ Generate a screenshot URL without executing request:
23
+ ```go
24
+
25
+ ```
26
+
27
+ Take a screenshot and save the image in the file:
28
+ ```go
29
+
30
+ ```
31
+
32
+ ## License
33
+
34
+ `screenshotone/tssdk` is released under [the MIT license](LICENSE).
package/lib/main.ts ADDED
@@ -0,0 +1,295 @@
1
+ import * as crypto from 'crypto';
2
+ import Big from 'big.js';
3
+ import fetch from 'node-fetch';
4
+
5
+ /**
6
+ * Represents an API client for the screenshotone.com API.
7
+ */
8
+ export class Client {
9
+ private readonly API_BASE_URL = "https://api.screenshotone.com";
10
+ private readonly API_TAKE_PATH = "/take";
11
+
12
+ private readonly accessKey: string;
13
+ private readonly secretKey: string;
14
+
15
+ constructor(accessKey: string, secretKey: string) {
16
+ this.accessKey = accessKey;
17
+ this.secretKey = secretKey;
18
+ }
19
+
20
+ generateTakeURL(options: TakeOptions): string {
21
+ const query = options.toQuery();
22
+ query.append("access_key", this.accessKey);
23
+ let queryString = query.toString();
24
+
25
+ const signature = crypto.createHmac("sha256", this.secretKey)
26
+ .update(queryString, 'utf-8')
27
+ .digest('hex');
28
+ queryString += '&signature=' + signature;
29
+
30
+ return `${this.API_BASE_URL}${this.API_TAKE_PATH}?${queryString}`;
31
+ }
32
+
33
+ async take(options: TakeOptions): Promise<Blob> {
34
+ const url = this.generateTakeURL(options);
35
+ const response = await fetch(url);
36
+ if (response.ok) {
37
+ return await response.blob()
38
+ }
39
+
40
+ throw new Error(`failed to take screenshot, response returned ${response.status} ${response.statusText}`);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Represents options for taking screenshots.
46
+ */
47
+ export class TakeOptions {
48
+ private readonly query: URLSearchParams;
49
+
50
+ private constructor(url: string) {
51
+ const query = new URLSearchParams();
52
+ query.append("url", url);
53
+
54
+ this.query = query;
55
+ }
56
+
57
+ /**
58
+ * Initializes take screenshot options with provided website URL.
59
+ */
60
+ static url(url: string): TakeOptions {
61
+ return new TakeOptions(url);
62
+ }
63
+
64
+ private put(key: string, ...values: string[]) {
65
+ for (const value of values) {
66
+ this.query.append(key, value);
67
+ }
68
+ };
69
+
70
+ /**
71
+ * Renders the full page.
72
+ */
73
+ fullPage(fullPage: boolean): TakeOptions {
74
+ this.put("full_page", fullPage ? "true" : "false");
75
+
76
+ return this;
77
+ }
78
+
79
+ /**
80
+ * Sets response format, one of: "png", "jpeg", "webp" or "jpg".
81
+ */
82
+ format(format: string): TakeOptions {
83
+ this.put("format", format);
84
+
85
+ return this;
86
+ }
87
+
88
+ /**
89
+ * Renders image with the specified quality. Available for the next formats: "jpeg" ("jpg"), "webp".
90
+ */
91
+ imageQuality(imageQuality: number): TakeOptions {
92
+ this.put("image_quality", imageQuality.toString());
93
+
94
+ return this;
95
+ }
96
+
97
+ /**
98
+ * Renders a transparent background for the image. Works only if the site has not defined background color.
99
+ * Available for the following response formats: "png", "webp".
100
+ */
101
+ omitBackground(omitBackground: boolean): TakeOptions {
102
+ this.put("omit_background", omitBackground ? "true" : "false");
103
+
104
+ return this;
105
+ }
106
+
107
+ /**
108
+ * Sets the width of the browser viewport (pixels).
109
+ */
110
+ viewportWidth(viewportWidth: number): TakeOptions {
111
+ this.put("viewport_width", viewportWidth.toString());
112
+
113
+ return this;
114
+ }
115
+
116
+ /**
117
+ * Sets the height of the browser viewport (pixels).
118
+ */
119
+ viewportHeight(viewportHeight: number): TakeOptions {
120
+ this.put("viewport_height", viewportHeight.toString());
121
+
122
+ return this;
123
+ }
124
+
125
+ /**
126
+ * Sets the device scale factor. Acceptable value is one of: 1, 2 or 3.
127
+ */
128
+ deviceScaleFactor(deviceScaleFactor: number): TakeOptions {
129
+ this.put("device_scale_factor", deviceScaleFactor.toString());
130
+
131
+ return this;
132
+ }
133
+
134
+ /**
135
+ * Sets geolocation latitude for the request.
136
+ * Both latitude and longitude are required if one of them is set.
137
+ */
138
+ geolocationLatitude(latitude: number): TakeOptions {
139
+ this.put("geolocation_latitude", new Big(latitude).toFixed(20).replace(/0+$/, ""));
140
+
141
+ return this;
142
+ }
143
+
144
+ /**
145
+ * Sets geolocation longitude for the request. Both latitude and longitude are required if one of them is set.
146
+ */
147
+ geolocationLongitude(longitude: number): TakeOptions {
148
+ this.put("geolocation_longitude", new Big(longitude).toFixed(20).replace(/0+$/, ""));
149
+
150
+ return this;
151
+ }
152
+
153
+ /**
154
+ * Sets the geolocation accuracy in meters.
155
+ */
156
+ geolocationAccuracy(accuracy: number): TakeOptions {
157
+ this.put("geolocation_accuracy", accuracy.toString());
158
+
159
+ return this;
160
+ }
161
+
162
+ /**
163
+ * Blocks ads.
164
+ */
165
+ blockAds(blockAds: boolean): TakeOptions {
166
+ this.put("block_ads", blockAds ? "true" : "false");
167
+
168
+ return this;
169
+ }
170
+
171
+ /**
172
+ * Blocks trackers.
173
+ */
174
+ blockTrackers(blockTrackers: boolean): TakeOptions {
175
+ this.put("block_trackers", blockTrackers ? "true" : "false");
176
+
177
+ return this;
178
+ }
179
+
180
+ /**
181
+ * Blocks requests by specifying URL, domain, or even a simple pattern.
182
+ */
183
+ blockRequests(...blockRequests: string[]): TakeOptions {
184
+ this.put("block_requests", ...blockRequests);
185
+
186
+ return this;
187
+ }
188
+
189
+ /**
190
+ * Blocks loading resources by type. Available resource types are: "document", "stylesheet", "image", "media",
191
+ * "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other".
192
+ */
193
+ blockResources(...blockResources: string[]): TakeOptions {
194
+ this.put("block_resources", ...blockResources);
195
+
196
+ return this;
197
+ }
198
+
199
+ /**
200
+ * Enables caching.
201
+ */
202
+ cache(cache: boolean): TakeOptions {
203
+ this.put("cache", cache ? "true" : "false");
204
+
205
+ return this;
206
+ }
207
+
208
+ /**
209
+ * Sets cache TTL.
210
+ */
211
+ cacheTtl(cacheTTL: number): TakeOptions {
212
+ this.put("cache_ttl", cacheTTL.toString());
213
+
214
+ return this;
215
+ }
216
+
217
+ /**
218
+ * Sets cache key.
219
+ */
220
+ cacheKey(cacheKey: string): TakeOptions {
221
+ this.put("cache_key", cacheKey);
222
+
223
+ return this;
224
+ }
225
+
226
+ /**
227
+ * Sets a user agent for the request.
228
+ */
229
+ userAgent(userAgent: string): TakeOptions {
230
+ this.put("user_agent", userAgent);
231
+
232
+ return this;
233
+ }
234
+
235
+ /**
236
+ * Sets an authorization header for the request.
237
+ */
238
+ authorization(authorization: string): TakeOptions {
239
+ this.put("authorization", authorization);
240
+
241
+ return this;
242
+ }
243
+
244
+ /**
245
+ * Set cookies for the request.
246
+ */
247
+ cookies(...cookies: string[]): TakeOptions {
248
+ this.put("cookies", ...cookies);
249
+
250
+ return this;
251
+ }
252
+
253
+ /**
254
+ * Sets extra headers for the request.
255
+ */
256
+ headers(...headers: string[]) {
257
+ this.put("headers", ...headers);
258
+
259
+ return this;
260
+ }
261
+
262
+ /**
263
+ * TimeZone sets time zone for the request.
264
+ * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil",
265
+ * "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev",
266
+ * "Asia/Tashkent", "Europe/London".
267
+ */
268
+ timeZone(timeZone: string): TakeOptions {
269
+ this.put("time_zone", timeZone);
270
+
271
+ return this;
272
+ }
273
+
274
+ /**
275
+ * Sets delay.
276
+ */
277
+ delay(delay: number): TakeOptions {
278
+ this.put("delay", delay.toString());
279
+
280
+ return this;
281
+ }
282
+
283
+ /**
284
+ * Sets timeout.
285
+ */
286
+ timeout(timeout: number): TakeOptions {
287
+ this.put("timeout", timeout.toString());
288
+
289
+ return this;
290
+ }
291
+
292
+ toQuery(): URLSearchParams {
293
+ return new URLSearchParams(this.query.toString());
294
+ }
295
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "screenshotone-api-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Use ScreenshotOne.com API to generate screenshots of any website.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/screenshotone/tssdk.git"
8
+ },
9
+ "keywords": [
10
+ "screenshot",
11
+ "phantom",
12
+ "phantomjs",
13
+ "screenshots",
14
+ "webkit",
15
+ "webkit2png",
16
+ "url2png",
17
+ "headless",
18
+ "chrome",
19
+ "chromium",
20
+ "blink",
21
+ "render"
22
+ ],
23
+ "author": "Dmytro Krasun",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/screenshotone/tssdk/issues"
27
+ },
28
+ "type": "module",
29
+ "main": "./lib/main.js",
30
+ "devDependencies": {
31
+ "typescript": "^4.7.3",
32
+ "@types/node": "^17.0.40",
33
+ "@types/big.js": "^6.1.3"
34
+ },
35
+ "dependencies": {
36
+ "big.js": "^6.2.0",
37
+ "node-fetch": "^3.2.5"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc --build"
41
+ }
42
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "es2022",
4
+ "target": "es2017",
5
+ "noImplicitAny": true,
6
+ "removeComments": true,
7
+ "preserveConstEnums": true,
8
+ "sourceMap": true,
9
+ "moduleResolution": "node",
10
+ },
11
+ "include": ["lib/**/*"],
12
+ "exclude": ["node_modules", "**/*.spec.ts"]
13
+ }