Skip to content

Commit 97ef0fc

Browse files
Merge branch 'main' of github.com:theRealPadster/diffbot-api-node
2 parents c0ada40 + 3507135 commit 97ef0fc

15 files changed

+1088
-979
lines changed

Diff for: README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ Diffbot-API-Node is a Promise-based library to use the [Diffbot](https://www.dif
1212
## Features
1313

1414
Currently supports the following features:
15-
* [Analyze](#analyze-api) (with HTML POST support)
16-
* [Article](#article-api) (with HTML and plaintext POST support)
17-
* [Discussion](#discussion-api) (with HTML POST support)
18-
* [Event](#event-api-beta) (beta) (with HTML POST support)
19-
* [Image](#image-api) (with HTML POST support)
20-
* [Product](#product-api) (with HTML POST support)
21-
* [Video](#video-api) (with HTML POST support)
15+
* [Analyze](#analyze-api)
16+
* [Article](#article-api)
17+
* [Discussion](#discussion-api)
18+
* [Event](#event-api-beta) (beta)
19+
* [Image](#image-api)
20+
* [Product](#product-api)
21+
* [Video](#video-api)
2222
* [Knowledge Graph](#knowledge-graph-api)
2323
* [Crawl](#crawl-api)
2424
* New (all params supported except `customHeaders`)

Diff for: package-lock.json

+905-851
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"api",
77
"node"
88
],
9-
"version": "0.4.2",
9+
"version": "0.9.0",
1010
"repository": {
1111
"type": "git",
1212
"url": "https://github.com/therealpadster/diffbot-api-node.git"

Diff for: src/diffbot.d.ts

+49-35
Large diffs are not rendered by default.

Diff for: src/diffbot.js

+80-52
Large diffs are not rendered by default.

Diff for: src/lib/request.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export function generate(url: string, method?: string, body?: string, customHeaders?: any): Request;
1+
export function generate(url: string, method?: string, body?: string, customHeaders?: object): Request;
22
export function exec(request: Request): any;
33
/**
44
* Generate a simple request object
55
*/
6-
export type Request = any;
6+
export type Request = object;

Diff for: src/lib/request.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@ const axios = require('axios');
33
// TODO: figure out how to get JSDocs to work properly
44
/**
55
* Generate a simple request object
6-
* @typedef {Object} Request
6+
* @typedef {object} Request
77
* @param {string} url The URL
88
* @param {string} method The HTTP method to use (defaults to GET)
99
* @param {string} [body] Optional HTML markup or plaintext to pass as POST body
10-
* @param {Object} headers The request headers
10+
* @param {object} headers The request headers
1111
*/
1212

1313
/**
1414
* Generate a simple request object
1515
* @param {string} url The URL
1616
* @param {string} [method] The HTTP method to use (defaults to GET)
1717
* @param {string} [body] Optional HTML markup or plaintext to pass as POST body
18-
* @param {Object} [customHeaders] Optional additional request headers object
18+
* @param {object} [customHeaders] Optional additional request headers object
1919
* @returns {Request} The request object
2020
*/
2121
exports.generate = function(url, method = 'GET', body, customHeaders) {
22-
let headers = { ...customHeaders };
22+
// Add 'X-Forward-' to all the custom headers
23+
const headers = Object.entries({...customHeaders}).reduce((acc, [key, value]) => {
24+
acc['X-Forward-' + key] = value;
25+
return acc;
26+
}, {});
27+
2328
if (body) {
2429
if (body.startsWith('<'))
25-
headers = { 'Content-Type': 'text/html' };
30+
headers['Content-Type'] = 'text/html';
2631
else
27-
headers = { 'Content-Type': 'text/plain' };
32+
headers['Content-Type'] = 'text/plain';
2833
}
2934

3035
return {

Diff for: test/analyze.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customHeaders, customJS } = require('./global');
22

33
describe('Analyze Tests', function() {
44

@@ -24,16 +24,17 @@ describe('Analyze Tests', function() {
2424
return Promise.resolve(true);
2525
});
2626

27-
it('should generate the analyze GET request with custom JS', async () => {
27+
it('should generate the analyze GET request with custom headers', async () => {
2828
const url = 'https://www.theverge.com/2020/8/25/21400240/epic-apple-ruling-unreal-engine-fortnite-temporary-restraining-order';
2929

30-
let request = await diffbot.analyze({ url, customJS });
30+
let request = await diffbot.analyze({ url, customJS, customHeaders });
3131

3232
expect(request.url).to.equal(`https://api.diffbot.com/v3/analyze?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
3333
expect(request.method).to.equal('GET');
3434
expect(request.body).to.be.undefined;
3535
expect(request.headers).to.be.an('object');
3636
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
37+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3738

3839
return Promise.resolve(true);
3940
});

Diff for: test/article.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Article Tests', function() {
44

@@ -25,16 +25,17 @@ describe('Article Tests', function() {
2525
return Promise.resolve(true);
2626
});
2727

28-
it('should generate the article GET request with custom JS', async () => {
28+
it('should generate the article GET request with custom headers', async () => {
2929
const url = 'https://www.theverge.com/2020/8/25/21400240/epic-apple-ruling-unreal-engine-fortnite-temporary-restraining-order';
3030

31-
let request = await diffbot.article({ url, customJS });
31+
let request = await diffbot.article({ url, customJS, customHeaders });
3232

3333
expect(request.url).to.equal(`https://api.diffbot.com/v3/article?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
3434
expect(request.method).to.equal('GET');
3535
expect(request.body).to.be.undefined;
3636
expect(request.headers).to.be.an('object');
3737
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
38+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3839

3940
return Promise.resolve(true);
4041
});

Diff for: test/discussion.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Discussion Tests', function() {
44

@@ -21,16 +21,17 @@ describe('Discussion Tests', function() {
2121
return Promise.resolve(true);
2222
});
2323

24-
it('should generate the discussion GET request with custom JS', async () => {
24+
it('should generate the discussion GET request with custom headers', async () => {
2525
const url = 'https://www.theverge.com/2020/8/25/21400240/epic-apple-ruling-unreal-engine-fortnite-temporary-restraining-order';
2626

27-
let request = await diffbot.discussion({ url, customJS });
27+
let request = await diffbot.discussion({ url, customJS, customHeaders });
2828

2929
expect(request.url).to.equal(`https://api.diffbot.com/v3/discussion?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
3030
expect(request.method).to.equal('GET');
3131
expect(request.body).to.be.undefined;
3232
expect(request.headers).to.be.an('object');
3333
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
34+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3435

3536
return Promise.resolve(true);
3637
});

Diff for: test/event.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Event Tests', function() {
44

@@ -21,14 +21,15 @@ describe('Event Tests', function() {
2121
return Promise.resolve(true);
2222
});
2323

24-
it('should generate the event GET request with custom JS', async () => {
25-
let request = await diffbot.event({ url, customJS });
24+
it('should generate the event GET request with custom headers', async () => {
25+
let request = await diffbot.event({ url, customJS, customHeaders });
2626

2727
expect(request.url).to.equal(`https://api.diffbot.com/v3/event?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
2828
expect(request.method).to.equal('GET');
2929
expect(request.body).to.be.undefined;
3030
expect(request.headers).to.be.an('object');
3131
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
32+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3233

3334
return Promise.resolve(true);
3435
});

Diff for: test/global.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const chai = require('chai');
33
const chaiAsPromised = require('chai-as-promised');
44
chai.use(chaiAsPromised);
55

6-
function start(){};
7-
function end(){};
6+
function start(){}
7+
function end(){}
88
const customJS = function() {
99
start();
1010
setTimeout(function() {
@@ -18,9 +18,10 @@ const customJS = function() {
1818
end();
1919
}
2020
}, 500);
21-
}.toString();
21+
};
2222

2323
const FAKE_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
2424
exports.diffbot = new Diffbot(FAKE_TOKEN, true);
2525
exports.expect = chai.expect;
26-
exports.customJS = customJS;
26+
exports.customJS = customJS.toString();
27+
exports.customHeaders = { 'User-Agent': 'Diffbot' };

Diff for: test/image.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Image Tests', function() {
44

@@ -20,16 +20,17 @@ describe('Image Tests', function() {
2020
return Promise.resolve(true);
2121
});
2222

23-
it('should generate the image GET request with custom JS', async () => {
23+
it('should generate the image GET request with custom headers', async () => {
2424
const url = 'https://www.deviantart.com/up-tchi/art/Coral-village-852927725';
2525

26-
let request = await diffbot.image({ url, customJS });
26+
let request = await diffbot.image({ url, customJS, customHeaders });
2727

2828
expect(request.url).to.equal(`https://api.diffbot.com/v3/image?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
2929
expect(request.method).to.equal('GET');
3030
expect(request.body).to.be.undefined;
3131
expect(request.headers).to.be.an('object');
3232
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
33+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3334

3435
return Promise.resolve(true);
3536
});

Diff for: test/product.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Product Tests', function() {
44

@@ -35,14 +35,15 @@ describe('Product Tests', function() {
3535
return Promise.resolve(true);
3636
});
3737

38-
it('should generate the product GET request with custom JS', async () => {
39-
let request = await diffbot.product({ url, customJS });
38+
it('should generate the product GET request with custom headers', async () => {
39+
let request = await diffbot.product({ url, customJS, customHeaders });
4040

4141
expect(request.url).to.equal(`https://api.diffbot.com/v3/product?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
4242
expect(request.method).to.equal('GET');
4343
expect(request.body).to.be.undefined;
4444
expect(request.headers).to.be.an('object');
4545
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
46+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
4647

4748
return Promise.resolve(true);
4849
});

Diff for: test/video.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { diffbot, expect, customJS } = require('./global');
1+
const { diffbot, expect, customJS, customHeaders } = require('./global');
22

33
describe('Video Tests', function() {
44

@@ -20,16 +20,17 @@ describe('Video Tests', function() {
2020
return Promise.resolve(true);
2121
});
2222

23-
it('should generate the video GET request with custom JS', async () => {
23+
it('should generate the video GET request with custom headers', async () => {
2424
const url = 'https://www.youtube.com/watch?v=HeiPdaTQTfo';
2525

26-
let request = await diffbot.video({ url, customJS });
26+
let request = await diffbot.video({ url, customJS, customHeaders });
2727

2828
expect(request.url).to.equal(`https://api.diffbot.com/v3/video?token=${diffbot.token}&url=${encodeURIComponent(url)}`);
2929
expect(request.method).to.equal('GET');
3030
expect(request.body).to.be.undefined;
3131
expect(request.headers).to.be.an('object');
3232
expect(request.headers['X-Forward-X-Evaluate']).to.equal(customJS.replace(/(\r?\n|\r)\s+/g, ''));
33+
expect(request.headers['X-Forward-User-Agent']).to.equal(customHeaders['User-Agent']);
3334

3435
return Promise.resolve(true);
3536
});

0 commit comments

Comments
 (0)