@fatmatto/ptth
TypeScript icon, indicating that this package has built-in type declarations

1.6.0 • Public • Published

PTTH

Small, promise-based wrapper around node native http module


Hello!

I wanted a simple promise layer on top of native http/https nodejs modules

List of features

  • Promises
  • Simplicity

GET request example

const http = require('ptth')

http({
  method: 'GET',
  uri: 'http://example.org',
  query: {
    boolean: true,
    number:1,
    string: "Ciao"
  },
  headers: {
    'x-request-id': '121121212'
  }
})
  .then((resp) => {
    console.log(`Status code is ${resp.statusCode}`)
    console.log('Headers:', resp.headers)
    console.log('Body:', resp.body)
  })
  .catch(console.error)

POST request example

  http({
  method: 'POST',
  uri: 'http://example.org',
  body: {
    boolean: true,
    number:1,
    string: "Ciao"
  },
  headers: {
    'x-request-id': '121121212'
  }
})
  .then((resp) => {
    console.log(`Status code is ${resp.statusCode}`)
    console.log('Headers:', resp.headers)
    console.log('Body:', resp.body)
  })
  .catch(console.error)

Using Async/Await

  let response = await http({
  method: 'POST',
  uri: 'http://example.org',
  body: {
    boolean: true,
    number:1,
    string: "Ciao"
  },
  headers: {
    'x-request-id': '121121212'
  }
})

console.log(`Status code is ${response.statusCode}`)
console.log('Headers:', response.headers)
console.log('Body:', response.body)

Using a custom HTTP Agent

const https = require('https')

  let response = await http({
    agent: new https.Agent({
      rejectUnauthorized: false
    }),
    method: 'GET',
    uri: 'https://somesitewithabadSSLcertificate.com'
  })
}

console.log(`Status code is ${response.statusCode}`)
console.log('Headers:', response.headers)
console.log('Body:', response.body)

Params

Name Type Description Required Default
method String HTTP Method Yes -
uri String HTTP Uri Yes -
headers Object HTTP headers No -
query Object HTTP query object that builds the querystring No -
body Object HTTP body No -
pipeTo Stream Writable stream No -
json Boolean If set to false the response is not parsed as json No true

Interceptors

Request interceptors

  http.interceptors.request.use(config => {
    config.headers.foo = 'bar'
    return config
  })

Response interceptors

  http.interceptors.response.use(response => {
    // Do something with response
    return response
  })

Download & Installation

$ npm i @fatmatto/ptth

Contributing

Keep it simple. Keep it minimal. Don't put every single feature just because you can.

Authors or Acknowledgments

  • Mattia 'fat' Alfieri

License

This project is licensed under the MIT License

Readme

Keywords

Package Sidebar

Install

npm i @fatmatto/ptth

Weekly Downloads

145

Version

1.6.0

License

MIT

Unpacked Size

10.9 kB

Total Files

5

Last publish

Collaborators

  • fatmatto