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

README.md

Lines changed: 7 additions & 7 deletions
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`)

package-lock.json

Lines changed: 905 additions & 851 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
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"

src/diffbot.d.ts

Lines changed: 49 additions & 35 deletions
Large diffs are not rendered by default.

src/diffbot.js

Lines changed: 80 additions & 52 deletions
Large diffs are not rendered by default.

src/lib/request.d.ts

Lines changed: 2 additions & 2 deletions
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;

src/lib/request.js

Lines changed: 11 additions & 6 deletions
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 {

test/analyze.test.js

Lines changed: 4 additions & 3 deletions
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
});

test/article.test.js

Lines changed: 4 additions & 3 deletions
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
});

test/discussion.test.js

Lines changed: 4 additions & 3 deletions
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
});

0 commit comments

Comments
 (0)