1.2.16 • Published 4 months ago

rabbit-sdk v1.2.16

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

rabbit 封装的 ajax 访问插件

Install

$ npm install rabbit-sdk  --save

Usage

const rabbit = require('rabbit-sdk')
// Make a request for a user with a given ID
rabbit
  .get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response)
  })
  .catch(function (error) {
    // handle error
    console.log(error)
  })
  .finally(function () {
    // always executed
  })

// Optionally the request above could also be done as
rabbit
  .get('/user', {
    params: {
      ID: 12345,
    },
  })
  .then(function (response) {
    console.log(response)
  })
  .catch(function (error) {
    console.log(error)
  })
  .finally(function () {
    // always executed
  })

Performing a POST request

rabbit
  .post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone',
  })
  .then(function (response) {
    console.log(response)
  })
  .catch(function (error) {
    console.log(error)
  })

rabbit
  .post(
    '/user',
    {
      firstName: 'Fred',
      lastName: 'Flintstone',
    },
    {
      params: {
        ID: 12345,
      },
    }
  )
  .then(function (response) {
    console.log(response)
  })
  .catch(function (error) {
    console.log(error)
  })

rabbit API

Requests can be made by passing the relevant config to rabbit.

rabbit(config)
// Send a POST request
rabbit.ajax({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone',
  },
})
// GET request for remote image
rabbit
  .ajax({
    method: 'get',
    url: 'http://bit.ly/2mTM3nY',
    responseType: 'stream',
  })
  .then(function (response) {
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
  })

For convenience aliases have been provided for all supported request methods.

rabbit.ajax(config)
rabbit.get(url, config)
rabbit.delete(url, config)
rabbit.head(url, config)
rabbit.options(url, config)
rabbit.post(url[, data, config])
rabbit.put(url[, data, config])
rabbit.patch(url[, data, config])

Request Config

These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not specified.

{
  // `url` is the server URL that will be used for the request
  url: '/user',

  // `method` is the request method to be used when making the request
  method: 'get', // default


  // `headers` are custom headers to be sent
  headers: {'X-Requested-With': 'XMLHttpRequest'},

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },

//contentType
 contentType:"application/json; charset=UTF-8",// default

  //async
  async:ture,// default

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  },

  // syntax alternative to send data into the body
  // method post
  // only the value is sent, not the key
  data: 'Country=Brasil&City=Belo Horizonte',

  // `timeout` specifies the number of milliseconds before the request times out.
  // If the request takes longer than `timeout`, the request will be aborted.
  timeout: 1000, // default is `0` (no timeout)


  // `dataType` indicates the type of data that the server will respond with
  // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
  //   browser only: 'blob'
  dataType: 'json', // default

    //Method executed after successful operation
  success:function (res, status, errCode) {

    },
    //Method to be executed after running failure
  error:function (res, status, errCode) {
    },
    //The method executed after the operation is completed
  complete:function (xdr,status, errCode) {
    },
}

License

更新记录

1 添加Data对象操作,路径是相对于整个服务的;与Resource的区别在与,Resource是定制出来的服务,相对于Resource目录的,Data比Resource调用时的相对路径要多写一级路径。

MIT
1.2.16

4 months ago

1.2.15

4 months ago

1.2.14

4 months ago

1.2.8

6 months ago

1.2.7

6 months ago

1.2.6

6 months ago

1.2.10

6 months ago

1.2.11

6 months ago

1.2.9

6 months ago

1.2.5

9 months ago

1.2.4

12 months ago

1.2.3

1 year ago

1.2.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.34

1 year ago

1.1.33

1 year ago

1.1.38

1 year ago

1.1.37

1 year ago

1.1.36

1 year ago

1.1.35

1 year ago

1.1.41

1 year ago

1.1.40

1 year ago

1.1.29

1 year ago

1.1.30

1 year ago

1.1.32

1 year ago

1.1.31

1 year ago

1.1.28

2 years ago

1.1.27

2 years ago

1.1.26

2 years ago

1.1.25

2 years ago

1.1.23

2 years ago

1.1.24

2 years ago

1.1.22

3 years ago

1.1.19

3 years ago

1.1.18

3 years ago

1.1.21

3 years ago

1.1.20

3 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.9

3 years ago

1.1.10

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.0.10

3 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.2

4 years ago

1.0.4

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago