Skip to content

Instantly share code, notes, and snippets.

@uxiew
Created February 26, 2023 10:58
Show Gist options
  • Select an option

  • Save uxiew/4b15dd41595fd70e30ef20a63dfb27fd to your computer and use it in GitHub Desktop.

Select an option

Save uxiew/4b15dd41595fd70e30ef20a63dfb27fd to your computer and use it in GitHub Desktop.
some code files
// Copyright (c) 2022 ChandlerVer5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import Cheerio, { CheerioAPI } from 'cheerio';
import tinyreq from 'tinyreq';
type Result = {
$: CheerioAPI;
res: Response;
body: BodyInit;
};
/**
* cheerioReq
* An http request module sending back a Cheerio object.
*
* You can change the `request` function by overriding the `request` field.
*
* @name cheerioReq
* @function
* @param {Object|String} opts The request url or an object passed to
* [`tinyrequest`](https://github.com/IonicaBizau/tinyreq).
* @param {Function} cb The callback function.
* @returns {Request} The [`tinyrequest`](https://github.com/IonicaBizau/tinyreq) object.
*/
export default function creq(opts): Promise<Result> {
return new Promise((resolve, reject) => {
tinyreq(
{
url: opts.url || opts,
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
...(typeof opts === 'object' && opts)
},
...(opts.url && opts)
},
(err, body, res) => {
if (err) {
return reject(err);
}
resolve({ $: Cheerio.load(body), res, body });
}
);
});
}
export { tinyreq as request };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment