1- # egg- jsonp
1+ # @ eggjs/ jsonp
22
33[ ![ 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 )
55[ ![ Test coverage] [ codecov-image ]] [ codecov-url ]
6- [ ![ David deps] [ david-image ]] [ david-url ]
76[ ![ Known Vulnerabilities] [ snyk-image ]] [ snyk-url ]
87[ ![ 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
2219
2320An egg plugin for jsonp support.
2421
22+ ## Requirements
23+
24+ - egg >= 4.x
25+
2526## Install
2627
2728``` bash
28- $ npm i egg- jsonp --save
29+ npm i @eggjs/ jsonp
2930```
3031
3132## Usage
3233
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+ },
3842};
3943```
4044
4145## Configuration
4246
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
4751
4852if whiteList's type is ` RegExp ` , referrer must match ` whiteList ` , pay attention to the first ` ^ ` and last ` / ` .
4953
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+
5461// matchs referrer:
5562// https://test.com/hello
5663// http://test.com/
5764```
5865
5966if whiteList's type is ` String ` and starts with ` . ` :
6067
61- ``` js
62- exports .jsonp = {
63- whiteList: ' .test.com' ,
68+ ``` ts
69+ export default {
70+ jsonp: {
71+ whiteList: ' .test.com' ,
72+ },
6473};
74+
6575// matchs domain test.com:
6676// https://test.com/hello
6777// http://test.com/
@@ -73,40 +83,46 @@ exports.jsonp = {
7383
7484if whiteList's type is ` String ` and not starts with ` . ` :
7585
76- ``` js
77- exports .jsonp = {
78- whiteList: ' sub.test.com' ,
86+ ``` ts
87+ export default {
88+ jsonp: {
89+ whiteList: ' sub.test.com' ,
90+ },
7991};
92+
8093// only matchs domain sub.test.com:
8194// https://sub.test.com/hello
8295// http://sub.test.com/
8396```
8497
8598whiteList also can be an array:
8699
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+ },
90105};
91106```
92107
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.
94109
95110## API
96111
97- * ctx.acceptJSONP - detect if response should be jsonp, readonly
112+ - ctx.acceptJSONP - detect if response should be jsonp, readonly
98113
99114## Example
100115
101- In ` app/router.js `
116+ In ` app/router.ts `
102117
103- ``` js
118+ ``` ts
104119// Create once and use in any router you want to support jsonp.
105120const jsonp = app .jsonp ();
121+
106122app .get (' /default' , jsonp , ' jsonp.index' );
107123app .get (' /another' , jsonp , ' jsonp.another' );
108124
109- // Customize by create another jsonp middleware with specific sonfigurations .
125+ // Customize by create another jsonp middleware with specific configurations .
110126app .get (' /customize' , app .jsonp ({ callback: ' fn' }), ' jsonp.customize' );
111127```
112128
@@ -116,5 +132,10 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
116132
117133## License
118134
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 )
120140
141+ Made with [ contributors-img] ( https://contrib.rocks ) .
0 commit comments