@@ -20,7 +20,10 @@ declare module 'vue/types/vue' {
2020}
2121
2222/**
23- * Vue JSONP.
23+ * JSONP Vue plugin.
24+ *
25+ * @example
26+ * Vue.use(VueJsonp)
2427 */
2528// tslint:disable-next-line:variable-name
2629const VueJsonp : PluginObject < never > = {
@@ -30,24 +33,26 @@ const VueJsonp: PluginObject<never> = {
3033}
3134
3235/**
33- * JSONP function .
36+ * Make a json request .
3437 *
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> }
3843 *
3944 * @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+ * })
4649 */
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 >
4752function jsonp < T = any > (
4853 url : string ,
4954 param : IJsonpParam = { } ,
50- timeout ?: number
55+ config ?: undefined | number | IConfig
5156) : Promise < T > {
5257 if ( typeof url !== 'string' ) {
5358 throw new Error ( '[Vue-jsonp] Type of param "url" is not string.' )
@@ -57,9 +62,17 @@ function jsonp<T = any> (
5762 throw new Error ( '[Vue-jsonp] Invalid params, should be an object.' )
5863 }
5964
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+ }
6376
6477 return new Promise < T > ( ( resolve , reject ) => {
6578 const callbackQuery = typeof param . callbackQuery === 'string'
@@ -78,7 +91,7 @@ function jsonp<T = any> (
7891 // Convert params to querying str.
7992 let queryStrs : ( string [ ] ) [ ] = [ ]
8093 Object . keys ( param ) . forEach ( queryKey => {
81- queryStrs = queryStrs . concat ( formatParams ( queryKey , param [ queryKey ] ) )
94+ queryStrs = queryStrs . concat ( formatParams ( queryKey , param [ queryKey ] , arrayIndicator ) )
8295 } )
8396
8497 const queryStr = flatten ( queryStrs ) . join ( '&' )
@@ -151,7 +164,7 @@ interface IJsonpParam {
151164 *
152165 * @example
153166 * // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
154- * jsonp('/some-url', {
167+ * const result = await jsonp('/some-url', {
155168 * callbackQuery: 'myCallback',
156169 * callbackName: 'jsonp_func',
157170 * myCustomUrlParam: 'veryNice'
@@ -167,7 +180,7 @@ interface IJsonpParam {
167180 *
168181 * @example
169182 * // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
170- * jsonp('/some-url', {
183+ * const result = await jsonp('/some-url', {
171184 * callbackQuery: 'myCallback',
172185 * callbackName: 'jsonp_func',
173186 * myCustomUrlParam: 'veryNice'
@@ -182,3 +195,29 @@ interface IJsonpParam {
182195 */
183196 [ key : string ] : any
184197}
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