1
- # egg- jsonp
1
+ # @ eggjs/ jsonp
2
2
3
3
[ ![ NPM version] [ npm-image ]] [ npm-url ]
4
- [ ![ build status ] [ travis-image ]] [ travis-url ]
4
+ [ ![ Node.js CI ] ( https://github.com/eggjs/jsonp/actions/workflows/nodejs.yml/badge.svg )] ( https://github.com/eggjs/jsonp/actions/workflows/nodejs.yml )
5
5
[ ![ Test coverage] [ codecov-image ]] [ codecov-url ]
6
- [ ![ David deps] [ david-image ]] [ david-url ]
7
6
[ ![ Known Vulnerabilities] [ snyk-image ]] [ snyk-url ]
8
7
[ ![ npm download] [ download-image ]] [ download-url ]
9
-
10
- [ npm-image ] : https://img.shields.io/npm/v/egg-jsonp.svg?style=flat-square
11
- [ npm-url ] : https://npmjs.org/package/egg-jsonp
12
- [ travis-image ] : https://img.shields.io/travis/eggjs/egg-jsonp.svg?style=flat-square
13
- [ travis-url ] : https://travis-ci.org/eggjs/egg-jsonp
14
- [ codecov-image ] : https://img.shields.io/codecov/c/github/eggjs/egg-jsonp.svg?style=flat-square
15
- [ codecov-url ] : https://codecov.io/github/eggjs/egg-jsonp?branch=master
16
- [ david-image ] : https://img.shields.io/david/eggjs/egg-jsonp.svg?style=flat-square
17
- [ david-url ] : https://david-dm.org/eggjs/egg-jsonp
18
- [ snyk-image ] : https://snyk.io/test/npm/egg-jsonp/badge.svg?style=flat-square
19
- [ snyk-url ] : https://snyk.io/test/npm/egg-jsonp
20
- [ download-image ] : https://img.shields.io/npm/dm/egg-jsonp.svg?style=flat-square
21
- [ download-url ] : https://npmjs.org/package/egg-jsonp
8
+ [ ![ Node.js Version] ( https://img.shields.io/node/v/@eggjs/jsonp.svg?style=flat )] ( https://nodejs.org/en/download/ )
9
+ [ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square )] ( https://makeapullrequest.com )
10
+
11
+ [ npm-image ] : https://img.shields.io/npm/v/@eggjs/jsonp.svg?style=flat-square
12
+ [ npm-url ] : https://npmjs.org/package/@eggjs/jsonp
13
+ [ codecov-image ] : https://img.shields.io/codecov/c/github/eggjs/jsonp.svg?style=flat-square
14
+ [ codecov-url ] : https://codecov.io/github/eggjs/jsonp?branch=master
15
+ [ snyk-image ] : https://snyk.io/test/npm/@eggjs/jsonp/badge.svg?style=flat-square
16
+ [ snyk-url ] : https://snyk.io/test/npm/@eggjs/jsonp
17
+ [ download-image ] : https://img.shields.io/npm/dm/@eggjs/jsonp.svg?style=flat-square
18
+ [ download-url ] : https://npmjs.org/package/@eggjs/jsonp
22
19
23
20
An egg plugin for jsonp support.
24
21
22
+ ## Requirements
23
+
24
+ - egg >= 4.x
25
+
25
26
## Install
26
27
27
28
``` bash
28
- $ npm i egg- jsonp --save
29
+ npm i @eggjs/ jsonp
29
30
```
30
31
31
32
## Usage
32
33
33
- ``` js
34
- // {app_root}/config/plugin.js
35
- exports .jsonp = {
36
- enable: true ,
37
- package: ' egg-jsonp' ,
34
+ ``` ts
35
+ // {app_root}/config/plugin.ts
36
+
37
+ export default {
38
+ jsonp: {
39
+ enable: true ,
40
+ package: ' @eggjs/jsonp' ,
41
+ },
38
42
};
39
43
```
40
44
41
45
## Configuration
42
46
43
- * {String|Array} callback - jsonp callback method key, default to ` [ '_callback', 'callback' ] `
44
- * {Number} limit - callback method name's max length, default to ` 50 `
45
- * {Boolean} csrf - enable csrf check or not. default to false
46
- * {String|RegExp|Array} whiteList - referrer white list
47
+ - {String|Array} callback - jsonp callback method key, default to ` [ '_callback', 'callback' ] `
48
+ - {Number} limit - callback method name's max length, default to ` 50 `
49
+ - {Boolean} csrf - enable csrf check or not. default to false
50
+ - {String|RegExp|Array} whiteList - referrer white list
47
51
48
52
if whiteList's type is ` RegExp ` , referrer must match ` whiteList ` , pay attention to the first ` ^ ` and last ` / ` .
49
53
50
- ``` js
51
- exports .jsonp = {
52
- whiteList: / ^ https? :\/\/ test. com\/ / ,
53
- }
54
+ ``` ts
55
+ export default {
56
+ jsonp: {
57
+ whiteList: / ^ https? :\/\/ test. com\/ / ,
58
+ },
59
+ };
60
+
54
61
// matchs referrer:
55
62
// https://test.com/hello
56
63
// http://test.com/
57
64
```
58
65
59
66
if whiteList's type is ` String ` and starts with ` . ` :
60
67
61
- ``` js
62
- exports .jsonp = {
63
- whiteList: ' .test.com' ,
68
+ ``` ts
69
+ export default {
70
+ jsonp: {
71
+ whiteList: ' .test.com' ,
72
+ },
64
73
};
74
+
65
75
// matchs domain test.com:
66
76
// https://test.com/hello
67
77
// http://test.com/
@@ -73,40 +83,46 @@ exports.jsonp = {
73
83
74
84
if whiteList's type is ` String ` and not starts with ` . ` :
75
85
76
- ``` js
77
- exports .jsonp = {
78
- whiteList: ' sub.test.com' ,
86
+ ``` ts
87
+ export default {
88
+ jsonp: {
89
+ whiteList: ' sub.test.com' ,
90
+ },
79
91
};
92
+
80
93
// only matchs domain sub.test.com:
81
94
// https://sub.test.com/hello
82
95
// http://sub.test.com/
83
96
```
84
97
85
98
whiteList also can be an array:
86
99
87
- ``` js
88
- exports .jsonp = {
89
- whiteList: [ ' .foo.com' , ' .bar.com' ],
100
+ ``` ts
101
+ export default {
102
+ jsonp: {
103
+ whiteList: [ ' .foo.com' , ' .bar.com' ],
104
+ },
90
105
};
91
106
```
92
107
93
- see [ config/config.default.js ] ( https://github.com/eggjs/egg- jsonp/blob/master/config/config.default.js ) for more detail.
108
+ see [ config/config.default.ts ] ( https://github.com/eggjs/jsonp/blob/master/src/ config/config.default.ts ) for more detail.
94
109
95
110
## API
96
111
97
- * ctx.acceptJSONP - detect if response should be jsonp, readonly
112
+ - ctx.acceptJSONP - detect if response should be jsonp, readonly
98
113
99
114
## Example
100
115
101
- In ` app/router.js `
116
+ In ` app/router.ts `
102
117
103
- ``` js
118
+ ``` ts
104
119
// Create once and use in any router you want to support jsonp.
105
120
const jsonp = app .jsonp ();
121
+
106
122
app .get (' /default' , jsonp , ' jsonp.index' );
107
123
app .get (' /another' , jsonp , ' jsonp.another' );
108
124
109
- // Customize by create another jsonp middleware with specific sonfigurations .
125
+ // Customize by create another jsonp middleware with specific configurations .
110
126
app .get (' /customize' , app .jsonp ({ callback: ' fn' }), ' jsonp.customize' );
111
127
```
112
128
@@ -116,5 +132,10 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
116
132
117
133
## License
118
134
119
- [ MIT] ( https://github.com/eggjs/egg-jsonp/blob/master/LICENSE )
135
+ [ MIT] ( LICENSE )
136
+
137
+ ## Contributors
138
+
139
+ [ ![ Contributors] ( https://contrib.rocks/image?repo=eggjs/jsonp )] ( https://github.com/eggjs/jsonp/graphs/contributors )
120
140
141
+ Made with [ contributors-img] ( https://contrib.rocks ) .
0 commit comments