npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

think-fetch

v1.1.2

Published

Fetch for ThinkJS 3.x

Downloads

56

Readme

think-fetch

npm Build Status Coverage Status

Fetch for ThinkJS 3.x

Install

$ npm install think-fetch --save

How to use

config file src/config/extend.js

const fetch = require('think-fetch');

module.exports = [
  fetch, // HTTP request client.
];

Methods in Controller

module.exports = class extends think.Controller {
  async indexAction () {

    // plain text or html
    const text = await this.fetch('https://github.com/').then(res => res.text());

    // json
    const json = await this.fetch('https://api.github.com/repos/thinkjs/think-fetch').then(res => res.json());

    // post
    const body = await this.fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' }).then(res => res.json());

    // stream
    const res = await this.fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png');
    const dest = fs.createWriteStream('./octocat.png');
    res.body.pipe(dest);

    // post with stream from file
    const stream = fs.createReadStream('input.txt');
    const result = this.fetch('http://httpbin.org/post', { method: 'POST', body: stream }).then(res => res.json());
  }
}

API

fetch(url[, options])

  • url A string representing the URL for fetching
  • options Options for the HTTP(S) request
  • Returns: Promise<Response>

Perform an HTTP(S) fetch.

url should be an absolute url, such as http://example.com/. A path-relative URL (/file/under/root) or protocol-relative URL (//can-be-http-or-https.com/) will result in a rejected promise.

Options

The default values are shown after each option key.

{
  // These properties are part of the Fetch Standard
  method: 'GET',
  headers: {},        // request headers. format is the identical to that accepted by the Headers constructor (see below)
  body: null,         // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
  redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect

  // The following properties are node-fetch extensions
  follow: 20,         // maximum redirect count. 0 to not follow redirect
  timeout: 0,         // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
  compress: true,     // support gzip/deflate content encoding. false to disable
  size: 0,            // maximum response body size in bytes. 0 to disable
  agent: null         // http(s).Agent instance, allows custom proxy, certificate etc.
}
Default Headers

If no values are set, the following request headers will be sent automatically:

| Header | Value | | :------: | :------: | | Accept-Encoding | gzip,deflate (when options.compress === true) | | Accept | */* | | Connection | close (when no options.agent is present) | | Content-Length | (automatically calculated, if possible) | | User-Agent | node-fetch/1.0 (+https://github.com/bitinn/node-fetch) |

More Methods Click