Nondeterministic Programming Methodology

npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

url-scheme

1.0.5 • Public • Published

url-scheme

npm version js-standard-style Build Status codecov

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

License

MIT

install

npm i url-scheme

Downloadsweekly downloads

20

version

1.0.5

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability