@dobrodesk/widget 0.1.0
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/README.md +33 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +91 -0
- package/dist/types.d.ts +35 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @dobrodesk/widget
|
|
2
|
+
|
|
3
|
+
Browser SDK for the [DobroDesk](https://dobrodesk.com) website support widget.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @dobrodesk/widget
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Get the Widget ID
|
|
12
|
+
|
|
13
|
+
In DobroDesk open **Admin > Channels > Website widget**. Create a widget or select an existing one,
|
|
14
|
+
then copy its **Widget ID**.
|
|
15
|
+
|
|
16
|
+
## Initialize
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { createDobroDeskWidget } from "@dobrodesk/widget";
|
|
20
|
+
|
|
21
|
+
const widget = createDobroDeskWidget({
|
|
22
|
+
integrationId: "acme-support",
|
|
23
|
+
locale: "auto",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
await widget.ready;
|
|
27
|
+
widget.open();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`integrationId` is the Widget ID from DobroDesk Admin. Initialize the SDK in browser-side code.
|
|
31
|
+
|
|
32
|
+
Full setup, API and localization instructions:
|
|
33
|
+
[dobrodesk.com/help/initialize-dobrodesk-widget-programmatically](https://dobrodesk.com/help/initialize-dobrodesk-widget-programmatically).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DobroDeskWidgetClient, DobroDeskWidgetCommandFunction, DobroDeskWidgetErrorDetail, DobroDeskWidgetOptions, DobroDeskWidgetReadyDetail } from "./types";
|
|
2
|
+
declare global {
|
|
3
|
+
interface Window {
|
|
4
|
+
DobroDeskWidget?: DobroDeskWidgetCommandFunction | unknown[][];
|
|
5
|
+
}
|
|
6
|
+
interface WindowEventMap {
|
|
7
|
+
"dobrodesk:error": CustomEvent<DobroDeskWidgetErrorDetail>;
|
|
8
|
+
"dobrodesk:ready": CustomEvent<DobroDeskWidgetReadyDetail>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export declare const createDobroDeskWidget: (options: DobroDeskWidgetOptions) => DobroDeskWidgetClient;
|
|
12
|
+
export type { DobroDeskWidgetClient, DobroDeskWidgetCommand, DobroDeskWidgetOptions, DobroDeskWidgetPrefill, } from "./types";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// packages/widget-sdk/src/index.ts
|
|
2
|
+
var DEFAULT_LOADER_URL = "https://dobrodesk.com/widget/loader/v1.min.js";
|
|
3
|
+
var activeClient = null;
|
|
4
|
+
var requireBrowser = () => {
|
|
5
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
6
|
+
throw new Error("DobroDesk widget initialization requires a browser document.");
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
var requireIntegrationId = (integrationId) => {
|
|
10
|
+
const normalizedIntegrationId = integrationId.trim();
|
|
11
|
+
if (!normalizedIntegrationId) {
|
|
12
|
+
throw new Error("DobroDesk integrationId is required.");
|
|
13
|
+
}
|
|
14
|
+
return normalizedIntegrationId;
|
|
15
|
+
};
|
|
16
|
+
var queueCommand = (command, value) => {
|
|
17
|
+
if (typeof window.DobroDeskWidget === "function") {
|
|
18
|
+
window.DobroDeskWidget(command, value);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const queue = Array.isArray(window.DobroDeskWidget) ? window.DobroDeskWidget : [];
|
|
22
|
+
queue.push(value === undefined ? [command] : [command, value]);
|
|
23
|
+
window.DobroDeskWidget = queue;
|
|
24
|
+
};
|
|
25
|
+
var createDobroDeskWidget = (options) => {
|
|
26
|
+
requireBrowser();
|
|
27
|
+
if (activeClient) {
|
|
28
|
+
throw new Error("DobroDesk widget has already been initialized on this page.");
|
|
29
|
+
}
|
|
30
|
+
const integrationId = requireIntegrationId(options.integrationId);
|
|
31
|
+
const locale = options.locale?.trim() || "auto";
|
|
32
|
+
const loaderUrl = options.loaderUrl?.trim() || DEFAULT_LOADER_URL;
|
|
33
|
+
const script = document.createElement("script");
|
|
34
|
+
script.async = true;
|
|
35
|
+
script.src = loaderUrl;
|
|
36
|
+
script.dataset.dobrodeskIntegration = integrationId;
|
|
37
|
+
script.dataset.dobrodeskLocale = locale;
|
|
38
|
+
script.dataset.dobrodeskLoader = "true";
|
|
39
|
+
if (options.prefill) {
|
|
40
|
+
queueCommand("prefill", options.prefill);
|
|
41
|
+
}
|
|
42
|
+
if (options.openOnReady) {
|
|
43
|
+
queueCommand("open");
|
|
44
|
+
}
|
|
45
|
+
const ready = new Promise((resolve, reject) => {
|
|
46
|
+
const cleanup = () => {
|
|
47
|
+
window.removeEventListener("dobrodesk:ready", handleReady);
|
|
48
|
+
window.removeEventListener("dobrodesk:error", handleError);
|
|
49
|
+
script.removeEventListener("error", handleScriptError);
|
|
50
|
+
};
|
|
51
|
+
const handleReady = (event) => {
|
|
52
|
+
if (event.detail.integrationId !== integrationId)
|
|
53
|
+
return;
|
|
54
|
+
cleanup();
|
|
55
|
+
resolve();
|
|
56
|
+
};
|
|
57
|
+
const handleError = (event) => {
|
|
58
|
+
if (event.detail.integrationId !== integrationId)
|
|
59
|
+
return;
|
|
60
|
+
cleanup();
|
|
61
|
+
reject(event.detail.error);
|
|
62
|
+
};
|
|
63
|
+
const handleScriptError = () => {
|
|
64
|
+
cleanup();
|
|
65
|
+
reject(new Error(`DobroDesk widget loader could not be loaded from ${loaderUrl}.`));
|
|
66
|
+
};
|
|
67
|
+
window.addEventListener("dobrodesk:ready", handleReady);
|
|
68
|
+
window.addEventListener("dobrodesk:error", handleError);
|
|
69
|
+
script.addEventListener("error", handleScriptError, { once: true });
|
|
70
|
+
});
|
|
71
|
+
const client = {
|
|
72
|
+
ready,
|
|
73
|
+
open: () => queueCommand("open"),
|
|
74
|
+
close: () => queueCommand("close"),
|
|
75
|
+
toggle: () => queueCommand("toggle"),
|
|
76
|
+
hide: () => queueCommand("hide"),
|
|
77
|
+
show: () => queueCommand("show"),
|
|
78
|
+
prefill: (value) => queueCommand("prefill", value),
|
|
79
|
+
setLocale: (value) => queueCommand("setLocale", value),
|
|
80
|
+
destroy: () => {
|
|
81
|
+
queueCommand("destroy");
|
|
82
|
+
script.remove();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
activeClient = client;
|
|
86
|
+
document.head.append(script);
|
|
87
|
+
return client;
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
createDobroDeskWidget
|
|
91
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type DobroDeskWidgetPrefill = {
|
|
2
|
+
readonly name?: string;
|
|
3
|
+
readonly email?: string;
|
|
4
|
+
readonly subject?: string;
|
|
5
|
+
readonly message?: string;
|
|
6
|
+
readonly fields?: Readonly<Record<string, string | boolean>>;
|
|
7
|
+
};
|
|
8
|
+
export type DobroDeskWidgetOptions = {
|
|
9
|
+
readonly integrationId: string;
|
|
10
|
+
readonly locale?: string;
|
|
11
|
+
readonly loaderUrl?: string;
|
|
12
|
+
readonly prefill?: DobroDeskWidgetPrefill;
|
|
13
|
+
readonly openOnReady?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type DobroDeskWidgetCommand = "open" | "close" | "toggle" | "hide" | "show" | "prefill" | "setLocale" | "destroy";
|
|
16
|
+
export type DobroDeskWidgetCommandFunction = (command: DobroDeskWidgetCommand, value?: DobroDeskWidgetPrefill | string) => void;
|
|
17
|
+
export type DobroDeskWidgetClient = {
|
|
18
|
+
readonly ready: Promise<void>;
|
|
19
|
+
readonly open: () => void;
|
|
20
|
+
readonly close: () => void;
|
|
21
|
+
readonly toggle: () => void;
|
|
22
|
+
readonly hide: () => void;
|
|
23
|
+
readonly show: () => void;
|
|
24
|
+
readonly prefill: (value: DobroDeskWidgetPrefill) => void;
|
|
25
|
+
readonly setLocale: (locale: string) => void;
|
|
26
|
+
readonly destroy: () => void;
|
|
27
|
+
};
|
|
28
|
+
export type DobroDeskWidgetReadyDetail = {
|
|
29
|
+
readonly integrationId: string;
|
|
30
|
+
readonly locale: string;
|
|
31
|
+
};
|
|
32
|
+
export type DobroDeskWidgetErrorDetail = {
|
|
33
|
+
readonly integrationId: string;
|
|
34
|
+
readonly error: unknown;
|
|
35
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dobrodesk/widget",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Programmatic browser SDK for the DobroDesk website support widget.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"customer-support",
|
|
7
|
+
"support-widget",
|
|
8
|
+
"website-widget"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://dobrodesk.com",
|
|
11
|
+
"author": "Comstudio",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/comstud-io/dobrodesk.git",
|
|
15
|
+
"directory": "packages/widget-sdk"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"registry": "https://registry.npmjs.org"
|
|
33
|
+
}
|
|
34
|
+
}
|