Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ejnshtein/smol-request

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ejnshtein/smol-request

Tiny http/https request wrapper for Node.js using ESM (13.5 and newer)

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-96.43%
Maintainers
1
Weekly downloads
 
Created
Source

Smol Request

npm version install size npm downloads

Small async request client for Node.js 13.5 and newer with 0 dependencies.
Mostly meant to be used as an http layer for some API library.

Install

npm i @ejnshtein/smol-request

Usage

JSON

import request from 'smol-request'

request('https://ghibliapi.herokuapp.com/films', { responseType: 'json' })
  .then(({ data }) => {
    console.log(`Studio Ghibli has ${response.data.length} movies out there!`)
  })

Text

request('https://bbc.com')
  .then(({ data }) => {
    //  bbc page is too big to log it to console, but we can save it to the drive!
    fs.promises.writeFile('./bbc.html', data) // we are using only Node 13.5 and newer and promises are stable here
      .then(() => {
        console.log('bbc page saved!')
      })
  })

Buffer

request('https://i.picsum.photos/id/1025/200/300.jpg', { responseType: 'buffer' })
  .then(({ data }) => {
    fs.promises.writeFile('./picture.jpg', data)
      .then(() => {
        console.log('picture saved!')
      })
  })

Stream

request('https://i.picsum.photos/id/1025/200/300.jpg', { responseType: 'stream' })
  .then(({ data }) => {
    const stream = fs.createWriteStream('./picture.jpg')

    data.pipe(stream)

    data.once('finish', () => {
      console.log('picture saved!')
    })
  })

OR you can get only headers without parsing body from request using responseType: 'headers'

API

request(url[, options[, formData]]): Promise<Response>

Types

RequestOptions

This client uses base http/https Node.js request client, so it inherits all options from it. (Description)

NameTypeDefaultDescription
paramsObject{}URLSearchParams of request url.(Example: { q: 'my search query' } becomes -> http://myurl?q=my+search+query)
responseTypeStringtextOne of these values: text, buffer, json, stream, headers

RequestResult

NameTypeDescription
dataReadableStream | Object | String | Buffer | NullResponse data with choosen type from responseType .
headersObjectResponse headers.
statusNumberReponse status.
statusTextStringResponse status text.

formData

If you are sending form data note that you can send form from FormData using it's method form.submit(path, err => {}). This option can be Object(then it will become string) or your custom property that will be written to request body with req.write(). ( ‾ʖ̫‾)

Keywords

FAQs

Package last updated on 28 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc