1.25.2 • Published 3 years ago

@tuplo/fletch v1.25.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

fletch

HTTP request library, focused on web scraping.

Usage

import fletch from '@tuplo/fletch';

const $page = await fletch.html('https://foo.com/page.html');
const heading = $page.find('body > h1');

const { foo } = await fletch.json('https://foo.com/page.html');

const { foo } = await fletch.script('https://foo.com/page.html', {
  scriptPath: 'script:nth-of-type(3)',
});

const [jsonld] = await fletch.jsonld('https://foo.com/page.html)

Options

OptionDescriptionDefault
cacheCaches requests in memoryfalse
delayIntroduce a delay before the request (ms)1_000
formDataObject with key/value pairs to send as form data
encodingThe encoding used by the source page, will be converted to UTF8
headersA simple multi-map of names to values
jsonDataObject with key/value pairs to send as json data
logShould log all request URLS to stderrfalse
proxyProxy configuration (host, port, username, password)
retryRetries failed responsesasync-retry
scriptFindFnA function to find a script element on the page, execute and return it
scriptPathA CSS selector to pick a script element on the page, execute and return it
scriptSandboxAn object to use as base on an execution of a piece of code found on the page
urlSearchParamsA key-value object listing what parameters to add to the query string of url
validateStatusA function to decide if the response status is an error and should be thrown

API

fletch(url: string, options?: FletchOptions) => http.Response

Generic utility to return a HTTP Response

fletch.html(url: string, options?: FletchOptions) => cheerio.Cheerio

Requests a HTTP resource, parses it using Cheerio and returns its

const $page = await fletch.html('https://foo.com/page.html');
const heading = $page.find('body > h1');

fletch.script<T>(url: string, options?: FletchOptions) => T

Requests a HTTP resource, finds a script on it, executes and returns its global context.

const { foo } = await fletch.script('https://foo.com/page.html', {
  scriptPath: 'script:nth-of-type(3)',
});

fletch.text(url: string, options?: FletchOptions) => string

Requests a HTTP resource, returning it as a string

fletch.json<T>(url: string, options?: FletchOptions) => T

Requests a HTTP resource, returning it as a JSON object

fletch.jsonld(url: string, options?: FletchOptions) => unknown[]

Requests a HTTP resource, retrieving all the JSON-LD blocks found on the document

fletch.create(options: FletchOptions) => Object

Creates a new instance of fletch with a custom config

const instance = fletch.create({ headers: { foo: 'bar' } });
await instance.json('http://foo.com');

Install

$ npm install @tuplo/fletch

# or with yarn
$ yarn add @tuplo/fletch

Contribute

Contributions are always welcome!

License

The MIT License (MIT)

Copyright (c) 2020 - 2021 Tuplo.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.25.2

3 years ago

1.25.1

3 years ago

1.25.0

3 years ago

1.24.0

3 years ago

1.23.1

3 years ago

1.23.0

3 years ago

1.22.0

3 years ago

1.21.1

3 years ago

1.21.0

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.0

3 years ago

1.17.0

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.16.0

3 years ago

1.12.0

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.2.0

3 years ago

1.3.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago