Skip to content

Commit a3217be

Browse files
committed
[BREAKING CHANGE] Remove preq from library
Remove preq from library entirely and replace with native fetch. This is not backwards compatible if third party users are a) using the parseAll function, and b) other features of preq, such as the internalUri parameter or wrapped errors; see https://github.com/wikimedia/preq for more details on how preq alters the Response object. Bug: T361602
1 parent a65b493 commit a3217be

File tree

5 files changed

+66
-742
lines changed

5 files changed

+66
-742
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ The scrape method used here invokes the parseAll() method, which uses all the av
4242
Promise-based:
4343
```js
4444
var cheerio = require('cheerio');
45-
var preq = require('preq'); // Promisified request library
4645
var parseDublinCore = require('html-metadata').parseDublinCore;
4746

4847
var url = "http://blog.woorank.com/2013/04/dublin-core-metadata-for-seo-and-usability/";
4948

50-
preq(url).then(function(response){
49+
fetch(url).then(function(response){
5150
$ = cheerio.load(response.body);
5251
return parseDublinCore($).then(function(metadata){
5352
console.log(metadata);

index.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Import modules
1212
*/
1313
const BBPromise = require( 'bluebird' );
1414
const cheerio = require( 'cheerio' );
15-
const preq = require( 'preq' ); // Promisified Request library
1615
const fs = BBPromise.promisifyAll( require( 'fs' ) );
1716

1817
const index = require( './lib/index.js' );
@@ -27,8 +26,27 @@ const index = require( './lib/index.js' );
2726
* @return {Object} BBPromise for metadata
2827
*/
2928
exports = module.exports = function ( urlOrOpts, callback ) {
30-
return preq.get( urlOrOpts
31-
).then( ( response ) => index.parseAll( cheerio.load( response.body ) ) ).nodeify( callback );
29+
let url, opts;
30+
if ( urlOrOpts instanceof Object ) {
31+
if ( urlOrOpts.uri ) {
32+
url = urlOrOpts.uri;
33+
}
34+
opts = urlOrOpts;
35+
} else if ( typeof urlOrOpts === String ) {
36+
url = urlOrOpts;
37+
}
38+
if ( !url ) {
39+
return BBPromise.reject( 'No uri supplied in argument' ).nodeify( callback );
40+
} else {
41+
return BBPromise.resolve(
42+
fetch( url, opts ).then( ( response ) => {
43+
// todo check if either urloropts still works with fetch
44+
return response.text().then( ( body ) => {
45+
return index.parseAll( cheerio.load( body ) );
46+
} );
47+
} )
48+
).nodeify( callback );
49+
}
3250
};
3351

3452
/**

0 commit comments

Comments
 (0)