smol-request

Tiny http/https request wrapper for Node.js 10 and newer

Usage no npm install needed!

<script type="module">
  import smolRequest from 'https://cdn.skypack.dev/smol-request';
</script>

README

Smol Request

npm version npm downloads install size

Small async request client for Node.js 10+ and newer with 0 dependencies.

Install

npm i 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)
      .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!')
    })
  })

Headers

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

request('https://picsum.photos/200/300', { responseType: 'headers' })
  .then(({ headers }) => {
    console.log('Picture location - ', headers.location)
  })

Example

There are a few examples in example folder in the repo.

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)

|Name|Type|Default|Description| |-|-|-|-| |params|Object|{}|URLSearchParams of request url.(Example: { q: 'my search query' } becomes -> http://myurl?q=my+search+query)| |responseType|String|text|One of these values: text, buffer, json, stream, headers |

RequestResult

|Name|Type|Description| |-|-|-| |data|ReadableStream | Object | String | Buffer | Null | Response data with choosen type from responseType .| |headers|Object| Response headers. | |status|Number| Reponse status. | |statusText|String| Response 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(). ( ‾ʖ̫‾)