1.9.5 • Published 5 years ago

request-watcher v1.9.5

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

request-watcher

A tool that forwards network requests

npm.io npm.io

Quick Start for using axios

install packages

$ npm install -g request-watcher-server
$ npm install -D request-watcher
$ npm install -D request-watcher-axios

start server

$ rws
$ open http://localhost:2333

inject watcher code

const axios = require('axios')
const watcher = require('request-watcher')
const axiosWatcher = require('request-watcher-axios')(axios)
watcher.use(axiosWatcher)

Quick Start for using fetch

install packages

$ npm install -g request-watcher-server
$ npm install -D request-watcher
$ npm install -D request-watcher-fetch

start server

$ rws
$ open http://localhost:2333

inject watcher code

const watcher = require('request-watcher')
const fetchWatcher = require('request-watcher-fetch')(fetch)
watcher.use(fetchWatcher)

Install

$ npm install -g request-watcher-server
$ npm install -D request-watcher

Usage

Start Server

The server requires node v7.6.0 or higher for ES2015 and async function support

Once you globally installed the request-watcher-server, you will get a global cmd request-watcher-server and also rws for short.

And then you can use the following cmd:

$ rws [-a 0.0.0.0] [-p 2333]

default address is 0.0.0.0:2333 you can use rws -h to get more help

Watch Request

const watcher = require('request-watcher')
const watcherParams = {
  username: 'username',
  appname: 'appname',
  labels: [],
}

// e.g.
// before send a request, emit the request to request-watcher-server
const { emitReq, emitRes } = watcher(watcherParams)
const requestParams = { url, params, headers, method: 'POST' }
emitReq(requestParams)
axios.post(url, params, headers)
  .then(res => {
    // after get the response, emit the response to request-watcher-server
    const { status, data, headers } = res
    const responseParams = { status, data, headers }
    emitRes(responseParams)
    // your biz code bellow
  })
  .catch(err => {
  })

Note that each time you watch a request, you need to use the watcher function to regenerate the matching emitReq and emitRes

watcherParams

watcherParams is an Object Containing the following properties

ArgsTypeDescription
originString (optional)watcher origin
usernameString (required)one of the request markers
appnameString (required)one of the request markers
labels[String] (optional)add extra label to request to differentiate

requestParams

requestParams is an Object Containing the following properties

ArgsTypeDescription
urlString (optional)request url
methodString (optional)request method
headersObject (optional)request headers
paramsObject (optional)request params in body

responseParams

responseParams is an Object Containing the following properties

ArgsTypeDescription
statusInteger (optional)response status
headersObject (optional)response headers
dataObject (optional)response data

Watch Logger

You can just emit a log to the server, like bellow:

const { emitLog } = watcher(watcherParams)

const loggerParams = { title: 'logger', content: 'this is a log' }
emitLog(loggerParams)

Notice! everytime you emit a logger need to regenerate a new emitLog function. It's Inconvenient(indeed it's for plugin using), thus we supply a more convenient way to do same thing:

// Only define once
const watcherParams = {labels: ['log']}
const logger = watcher.logger({watcherParams})

// use everywhere
logger('logger title', 'logger content')
// or
logger('logger content')

loggerParams

loggerParams is an Object Containing the following properties

ArgsTypeDescription
titleString (optional)logger title
contentAny (optional)logger content

Global Config

You can use watcher.global to define global params, and thus you can just use watcher() without passing params.

watcher.global.origin = 'http://127.0.0.1:8080' // default is 'http://0.0.0.0:2333'
watcher.global.username = 'request-watcher' // default is 'username'
watcher.global.appname = 'request-watcher-app' // default is 'appname'

const { emitReq, emitRes } = watcher()
const { emitLog } = watcher()

Plugins

We support plugin to simplify the configs. And now we have these plugins:

Plugin Usage

Using axios for example:

const watcher = require('request-watcher')
watcher.use(require('request-watcher-axios')(axios))

And then, you can just focus on your biz code without inserting redundant code before or after the ajaxing code.

Eggs

  • Clicking the Console button outputs the data to the browser's console and obtains a global variable $it pointing to that data.

Example

Refer to this example

Enjoy! :)

1.9.5

5 years ago

1.9.4

5 years ago

1.9.3

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.2

6 years ago

1.8.1

6 years ago

1.8.0

6 years ago

1.7.8

6 years ago

1.7.7

6 years ago

1.7.6

6 years ago

1.7.5

7 years ago

1.7.4

7 years ago

1.7.3

7 years ago

1.7.2

7 years ago

1.7.0

7 years ago

1.7.1

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.5.8

7 years ago

1.4.8

7 years ago

1.4.7

7 years ago

1.4.6

7 years ago

1.4.5

7 years ago

1.4.4

7 years ago

1.4.3

7 years ago

1.4.2

7 years ago

1.4.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago