egg-http-proxy-plus 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,33 +1,38 @@
1
+ <div style="text-align: center;" align="center">
2
+
1
3
  # egg-http-proxy-plus
2
- fork from egg-http-proxy
3
4
 
5
+ </div>
6
+
7
+ <div style="text-align: center;" align="center">
8
+
9
+ 支持转发文件上传接口,支持自定义匹配方法,ctx 透传
10
+
11
+ </div>
12
+
13
+ <div style="text-align: center;" align="center">
4
14
 
5
15
  [![NPM version][npm-image]][npm-url]
16
+ [![Codacy Badge][codacy-image]][codacy-url]
6
17
  [![build status][travis-image]][travis-url]
7
18
  [![Test coverage][codecov-image]][codecov-url]
8
- [![David deps][david-image]][david-url]
9
- [![Known Vulnerabilities][snyk-image]][snyk-url]
10
19
  [![npm download][download-image]][download-url]
20
+ [![License][license-image]][license-url]
11
21
 
12
- [npm-image]: https://img.shields.io/npm/v/egg-http-proxy-plus.svg?style=flat-square
13
- [npm-url]: https://npmjs.org/package/egg-http-proxy-plus
14
- [travis-image]: https://travis-ci.org/saqqdy/egg-http-proxy-plus.svg?branch=master
15
- [travis-url]: https://travis-ci.org/saqqdy/egg-http-proxy-plus
16
- [codecov-image]: https://img.shields.io/codecov/c/github/saqqdy/egg-http-proxy-plus.svg?style=flat-square
17
- [codecov-url]: https://codecov.io/github/saqqdy/egg-http-proxy-plus?branch=master
18
- [david-image]: https://img.shields.io/david/saqqdy/egg-http-proxy-plus.svg?style=flat-square
19
- [david-url]: https://david-dm.org/saqqdy/egg-http-proxy-plus
20
- [snyk-image]: https://snyk.io/test/npm/egg-http-proxy-plus/badge.svg?style=flat-square
21
- [snyk-url]: https://snyk.io/test/npm/egg-http-proxy-plus
22
- [download-image]: https://img.shields.io/npm/dm/egg-http-proxy-plus.svg?style=flat-square
23
- [download-url]: https://npmjs.org/package/egg-http-proxy-plus
22
+ [![Sonar][sonar-image]][sonar-url]
23
+
24
+ </div>
24
25
 
25
26
  Configure proxy middleware for egg. Use [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware).
26
27
 
27
28
  ## Install
28
29
 
29
30
  ```bash
31
+ # use npm
30
32
  $ npm i egg-http-proxy-plus --save
33
+
34
+ # use yarn
35
+ $ yarn add egg-http-proxy-plus
31
36
  ```
32
37
 
33
38
  ## Usage
@@ -36,22 +41,29 @@ $ npm i egg-http-proxy-plus --save
36
41
  // {app_root}/config/plugin.js
37
42
  exports.httpProxyPlus = {
38
43
  enable: true,
39
- package: 'egg-http-proxy-plus',
40
- };
44
+ package: 'egg-http-proxy-plus'
45
+ }
41
46
  ```
42
47
 
43
48
  ## Configuration
44
49
 
45
- Proxy `/api` requests to `http://www.example.org`:
50
+ ### Proxy `/api` requests to `http://www.example.org`
46
51
 
47
52
  ```js
48
53
  // {app_root}/config/config.default.js
49
54
  exports.httpProxyPlus = {
50
55
  '/api': 'http://www.example.org'
51
- };
56
+ }
57
+ // or
58
+ exports.httpProxyPlus = [
59
+ {
60
+ origin: '/api',
61
+ options: 'http://www.example.org'
62
+ }
63
+ ]
52
64
  ```
53
65
 
54
- A request to `/api/users` will now proxy the request to `http://www.example.org/api/users`.
66
+ #### A request to `/api/users` will now proxy the request to `http://www.example.org/api/users`
55
67
 
56
68
  If you don't want `/api` to be passed along, we need to rewrite the path:
57
69
 
@@ -60,9 +72,55 @@ If you don't want `/api` to be passed along, we need to rewrite the path:
60
72
  exports.httpProxyPlus = {
61
73
  '/api': {
62
74
  target: 'http://www.example.org',
63
- pathRewrite: {'^/api' : ''}
75
+ pathRewrite: { '^/api': '' }
64
76
  }
65
- };
77
+ }
78
+ // or
79
+ exports.httpProxyPlus = [
80
+ {
81
+ origin: '/api',
82
+ options: {
83
+ target: 'http://www.example.org',
84
+ pathRewrite: { '^/api': '' }
85
+ // ...
86
+ }
87
+ }
88
+ ]
89
+ ```
90
+
91
+ #### custom matching
92
+
93
+ For full control you can provide a custom function to determine which requests should be proxied or not.
94
+
95
+ ```js
96
+ // {app_root}/config/config.default.js
97
+ exports.httpProxyPlus = [
98
+ {
99
+ origin(pathname, req) {
100
+ return pathname.match('^/api') && req.method === 'GET'
101
+ },
102
+ options: {}
103
+ }
104
+ ]
105
+ ```
106
+
107
+ #### http-proxy events
108
+
109
+ Pay attention to the fourth parameter, the plug-in transparently transmits the ctx context
110
+
111
+ ```js
112
+ // {app_root}/config/config.default.js
113
+ exports.httpProxyPlus = {
114
+ '/api': {
115
+ target: 'http://www.example.org',
116
+ onProxyReq(proxyReq, req, res, ctx) {
117
+ if (req.method.toLowerCase() === 'post') {
118
+ const token = ctx.cookies.get('access_token')
119
+ token && proxyReq.setHeader('authorization', token)
120
+ }
121
+ }
122
+ }
123
+ }
66
124
  ```
67
125
 
68
126
  For more advanced usages, checkout [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware#options) options documentation.
@@ -74,3 +132,20 @@ Please open an issue [here](https://github.com/saqqdy/egg-http-proxy-plus/issues
74
132
  ## License
75
133
 
76
134
  [MIT](LICENSE)
135
+
136
+
137
+ [npm-image]: https://img.shields.io/npm/v/egg-http-proxy-plus.svg?style=flat-square
138
+ [npm-url]: https://npmjs.org/package/egg-http-proxy-plus
139
+ [codacy-image]: https://app.codacy.com/project/badge/Grade/f70d4880e4ad4f40aa970eb9ee9d0696
140
+ [codacy-url]: https://www.codacy.com/gh/saqqdy/egg-http-proxy-plus/dashboard?utm_source=github.com&utm_medium=referral&utm_content=saqqdy/egg-http-proxy-plus&utm_campaign=Badge_Grade
141
+ [travis-image]: https://travis-ci.com/saqqdy/egg-http-proxy-plus.svg?branch=master
142
+ [travis-url]: https://travis-ci.com/saqqdy/egg-http-proxy-plus
143
+ [codecov-image]: https://img.shields.io/codecov/c/github/saqqdy/egg-http-proxy-plus.svg?style=flat-square
144
+ [codecov-url]: https://codecov.io/github/saqqdy/egg-http-proxy-plus?branch=master
145
+ [download-image]: https://img.shields.io/npm/dm/egg-http-proxy-plus.svg?style=flat-square
146
+ [download-url]: https://npmjs.org/package/egg-http-proxy-plus
147
+ [license-image]: https://img.shields.io/badge/License-MIT-blue.svg
148
+ [license-url]: LICENSE
149
+ [sonar-image]: https://sonarcloud.io/api/project_badges/quality_gate?project=saqqdy_egg-http-proxy-plus
150
+ [sonar-url]: https://sonarcloud.io/dashboard?id=saqqdy_egg-http-proxy-plus
151
+
@@ -1,62 +1,55 @@
1
- const proxy = require('http-proxy-middleware')
2
- const c2k = require('koa2-connect')
1
+ const { createProxyMiddleware } = require('http-proxy-middleware')
2
+ const c2k = require('koa-connect')
3
3
  const pathMatching = require('egg-path-matching')
4
- const getType = obj => {
5
- const type = {
6
- '[object Array]': 'array',
7
- '[object Boolean]': 'boolean',
8
- '[object Date]': 'date',
9
- '[object Function]': 'function',
10
- '[object Number]': 'number',
11
- '[object Object]': 'object',
12
- '[object RegExp]': 'regexp',
13
- '[object String]': 'string'
14
- }
15
- if (obj === null) return obj + ''
16
- return typeof obj === 'object' || typeof obj === 'function' ? type[Object.prototype.toString.call(obj)] || 'object' : typeof obj
17
- }
4
+ const { getType } = require('js-cool')
5
+
18
6
  module.exports = options => {
19
- return async function httpProxyPlus(ctx, next) {
7
+ return async (ctx, next) => {
20
8
  const path = ctx.request.originalUrl || ctx.request.url
21
9
  const optType = getType(options)
22
- if (optType === 'array') {
23
- options.forEach(context => {
24
- let proxyOptions = context.options || {}
25
- if (typeof proxyOptions === 'string') proxyOptions = { target: proxyOptions }
26
- const { onProxyReq = null, onProxyRes = null } = Object.assign({}, proxyOptions)
27
- if (onProxyReq) proxyOptions.onProxyReq = (...args) => onProxyReq.call(null, ctx, ...args)
28
- if (onProxyRes) proxyOptions.onProxyRes = (...args) => onProxyRes.call(null, ctx, ...args)
29
- if (getType(context.origin) === 'function') {
30
- // custom matching
31
- const isMatch = context.origin(path.split('?')[0], ctx.req)
32
- isMatch && c2k(proxy(context.origin, proxyOptions))(ctx, next)
33
- return isMatch
10
+ if (optType === 'object') {
11
+ options = Object.keys(options).map(context => ({
12
+ origin: context,
13
+ options: options[context]
14
+ }))
15
+ }
16
+ for (const context of options) {
17
+ let proxyOptions = context.options || {},
18
+ isMatch
19
+ if (typeof proxyOptions === 'string') proxyOptions = { target: proxyOptions }
20
+ const { onProxyReq = null, onProxyRes = null } = proxyOptions
21
+ proxyOptions = {
22
+ ...proxyOptions,
23
+ // eslint-disable-next-line no-unused-vars
24
+ onProxyReq(proxyReq, req, res, context = ctx) {
25
+ if (onProxyReq && typeof onProxyReq === 'function') {
26
+ onProxyReq(proxyReq, req, res, ctx)
27
+ }
28
+ const { rawBody, body: requestBody } = ctx.request
29
+ if (requestBody && rawBody) {
30
+ proxyReq.setHeader('Content-Length', Buffer.byteLength(rawBody))
31
+ proxyReq.write(rawBody)
32
+ proxyReq.end()
33
+ }
34
+ return proxyReq
35
+ },
36
+ // eslint-disable-next-line no-unused-vars
37
+ onProxyRes(proxyRes, req, res, context = ctx) {
38
+ if (onProxyRes && typeof onProxyRes === 'function') {
39
+ onProxyRes(proxyRes, req, res, ctx)
40
+ }
41
+ return proxyRes
34
42
  }
43
+ }
44
+ if (getType(context.origin) === 'function') {
45
+ // custom matching
46
+ isMatch = context.origin(path.split('?')[0], ctx.req)
47
+ } else {
35
48
  // context.origin配置的数组、字符串
36
49
  const match = pathMatching({ match: context.origin })
37
- const isMatch = match({ path })
38
- if (isMatch) {
39
- c2k(proxy(context.origin, proxyOptions))(ctx, next)
40
- }
41
- return isMatch
42
- })
43
- } else if (optType === 'object') {
44
- ;[('enable', 'match', 'ignore')].forEach(el => {
45
- if (options.hasOwnProperty(el)) delete options[el]
46
- })
47
- Object.keys(options).some(async context => {
48
- const match = pathMatching({ match: context })
49
- const isMatch = match({ path })
50
- if (isMatch) {
51
- let proxyOptions = options[context]
52
- if (typeof proxyOptions === 'string') proxyOptions = { target: proxyOptions }
53
- const { onProxyReq = null, onProxyRes = null } = Object.assign({}, proxyOptions)
54
- if (onProxyReq) proxyOptions.onProxyReq = (...args) => onProxyReq.call(null, ctx, ...args)
55
- if (onProxyRes) proxyOptions.onProxyRes = (...args) => onProxyRes.call(null, ctx, ...args)
56
- await c2k(proxy(context, proxyOptions))(ctx, next)
57
- }
58
- return isMatch
59
- })
50
+ isMatch = match({ path })
51
+ }
52
+ isMatch && (await c2k(createProxyMiddleware(context.origin, proxyOptions))(ctx, next))
60
53
  }
61
54
  await next()
62
55
  }
package/package.json CHANGED
@@ -1,67 +1,107 @@
1
1
  {
2
2
  "name": "egg-http-proxy-plus",
3
- "version": "1.0.2",
4
3
  "description": "powerfull proxy middleware plugin for egg",
4
+ "version": "2.0.0",
5
+ "packageManager": "pnpm@8.5.1",
5
6
  "eggPlugin": {
6
7
  "name": "httpProxyPlus"
7
8
  },
8
- "keywords": [
9
- "egg",
10
- "eggPlugin",
11
- "egg-plugin",
12
- "proxy",
13
- "http-proxy",
14
- "http-proxy-plus",
15
- "http-proxy-middleware"
9
+ "files": [
10
+ "app",
11
+ "config",
12
+ "app.js",
13
+ "index.d.ts"
16
14
  ],
15
+ "scripts": {
16
+ "test": "pnpm run lint -- --fix && egg-bin pkgfiles && pnpm run test-local",
17
+ "test-local": "egg-bin test",
18
+ "cov": "egg-bin cov",
19
+ "lint": "eslint .",
20
+ "ci": "egg-bin pkgfiles --check && pnpm run lint && pnpm run cov",
21
+ "pkgfiles": "egg-bin pkgfiles",
22
+ "pub": "tscjs scripts/publish",
23
+ "unpub": "tscjs scripts/unpublish",
24
+ "sync": "tscjs scripts/sync",
25
+ "workflow:publish-test": "zx scripts/workflow.mjs",
26
+ "dist": "run-s eslint prettier",
27
+ "autod": "autod",
28
+ "eslint": "eslint --fix --ext .ts,.js ./",
29
+ "prettier": "prettier --write \"**/*.{js,ts,json,md}\""
30
+ },
17
31
  "dependencies": {
18
32
  "egg-path-matching": "^1.0.1",
19
- "http-proxy-middleware": "0.20.0",
20
- "koa2-connect": "^1.0.2"
33
+ "http-proxy-middleware": "^2.0.6",
34
+ "js-cool": "^4.0.0",
35
+ "koa-connect": "^2.1.0"
21
36
  },
22
37
  "devDependencies": {
23
- "autod": "^3.1.1",
38
+ "@eslint-sets/eslint-config-basic": "^5.2.0",
39
+ "autod": "^3.1.2",
24
40
  "autod-egg": "^1.1.0",
25
- "egg": "^2.29.1",
26
- "egg-bin": "^4.15.0",
27
- "egg-ci": "^1.18.0",
28
- "egg-mock": "^4.0.1",
29
- "egg-path-matching": "^1.0.1",
30
- "eslint": "^7.15.0",
31
- "eslint-config-egg": "^9.0.0",
32
- "express": "^4.17.1",
33
- "prettier": "^2.2.1",
34
- "webstorm-disable-index": "^1.2.0"
41
+ "egg": "^3.16.0",
42
+ "egg-bin": "^6.4.0",
43
+ "egg-ci": "^2.2.0",
44
+ "egg-mock": "^5.10.6",
45
+ "eslint": "^8.40.0",
46
+ "eslint-config-egg": "^12.2.1",
47
+ "eslint-plugin-jsdoc": "^44.2.4",
48
+ "load-yml": "^1.3.0",
49
+ "npm-run-all": "^4.1.5",
50
+ "prettier": "^2.8.8",
51
+ "prettier-config-common": "^1.4.0",
52
+ "reinstaller": "^3.0.0",
53
+ "tsnd": "^1.1.0",
54
+ "zx": "^7.2.2"
55
+ },
56
+ "peerDependencyRules": {
57
+ "http-proxy-middleware": ">=2",
58
+ "js-cool": ">=2 <=4"
35
59
  },
36
60
  "engines": {
37
- "node": ">=8.0.0"
61
+ "node": ">=12.0.0"
38
62
  },
39
- "scripts": {
40
- "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local",
41
- "test-local": "egg-bin test",
42
- "cov": "egg-bin cov",
43
- "lint": "eslint .",
44
- "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov",
45
- "pkgfiles": "egg-bin pkgfiles",
46
- "autod": "autod"
63
+ "resolutions": {
64
+ "fsevents": ">= 2.0.0"
47
65
  },
48
- "files": [
49
- "app",
50
- "config",
51
- "app.js",
52
- "index.d.ts"
66
+ "pnpm": {
67
+ "peerDependencyRules": {
68
+ "ignoreMissing": [
69
+ "@babel/core",
70
+ "@types/node",
71
+ "webpack",
72
+ "typescript",
73
+ "mocha"
74
+ ],
75
+ "allowedVersions": {
76
+ "eslint": ">= 8.0.0",
77
+ "fsevents": ">= 2.0.0"
78
+ }
79
+ }
80
+ },
81
+ "keywords": [
82
+ "egg",
83
+ "eggPlugin",
84
+ "egg-plugin",
85
+ "proxy",
86
+ "http-proxy",
87
+ "http-proxy-plus",
88
+ "http-proxy-middleware"
53
89
  ],
54
- "ci": {
55
- "version": "8, 9"
90
+ "publishConfig": {
91
+ "registry": "https://registry.npmjs.org",
92
+ "access": "public"
56
93
  },
57
- "repository": {
58
- "type": "git",
59
- "url": "git+https://github.com/saqqdy/egg-http-proxy-plus.git"
94
+ "ci": {
95
+ "version": "12, 14, 16, 18"
60
96
  },
97
+ "license": "MIT",
98
+ "author": "saqqdy <https://github.com/saqqdy>",
99
+ "homepage": "https://github.com/saqqdy/egg-http-proxy-plus#readme",
61
100
  "bugs": {
62
101
  "url": "https://github.com/saqqdy/egg-http-proxy-plus/issues"
63
102
  },
64
- "homepage": "https://github.com/saqqdy/egg-http-proxy-plus#readme",
65
- "author": "saqqdy <saqqdy@qq.com>",
66
- "license": "MIT"
103
+ "repository": {
104
+ "type": "git",
105
+ "url": "git+https://github.com/saqqdy/egg-http-proxy-plus.git"
106
+ }
67
107
  }
package/app/.DS_Store DELETED
Binary file
package/index.d.ts DELETED
@@ -1,19 +0,0 @@
1
- import 'egg'
2
- import * as httpProxyMiddleware from 'http-proxy-middleware'
3
-
4
- declare module 'egg' {
5
- interface ProxyConfigMap {
6
- [url: string]: string | httpProxyMiddleware.Config
7
- }
8
-
9
- type ProxyConfigArrayItem = {
10
- path?: string | string[]
11
- context?: string | string[] | httpProxyMiddleware.Filter
12
- } & httpProxyMiddleware.Config
13
-
14
- type ProxyConfigArray = ProxyConfigArrayItem[]
15
-
16
- interface EggAppConfig {
17
- httpProxyPlus: ProxyConfigMap | ProxyConfigArray
18
- }
19
- }