GithubHelp home page GithubHelp logo

isabella232 / teeny-request Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googleapis/teeny-request

0.0 0.0 0.0 302 KB

Like request, but tiny. Fetch under the hood.

License: Apache License 2.0

TypeScript 93.58% JavaScript 5.80% Python 0.61%

teeny-request's Introduction

Build Status

teeny-request

Like request, but much smaller - and with less options. Uses node-fetch under the hood. Pop it in where you would use request. Improves load and parse time of modules.

const request = require('teeny-request').teenyRequest;

request({uri: 'http://ip.jsontest.com/'}, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the JSON.
});

For TypeScript, you can use @types/request.

import {teenyRequest as request} from 'teeny-request';
import r as * from 'request'; // Only for type declarations

request({uri: 'http://ip.jsontest.com/'}, (error: any, response: r.Response, body: any) => {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the JSON.
});

teenyRequest(options, callback)

Options are limited to the following

  • uri
  • method, default GET
  • headers
  • json
  • qs
  • useQuerystring
  • timeout in ms
  • gzip
  • proxy
request({uri:'http://service.com/upload', method:'POST', json: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })

The callback argument gets 3 arguments:

  • An error when applicable (usually from http.ClientRequest object)
  • An response object with statusCode, a statusMessage, and a body
  • The third is the response body (JSON object)

defaults(options)

Set default options for every teenyRequest call.

let defaultRequest = teenyRequest.defaults({timeout: 60000});
      defaultRequest({uri: 'http://ip.jsontest.com/'}, function (error, response, body) {
            assert.ifError(error);
            assert.strictEqual(response.statusCode, 200);
            console.log(body.ip);
            assert.notEqual(body.ip, null);
            
            done();
        });

Proxy environment variables

If environment variables HTTP_PROXY or HTTPS_PROXY are set, they are respected. NO_PROXY is currently not implemented.

Building with Webpack 4+

Since 4.0.0, Webpack uses javascript/esm for .mjs files which handles ESM more strictly compared to javascript/auto. If you get the error Can't import the named export 'PassThrough' from non EcmaScript module, please add the following to your Webpack config:

{
    test: /\.mjs$/,
    type: 'javascript/auto',
},

Motivation

request has a ton of options and features and is accordingly large. Requiering a module incurs load and parse time. For request, that is around 600ms.

Load time of request measured with require-so-slow

teeny-request doesn't have any of the bells and whistles that request has, but is so much faster to load. If startup time is an issue and you don't need much beyong a basic GET and POST, you can use teeny-request.

Thanks

Special thanks to billyjacobson for suggesting the name. Please report all bugs to them. Just kidding. Please open issues.

teeny-request's People

Contributors

alexander-fenster avatar bcoe avatar callmehiphop avatar davidkhala avatar fhinkel avatar goatandsheep avatar jkwlui avatar justinbeckwith avatar kjin avatar release-please[bot] avatar renovate-bot avatar renovate[bot] avatar stephenplusplus avatar summer-ji-eng avatar yoshi-automation avatar zamnuts avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.