url-scheme
v1.0.5
Published
A promisified url schemes creator for communication between webview & native
Downloads
31
Maintainers
Readme
url-scheme
A promisified url schemes creator for communication between webview & native
Install
npm install url-scheme --save
// or
yarn add url-scheme
Usage
const UrlScheme = require('url-scheme')
const CancelToken = UrlScheme.CancelToken
// or ES2015+
import UrlScheme, { CancelToken } from 'url-scheme'
Example
Basic usage:
// Only url param is required.
// Others are optional.
new UrlScheme({ url: 'foo://bar?baz=true' })
.then(res => console.log(res))
.catch(err => console.error(err))
Advanced usage:
import UrlScheme, { CancelToken } from 'url-scheme'
let CancelToken = UrlScheme.CancelToken
let source = CancelToken.source()
new UrlScheme({
url: 'FooScheme://foo/bar?baz=true',
query: {
biz: 'foobar',
boo: [1, 2, 3]
},
param: 'callbackName', // default is callback
prefix: 'callbackPrefix', // default is __jsonp
beforeSend ({ schemeUrl, jsonpId }) {
console.log(schemeUrl, jsonpId)
},
timeout: 20000, // default is 0, 0/Infinity means never timeout
cancelToken: source.token
})
.then(res => console.log(res))
.catch(err => {
if (UrlScheme.isCancel(err)) {
return console.log(err.message || 'It\'s been canceled!')
}
console.error(err)
})
// Native execute script
window.callbackPrefix1492871429491({ foo: 'bar' })
// or cancel the request
source.cancel('No reason')
Defaults
You can specify defaults options that will be applied to every request.
UrlScheme.defaults.scheme = 'myscheme'
UrlScheme.defaults.timeout = 20000
// then you can create UrlScheme without specify the scheme
new UrlScheme({ url: 'foo/bar?baz=true' })
// myscheme://foo/bar?baz=true