Skip to content

Commit acd2469

Browse files
authored
Develop (#42)
* + Update dev dependencies. * + Update document. (#16) * + Add array indicator option. * + Fix for #41.
1 parent ff97728 commit acd2469

File tree

5 files changed

+11044
-3592
lines changed

5 files changed

+11044
-3592
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Vue-jsonp
22

3+
[![npm version](https://badge.fury.io/js/vue-jsonp.svg)](https://badge.fury.io/js/vue-jsonp)
34
[![VueJsonp](https://github.com/LancerComet/vue-jsonp/workflows/Test/badge.svg)](https://github.com/LancerComet/vue-jsonp/actions)
45

56
A tiny library for handling JSONP request.
@@ -14,9 +15,9 @@ import { VueJsonp } from 'vue-jsonp'
1415
// Vue Plugin.
1516
Vue.use(VueJsonp)
1617

17-
// Now you can use this.$jsonp in Vue components.
18+
// Now you can use "$jsonp" on Vue components.
1819
const vm = new Vue()
19-
vm.$jsonp('/some-jsonp-url', {
20+
const data = await vm.$jsonp('/some-jsonp-url', {
2021
myCustomUrlParam: 'veryNice'
2122
})
2223
```
@@ -26,7 +27,8 @@ Use function directly:
2627
```ts
2728
import { jsonp } from 'vue-jsonp'
2829

29-
jsonp('/some-jsonp-url', {
30+
// Pass a response type.
31+
const data = await jsonp<string>('/some-jsonp-url', {
3032
myCustomUrlParam: 'veryNice'
3133
})
3234
```
@@ -36,26 +38,26 @@ jsonp('/some-jsonp-url', {
3638
### Send data
3739

3840
```ts
39-
// The request url will be "/some-jsonp-url?name=LancerComet&age=100&callback=jsonp_{RANDOM_STR}".
40-
jsonp('/some-jsonp-url', {
41-
name: 'LancerComet',
42-
age: 100
41+
// The request url will be "/some-jsonp-url?type=admin&date=2020&callback=jsonp_{RANDOM_STR}".
42+
const data = await jsonp('/some-jsonp-url', {
43+
type: 'admin',
44+
date: 2020
4345
})
4446
```
4547

4648
### Custom query & function name
4749

48-
The url uniform is `/url?{callbackQuery}={callbackName}&...`, the default is `/url?callback=jsonp_{RANDOM_STRING}&...`.
50+
The url uniform is `/url?{callbackQuery}={callbackName}&...` and the default is `/url?callback=jsonp_{RANDOM_STRING}&...`.
4951

5052
And you can change it like this:
5153

5254
```ts
53-
// The request url will be "/some-jsonp-url?name=LancerComet&age=100&cb=jsonp_func".
55+
// The request url will be "/some-jsonp-url?type=admin&date=2020&cb=jsonp_func".
5456
jsonp('/some-jsonp-url', {
5557
callbackQuery: 'cb',
5658
callbackName: 'jsonp_func',
57-
name: 'LancerComet',
58-
age: 100
59+
type: 'admin',
60+
date: 2020
5961
})
6062
```
6163

@@ -82,7 +84,7 @@ interface IJsonpParam {
8284
*
8385
* @example
8486
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
85-
* jsonp('/some-url', {
87+
* const result = await jsonp('/some-url', {
8688
* callbackQuery: 'myCallback',
8789
* callbackName: 'jsonp_func',
8890
* myCustomUrlParam: 'veryNice'
@@ -98,7 +100,7 @@ interface IJsonpParam {
98100
*
99101
* @example
100102
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
101-
* jsonp('/some-url', {
103+
* const result = await jsonp('/some-url', {
102104
* callbackQuery: 'myCallback',
103105
* callbackName: 'jsonp_func',
104106
* myCustomUrlParam: 'veryNice'

lib/index.ts

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2629
const 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>
4752
function 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+
}

lib/utils/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ function randomStr () {
1212
*
1313
* @return {string[]}
1414
*/
15-
function formatParams (queryKey: string, value: any): string[] {
15+
function formatParams (queryKey: string, value: any, arrayIndicator: string = '[]'): string[] {
1616
queryKey = queryKey.replace(/=/g, '')
1717
let result: string[] = []
1818

19+
if (value === null || typeof value === 'undefined') {
20+
return result
21+
}
22+
1923
switch (value.constructor) {
2024
case String:
2125
case Number:
@@ -25,14 +29,14 @@ function formatParams (queryKey: string, value: any): string[] {
2529

2630
case Array:
2731
value.forEach(function (item) {
28-
result = result.concat(formatParams(queryKey + '[]=', item))
32+
result = result.concat(formatParams(`${queryKey}${arrayIndicator}=`, item, arrayIndicator))
2933
})
3034
break
3135

3236
case Object:
3337
Object.keys(value).forEach(function (key) {
3438
const item = value[key]
35-
result = result.concat(formatParams(queryKey + '[' + key + ']', item))
39+
result = result.concat(formatParams(queryKey + '[' + key + ']', item, arrayIndicator))
3640
})
3741
break
3842
}

0 commit comments

Comments
 (0)