0.4.35 ā€¢ Published 1 month ago

@wooksjs/http-proxy v0.4.35

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Wooks Proxy

!!! This is work-in-progress library, breaking changes are expected !!!

Wooks Proxy is composable proxy for @wooksjs/event-http

šŸ”„ An easy way to proxy request!

Installation

npm install @wooksjs/http-proxy

Usage

import { useProxy } from '@wooksjs/http-proxy'
app.get('/to-proxy', () => {
  const proxy = useProxy()
  return proxy('https://target-website.com/target-path?query=123')
})

Restrict cookies/headers to pass

import { useProxy } from '@wooksjs/http-proxy'
app.get('/to-proxy', () => {
  const proxy = useProxy()
  return proxy('https://target-website.com/target-path?query=123', {
    reqHeaders: { block: ['referer'] }, // block referer header
    reqCookies: { block: '*' }, // block all req cookies
  })
})

Change response

It's easy as proxy returns fetch response

import { useProxy } from '@wooksjs/http-proxy'
app.get('/to-proxy', async () => {
  const proxy = useProxy()
  const response = proxy('https://mayapi.com/json-api')
  const data = { ...(await response.json()), newField: 'new value' }
  return data
})

Proxy advanced options

import { useProxy } from '@wooksjs/http-proxy'
import { useRequest } from '@wooksjs/composables'
//...
app.get('*', async () => {
  const proxy = useProxy()
  const { url } = useRequest()
  const fetchResponse = await proxy('https://www.google.com' + url, {
    // optional method, be default is set with
    // the original request method
    method: 'GET',

    // the next four options help to filter out
    // request/response headers/cookies
    // each of the option accepts an object with:
    // - allow: '*' | (string | RegExp)[] - a list to allow (default '*')
    // - block: '*' | (string | RegExp)[] - a list to block
    // - overwrite: Record<string| string> | ((data: object) -> object) - object or fn to overwrite data
    reqHeaders: { block: ['referer'] },
    reqCookies: { allow: ['cookie-to-pass-upstream'] },
    resHeaders: { overwrite: { 'x-proxied-by': 'wooks-proxy' } },
    resCookies: { allow: ['cookie-to-pass-downstream'] },

    // debug: true - will print proxy paths and headers/cookies
    debug: true,
  })
  return fetchResponse // fetch response is supported, the body will be downstreamed

  // > you can also return fully buffered body as Uint8Array
  // return new Uint8Array(await fetchResponse.arrayBuffer())

  // > or as string
  // return fetchResponse.text()

  // > or change response before return
  // const data = await fetchResponse.text() + '<new data>'
  // return data
})
//...

Documentation

To check out docs, visit wooks.moost.org.

0.4.35

1 month ago

0.4.33

1 month ago

0.4.34

1 month ago

0.4.31

1 month ago

0.4.32

1 month ago

0.4.30

2 months ago

0.4.28

3 months ago

0.4.29

3 months ago

0.4.27

3 months ago

0.4.26

3 months ago

0.4.24

4 months ago

0.4.23

4 months ago

0.4.22

4 months ago

0.4.20

4 months ago

0.4.21

4 months ago

0.4.19

4 months ago

0.4.18

4 months ago

0.4.17

4 months ago

0.4.15

4 months ago

0.4.16

4 months ago

0.4.14

4 months ago

0.4.13

5 months ago

0.4.12

5 months ago

0.4.10

5 months ago

0.4.11

5 months ago

0.4.9

6 months ago

0.4.8

7 months ago

0.3.8

10 months ago

0.3.7

10 months ago

0.3.9

10 months ago

0.3.12

10 months ago

0.3.11

10 months ago

0.3.10

10 months ago

0.4.5

7 months ago

0.4.4

7 months ago

0.4.7

7 months ago

0.4.6

7 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.4.3

7 months ago

0.4.2

9 months ago

0.2.23

12 months ago

0.2.22

12 months ago

0.2.21

12 months ago

0.2.20

12 months ago

0.2.19

12 months ago

0.3.0

12 months ago

0.3.6

11 months ago

0.3.5

11 months ago

0.3.2

12 months ago

0.3.1

12 months ago

0.3.4

11 months ago

0.3.3

12 months ago

0.2.16

1 year ago

0.2.15

1 year ago

0.2.14

1 year ago

0.2.18

1 year ago

0.2.17

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.1.0

1 year ago