@dustid/dust-go-connect 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/chromeWebView.d.ts +9 -0
- package/lib/chromeWebView.js +23 -0
- package/lib/dispatcher.d.ts +43 -0
- package/lib/dispatcher.js +77 -0
- package/lib/index.d.ts +14 -0
- package/lib/index.js +37 -0
- package/lib/reactNativeWebView.d.ts +10 -0
- package/lib/reactNativeWebView.js +29 -0
- package/lib/types.d.ts +72 -0
- package/lib/types.js +1 -0
- package/package.json +13 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dispatcher } from './dispatcher';
|
|
2
|
+
import { DustGoConnector } from './types';
|
|
3
|
+
export declare class ChromeWebViewDustGoConnector extends Dispatcher implements DustGoConnector {
|
|
4
|
+
static detected: boolean;
|
|
5
|
+
constructor();
|
|
6
|
+
rewriteRedirect(url: URL): URL;
|
|
7
|
+
showScanner(): void;
|
|
8
|
+
hideScanner(): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var _a, _b;
|
|
2
|
+
import { Dispatcher } from './dispatcher';
|
|
3
|
+
export class ChromeWebViewDustGoConnector extends Dispatcher {
|
|
4
|
+
constructor() {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
if (!((_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.chrome) === null || _b === void 0 ? void 0 : _b.webview)) {
|
|
7
|
+
throw new Error('Could not init ChromeWebViewDustGoConnector');
|
|
8
|
+
}
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
rewriteRedirect(url) {
|
|
12
|
+
return url;
|
|
13
|
+
}
|
|
14
|
+
showScanner() {
|
|
15
|
+
var _a, _b, _c;
|
|
16
|
+
(_c = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.chrome) === null || _b === void 0 ? void 0 : _b.webview) === null || _c === void 0 ? void 0 : _c.postMessage('show');
|
|
17
|
+
}
|
|
18
|
+
hideScanner() {
|
|
19
|
+
var _a, _b, _c;
|
|
20
|
+
(_c = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.chrome) === null || _b === void 0 ? void 0 : _b.webview) === null || _c === void 0 ? void 0 : _c.postMessage('hide');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
ChromeWebViewDustGoConnector.detected = Boolean((_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.chrome) === null || _b === void 0 ? void 0 : _b.webview);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { EventType, ScanCallback, ScanEvent } from "./types";
|
|
2
|
+
export declare class Dispatcher {
|
|
3
|
+
private callbacks;
|
|
4
|
+
/**
|
|
5
|
+
* Adds an event listener for the specified event type and id.
|
|
6
|
+
* * @param id - A unique identifier for the event listener. This allows multiple listeners for the same event type.
|
|
7
|
+
* * @param cb - The callback function to be called when the event is dispatched. The type of the callback depends on the event type:
|
|
8
|
+
* */
|
|
9
|
+
add(id: string, cb: ScanCallback): void;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if an event listener exists for the specified event type and id.
|
|
12
|
+
* */
|
|
13
|
+
has(id: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Remove all event listeners.
|
|
16
|
+
*/
|
|
17
|
+
clear(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Removes an event listener for the specified event type and id.
|
|
20
|
+
*
|
|
21
|
+
* @param event - The type of event to stop listening for, either "scan", "show", or "hide".
|
|
22
|
+
* @param id - The unique identifier for the event listener to be removed. If not provided, all listeners for the specified event type are removed.
|
|
23
|
+
* if no id is provided, all listeners for that event type are removed.
|
|
24
|
+
* */
|
|
25
|
+
remove(id?: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Fires the specified event, calling all registered callbacks for that event type.
|
|
28
|
+
* */
|
|
29
|
+
dispatch(event: ScanEvent): void;
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated
|
|
32
|
+
* This keeps compatibility with the previous dispatch API.
|
|
33
|
+
* Still currently used by the `dust-go` react native package.
|
|
34
|
+
*
|
|
35
|
+
* @param event - The type of event to dispatch, either "scan", "show", or "hide".
|
|
36
|
+
* @param data - The data to be passed to the event listeners.
|
|
37
|
+
* For "scan" events, this should include a `data` string and optionally `metadata`.
|
|
38
|
+
* */
|
|
39
|
+
dispatchEvent(event: EventType, data: {
|
|
40
|
+
data: string;
|
|
41
|
+
metadata?: Record<string, any>;
|
|
42
|
+
}): void;
|
|
43
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export class Dispatcher {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.callbacks = new Map();
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Adds an event listener for the specified event type and id.
|
|
7
|
+
* * @param id - A unique identifier for the event listener. This allows multiple listeners for the same event type.
|
|
8
|
+
* * @param cb - The callback function to be called when the event is dispatched. The type of the callback depends on the event type:
|
|
9
|
+
* */
|
|
10
|
+
add(id, cb) {
|
|
11
|
+
this.callbacks.set(id, cb);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Checks if an event listener exists for the specified event type and id.
|
|
15
|
+
* */
|
|
16
|
+
has(id) {
|
|
17
|
+
return this.callbacks.has(id);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Remove all event listeners.
|
|
21
|
+
*/
|
|
22
|
+
clear() {
|
|
23
|
+
this.callbacks.clear();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Removes an event listener for the specified event type and id.
|
|
27
|
+
*
|
|
28
|
+
* @param event - The type of event to stop listening for, either "scan", "show", or "hide".
|
|
29
|
+
* @param id - The unique identifier for the event listener to be removed. If not provided, all listeners for the specified event type are removed.
|
|
30
|
+
* if no id is provided, all listeners for that event type are removed.
|
|
31
|
+
* */
|
|
32
|
+
remove(id) {
|
|
33
|
+
if (!id && !this.callbacks.size) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (!id) {
|
|
37
|
+
this.callbacks.clear();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.callbacks.delete(id);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fires the specified event, calling all registered callbacks for that event type.
|
|
44
|
+
* */
|
|
45
|
+
dispatch(event) {
|
|
46
|
+
this.callbacks.forEach((cb) => {
|
|
47
|
+
cb(event);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @deprecated
|
|
52
|
+
* This keeps compatibility with the previous dispatch API.
|
|
53
|
+
* Still currently used by the `dust-go` react native package.
|
|
54
|
+
*
|
|
55
|
+
* @param event - The type of event to dispatch, either "scan", "show", or "hide".
|
|
56
|
+
* @param data - The data to be passed to the event listeners.
|
|
57
|
+
* For "scan" events, this should include a `data` string and optionally `metadata`.
|
|
58
|
+
* */
|
|
59
|
+
dispatchEvent(event, data) {
|
|
60
|
+
switch (event) {
|
|
61
|
+
case "scan":
|
|
62
|
+
this.dispatch({
|
|
63
|
+
type: "scan",
|
|
64
|
+
payload: data,
|
|
65
|
+
});
|
|
66
|
+
break;
|
|
67
|
+
case "hide":
|
|
68
|
+
case "show":
|
|
69
|
+
this.dispatch({
|
|
70
|
+
type: event,
|
|
71
|
+
});
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
throw new Error(`Unknown event type: ${event}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ChromeWebView, DustGoConnector, ReactNativeWebView, ScanPayload } from "./types";
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
dustGoConnector?: DustGoConnector;
|
|
5
|
+
ReactNativeWebView?: ReactNativeWebView;
|
|
6
|
+
chrome?: ChromeWebView;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Presents the scanner, and awaits a scan.
|
|
11
|
+
* Rejects if scanner is hidden.
|
|
12
|
+
* */
|
|
13
|
+
export declare const scanAsync: () => Promise<ScanPayload>;
|
|
14
|
+
export declare const connector: DustGoConnector | undefined;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { ChromeWebViewDustGoConnector } from "./chromeWebView";
|
|
3
|
+
import { ReactNativeWebViewDustGoConnector } from "./reactNativeWebView";
|
|
4
|
+
/**
|
|
5
|
+
* Presents the scanner, and awaits a scan.
|
|
6
|
+
* Rejects if scanner is hidden.
|
|
7
|
+
* */
|
|
8
|
+
export const scanAsync = () => {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
if (!window.dustGoConnector) {
|
|
11
|
+
reject("DUST Go connector is not present");
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const id = Math.random().toString(36).substring(2, 15);
|
|
15
|
+
const connector = window.dustGoConnector;
|
|
16
|
+
connector.showScanner();
|
|
17
|
+
const res = (val) => {
|
|
18
|
+
switch (val.type) {
|
|
19
|
+
case "scan":
|
|
20
|
+
resolve(val.payload);
|
|
21
|
+
break;
|
|
22
|
+
case "hide":
|
|
23
|
+
reject("Scanner was hidden");
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
connector.remove(id);
|
|
27
|
+
};
|
|
28
|
+
connector.add(id, res);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
if (ReactNativeWebViewDustGoConnector.detected) {
|
|
32
|
+
globalThis.window.dustGoConnector = new ReactNativeWebViewDustGoConnector();
|
|
33
|
+
}
|
|
34
|
+
else if (ChromeWebViewDustGoConnector.detected) {
|
|
35
|
+
globalThis.window.dustGoConnector = new ChromeWebViewDustGoConnector();
|
|
36
|
+
}
|
|
37
|
+
export const connector = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.dustGoConnector;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Dispatcher } from './dispatcher';
|
|
2
|
+
import { DustGoConnector } from './types';
|
|
3
|
+
export declare class ReactNativeWebViewDustGoConnector extends Dispatcher implements DustGoConnector {
|
|
4
|
+
private appId;
|
|
5
|
+
static detected: boolean;
|
|
6
|
+
constructor();
|
|
7
|
+
rewriteRedirect(url: URL): URL;
|
|
8
|
+
showScanner(): void;
|
|
9
|
+
hideScanner(): void;
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { Dispatcher } from './dispatcher';
|
|
3
|
+
export class ReactNativeWebViewDustGoConnector extends Dispatcher {
|
|
4
|
+
constructor() {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
if (!((_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ReactNativeWebView)) {
|
|
7
|
+
throw new Error('Could not init ReactNativeWebViewDustGoConnector');
|
|
8
|
+
}
|
|
9
|
+
const raw = (_b = globalThis.window) === null || _b === void 0 ? void 0 : _b.ReactNativeWebView.injectedObjectJson();
|
|
10
|
+
const parsed = JSON.parse(raw !== null && raw !== void 0 ? raw : '{}');
|
|
11
|
+
if (!('appId' in parsed) || !(typeof parsed.appId === 'string')) {
|
|
12
|
+
throw new Error('Could not validate injected JSON');
|
|
13
|
+
}
|
|
14
|
+
super();
|
|
15
|
+
this.appId = parsed.appId;
|
|
16
|
+
}
|
|
17
|
+
rewriteRedirect(url) {
|
|
18
|
+
return new URL(url.href.replace(url.protocol, `${this.appId}:`));
|
|
19
|
+
}
|
|
20
|
+
showScanner() {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
(_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ReactNativeWebView) === null || _b === void 0 ? void 0 : _b.postMessage('show');
|
|
23
|
+
}
|
|
24
|
+
hideScanner() {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
(_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ReactNativeWebView) === null || _b === void 0 ? void 0 : _b.postMessage('hide');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
ReactNativeWebViewDustGoConnector.detected = Boolean((_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.ReactNativeWebView);
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type ChromeWebView = {
|
|
2
|
+
webview?: {
|
|
3
|
+
postMessage(val: string): void;
|
|
4
|
+
};
|
|
5
|
+
};
|
|
6
|
+
export type ReactNativeWebView = {
|
|
7
|
+
postMessage(val: string): void;
|
|
8
|
+
injectedObjectJson(): string | undefined;
|
|
9
|
+
};
|
|
10
|
+
export type EventType = "scan" | "hide" | "show";
|
|
11
|
+
export type ScanPayload = {
|
|
12
|
+
data: string;
|
|
13
|
+
metadata?: Record<string, any>;
|
|
14
|
+
};
|
|
15
|
+
export type ScanEvent = {
|
|
16
|
+
type: "scan";
|
|
17
|
+
payload: ScanPayload;
|
|
18
|
+
} | {
|
|
19
|
+
type: "hide";
|
|
20
|
+
} | {
|
|
21
|
+
type: "show";
|
|
22
|
+
};
|
|
23
|
+
export type ScanCallback = (event: ScanEvent) => void;
|
|
24
|
+
export interface DustGoConnector {
|
|
25
|
+
showScanner(): void;
|
|
26
|
+
hideScanner(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Rewrites the URL protocol for redirection on different platforms.
|
|
29
|
+
*
|
|
30
|
+
* e.g. `https://example.com/scan?data=123` to `dustgo://example.com/scan?data=123`
|
|
31
|
+
* */
|
|
32
|
+
rewriteRedirect(url: URL): URL;
|
|
33
|
+
/**
|
|
34
|
+
* Adds an event listener for the specified event type and id.
|
|
35
|
+
* @param id - A unique identifier for the event listener. This allows multiple listeners for the same event type.
|
|
36
|
+
* @param cb - The callback function to be called when the event is dispatched. The type of the callback depends on the event type:
|
|
37
|
+
* */
|
|
38
|
+
add(id: string, cb: ScanCallback): void;
|
|
39
|
+
/**
|
|
40
|
+
* Removes an event listener for the specified event type and id.
|
|
41
|
+
*
|
|
42
|
+
* @param event - The type of event to stop listening for, either "scan", "show", or "hide".
|
|
43
|
+
* @param id - The unique identifier for the event listener to be removed. If not provided, all listeners for the specified event type are removed.
|
|
44
|
+
* if no id is provided, all listeners for that event type are removed.
|
|
45
|
+
* */
|
|
46
|
+
remove(id: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Remove all event listeners.
|
|
49
|
+
*/
|
|
50
|
+
clear(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Checks if an event listener exists for the specified event type and id.
|
|
53
|
+
* */
|
|
54
|
+
has(id: string): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Fires the specified event, calling all registered callbacks for that event type.
|
|
57
|
+
* */
|
|
58
|
+
dispatch(event: ScanEvent): void;
|
|
59
|
+
/**
|
|
60
|
+
* @deprecated
|
|
61
|
+
* This keeps compatibility with the previous dispatch API.
|
|
62
|
+
* Still currently used by the `dust-go` react native package.
|
|
63
|
+
*
|
|
64
|
+
* @param event - The type of event to dispatch, either "scan", "show", or "hide".
|
|
65
|
+
* @param data - The data to be passed to the event listeners.
|
|
66
|
+
* For "scan" events, this should include a `data` string and optionally `metadata`.
|
|
67
|
+
*/
|
|
68
|
+
dispatchEvent(event: EventType, data: {
|
|
69
|
+
data: string;
|
|
70
|
+
metadata?: Record<string, any>;
|
|
71
|
+
}): void;
|
|
72
|
+
}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dustid/dust-go-connect",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A library for connecting a fronten application to the DUST GO react natie application",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -23,6 +23,18 @@
|
|
|
23
23
|
"email": "",
|
|
24
24
|
"url": "https://github.com/sweeneytr"
|
|
25
25
|
},
|
|
26
|
+
"contributors": [
|
|
27
|
+
{
|
|
28
|
+
"name": "Daniel Capecci",
|
|
29
|
+
"email": "dcapecci@dustidentity.com",
|
|
30
|
+
"url": ""
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"name": "Chris Mcgee",
|
|
34
|
+
"email": "",
|
|
35
|
+
"url": ""
|
|
36
|
+
}
|
|
37
|
+
],
|
|
26
38
|
"engines": {
|
|
27
39
|
"node": ">=12.0"
|
|
28
40
|
},
|