-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
import protocolDetection from 'custom-protocol-detection';
import invariant from 'invariant';
import { stringify } from '@w6s/query-string';
import isString from 'lodash.isstring';
import isFunction from 'lodash.isfunction';
import isObject from 'lodash.isobject';
/**
* open-desktop-application
*
* @param object { protocol, action, query, fail, success }
*
* protocol: string (eg, workplus)
* action: string (eg, joinchat)
* query: object (eg, { id: 1, name: 'test' })
* fail: function
* success: function
*
* return => workplus://joinchat/?id=1&name=test
*/
function openDesktopApplication(params) {
try {
const { protocol, action, query, fail, success } = params;
invariant(isString(protocol), '[protocol] Must be a non-empty string');
invariant(isString(action), '[action] Must be a non-empty string');
let openUri = `${protocol}://${action}`;
if (isObject(query)) {
openUri += `?${stringify(query)}`;
}
protocolDetection(
openUri,
function failCb(e) {
isFunction(fail) && fail(e);
},
function successCb() {
isFunction(success) && success();
},
function unsupportedCb() {
isFunction(fail) && fail({ supported: false });
}
);
} catch (error) {
isFunction(fail) && fail(error);
console.log(error);
}
}
export default openDesktopApplication;