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