egg-http-proxy-plus 1.1.0 → 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,35 +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
 
4
- ### 支持转发文件上传接口,支持自定义匹配方法,ctx透传
5
+ </div>
6
+
7
+ <div style="text-align: center;" align="center">
8
+
9
+ 支持转发文件上传接口,支持自定义匹配方法,ctx 透传
5
10
 
11
+ </div>
12
+
13
+ <div style="text-align: center;" align="center">
6
14
 
7
15
  [![NPM version][npm-image]][npm-url]
16
+ [![Codacy Badge][codacy-image]][codacy-url]
8
17
  [![build status][travis-image]][travis-url]
9
18
  [![Test coverage][codecov-image]][codecov-url]
10
- [![David deps][david-image]][david-url]
11
- [![Known Vulnerabilities][snyk-image]][snyk-url]
12
19
  [![npm download][download-image]][download-url]
20
+ [![License][license-image]][license-url]
13
21
 
14
- [npm-image]: https://img.shields.io/npm/v/egg-http-proxy-plus.svg?style=flat-square
15
- [npm-url]: https://npmjs.org/package/egg-http-proxy-plus
16
- [travis-image]: https://travis-ci.org/saqqdy/egg-http-proxy-plus.svg?branch=master
17
- [travis-url]: https://travis-ci.org/saqqdy/egg-http-proxy-plus
18
- [codecov-image]: https://img.shields.io/codecov/c/github/saqqdy/egg-http-proxy-plus.svg?style=flat-square
19
- [codecov-url]: https://codecov.io/github/saqqdy/egg-http-proxy-plus?branch=master
20
- [david-image]: https://img.shields.io/david/saqqdy/egg-http-proxy-plus.svg?style=flat-square
21
- [david-url]: https://david-dm.org/saqqdy/egg-http-proxy-plus
22
- [snyk-image]: https://snyk.io/test/npm/egg-http-proxy-plus/badge.svg?style=flat-square
23
- [snyk-url]: https://snyk.io/test/npm/egg-http-proxy-plus
24
- [download-image]: https://img.shields.io/npm/dm/egg-http-proxy-plus.svg?style=flat-square
25
- [download-url]: https://npmjs.org/package/egg-http-proxy-plus
22
+ [![Sonar][sonar-image]][sonar-url]
23
+
24
+ </div>
26
25
 
27
26
  Configure proxy middleware for egg. Use [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware).
28
27
 
29
28
  ## Install
30
29
 
31
30
  ```bash
31
+ # use npm
32
32
  $ npm i egg-http-proxy-plus --save
33
+
34
+ # use yarn
35
+ $ yarn add egg-http-proxy-plus
33
36
  ```
34
37
 
35
38
  ## Usage
@@ -38,22 +41,29 @@ $ npm i egg-http-proxy-plus --save
38
41
  // {app_root}/config/plugin.js
39
42
  exports.httpProxyPlus = {
40
43
  enable: true,
41
- package: 'egg-http-proxy-plus',
42
- };
44
+ package: 'egg-http-proxy-plus'
45
+ }
43
46
  ```
44
47
 
45
48
  ## Configuration
46
49
 
47
- Proxy `/api` requests to `http://www.example.org`:
50
+ ### Proxy `/api` requests to `http://www.example.org`
48
51
 
49
52
  ```js
50
53
  // {app_root}/config/config.default.js
51
54
  exports.httpProxyPlus = {
52
55
  '/api': 'http://www.example.org'
53
- };
56
+ }
57
+ // or
58
+ exports.httpProxyPlus = [
59
+ {
60
+ origin: '/api',
61
+ options: 'http://www.example.org'
62
+ }
63
+ ]
54
64
  ```
55
65
 
56
- 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`
57
67
 
58
68
  If you don't want `/api` to be passed along, we need to rewrite the path:
59
69
 
@@ -62,9 +72,55 @@ If you don't want `/api` to be passed along, we need to rewrite the path:
62
72
  exports.httpProxyPlus = {
63
73
  '/api': {
64
74
  target: 'http://www.example.org',
65
- pathRewrite: {'^/api' : ''}
75
+ pathRewrite: { '^/api': '' }
76
+ }
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
+ }
66
122
  }
67
- };
123
+ }
68
124
  ```
69
125
 
70
126
  For more advanced usages, checkout [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware#options) options documentation.
@@ -76,3 +132,20 @@ Please open an issue [here](https://github.com/saqqdy/egg-http-proxy-plus/issues
76
132
  ## License
77
133
 
78
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,21 +1,7 @@
1
1
  const { createProxyMiddleware } = require('http-proxy-middleware')
2
2
  const c2k = require('koa-connect')
3
3
  const pathMatching = require('egg-path-matching')
4
-
5
- const getType = obj => {
6
- const type = {
7
- '[object Array]': 'array',
8
- '[object Boolean]': 'boolean',
9
- '[object Date]': 'date',
10
- '[object Function]': 'function',
11
- '[object Number]': 'number',
12
- '[object Object]': 'object',
13
- '[object RegExp]': 'regexp',
14
- '[object String]': 'string'
15
- }
16
- if (obj === null) return obj + ''
17
- return typeof obj === 'object' || typeof obj === 'function' ? type[Object.prototype.toString.call(obj)] || 'object' : typeof obj
18
- }
4
+ const { getType } = require('js-cool')
19
5
 
20
6
  module.exports = options => {
21
7
  return async (ctx, next) => {
@@ -34,6 +20,7 @@ module.exports = options => {
34
20
  const { onProxyReq = null, onProxyRes = null } = proxyOptions
35
21
  proxyOptions = {
36
22
  ...proxyOptions,
23
+ // eslint-disable-next-line no-unused-vars
37
24
  onProxyReq(proxyReq, req, res, context = ctx) {
38
25
  if (onProxyReq && typeof onProxyReq === 'function') {
39
26
  onProxyReq(proxyReq, req, res, ctx)
@@ -46,6 +33,7 @@ module.exports = options => {
46
33
  }
47
34
  return proxyReq
48
35
  },
36
+ // eslint-disable-next-line no-unused-vars
49
37
  onProxyRes(proxyRes, req, res, context = ctx) {
50
38
  if (onProxyRes && typeof onProxyRes === 'function') {
51
39
  onProxyRes(proxyRes, req, res, ctx)
package/package.json CHANGED
@@ -1,67 +1,107 @@
1
1
  {
2
2
  "name": "egg-http-proxy-plus",
3
- "version": "1.1.0",
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": "^1.0.6",
33
+ "http-proxy-middleware": "^2.0.6",
34
+ "js-cool": "^4.0.0",
20
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
@@ -1,71 +0,0 @@
1
- const proxy = require('http-proxy-middleware')
2
- const c2k = require('koa2-connect')
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
- }
18
- module.exports = options => {
19
- return async function httpProxyPlus(ctx, next) {
20
- const path = ctx.request.originalUrl || ctx.request.url
21
- 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
34
- }
35
- // context.origin配置的数组、字符串
36
- const match = pathMatching({ match: context.origin })
37
- const isMatch = match({ path })
38
- if (isMatch) {
39
- try {
40
- c2k(proxy(context.origin, proxyOptions))(ctx, next)
41
- } catch (err) {
42
- ctx.body = {
43
- code: 403,
44
- data: '',
45
- msg: 'http-proxy代理错误'
46
- }
47
- }
48
- }
49
- return isMatch
50
- })
51
- } else if (optType === 'object') {
52
- ;[('enable', 'match', 'ignore')].forEach(el => {
53
- if (options.hasOwnProperty(el)) delete options[el]
54
- })
55
- Object.keys(options).some(context => {
56
- const match = pathMatching({ match: context })
57
- const isMatch = match({ path })
58
- if (isMatch) {
59
- let proxyOptions = options[context]
60
- if (typeof proxyOptions === 'string') proxyOptions = { target: proxyOptions }
61
- const { onProxyReq = null, onProxyRes = null } = Object.assign({}, proxyOptions)
62
- if (onProxyReq) proxyOptions.onProxyReq = (...args) => onProxyReq.call(ctx, ...args)
63
- if (onProxyRes) proxyOptions.onProxyRes = (...args) => onProxyRes.call(ctx, ...args)
64
- c2k(proxy(context, proxyOptions))(ctx, next)
65
- }
66
- return isMatch
67
- })
68
- }
69
- await next()
70
- }
71
- }
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
- }