@npmforge/snapforge 0.1.1

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,12 @@
1
+ import type { SnapshotRequest, SnapshotResult, SnapforgeClientOptions } from "./types.js";
2
+ export declare class SnapforgeClient {
3
+ private readonly baseUrl;
4
+ private readonly apiKey;
5
+ private readonly defaultTtlSeconds?;
6
+ private readonly fetchImpl;
7
+ constructor(options: SnapforgeClientOptions);
8
+ getSnapshot(request: SnapshotRequest): Promise<SnapshotResult>;
9
+ private requestJson;
10
+ private toDataUrl;
11
+ }
12
+ export declare function createSnapforgeClient(options: SnapforgeClientOptions): SnapforgeClient;
package/dist/client.js ADDED
@@ -0,0 +1,80 @@
1
+ export class SnapforgeClient {
2
+ baseUrl;
3
+ apiKey;
4
+ defaultTtlSeconds;
5
+ fetchImpl;
6
+ constructor(options) {
7
+ if (!options.baseUrl) {
8
+ throw new Error("baseUrl is required.");
9
+ }
10
+ if (!options.apiKey) {
11
+ throw new Error("apiKey is required.");
12
+ }
13
+ this.baseUrl = new URL(options.baseUrl);
14
+ this.apiKey = options.apiKey;
15
+ this.defaultTtlSeconds = options.defaultTtlSeconds;
16
+ this.fetchImpl = options.fetchImpl ?? fetch;
17
+ }
18
+ async getSnapshot(request) {
19
+ if (!request.url) {
20
+ throw new Error("request.url is required.");
21
+ }
22
+ const captureBody = {
23
+ url: request.url,
24
+ ttlOverrideSeconds: request.ttlOverrideSeconds ?? this.defaultTtlSeconds,
25
+ width: request.width,
26
+ height: request.height,
27
+ fullPage: request.fullPage,
28
+ format: request.format,
29
+ quality: request.quality
30
+ };
31
+ const capture = await this.requestJson({
32
+ method: "POST",
33
+ path: "/capture",
34
+ body: captureBody
35
+ });
36
+ const imageUrl = new URL(capture.imagePath, this.baseUrl);
37
+ const imageResponse = await this.fetchImpl(imageUrl, {
38
+ method: "GET",
39
+ headers: {
40
+ "x-api-key": this.apiKey
41
+ }
42
+ });
43
+ if (!imageResponse.ok) {
44
+ const body = await imageResponse.text();
45
+ throw new Error(`Failed to fetch image (${imageResponse.status}): ${body || imageResponse.statusText}`);
46
+ }
47
+ const arrayBuffer = await imageResponse.arrayBuffer();
48
+ const bytes = new Uint8Array(arrayBuffer);
49
+ const dataUrl = this.toDataUrl(capture.mimeType, bytes);
50
+ return {
51
+ ...capture,
52
+ bytes,
53
+ dataUrl
54
+ };
55
+ }
56
+ async requestJson(options) {
57
+ const url = new URL(options.path, this.baseUrl);
58
+ const response = await this.fetchImpl(url, {
59
+ method: options.method,
60
+ headers: {
61
+ "content-type": "application/json",
62
+ "x-api-key": this.apiKey
63
+ },
64
+ body: options.body ? JSON.stringify(options.body) : undefined
65
+ });
66
+ if (!response.ok) {
67
+ const body = await response.text();
68
+ throw new Error(`Snapforge request failed (${response.status}): ${body || response.statusText}`);
69
+ }
70
+ return (await response.json());
71
+ }
72
+ toDataUrl(mimeType, bytes) {
73
+ const base64 = Buffer.from(bytes).toString("base64");
74
+ return `data:${mimeType};base64,${base64}`;
75
+ }
76
+ }
77
+ export function createSnapforgeClient(options) {
78
+ return new SnapforgeClient(options);
79
+ }
80
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,eAAe;IACT,OAAO,CAAM;IACb,MAAM,CAAS;IACf,iBAAiB,CAAU;IAC3B,SAAS,CAAe;IAEzC,YAAY,OAA+B;QACzC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACzC,CAAC;QAED,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACx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AAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACrD,OAAO,QAAQ,QAAQ,WAAW,MAAM,EAAE,CAAC;IAC7C,CAAC;CACF;AAED,MAAM,UAAU,qBAAqB,CACnC,OAA+B;IAE/B,OAAO,IAAI,eAAe,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { createSnapforgeClient, SnapforgeClient } from "./client.js";
2
+ export type { CaptureResponsePayload, ImageFormat, SnapshotRequest, SnapshotResult, SnapforgeClientOptions } from "./types.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createSnapforgeClient, SnapforgeClient } from "./client.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,31 @@
1
+ export type ImageFormat = "png" | "jpeg" | "webp";
2
+ export interface SnapforgeClientOptions {
3
+ baseUrl: string;
4
+ apiKey: string;
5
+ defaultTtlSeconds?: number;
6
+ fetchImpl?: typeof fetch;
7
+ }
8
+ export interface SnapshotRequest {
9
+ url: string;
10
+ ttlOverrideSeconds?: number;
11
+ width?: number;
12
+ height?: number;
13
+ fullPage?: boolean;
14
+ format?: ImageFormat;
15
+ quality?: number;
16
+ }
17
+ export interface CaptureResponsePayload {
18
+ key: string;
19
+ sourceUrl: string;
20
+ cached: boolean;
21
+ capturedAt: string;
22
+ expiresAt: string;
23
+ ttlSeconds: number;
24
+ mimeType: string;
25
+ imagePath: string;
26
+ imageUrl: string;
27
+ }
28
+ export interface SnapshotResult extends CaptureResponsePayload {
29
+ bytes: Uint8Array;
30
+ dataUrl: string;
31
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@npmforge/snapforge",
3
+ "version": "0.1.1",
4
+ "description": "TypeScript client for the self-hosted Snapforge screenshot service.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/clow99/getsnapforge.com"
8
+ },
9
+ "type": "module",
10
+ "main": "./dist/index.js",
11
+ "types": "./src/index.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./src/index.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.json",
23
+ "dev": "tsc -p tsconfig.json --watch",
24
+ "typecheck": "tsc -p tsconfig.json --noEmit",
25
+ "lint": "tsc -p tsconfig.json --noEmit"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "*",
29
+ "typescript": "*"
30
+ }
31
+ }