1.6.0 • Published 4 months ago

@fatmatto/ptth v1.6.0

Weekly downloads
189
License
MIT
Repository
gitlab
Last release
4 months ago

PTTH

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

NameTypeDescriptionRequiredDefault
methodStringHTTP MethodYes-
uriStringHTTP UriYes-
headersObjectHTTP headersNo-
queryObjectHTTP query object that builds the querystringNo-
bodyObjectHTTP bodyNo-
pipeToStreamWritable streamNo-
jsonBooleanIf set to false the response is not parsed as jsonNotrue

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

1.6.0

4 months ago

1.5.7

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

4 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago