1 |
|
2 |
|
3 | /**
|
4 | * Module dependencies
|
5 | */
|
6 | const fetch = require('node-fetch')
|
7 | const cheerio = require('cheerio')
|
8 |
|
9 | module.exports = function (url, opts) {
|
10 | if (typeof url !== 'string') {
|
11 | throw new TypeError('Expected a string')
|
12 | }
|
13 |
|
14 | opts = Object.assign({
|
15 | // keep the original unicode chars
|
16 | decodeEntities: false
|
17 | }, opts)
|
18 |
|
19 | return fetch(url)
|
20 | .then(data => {
|
21 | if (data.status !== 200) {
|
22 | return Promise.reject(new Error(data.statusText))
|
23 | }
|
24 | return data.text()
|
25 | })
|
26 | .then(data => cheerio.load(data, opts))
|
27 | }
|