@deskcrew/sveltekit 1.0.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/LICENSE +21 -0
- package/README.md +57 -0
- package/build-tag.js +143 -0
- package/index.d.ts +19 -0
- package/index.js +31 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeskCrew
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @deskcrew/sveltekit
|
|
2
|
+
|
|
3
|
+
Add the [DeskCrew](https://deskcrew.io) support widget to a SvelteKit app: live chat, AI answers grounded in your knowledge base, and a help center. One server hook, and every page (server-rendered or prerendered) carries the widget, isolated in a Shadow DOM.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm install @deskcrew/sveltekit
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
In `src/hooks.server.js` (or `.ts`):
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { deskcrewHandle } from '@deskcrew/sveltekit'
|
|
15
|
+
|
|
16
|
+
export const handle = deskcrewHandle({ widgetKey: 'pub_your_widget_key', board: 'your-board' })
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Already have a `handle`? Compose them:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { sequence } from '@sveltejs/kit/hooks'
|
|
23
|
+
import { deskcrewHandle } from '@deskcrew/sveltekit'
|
|
24
|
+
|
|
25
|
+
export const handle = sequence(myHandle, deskcrewHandle({ widgetKey: 'pub_your_widget_key' }))
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Prefer a static tag? Paste the output of `buildTag(options).tag` into `src/app.html` before `</body>`; the hook does the same thing at request time.
|
|
29
|
+
|
|
30
|
+
Get the key from the Install page of your DeskCrew dashboard.
|
|
31
|
+
|
|
32
|
+
## What you get
|
|
33
|
+
|
|
34
|
+
- **AI answers grounded in your own help articles.** The assistant only answers from the knowledge base you publish, so it cannot invent product facts.
|
|
35
|
+
- **A human approves before anything sends.** Every AI draft waits in an approval queue. Nothing reaches a customer unreviewed.
|
|
36
|
+
- **Every conversation becomes a ticket.** Widget chats, emails and board posts land in one dashboard with full history.
|
|
37
|
+
- **Visitors who leave still get answered.** Leave an email address and the reply arrives by email.
|
|
38
|
+
|
|
39
|
+
## Options
|
|
40
|
+
|
|
41
|
+
| Option | Required | Notes |
|
|
42
|
+
| ----------- | -------- | ------------------------------------------------------------------------------------ |
|
|
43
|
+
| `widgetKey` | yes | Your public widget key, `pub_...`, from the Install page of your DeskCrew dashboard. |
|
|
44
|
+
| `board` | no | Your board slug (lowercase letters, numbers, dashes). Enables the feedback link. |
|
|
45
|
+
| `color` | no | Accent colour as a 6-digit hex value, e.g. `#4f46e5`. |
|
|
46
|
+
| `position` | no | `right` (default) or `left`. |
|
|
47
|
+
| `greeting` | no | First line the launcher shows. |
|
|
48
|
+
|
|
49
|
+
Invalid optional values are dropped with a console warning; a missing or malformed key means the widget is not added at all, so a bad config can never put arbitrary markup on your page.
|
|
50
|
+
|
|
51
|
+
## Privacy
|
|
52
|
+
|
|
53
|
+
The only thing this package adds to your site is one `<script>` tag that loads `https://deskcrew.io/desk.js` with your public key. The widget runs inside a Shadow DOM and does not touch your styles. Terms: https://deskcrew.io/terms. Privacy: https://deskcrew.io/privacy.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
package/build-tag.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Framework-agnostic core for the DeskCrew widget packages.
|
|
2
|
+
//
|
|
3
|
+
// Everything here is a pure function of its options: no Astro imports, no I/O,
|
|
4
|
+
// so it can be unit-tested on its own (see test/build-tag.mjs).
|
|
5
|
+
|
|
6
|
+
/** The canonical DeskCrew widget bundle. desk.js derives its API origin from this src. */
|
|
7
|
+
export const WIDGET_SRC = 'https://deskcrew.io/desk.js'
|
|
8
|
+
|
|
9
|
+
// Strict shapes: a value is only ever placed into markup after it matches one of
|
|
10
|
+
// these, so nothing arbitrary can reach the page.
|
|
11
|
+
const KEY_RE = /^pub_[A-Za-z0-9]{8,64}$/
|
|
12
|
+
const BOARD_RE = /^[a-z0-9-]+$/
|
|
13
|
+
const COLOR_RE = /^#[0-9a-fA-F]{6}$/
|
|
14
|
+
const POSITIONS = new Set(['left', 'right'])
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Validate options and produce the ordered [name, value] attribute list for the
|
|
18
|
+
* widget <script>. Returns `{ attrs: null }` (nothing should be injected) when
|
|
19
|
+
* the widget key is missing or malformed; invalid *optional* values are dropped
|
|
20
|
+
* with a warning but never block the widget.
|
|
21
|
+
*
|
|
22
|
+
* @param {Record<string, unknown> | undefined | null} options
|
|
23
|
+
* @returns {{ attrs: string[][] | null, warnings: string[] }}
|
|
24
|
+
*/
|
|
25
|
+
export function buildAttrs(options) {
|
|
26
|
+
const warnings = []
|
|
27
|
+
const opts = options || {}
|
|
28
|
+
|
|
29
|
+
const key = typeof opts.widgetKey === 'string' ? opts.widgetKey.trim() : ''
|
|
30
|
+
if (!key) {
|
|
31
|
+
warnings.push(
|
|
32
|
+
'[deskcrew] No widgetKey was provided. The support widget was NOT added. ' +
|
|
33
|
+
'Copy your pub_ key from https://deskcrew.io/dashboard/install.',
|
|
34
|
+
)
|
|
35
|
+
return { attrs: null, warnings }
|
|
36
|
+
}
|
|
37
|
+
if (!KEY_RE.test(key)) {
|
|
38
|
+
warnings.push(
|
|
39
|
+
`[deskcrew] widgetKey "${key}" is not a valid public key (expected pub_...). ` +
|
|
40
|
+
'the support widget was NOT added.',
|
|
41
|
+
)
|
|
42
|
+
return { attrs: null, warnings }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const attrs = [
|
|
46
|
+
['src', WIDGET_SRC],
|
|
47
|
+
['data-key', key],
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
const board = typeof opts.board === 'string' ? opts.board.trim() : ''
|
|
51
|
+
if (board) {
|
|
52
|
+
if (BOARD_RE.test(board)) attrs.push(['data-board', board])
|
|
53
|
+
else
|
|
54
|
+
warnings.push(`[deskcrew] board "${board}" is not a valid slug (a-z, 0-9, dashes). Ignored.`)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const color = typeof opts.color === 'string' ? opts.color.trim() : ''
|
|
58
|
+
if (color) {
|
|
59
|
+
if (COLOR_RE.test(color)) attrs.push(['data-color', color])
|
|
60
|
+
else
|
|
61
|
+
warnings.push(`[deskcrew] color "${color}" is not a 6-digit hex value like #4f46e5. Ignored.`)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const position = typeof opts.position === 'string' ? opts.position.trim() : ''
|
|
65
|
+
if (position) {
|
|
66
|
+
if (POSITIONS.has(position)) attrs.push(['data-position', position])
|
|
67
|
+
else warnings.push(`[deskcrew] position "${position}" must be "left" or "right". Ignored.`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const greeting = typeof opts.greeting === 'string' ? opts.greeting.trim() : ''
|
|
71
|
+
if (greeting) attrs.push(['data-greeting', greeting])
|
|
72
|
+
|
|
73
|
+
return { attrs, warnings }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Escape a value for use inside a double-quoted HTML attribute.
|
|
78
|
+
* @param {unknown} value
|
|
79
|
+
*/
|
|
80
|
+
function escapeHtmlAttr(value) {
|
|
81
|
+
return String(value)
|
|
82
|
+
.replace(/&/g, '&')
|
|
83
|
+
.replace(/"/g, '"')
|
|
84
|
+
.replace(/</g, '<')
|
|
85
|
+
.replace(/>/g, '>')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Build the canonical `<script ...>` tag string for the widget (handy for
|
|
90
|
+
* debugging or manual placement), or `null` when options are invalid.
|
|
91
|
+
*
|
|
92
|
+
* @param {Record<string, unknown> | undefined | null} options
|
|
93
|
+
* @returns {{ tag: string | null, warnings: string[] }}
|
|
94
|
+
*/
|
|
95
|
+
export function buildTag(options) {
|
|
96
|
+
const { attrs, warnings } = buildAttrs(options)
|
|
97
|
+
if (!attrs) return { tag: null, warnings }
|
|
98
|
+
const rendered = attrs.map(([name, value]) => `${name}="${escapeHtmlAttr(value)}"`).join(' ')
|
|
99
|
+
return { tag: `<script ${rendered} defer></script>`, warnings }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Escape a string for safe embedding as a JS literal inside an inline <script>.
|
|
104
|
+
* `<`, `>` and `&` are escaped so a value can never close the inline script tag.
|
|
105
|
+
* @param {unknown} value
|
|
106
|
+
*/
|
|
107
|
+
function jsLiteral(value) {
|
|
108
|
+
return JSON.stringify(String(value))
|
|
109
|
+
.replace(/</g, '\\u003c')
|
|
110
|
+
.replace(/>/g, '\\u003e')
|
|
111
|
+
.replace(/&/g, '\\u0026')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Astro's `injectScript('head-inline', ...)` takes JS, not a raw external tag,
|
|
116
|
+
* so we emit a tiny bootstrap that creates the widget <script> element and
|
|
117
|
+
* appends it. Returns `{ code: null }` when options are invalid.
|
|
118
|
+
*
|
|
119
|
+
* @param {Record<string, unknown> | undefined | null} options
|
|
120
|
+
* @returns {{ code: string | null, warnings: string[] }}
|
|
121
|
+
*/
|
|
122
|
+
export function buildBootstrap(options) {
|
|
123
|
+
const { attrs, warnings } = buildAttrs(options)
|
|
124
|
+
if (!attrs) return { code: null, warnings }
|
|
125
|
+
|
|
126
|
+
const dataLines = attrs
|
|
127
|
+
.filter(([name]) => name !== 'src')
|
|
128
|
+
.map(([name, value]) => ` s.setAttribute(${jsLiteral(name)}, ${jsLiteral(value)});`)
|
|
129
|
+
.join('\n')
|
|
130
|
+
|
|
131
|
+
const guard = `script[src=${JSON.stringify(WIDGET_SRC)}]`
|
|
132
|
+
const code =
|
|
133
|
+
'(function () {\n' +
|
|
134
|
+
` if (document.querySelector(${jsLiteral(guard)})) return;\n` +
|
|
135
|
+
' var s = document.createElement("script");\n' +
|
|
136
|
+
` s.src = ${jsLiteral(WIDGET_SRC)};\n` +
|
|
137
|
+
' s.defer = true;\n' +
|
|
138
|
+
(dataLines ? dataLines + '\n' : '') +
|
|
139
|
+
' document.head.appendChild(s);\n' +
|
|
140
|
+
'})();'
|
|
141
|
+
|
|
142
|
+
return { code, warnings }
|
|
143
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface DeskcrewOptions {
|
|
2
|
+
/** Your DeskCrew public widget key, e.g. "pub_xxxxxxxx". Required. */
|
|
3
|
+
widgetKey: string
|
|
4
|
+
/** Board slug (lowercase letters, numbers and dashes). Optional. */
|
|
5
|
+
board?: string
|
|
6
|
+
/** Accent colour as a 6-digit hex value, e.g. "#4f46e5". Optional. */
|
|
7
|
+
color?: string
|
|
8
|
+
/** Which side the launcher sits on. Optional (defaults to the widget's own default). */
|
|
9
|
+
position?: 'left' | 'right'
|
|
10
|
+
/** Greeting shown on the launcher. Optional. */
|
|
11
|
+
greeting?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
import type { Handle } from '@sveltejs/kit'
|
|
15
|
+
|
|
16
|
+
/** A `handle` hook for src/hooks.server.js that injects the widget on every HTML page. */
|
|
17
|
+
export function deskcrewHandle(options: DeskcrewOptions): Handle
|
|
18
|
+
export default deskcrewHandle
|
|
19
|
+
export function buildTag(options: DeskcrewOptions): { tag: string | null; warnings: string[] }
|
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { buildTag } from './build-tag.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A SvelteKit `handle` hook that injects the DeskCrew widget <script> before
|
|
5
|
+
* </body> on every HTML page, server-rendered or prerendered.
|
|
6
|
+
*
|
|
7
|
+
* // src/hooks.server.js
|
|
8
|
+
* import { deskcrewHandle } from '@deskcrew/sveltekit'
|
|
9
|
+
* export const handle = deskcrewHandle({ widgetKey: 'pub_...' })
|
|
10
|
+
*
|
|
11
|
+
* Compose with your own hooks through `sequence` from '@sveltejs/kit/hooks'.
|
|
12
|
+
*
|
|
13
|
+
* @param {import('./index.js').DeskcrewOptions} options
|
|
14
|
+
*/
|
|
15
|
+
export function deskcrewHandle(options) {
|
|
16
|
+
const { tag, warnings } = buildTag(options)
|
|
17
|
+
for (const message of warnings) console.warn(message)
|
|
18
|
+
return async ({ event, resolve }) => {
|
|
19
|
+
if (!tag) return resolve(event)
|
|
20
|
+
return resolve(event, {
|
|
21
|
+
transformPageChunk: ({ html, done }) => {
|
|
22
|
+
if (!done) return html
|
|
23
|
+
if (html.includes('https://deskcrew.io/desk.js')) return html
|
|
24
|
+
return html.replace(/<\/body>/i, `${tag}\n</body>`)
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default deskcrewHandle
|
|
31
|
+
export { buildTag }
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deskcrew/sveltekit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SvelteKit server hook that adds the DeskCrew support widget (live chat, AI answers from your knowledge base, a help center) to every page.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"svelte",
|
|
7
|
+
"sveltekit",
|
|
8
|
+
"svelte-kit",
|
|
9
|
+
"hooks",
|
|
10
|
+
"vite",
|
|
11
|
+
"support",
|
|
12
|
+
"helpdesk",
|
|
13
|
+
"help desk",
|
|
14
|
+
"customer support",
|
|
15
|
+
"live chat",
|
|
16
|
+
"chat widget",
|
|
17
|
+
"support widget",
|
|
18
|
+
"ai chat",
|
|
19
|
+
"ai support",
|
|
20
|
+
"ticketing",
|
|
21
|
+
"support tickets",
|
|
22
|
+
"knowledge base",
|
|
23
|
+
"deskcrew"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/webmilmind1/sveltekit-deskcrew.git"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://deskcrew.io/integrations/sveltekit",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/webmilmind1/sveltekit-deskcrew/issues",
|
|
32
|
+
"email": "hello@deskcrew.io"
|
|
33
|
+
},
|
|
34
|
+
"author": "DeskCrew <hello@deskcrew.io> (https://deskcrew.io)",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./index.d.ts",
|
|
40
|
+
"import": "./index.js",
|
|
41
|
+
"default": "./index.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"main": "index.js",
|
|
45
|
+
"types": "index.d.ts",
|
|
46
|
+
"files": [
|
|
47
|
+
"index.js",
|
|
48
|
+
"build-tag.js",
|
|
49
|
+
"index.d.ts",
|
|
50
|
+
"README.md",
|
|
51
|
+
"LICENSE"
|
|
52
|
+
],
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18"
|
|
55
|
+
},
|
|
56
|
+
"scripts": {
|
|
57
|
+
"test": "node --test"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|