builtin-request
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

builtin-request

This package is a simple and tiny package to send request using node.js builtin features.
Send baseUrl in constructor is optional and you can send options.hostname instead of that

Some Examples:

Execute:

With this method you can run any type of request.

// An example of execute get request
(async () => {
  const options = {
    hostname: 'jsonplaceholder.typicode.com',
    port: 443,
    path: '/todos',
    method: 'GET'
  }
  const request = new Request()
  request
    .execute(options)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

// An example of execute post request 
(async () => {
  const options = {
    hostname: 'jsonplaceholder.typicode.com',
    port: 443,
    path: '/todos',
    method: 'POST',
  }
  const data = { title: 'Do something' }

  const request = new Request()
  request
    .execute(options, data)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Get:

This method is syntactic sugar of execute method. it's helping us to send get request more effective

(async () => {
  const request = new Request('jsonplaceholder.typicode.com')
  await request
    .get('/todos')
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Post:

This method is syntactic sugar of execute method. it's helping us to send post request more effective

(async () => {
  const data = { title: 'Do something' }

  const request = new Request('jsonplaceholder.typicode.com')
  request
    .post('/todos', data)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Put:

This method is syntactic sugar of execute method. it's helping us to send put request more effective

(async () => {
  const data = { title: 'Do something' }

  const request = new Request('jsonplaceholder.typicode.com')
  request
    .put('/todos/1', data)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Patch:

This method is syntactic sugar of execute method. it's helping us to send patch request more effective

(async () => {
  const data = { title: 'Do something' }

  const request = new Request('jsonplaceholder.typicode.com')
  request
    .patch('/todos/1', data)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Delete:

This method is syntactic sugar of execute method. it's helping us to send delete request more effective

(async () => {
  const data = { title: 'Do something' }

  const request = new Request('jsonplaceholder.typicode.com')
  request
    .delete('/todos/1', data)
    .then((response) => console.log({ response }))
    .catch((error) => console.log({ error }))
})()

Package Sidebar

Install

npm i builtin-request

Weekly Downloads

2

Version

1.1.4

License

MIT

Unpacked Size

9.61 kB

Total Files

7

Last publish

Collaborators

  • kasra.karami.kk