@to1source/request

Super simple http package using native http.request to replace the popular request package for our own use

Usage no npm install needed!

<script type="module">
  import to1sourceRequest from 'https://cdn.skypack.dev/@to1source/request';
</script>

README

@to1source/request

Description

Super simple http package to replace the popular request npm for our own use

This is node.js ONLY package

Installation


$ npm i @to1source/request 

Instructions

The easiest way:

const request = require('@to1source/request')
// default it's a get 
request('http://example.com')
  .then(({result, res}) => {
    // do your thing 
  })
  .catch(err => {
    // when statusCode !== 200 
    // we are young, so we don't know how to handle 301 yet
    // it will be in the future release
  })

Using the main request will return a promise that resolve

  • result: whatever the server respond with, and we will try to parse it as JSON
  • res: the original respond object, you can further process it

Or you need to do post?


request('http://example.com', {method: 'post', formData: {id: 1}})
  .then(({result, res}) => {
    // do your thing 
  })
  .catch(err => {
    
  })

Short hands methods

If you don't care much about the processing, and just want a quick result. We recommend you to use the short methods intead.

Everthing short hand will only return the server respond result; this is different from using just the request call.

request.get

const request = require('@to1source/request')
request.get('http://example.com')
  .then(result => {
    // do your thing
  })

request.post

request.post('http://example.com', {id: 1})
  .then(result => {
    // do your thing 
  })

request.form

request.form('http://example.com', {id: 1})
  .then(result => {
    // do your thing 
  })

This method will set the header to multipart/form-data

@TODO file upload

request.jsonql

This is a special method purposely develop for our own jsonql framework

To include this one need to use a different path

const request = require('@to1source/request/jsonql')

request.jsonql('http://example.com', payload, config)
  .then(result => {
    // do our thing 
  })

Since this is a very special method develop as another part of our framework component, we are not going to go into detail here (it's much more complex that it looks). If you are interested. You can check out our jsonql website.


Contribution

  1. Fork the repository
  2. Create Feat_xxx branch
  3. Commit your code
  4. Create Pull Request

Gitee Feature

  1. You can use Readme_XXX.md to support different languages, such as Readme_en.md, Readme_zh.md
  2. Gitee blog blog.gitee.com
  3. Explore open source project https://gitee.com/explore
  4. The most valuable open source project GVP
  5. The manual of Gitee https://gitee.com/help
  6. The most popular members https://gitee.com/gitee-stars/

TO1SOURCE

NEWBRAN LTD