snaprender 0.1.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.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +107 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SnapRender
|
|
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, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @snaprender/node
|
|
2
|
+
|
|
3
|
+
Official Node.js SDK for the [SnapRender](https://snap-render.com) Screenshot API.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @snaprender/node
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import SnapRender from '@snaprender/node';
|
|
15
|
+
|
|
16
|
+
const snap = new SnapRender({ apiKey: 'sk_live_...' });
|
|
17
|
+
|
|
18
|
+
// Capture a screenshot
|
|
19
|
+
const buffer = await snap.capture({ url: 'https://example.com' });
|
|
20
|
+
fs.writeFileSync('screenshot.png', buffer);
|
|
21
|
+
|
|
22
|
+
// Capture as JPEG with options
|
|
23
|
+
const jpg = await snap.capture({
|
|
24
|
+
url: 'https://example.com',
|
|
25
|
+
format: 'jpeg',
|
|
26
|
+
width: 1920,
|
|
27
|
+
height: 1080,
|
|
28
|
+
fullPage: true,
|
|
29
|
+
darkMode: true,
|
|
30
|
+
quality: 95,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Check cache status
|
|
34
|
+
const info = await snap.info({ url: 'https://example.com' });
|
|
35
|
+
console.log(info.cached); // true/false
|
|
36
|
+
|
|
37
|
+
// Get usage
|
|
38
|
+
const usage = await snap.usage();
|
|
39
|
+
console.log(`${usage.used}/${usage.limit} screenshots used`);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `new SnapRender(options)`
|
|
45
|
+
|
|
46
|
+
| Option | Type | Required | Default |
|
|
47
|
+
|--------|------|----------|---------|
|
|
48
|
+
| `apiKey` | string | Yes | — |
|
|
49
|
+
| `baseUrl` | string | No | `https://app.snap-render.com` |
|
|
50
|
+
|
|
51
|
+
### `snap.capture(options)` → `Promise<Buffer>`
|
|
52
|
+
|
|
53
|
+
| Option | Type | Default | Description |
|
|
54
|
+
|--------|------|---------|-------------|
|
|
55
|
+
| `url` | string | — | URL to capture (required) |
|
|
56
|
+
| `format` | `'png' \| 'jpeg' \| 'webp' \| 'pdf'` | `'png'` | Output format |
|
|
57
|
+
| `width` | number | `1280` | Viewport width |
|
|
58
|
+
| `height` | number | `800` | Viewport height |
|
|
59
|
+
| `fullPage` | boolean | `false` | Capture full scrollable page |
|
|
60
|
+
| `quality` | number | `90` | JPEG/WebP quality (1-100) |
|
|
61
|
+
| `delay` | number | `0` | Wait ms after page load |
|
|
62
|
+
| `darkMode` | boolean | `false` | Emulate dark mode |
|
|
63
|
+
| `blockAds` | boolean | `true` | Block ad networks |
|
|
64
|
+
| `blockCookieBanners` | boolean | `true` | Remove cookie banners |
|
|
65
|
+
| `device` | string | — | Device preset (e.g. `'iPhone 15'`) |
|
|
66
|
+
| `cache` | boolean | `true` | Use cache |
|
|
67
|
+
| `cacheTtl` | number | `86400` | Cache TTL in seconds |
|
|
68
|
+
|
|
69
|
+
### `snap.info(options)` → `Promise<CacheInfo>`
|
|
70
|
+
|
|
71
|
+
Check if a screenshot is cached without capturing.
|
|
72
|
+
|
|
73
|
+
### `snap.usage()` → `Promise<UsageResponse>`
|
|
74
|
+
|
|
75
|
+
Get current month's usage.
|
|
76
|
+
|
|
77
|
+
### `snap.usageDaily(days?)` → `Promise<DailyUsage>`
|
|
78
|
+
|
|
79
|
+
Get daily usage breakdown (default 30 days).
|
|
80
|
+
|
|
81
|
+
## Error Handling
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { SnapRenderError } from '@snaprender/node';
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await snap.capture({ url: 'https://example.com' });
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (err instanceof SnapRenderError) {
|
|
90
|
+
console.log(err.code); // 'QUOTA_EXCEEDED'
|
|
91
|
+
console.log(err.status); // 429
|
|
92
|
+
console.log(err.message); // 'Monthly quota exceeded'
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export interface SnapRenderOptions {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface CaptureOptions {
|
|
6
|
+
url: string;
|
|
7
|
+
format?: 'png' | 'jpeg' | 'webp' | 'pdf';
|
|
8
|
+
width?: number;
|
|
9
|
+
height?: number;
|
|
10
|
+
fullPage?: boolean;
|
|
11
|
+
quality?: number;
|
|
12
|
+
delay?: number;
|
|
13
|
+
darkMode?: boolean;
|
|
14
|
+
blockAds?: boolean;
|
|
15
|
+
blockCookieBanners?: boolean;
|
|
16
|
+
hideSelectors?: string;
|
|
17
|
+
clickSelector?: string;
|
|
18
|
+
device?: string;
|
|
19
|
+
userAgent?: string;
|
|
20
|
+
cache?: boolean;
|
|
21
|
+
cacheTtl?: number;
|
|
22
|
+
}
|
|
23
|
+
export interface CacheInfo {
|
|
24
|
+
url: string;
|
|
25
|
+
cached: boolean;
|
|
26
|
+
cachedAt?: string;
|
|
27
|
+
expiresAt?: string;
|
|
28
|
+
size?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface UsageResponse {
|
|
31
|
+
plan: string;
|
|
32
|
+
used: number;
|
|
33
|
+
limit: number;
|
|
34
|
+
remaining: number;
|
|
35
|
+
}
|
|
36
|
+
export interface DailyUsage {
|
|
37
|
+
days: number;
|
|
38
|
+
data: Array<{
|
|
39
|
+
date: string;
|
|
40
|
+
count: number;
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
43
|
+
export declare class SnapRenderError extends Error {
|
|
44
|
+
readonly code: string;
|
|
45
|
+
readonly status: number;
|
|
46
|
+
constructor(message: string, code: string, status: number);
|
|
47
|
+
}
|
|
48
|
+
export declare class SnapRender {
|
|
49
|
+
private readonly apiKey;
|
|
50
|
+
private readonly baseUrl;
|
|
51
|
+
constructor(options: SnapRenderOptions);
|
|
52
|
+
/**
|
|
53
|
+
* Capture a screenshot of a URL.
|
|
54
|
+
* Returns the image as a Buffer.
|
|
55
|
+
*/
|
|
56
|
+
capture(options: CaptureOptions): Promise<Buffer>;
|
|
57
|
+
/**
|
|
58
|
+
* Get cache info for a URL without capturing.
|
|
59
|
+
*/
|
|
60
|
+
info(options: CaptureOptions): Promise<CacheInfo>;
|
|
61
|
+
/**
|
|
62
|
+
* Get current usage for the authenticated account.
|
|
63
|
+
*/
|
|
64
|
+
usage(): Promise<UsageResponse>;
|
|
65
|
+
/**
|
|
66
|
+
* Get daily usage breakdown.
|
|
67
|
+
*/
|
|
68
|
+
usageDaily(days?: number): Promise<DailyUsage>;
|
|
69
|
+
private request;
|
|
70
|
+
private json;
|
|
71
|
+
}
|
|
72
|
+
export default SnapRender;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export class SnapRenderError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
status;
|
|
4
|
+
constructor(message, code, status) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.name = 'SnapRenderError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class SnapRender {
|
|
12
|
+
apiKey;
|
|
13
|
+
baseUrl;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
if (!options.apiKey)
|
|
16
|
+
throw new Error('apiKey is required');
|
|
17
|
+
this.apiKey = options.apiKey;
|
|
18
|
+
this.baseUrl = (options.baseUrl ?? 'https://app.snap-render.com').replace(/\/$/, '');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Capture a screenshot of a URL.
|
|
22
|
+
* Returns the image as a Buffer.
|
|
23
|
+
*/
|
|
24
|
+
async capture(options) {
|
|
25
|
+
const params = new URLSearchParams();
|
|
26
|
+
params.set('url', options.url);
|
|
27
|
+
if (options.format)
|
|
28
|
+
params.set('format', options.format);
|
|
29
|
+
if (options.width)
|
|
30
|
+
params.set('width', String(options.width));
|
|
31
|
+
if (options.height)
|
|
32
|
+
params.set('height', String(options.height));
|
|
33
|
+
if (options.fullPage !== undefined)
|
|
34
|
+
params.set('full_page', String(options.fullPage));
|
|
35
|
+
if (options.quality)
|
|
36
|
+
params.set('quality', String(options.quality));
|
|
37
|
+
if (options.delay)
|
|
38
|
+
params.set('delay', String(options.delay));
|
|
39
|
+
if (options.darkMode !== undefined)
|
|
40
|
+
params.set('dark_mode', String(options.darkMode));
|
|
41
|
+
if (options.blockAds !== undefined)
|
|
42
|
+
params.set('block_ads', String(options.blockAds));
|
|
43
|
+
if (options.blockCookieBanners !== undefined)
|
|
44
|
+
params.set('block_cookie_banners', String(options.blockCookieBanners));
|
|
45
|
+
if (options.hideSelectors)
|
|
46
|
+
params.set('hide_selectors', options.hideSelectors);
|
|
47
|
+
if (options.clickSelector)
|
|
48
|
+
params.set('click_selector', options.clickSelector);
|
|
49
|
+
if (options.device)
|
|
50
|
+
params.set('device', options.device);
|
|
51
|
+
if (options.userAgent)
|
|
52
|
+
params.set('user_agent', options.userAgent);
|
|
53
|
+
if (options.cache !== undefined)
|
|
54
|
+
params.set('cache', String(options.cache));
|
|
55
|
+
if (options.cacheTtl)
|
|
56
|
+
params.set('cache_ttl', String(options.cacheTtl));
|
|
57
|
+
const res = await this.request(`/v1/screenshot?${params}`);
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
const body = await res.json().catch(() => ({}));
|
|
60
|
+
throw new SnapRenderError(body.error?.message ?? `HTTP ${res.status}`, body.error?.code ?? 'UNKNOWN', res.status);
|
|
61
|
+
}
|
|
62
|
+
const arrayBuffer = await res.arrayBuffer();
|
|
63
|
+
return Buffer.from(arrayBuffer);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get cache info for a URL without capturing.
|
|
67
|
+
*/
|
|
68
|
+
async info(options) {
|
|
69
|
+
const params = new URLSearchParams();
|
|
70
|
+
params.set('url', options.url);
|
|
71
|
+
if (options.format)
|
|
72
|
+
params.set('format', options.format);
|
|
73
|
+
if (options.width)
|
|
74
|
+
params.set('width', String(options.width));
|
|
75
|
+
if (options.height)
|
|
76
|
+
params.set('height', String(options.height));
|
|
77
|
+
const res = await this.request(`/v1/screenshot/info?${params}`);
|
|
78
|
+
return this.json(res);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get current usage for the authenticated account.
|
|
82
|
+
*/
|
|
83
|
+
async usage() {
|
|
84
|
+
const res = await this.request('/v1/usage');
|
|
85
|
+
return this.json(res);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get daily usage breakdown.
|
|
89
|
+
*/
|
|
90
|
+
async usageDaily(days = 30) {
|
|
91
|
+
const res = await this.request(`/v1/usage/daily?days=${days}`);
|
|
92
|
+
return this.json(res);
|
|
93
|
+
}
|
|
94
|
+
async request(path) {
|
|
95
|
+
return fetch(`${this.baseUrl}${path}`, {
|
|
96
|
+
headers: { 'X-API-Key': this.apiKey },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async json(res) {
|
|
100
|
+
if (!res.ok) {
|
|
101
|
+
const body = await res.json().catch(() => ({}));
|
|
102
|
+
throw new SnapRenderError(body.error?.message ?? `HTTP ${res.status}`, body.error?.code ?? 'UNKNOWN', res.status);
|
|
103
|
+
}
|
|
104
|
+
return res.json();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export default SnapRender;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "snaprender",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official Node.js SDK for SnapRender Screenshot API",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["screenshot", "api", "snaprender", "capture", "webpage"],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"homepage": "https://snap-render.com",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/User0856/snaprender"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^5.7.0"
|
|
31
|
+
}
|
|
32
|
+
}
|