Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Capture Node SDK
Node Library for Capture by Techulus

Get your API Key and Secret from https://capture.techulus.in/console
Node Library for [capture.page](https://capture.page)

Get your API Key and Secret from [https://capture.page/console](https://capture.page/console)

List of all capture options: [https://docs.capture.page](https://docs.capture.page)

## Installation

Expand All @@ -11,25 +14,27 @@ npm install capture-node

## Usage

List of all capture options: https://docs.capture.techulus.in
List of all capture options: https://docs.capture.page

### Image

```javascript
import { Capture } from 'capture-node';
const capture = new Capture(YOUR_API_KEY, YOUR_API_SECRET);
// or initialize with edge endpoint
// const capture = new Capture(YOUR_API_KEY, YOUR_API_SECRET, { useEdge: true });

// var url = capture.buildImageUrl(URL_TO_CAPTURE, CAPTURE_OPTIONS);

const url = capture.buildImageUrl('https://capture.techulus.in/', {
const url = capture.buildImageUrl('https://capture.page/', {
full: true,
delay: 3,
t: Date.now()
});

// or

const image = await capture.fetchImage('https://capture.techulus.in/', {
const image = await capture.fetchImage('https://capture.page/', {
full: true,
delay: 3,
t: Date.now()
Expand All @@ -45,15 +50,15 @@ const capture = new Capture(YOUR_API_KEY, YOUR_API_SECRET);

// var url = capture.buildPdfUrl(URL_TO_CAPTURE, CAPTURE_OPTIONS);

const url = capture.buildPdfUrl('https://capture.techulus.in/', {
const url = capture.buildPdfUrl('https://capture.page/', {
full: true,
delay: 3,
t: Date.now()
});

// or

const pdf = capture.fetchPdf('https://capture.techulus.in/', {
const pdf = capture.fetchPdf('https://capture.page/', {
full: true,
delay: 3,
t: Date.now()
Expand All @@ -68,11 +73,11 @@ const capture = new Capture(YOUR_API_KEY, YOUR_API_SECRET);

// var url = capture.buildContentUrl(URL_TO_CAPTURE, CAPTURE_OPTIONS);

const url = capture.buildContentUrl('https://capture.techulus.in/');
const url = capture.buildContentUrl('https://capture.page/');

// or

const content = await capture.fetchContent('https://capture.techulus.in/');
const content = await capture.fetchContent('https://capture.page/');
```

### Metadata
Expand All @@ -83,9 +88,9 @@ const capture = new Capture(YOUR_API_KEY, YOUR_API_SECRET);

// var url = capture.buildMetadataUrl(URL_TO_CAPTURE, CAPTURE_OPTIONS);

const url = capture.buildMetadataUrl('https://capture.techulus.in/');
const url = capture.buildMetadataUrl('https://capture.page/');

// or

const content = await capture.fetchMetadata('https://capture.techulus.in/');
const content = await capture.fetchMetadata('https://capture.page/');
```
40 changes: 32 additions & 8 deletions __tests__/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ describe("Capture URL Builder", () => {
const url = capture.buildImageUrl("https://news.ycombinator.com/");
console.log(url);
expect(url).toBe(
"https://cdn.capture.techulus.in/test/f37d5fb3ee4540a05bf4ffeed6dffa28/image?url=https%3A%2F%2Fnews.ycombinator.com%2F",
"https://cdn.capture.page/test/f37d5fb3ee4540a05bf4ffeed6dffa28/image?url=https%3A%2F%2Fnews.ycombinator.com%2F",
);
});

it("buildImageUrl with options should return valid url", () => {
const url = capture.buildImageUrl("https://capture.techulus.in/", {
const url = capture.buildImageUrl("https://capture.page/", {
full: true,
delay: 3,
});
expect(url).toBe(
"https://cdn.capture.techulus.in/test/2cb8b7ed9dbdb5c0db4a5dc4523a0780/image?full=true&delay=3&url=https%3A%2F%2Fcapture.techulus.in%2F",
"https://cdn.capture.page/test/0e944abb6d823d0c8618dc22e508be6d/image?full=true&delay=3&url=https%3A%2F%2Fcapture.page%2F",
);
});
});
Expand All @@ -37,17 +37,17 @@ describe("Capture URL Builder", () => {
it("buildPdfUrl should return valid url", () => {
const url = capture.buildPdfUrl("https://news.ycombinator.com/");
expect(url).toBe(
"https://cdn.capture.techulus.in/test/f37d5fb3ee4540a05bf4ffeed6dffa28/pdf?url=https%3A%2F%2Fnews.ycombinator.com%2F",
"https://cdn.capture.page/test/f37d5fb3ee4540a05bf4ffeed6dffa28/pdf?url=https%3A%2F%2Fnews.ycombinator.com%2F",
);
});

it("buildPdfUrl with options should return valid url", () => {
const url = capture.buildPdfUrl("https://capture.techulus.in/", {
const url = capture.buildPdfUrl("https://capture.page/", {
full: true,
delay: 3,
});
expect(url).toBe(
"https://cdn.capture.techulus.in/test/2cb8b7ed9dbdb5c0db4a5dc4523a0780/pdf?full=true&delay=3&url=https%3A%2F%2Fcapture.techulus.in%2F",
"https://cdn.capture.page/test/0e944abb6d823d0c8618dc22e508be6d/pdf?full=true&delay=3&url=https%3A%2F%2Fcapture.page%2F",
);
});
});
Expand All @@ -61,7 +61,7 @@ describe("Capture URL Builder", () => {
it("buildContentUrl should return valid url", () => {
const url = capture.buildContentUrl("https://news.ycombinator.com/");
expect(url).toBe(
"https://cdn.capture.techulus.in/test/f37d5fb3ee4540a05bf4ffeed6dffa28/content?url=https%3A%2F%2Fnews.ycombinator.com%2F",
"https://cdn.capture.page/test/f37d5fb3ee4540a05bf4ffeed6dffa28/content?url=https%3A%2F%2Fnews.ycombinator.com%2F",
);
});
});
Expand All @@ -75,8 +75,32 @@ describe("Capture URL Builder", () => {
it("buildMetadataUrl should return valid url", () => {
const url = capture.buildMetadataUrl("https://news.ycombinator.com/");
expect(url).toBe(
"https://cdn.capture.techulus.in/test/f37d5fb3ee4540a05bf4ffeed6dffa28/metadata?url=https%3A%2F%2Fnews.ycombinator.com%2F",
"https://cdn.capture.page/test/f37d5fb3ee4540a05bf4ffeed6dffa28/metadata?url=https%3A%2F%2Fnews.ycombinator.com%2F",
);
});
});
});

describe("Capture URL Builder with useEdge", () => {
const edgeCapture = new Capture("test", "test", { useEdge: true });

it("buildImageUrl should use edge URL", () => {
const url = edgeCapture.buildImageUrl("https://news.ycombinator.com/");
expect(url.startsWith("https://edge.capture.page/")).toBe(true);
});

it("buildPdfUrl should use edge URL", () => {
const url = edgeCapture.buildPdfUrl("https://news.ycombinator.com/");
expect(url.startsWith("https://edge.capture.page/")).toBe(true);
});

it("buildContentUrl should use edge URL", () => {
const url = edgeCapture.buildContentUrl("https://news.ycombinator.com/");
expect(url.startsWith("https://edge.capture.page/")).toBe(true);
});

it("buildMetadataUrl should use edge URL", () => {
const url = edgeCapture.buildMetadataUrl("https://news.ycombinator.com/");
expect(url.startsWith("https://edge.capture.page/")).toBe(true);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "capture-node",
"version": "2.1.0",
"description": "Node Library for Capture by Techulus",
"description": "Node Library for capture.page",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ export type RequestType = "image" | "pdf" | "content" | "metadata";
export type RequestOptions = Record<string, string | number | boolean>;

export class Capture {
static API_URL = "https://cdn.capture.techulus.in";
static API_URL = "https://cdn.capture.page";
static EDGE_URL = "https://edge.capture.page";

key: string;
secret: string;
options: { useEdge?: boolean };

constructor(key: string, secret: string) {
constructor(key: string, secret: string, options?: { useEdge?: boolean }) {
this.key = key;
this.secret = secret;
this.options = options ?? { useEdge: false };
}

private _generateToken(secret: string, url: string) {
Expand Down Expand Up @@ -74,7 +77,7 @@ export class Capture {
query = this._toQueryString(options);
}
const token = this._generateToken(this.secret, query);
return `${Capture.API_URL}/${this.key}/${token}/${type}?${query}`;
return `${this.options.useEdge ? Capture.EDGE_URL : Capture.API_URL}/${this.key}/${token}/${type}?${query}`;
}

buildImageUrl(url: string, options?: RequestOptions) {
Expand Down