@@ -20,7 +20,10 @@ declare module 'vue/types/vue' {
20
20
}
21
21
22
22
/**
23
- * Vue JSONP.
23
+ * JSONP Vue plugin.
24
+ *
25
+ * @example
26
+ * Vue.use(VueJsonp)
24
27
*/
25
28
// tslint:disable-next-line:variable-name
26
29
const VueJsonp : PluginObject < never > = {
@@ -30,24 +33,26 @@ const VueJsonp: PluginObject<never> = {
30
33
}
31
34
32
35
/**
33
- * JSONP function .
36
+ * Make a json request .
34
37
*
35
- * @param { string } url Target URL address.
36
- * @param { IJsonpParam } param Querying params object.
37
- * @param { number } timeout Timeout setting (ms).
38
+ * @template T
39
+ * @param {string } url Target URL address.
40
+ * @param {IJsonpParam } [param={}] Querying params object.
41
+ * @param {number } [timeout] Timeout setting (ms).
42
+ * @returns {Promise<T> }
38
43
*
39
44
* @example
40
- * jsonp('/url', {
41
- * callbackQuery: ''
42
- * callbackName: '',
43
- * name: 'LancerComet',
44
- * age: 26
45
- * }, 1000)
45
+ * const data = await jsonp<string>('/url', {
46
+ * type: 'admin',
47
+ * date: '2020'
48
+ * })
46
49
*/
50
+ function jsonp < T = any > ( url : string , param ?: IJsonpParam , timeout ?: number ) : Promise < T >
51
+ function jsonp < T = any > ( url : string , param ?: IJsonpParam , config ?: IConfig ) : Promise < T >
47
52
function jsonp < T = any > (
48
53
url : string ,
49
54
param : IJsonpParam = { } ,
50
- timeout ?: number
55
+ config ?: undefined | number | IConfig
51
56
) : Promise < T > {
52
57
if ( typeof url !== 'string' ) {
53
58
throw new Error ( '[Vue-jsonp] Type of param "url" is not string.' )
@@ -57,9 +62,17 @@ function jsonp<T = any> (
57
62
throw new Error ( '[Vue-jsonp] Invalid params, should be an object.' )
58
63
}
59
64
60
- timeout = typeof timeout === 'number'
61
- ? timeout
62
- : DEFAULT_TIMEOUT
65
+ const timeout = typeof config === 'number'
66
+ ? config
67
+ : config ?. timeout ?? DEFAULT_TIMEOUT
68
+
69
+ let arrayIndicator = '[]'
70
+ if ( typeof config === 'object' ) {
71
+ const _indicator = config . arrayIndicator
72
+ if ( typeof _indicator === 'string' ) {
73
+ arrayIndicator = _indicator
74
+ }
75
+ }
63
76
64
77
return new Promise < T > ( ( resolve , reject ) => {
65
78
const callbackQuery = typeof param . callbackQuery === 'string'
@@ -78,7 +91,7 @@ function jsonp<T = any> (
78
91
// Convert params to querying str.
79
92
let queryStrs : ( string [ ] ) [ ] = [ ]
80
93
Object . keys ( param ) . forEach ( queryKey => {
81
- queryStrs = queryStrs . concat ( formatParams ( queryKey , param [ queryKey ] ) )
94
+ queryStrs = queryStrs . concat ( formatParams ( queryKey , param [ queryKey ] , arrayIndicator ) )
82
95
} )
83
96
84
97
const queryStr = flatten ( queryStrs ) . join ( '&' )
@@ -151,7 +164,7 @@ interface IJsonpParam {
151
164
*
152
165
* @example
153
166
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
154
- * jsonp('/some-url', {
167
+ * const result = await jsonp('/some-url', {
155
168
* callbackQuery: 'myCallback',
156
169
* callbackName: 'jsonp_func',
157
170
* myCustomUrlParam: 'veryNice'
@@ -167,7 +180,7 @@ interface IJsonpParam {
167
180
*
168
181
* @example
169
182
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
170
- * jsonp('/some-url', {
183
+ * const result = await jsonp('/some-url', {
171
184
* callbackQuery: 'myCallback',
172
185
* callbackName: 'jsonp_func',
173
186
* myCustomUrlParam: 'veryNice'
@@ -182,3 +195,29 @@ interface IJsonpParam {
182
195
*/
183
196
[ key : string ] : any
184
197
}
198
+
199
+ /**
200
+ * JSONP Config.
201
+ */
202
+ interface IConfig {
203
+ /**
204
+ * Request timeout, ms.
205
+ *
206
+ * @default 5000
207
+ */
208
+ timeout ?: number
209
+
210
+ /**
211
+ * This is the indicator that used in query string to indicate arrays.
212
+ *
213
+ * @example
214
+ * // When you pass a '[]' or nothing:
215
+ * a[]=1&a[]=2&a[]=3 // This form is used widely.
216
+ *
217
+ * // An empty sring was passed:
218
+ * a=1&a=2&a=3 // This is a custom example.
219
+ *
220
+ * @default '[]'
221
+ */
222
+ arrayIndicator ?: string
223
+ }
0 commit comments