Skip to content

Commit

Permalink
Feature/Project update (#15)
Browse files Browse the repository at this point in the history
* + Update project build.

* + Update badge.

* + Use vue directly from npm package for unit testing.
  • Loading branch information
LancerComet authored Sep 29, 2020
1 parent c317251 commit a061885
Show file tree
Hide file tree
Showing 25 changed files with 8,030 additions and 791 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Test

on: [push, pull_request]

jobs:
test-core-package:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Test core package
run: npm ci

- name: Run test
run: npm test
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ jspm_packages
*.tgz

#IDEs.
.ieda/
.idea/
.vscode/

# Dist.
dist/
7 changes: 0 additions & 7 deletions .npmignore

This file was deleted.

14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 LancerComet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
179 changes: 126 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,153 @@
# Vue-jsonp

![Travis](https://travis-ci.org/LancerComet/vue-jsonp.svg?branch=master)
[![VueJsonp](https://github.com/LancerComet/vue-jsonp/workflows/Test/badge.svg)](https://github.com/LancerComet/vue-jsonp/actions)

A tiny library for handling JSONP request.

## Usage.
## Quick Start

Static function:
`Vue.jsonp(url, dataObj, timeout)`
As Vue plugin:

In Vue component:
`this.$jsonp(url, dataObj, timeout)`
```ts
import { VueJsonp } from 'vue-jsonp'

## Params.
- url: Target url for request.
- dataObj: Object contains datas for querying.
- timeout: Timeout for jsonp request.
// Vue Plugin.
Vue.use(VueJsonp)

## URL.
```javascript
'/url?{callbackQuery}={callbackName}&...'
// Now you can use this.$jsonp in Vue components.
const vm = new Vue()
vm.$jsonp('/some-jsonp-url', {
myCustomUrlParam: 'veryNice'
})
```

// Default:
'/url?callback=jsonp_RANDOM_STRING&...'
```
Use function directly:

## Assign callback query name.
```javascript
this.$jsonp('/url', {
callbackQuery: 'cb' // Default: callback
```ts
import { jsonp } from 'vue-jsonp'

jsonp('/some-jsonp-url', {
myCustomUrlParam: 'veryNice'
})
```

// Then URL will be: '/url?cb=jsonp_aws84739ssu8e3'
## Send data and set query & function name

### Send data

```ts
// The request url will be "/some-jsonp-url?name=LancerComet&age=100&callback=jsonp_{RANDOM_STR}".
jsonp('/some-jsonp-url', {
name: 'LancerComet',
age: 100
})
```

## Assign callback function name.
```javascript
this.$jsonp('/url', {
callbackName: 'jsonpFunc'
### Custom query & function name

The url uniform is `/url?{callbackQuery}={callbackName}&...`, the default is `/url?callback=jsonp_{RANDOM_STRING}&...`.

And you can change it like this:

```ts
// The request url will be "/some-jsonp-url?name=LancerComet&age=100&cb=jsonp_func".
jsonp('/some-jsonp-url', {
callbackQuery: 'cb',
callbackName: 'jsonp_func',
name: 'LancerComet',
age: 100
})
```

// Then URL will be: '/url?callback=jsonpFunc'
## Module exports

- `VueJsonp: PluginObject<never>`

- `jsonp<T>: (url: string, param?: IJsonpParam, timeout?: number) => Promise<T>`

## API

### IJsonpParam

IJsonpParam is the type of param for jsonp function.

```ts
/**
* JSONP parameter declaration.
*/
interface IJsonpParam {
/**
* Callback query name.
* This param is used to define the query name of the callback function.
*
* @example
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
* jsonp('/some-url', {
* callbackQuery: 'myCallback',
* callbackName: 'jsonp_func',
* myCustomUrlParam: 'veryNice'
* })
*
* @default callback
*/
callbackQuery?: string

/**
* Callback function name.
* This param is used to define the jsonp function name.
*
* @example
* // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
* jsonp('/some-url', {
* callbackQuery: 'myCallback',
* callbackName: 'jsonp_func',
* myCustomUrlParam: 'veryNice'
* })
*
* @default jsonp_ + randomStr()
*/
callbackName?: string

/**
* Custom data.
*/
[key: string]: any
}
```

## Example.
```javascript
## Example

```ts
import Vue from 'vue'
import VueJsonp from 'vue-jsonp'
import { VueJsonp } from 'vue-jsonp'

Vue.use(VueJsonp)

// If you want to setup the global timeout, just:
Vue.use(VueJsonp, 5000)
// Now all requests will be expired after 5000ms.

// Use it in Vue Component.
const SomeComponent = Vue.extend({
methods: {
getData () {
this.$jsonp('http://www.some-site.com/data', { name: 'MyName', age: 20 }).then(json => {
// Success.
}).catch(err => {
// Failed.
})
}
const vm = new Vue()
const { code, data, message } = await vm.$jsonp<{
code: number,
message: string,
data: {
id: number,
nickname: string
}
}>('/my-awesome-url', {
name: 'MyName', age: 20
})

// Static Function.
// Request url will be 'http://www.some-site.com/data?name=MyName&age=20&cb=jsonpFunc'
Vue.jsonp('http://www.some-site.com/data', {
name: 'MyName', age: 20, callbackQuery: 'cb', callbackName: 'jsonpFunc'
}).then(json => {
// Success.
}).catch(err => {
// Failed.
})
assert(code === 0)
assert(message === 'ok')
assert(data.id === 1)
assert(data.nickname === 'John Smith')
```

```ts
import { jsonp } from 'vue-jsonp'

const result = await jsonp<string>('/my-awesome-url')
assert(result === 'such a jsonp')
```

## License
MIT.

MIT
Loading

0 comments on commit a061885

Please sign in to comment.