node-duckduckgo
Advanced tools
Comparing version 1.3.0 to 1.22.5
@@ -1,27 +0,2 @@ | ||
import * as request from 'request'; | ||
import { RequestCallback, RequestResponse } from 'request'; | ||
import { allowedFormat, IFormatter, JsonFormatter } from './formatHelper'; | ||
import { SafeSearchValues, StdOptionValues } from './optionHelper'; | ||
export { JsonFormatter, RequestCallback, RequestResponse }; | ||
export declare const safeSearch_strict = 1; | ||
export declare const safeSearch_moderate = -1; | ||
export declare const safeSearch_off = -2; | ||
export declare class Requester { | ||
private appName; | ||
private _baseUrl; | ||
private _formatter; | ||
private _noRedirect; | ||
private _noHtml; | ||
private _skipDisambig; | ||
private _safeSearch; | ||
constructor(appName?: string); | ||
requestP(search: string): Promise<RequestResponse>; | ||
request(search: string, callBack?: RequestCallback): request.Request; | ||
private buildQueryOptions; | ||
format: allowedFormat; | ||
readonly formatter: IFormatter; | ||
no_redirect: StdOptionValues; | ||
no_html: StdOptionValues; | ||
skip_disambig: StdOptionValues; | ||
safe_search: SafeSearchValues; | ||
} | ||
import { duckIt, ParentalFilter, SearchOption } from './duckIt'; | ||
export { ParentalFilter, SearchOption, duckIt }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const request = require("request"); | ||
const formatHelper_1 = require("./formatHelper"); | ||
exports.JsonFormatter = formatHelper_1.JsonFormatter; | ||
const optionHelper_1 = require("./optionHelper"); | ||
const queryBuilder_1 = require("./queryBuilder"); | ||
exports.safeSearch_strict = 1; | ||
exports.safeSearch_moderate = -1; | ||
exports.safeSearch_off = -2; | ||
// tslint:disable-next-line:min-class-cohesion | ||
class Requester { | ||
constructor(appName = 'node-duckduckgo') { | ||
this.appName = appName; | ||
this._baseUrl = 'https://api.duckduckgo.com/'; | ||
// tslint:disable-next-line:variable-name | ||
this._formatter = formatHelper_1.getFormatter('json'); | ||
this._noRedirect = new optionHelper_1.OptionHelper('no_redirect'); | ||
this._noHtml = new optionHelper_1.OptionHelper('no_html'); | ||
this._skipDisambig = new optionHelper_1.OptionHelper('skip_disambig'); | ||
this._safeSearch = new optionHelper_1.OptionHelper('kp', exports.safeSearch_strict); | ||
} | ||
requestP(search) { | ||
return new Promise((resolve, reject) => { | ||
const cb = (error, response, body) => { | ||
if (error !== undefined && error !== null) { | ||
reject(error); | ||
} | ||
resolve(response); | ||
}; | ||
this.request(search, cb); | ||
}); | ||
} | ||
request(search, callBack) { | ||
const builder = queryBuilder_1.getBuilder(); | ||
builder.q(search); | ||
builder.t(this.appName); | ||
this._formatter.buildQueryParam(builder); | ||
this.buildQueryOptions(builder); | ||
const queryString = builder.toString(); | ||
return request(`${this._baseUrl}?${queryString}`, callBack); | ||
} | ||
buildQueryOptions(builder) { | ||
this._noRedirect.buildQueryParam(builder); | ||
this._noHtml.buildQueryParam(builder); | ||
this._skipDisambig.buildQueryParam(builder); | ||
this._safeSearch.buildQueryParam(builder); | ||
} | ||
set format(format) { | ||
this._formatter = formatHelper_1.getFormatter(format); | ||
} | ||
get format() { | ||
return this._formatter.getFormat(); | ||
} | ||
get formatter() { | ||
return this._formatter; | ||
} | ||
set no_redirect(value) { | ||
this._noRedirect.option = value; | ||
} | ||
get no_redirect() { | ||
return this._noRedirect.option; | ||
} | ||
set no_html(value) { | ||
this._noHtml.option = value; | ||
} | ||
get no_html() { | ||
return this._noHtml.option; | ||
} | ||
set skip_disambig(value) { | ||
this._skipDisambig.option = value; | ||
} | ||
get skip_disambig() { | ||
return this._skipDisambig.option; | ||
} | ||
set safe_search(value) { | ||
this._safeSearch.option = value; | ||
} | ||
get safe_search() { | ||
return this._safeSearch.option; | ||
} | ||
} | ||
exports.Requester = Requester; | ||
//# sourceMappingURL=index.js.map | ||
const duckIt_1 = require("./duckIt"); | ||
exports.duckIt = duckIt_1.duckIt; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "node-duckduckgo", | ||
"version": "1.3.0", | ||
"version": "1.22.5", | ||
"description": "DuckDuckGo client for node", | ||
@@ -15,4 +15,5 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"test": "gulp test", | ||
"lint": "gulp lib:lint" | ||
"lib": "nss-run lib", | ||
"test": "nss-run test", | ||
"lint": "nss-run lib:lint" | ||
}, | ||
@@ -42,29 +43,36 @@ "repository": { | ||
"devDependencies": { | ||
"@types/chai": "^4.0.1", | ||
"@types/jsonstream": "^0.8.28", | ||
"@types/chai": "4.2.7", | ||
"@types/mocha": "^5.2.5", | ||
"@types/request": "^2.0.0", | ||
"JSONStream": "^1.3.0", | ||
"chai": "^4.1.0", | ||
"del": "^4.0.0", | ||
"gulp": "^3.9.1", | ||
"gulp-istanbul": "^1.1.1", | ||
"gulp-mocha": "^6.0.0", | ||
"gulp-sourcemaps": "^2.6.0", | ||
"gulp-tslint": "^8.1.1", | ||
"gulp-typescript": "^5.0.1", | ||
"gulp-use-tsconfig": "^1.1.3", | ||
"mocha": "^6.0.2", | ||
"remap-istanbul": "^0.13.0", | ||
"ts-node": "^8.0.3", | ||
"tslint": "^5.14.0", | ||
"tslint-clean-code": "^0.2.7", | ||
"typescript": "^3.3.3333", | ||
"yarn": "^1.7.0" | ||
"chai": "^4.2.0", | ||
"del": "^5.1.0", | ||
"mocha": "6.2.2", | ||
"nss-run": "^0.3.0", | ||
"shelljs": "^0.8.3", | ||
"ts-node": "8.5.4", | ||
"tslint": "5.20.1", | ||
"tslint-clean-code": "^0.2.10", | ||
"typescript": "3.7.4" | ||
}, | ||
"dependencies": { | ||
"any-promise": "^1.3.0", | ||
"query-string-builder": "0.0.1", | ||
"request": "^2.79.0" | ||
"axios": "^0.19.0" | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"src/**/*.ts", | ||
"src/**/*.js" | ||
], | ||
"extension": [ | ||
".ts", | ||
".js" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"reporter": [ | ||
"text-summary", | ||
"html" | ||
], | ||
"sourceMap": true, | ||
"instrument": true | ||
} | ||
} |
109
Readme.md
Yet another node lib for requesting [Duck Duck Go](https://duckduckgo.com/). | ||
This one has been written in typescript (2.x or above) and can be used by javascript (es6 or above) software or by typescript software. | ||
This one has been written in typescript (2.x or above) and can be used by javascript (es2018 or above) software or by typescript software. | ||
## Examples | ||
* TypeScript: | ||
```javascript | ||
import { Requester } from '../../lib/'; | ||
// Result as default JSON | ||
async function get() { | ||
try { | ||
const requester = new Requester('node-duckduckgo-example'); | ||
const response = await requester.requestP('bart simpsons'); | ||
console.log(response.toJSON()); | ||
const result = await duckIt('bart simpsons'); | ||
console.log(result.data.AbstractText); | ||
} catch (err) { | ||
@@ -21,93 +17,30 @@ console.error('oups', err); | ||
``` | ||
* Javascript: | ||
```javascript | ||
const { Requester } = require('../../lib/'); | ||
async function get() { | ||
try { | ||
const requester = new Requester('node-duckduckgo-example'); | ||
const response = await requester.requestP('bart simpsons'); | ||
console.log(response.toJSON()); | ||
} catch (err) { | ||
console.error('oups', err); | ||
} | ||
} | ||
``` | ||
You can also use "old style" with callback: | ||
* TypeScript: | ||
```javascript | ||
import { Requester } from "node-duckduckgo"; | ||
const requester = new Requester("node-duckduckgo-example"); | ||
requester.request("bart simpsons", (err, response, body) => { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log(body); | ||
}); | ||
``` | ||
* Javascript: | ||
```javascript | ||
const { Requester } = require("node-duckduckgo"); | ||
const requester = new Requester("node-duckduckgo-example"); | ||
requester.request("bart simpsons", (err, response, body) => { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log(body); | ||
}); | ||
``` | ||
## API | ||
* The requester object takes an application name in parameter: | ||
* duckIt uses [axios](https://github.com/axios/axios) and return an AxiosResponse: | ||
```javascript | ||
const requester = new Requester(<YOUR APP NAME>); | ||
const result = await duckIt('bart simpsons'); | ||
``` | ||
* If needed, configure the requester options: | ||
```javascript | ||
requester.no_html = 1; | ||
requester.no_redirect = 1; | ||
requester.safe_search = safeSearch_off; // safeSearch_moderate or safeSearch_strict, default: safeSearch_strict; | ||
``` | ||
* duckIt can be called with an options object, eahc item is optional: | ||
* appName: an application name, default node-duckduckgo | ||
* format: received format data, possible values: 'json' or 'xml', default 'json' | ||
* parentalFilter: parental filter activated or not, possible values 'Activated', 'Moderate' and 'Deactivated', default: 'Activated' | ||
* noRedirect: boolean, true to skip HTTP redirects | ||
* noHtml: boolean, true to remove HTML from text | ||
* skipDisambig: boolean, true to skip disambiguation | ||
(for information about this options see [Duck Duck GO API documentation](https://api.duckduckgo.com/api) | ||
* Execute your request and get your result | ||
This lib is backed by [Request](https://www.npmjs.com/package/request). Results can be got by either by async call, callback or by stream: | ||
```javascript | ||
async function get() { | ||
try { | ||
const requester = new Requester('node-duckduckgo-example'); | ||
const response = await requester.requestP('bart simpsons'); | ||
console.log(response.toJSON()); | ||
} catch (err) { | ||
console.error('oups', err); | ||
} | ||
} | ||
example: | ||
```javascript | ||
const result = await duckIt('bart simpsons', { noHtml: true, parentalFilter: 'Moderate' }); | ||
``` | ||
```javascript | ||
requester.request("bart simpsons", (err, response, body) => { | ||
if (err) { | ||
console.log(err); | ||
return; | ||
} | ||
console.log(body); | ||
}); | ||
``` | ||
```javascript | ||
requester.request("bart simpsons") | ||
.on("data", (data) => { | ||
console.log(data.toString()); | ||
}) | ||
.on("error", (err) => { | ||
console.log(err); | ||
}); | ||
``` | ||
Response format is the [Duck Duck GO response](https://api.duckduckgo.com/api) | ||
## Usage | ||
node-duckduckgo is open sources (MIT license) but it doe grant you to use Duck Duck Go. [Please read this page](https://api.duckduckgo.com/api) before any usage. | ||
## 2.x | ||
This major release offers a completly different interface than 1.x. You may continue to use 1.X but | ||
as it won't evolve anymore, your advise to migrate to 2.X. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
11
6780
8
95
45
1
+ Addedaxios@^0.19.0
+ Addedaxios@0.19.2(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
- Removedany-promise@^1.3.0
- Removedquery-string-builder@0.0.1
- Removedrequest@^2.79.0
- Removedajv@6.12.6(transitive)
- Removedany-promise@1.3.0(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedquery-string-builder@0.0.1(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)