@veroxm/sdk 1.14.0 → 1.15.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/CHANGELOG.md +9 -0
- package/dist/client.d.ts +10 -0
- package/dist/client.js +9 -1
- package/dist/embed.d.ts +24 -2
- package/dist/embed.js +44 -1
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +4 -2
- package/dist/generated/api.d.ts +1054 -31
- package/dist/http.d.ts +2 -0
- package/dist/http.js +18 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/push.d.ts +32 -0
- package/dist/push.js +26 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.15.0
|
|
4
|
+
|
|
5
|
+
- Form embeds for targeted surveys: `openOn: { on: 'exit' }` opens a popup or toast when the visitor looks like leaving
|
|
6
|
+
(the pointer leaves through the top of the window; on touch screens, a quick scroll back up after reading a screen
|
|
7
|
+
or more), also as `data-open-on="exit"`. `survey` passes a survey card's token to the form, so the response is linked
|
|
8
|
+
to its survey, test and variant (VeroXM's script tag does this for you), and `onOpen` says when it opened.
|
|
9
|
+
|
|
3
10
|
## 1.14.0
|
|
4
11
|
|
|
12
|
+
- Push notifications: `app.push.register(device)`, `unregister(token)`, and for server keys `send(message, { idempotencyKey })` and `status(pushId)` (`PushResource`, types `PushDevice`, `PushDeviceRegistered`, `PushMessageInput`, `PushStatus`).
|
|
13
|
+
- Errors in the user's language: `initializeApp({ ..., locale: 'ar' })` and `app.setLocale()` send `X-VeroXM-Locale` (else the browser's Accept-Language); `VeroXMApiError.userMessage` has the words to show, and `fieldErrors` what to show under each field of an entry that didn't validate.
|
|
5
14
|
- Private files: `app.files` (`list`, `all`, `upload`, `get`, `delete`, `link`, `grant`) with a server key that has
|
|
6
15
|
"Read private files" / "Write private files", and `uploadWithGrant(uploadUrl, file)` for a browser or app with a
|
|
7
16
|
one-time upload link (no key). Types `StoredFile`, `StoredFilePage`, `StoredFileLink`, `FileGrant`,
|
package/dist/client.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { ProcessesResource } from './processes.js';
|
|
|
8
8
|
import { FormsResource } from './forms.js';
|
|
9
9
|
import { FilesResource } from './files.js';
|
|
10
10
|
import { TranslationsResource } from './translations.js';
|
|
11
|
+
import { PushResource } from './push.js';
|
|
11
12
|
import { type TokenStorage } from './token-storage.js';
|
|
12
13
|
export interface VeroXMAppOptions {
|
|
13
14
|
/** Your VeroXM deployment's origin, e.g. "https://cms.example.com" -- no trailing /public/... needed, VeroXMApp builds that itself. */
|
|
@@ -29,6 +30,11 @@ export interface VeroXMAppOptions {
|
|
|
29
30
|
* Default: 0. A function answering GET runs again on a retry, so keep those safe to repeat.
|
|
30
31
|
*/
|
|
31
32
|
retries?: number;
|
|
33
|
+
/**
|
|
34
|
+
* The language error answers are worded in for your users (`VeroXMApiError.userMessage`), such as "ar". Default: the
|
|
35
|
+
* browser's own languages (Accept-Language), else English. Change it with `app.setLocale()`.
|
|
36
|
+
*/
|
|
37
|
+
locale?: string;
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
40
|
* The SDK's entry point -- one `initializeApp()` call, Firebase-style,
|
|
@@ -49,11 +55,15 @@ export declare class VeroXMApp {
|
|
|
49
55
|
readonly files: FilesResource;
|
|
50
56
|
/** The project's app translations: a locale's whole bundle in one request (any key, a browser key too). */
|
|
51
57
|
readonly translations: TranslationsResource;
|
|
58
|
+
/** Push notifications: register this device (any key, or signed in), or send (server keys with push:send). */
|
|
59
|
+
readonly push: PushResource;
|
|
52
60
|
private readonly apiBase;
|
|
53
61
|
private readonly timeoutMs;
|
|
54
62
|
private readonly retries;
|
|
55
63
|
private constructor();
|
|
56
64
|
static initializeApp(options: VeroXMAppOptions): VeroXMApp;
|
|
65
|
+
/** The language error answers are worded in from now on (`VeroXMApiError.userMessage`); null: the browser's own. */
|
|
66
|
+
setLocale(locale: string | null): void;
|
|
57
67
|
/** Typed access to one content collection's entries (list/get/create/update/delete/search). */
|
|
58
68
|
content<T = Record<string, unknown>>(collectionSlug: string): ContentResource<T>;
|
|
59
69
|
/** This project's media library (list/upload/updateCaption/delete). */
|
package/dist/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { checkRetries, checkTimeout, rawRequest } from './http.js';
|
|
1
|
+
import { checkRetries, checkTimeout, rawRequest, setRequestLocale } from './http.js';
|
|
2
2
|
import { VeroXMApiError } from './errors.js';
|
|
3
3
|
import { VeroXMAuth } from './auth.js';
|
|
4
4
|
import { ContentResource } from './content.js';
|
|
@@ -9,6 +9,7 @@ import { ProcessesResource } from './processes.js';
|
|
|
9
9
|
import { FormsResource } from './forms.js';
|
|
10
10
|
import { FilesResource } from './files.js';
|
|
11
11
|
import { TranslationsResource } from './translations.js';
|
|
12
|
+
import { PushResource } from './push.js';
|
|
12
13
|
import { MemoryTokenStorage, BrowserLocalStorageTokenStorage } from './token-storage.js';
|
|
13
14
|
function defaultTokenStorage(projectId) {
|
|
14
15
|
if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {
|
|
@@ -33,17 +34,24 @@ export class VeroXMApp {
|
|
|
33
34
|
this.forms = new FormsResource(this);
|
|
34
35
|
this.files = new FilesResource(this);
|
|
35
36
|
this.translations = new TranslationsResource(this);
|
|
37
|
+
this.push = new PushResource(this);
|
|
36
38
|
}
|
|
37
39
|
static initializeApp(options) {
|
|
38
40
|
checkTimeout(options.timeoutMs);
|
|
39
41
|
checkRetries(options.retries);
|
|
40
42
|
const base = options.baseUrl.replace(/\/$/, '');
|
|
41
43
|
const apiBase = `${base}/public/v2/projects/${options.projectId}`;
|
|
44
|
+
if (options.locale !== undefined)
|
|
45
|
+
setRequestLocale(apiBase, options.locale);
|
|
42
46
|
return new VeroXMApp(apiBase, options.tokenStorage ?? defaultTokenStorage(options.projectId), options.pendingStore, {
|
|
43
47
|
timeoutMs: options.timeoutMs,
|
|
44
48
|
retries: options.retries,
|
|
45
49
|
});
|
|
46
50
|
}
|
|
51
|
+
/** The language error answers are worded in from now on (`VeroXMApiError.userMessage`); null: the browser's own. */
|
|
52
|
+
setLocale(locale) {
|
|
53
|
+
setRequestLocale(this.apiBase, locale);
|
|
54
|
+
}
|
|
47
55
|
/** Typed access to one content collection's entries (list/get/create/update/delete/search). */
|
|
48
56
|
content(collectionSlug) {
|
|
49
57
|
return new ContentResource(this, collectionSlug);
|
package/dist/embed.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export type FormEmbedMode = 'inline' | 'popup' | 'fullpage' | 'toast';
|
|
2
2
|
export interface FormEmbedOpenTrigger {
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* When a popup or toast opens by itself: on page load, after `delaySeconds`, once the visitor has scrolled
|
|
5
|
+
* `scrollPercent` of the page, or when they look like leaving (`exit`: the pointer leaves through the top of the
|
|
6
|
+
* window; on touch screens, a quick scroll back up after reading a screen or more). Default: manual (call `open()`,
|
|
7
|
+
* or use `trigger`). `exit` since 1.15.0.
|
|
8
|
+
*/
|
|
9
|
+
on?: 'load' | 'delay' | 'scroll' | 'exit' | 'manual';
|
|
5
10
|
delaySeconds?: number;
|
|
6
11
|
scrollPercent?: number;
|
|
7
12
|
}
|
|
@@ -36,12 +41,23 @@ export interface FormEmbedOptions {
|
|
|
36
41
|
autoCloseSeconds?: number;
|
|
37
42
|
/** The iframe's accessible name (default "Form"). */
|
|
38
43
|
title?: string;
|
|
44
|
+
/**
|
|
45
|
+
* A survey card's token (VeroXM's script tag passes it for targeted surveys): sent with the response, so it is linked
|
|
46
|
+
* to its survey, test and variant.
|
|
47
|
+
* @since 1.15.0
|
|
48
|
+
*/
|
|
49
|
+
survey?: string;
|
|
39
50
|
/**
|
|
40
51
|
* Forms that need signing in: the signed-in person's access token for this project (from @veroxm/sdk's
|
|
41
52
|
* `app.auth.getAccessToken()`). Called when the form asks; return null if nobody is signed in.
|
|
42
53
|
*/
|
|
43
54
|
getToken?: () => string | null | undefined | Promise<string | null | undefined>;
|
|
44
55
|
onReady?: () => void;
|
|
56
|
+
/**
|
|
57
|
+
* The popup or toast opened (by itself or with `open()`).
|
|
58
|
+
* @since 1.15.0
|
|
59
|
+
*/
|
|
60
|
+
onOpen?: () => void;
|
|
45
61
|
/** The first answer. */
|
|
46
62
|
onStarted?: () => void;
|
|
47
63
|
/** The form was sent. */
|
|
@@ -73,6 +89,12 @@ export interface FormEmbed {
|
|
|
73
89
|
/** Remove everything this embed added to the page. */
|
|
74
90
|
destroy(): void;
|
|
75
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Calls `fire` once when the visitor looks like leaving: the pointer leaves through the top of the window (towards the
|
|
94
|
+
* tabs and address bar), or, on touch screens, a quick scroll back up (40% of the screen within half a second) after
|
|
95
|
+
* reading at least one screen. Returns a function that stops listening.
|
|
96
|
+
*/
|
|
97
|
+
export declare function onExitIntent(fire: () => void): () => void;
|
|
76
98
|
/**
|
|
77
99
|
* Shows a VeroXM form in the page. Returns a handle to open, close or remove it.
|
|
78
100
|
*
|
package/dist/embed.js
CHANGED
|
@@ -65,6 +65,43 @@ function storageSet(key, value) {
|
|
|
65
65
|
// Storage blocked: the toast just shows again next time.
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Calls `fire` once when the visitor looks like leaving: the pointer leaves through the top of the window (towards the
|
|
70
|
+
* tabs and address bar), or, on touch screens, a quick scroll back up (40% of the screen within half a second) after
|
|
71
|
+
* reading at least one screen. Returns a function that stops listening.
|
|
72
|
+
*/
|
|
73
|
+
export function onExitIntent(fire) {
|
|
74
|
+
let done = false;
|
|
75
|
+
let deepest = 0;
|
|
76
|
+
let mark = { y: window.scrollY, t: Date.now() };
|
|
77
|
+
const stop = () => {
|
|
78
|
+
document.removeEventListener('mouseout', onOut);
|
|
79
|
+
window.removeEventListener('scroll', onScroll);
|
|
80
|
+
};
|
|
81
|
+
const go = () => {
|
|
82
|
+
if (done)
|
|
83
|
+
return;
|
|
84
|
+
done = true;
|
|
85
|
+
stop();
|
|
86
|
+
fire();
|
|
87
|
+
};
|
|
88
|
+
const onOut = (e) => {
|
|
89
|
+
if (!e.relatedTarget && e.clientY <= 0)
|
|
90
|
+
go();
|
|
91
|
+
};
|
|
92
|
+
const onScroll = () => {
|
|
93
|
+
const y = window.scrollY;
|
|
94
|
+
const now = Date.now();
|
|
95
|
+
deepest = Math.max(deepest, y);
|
|
96
|
+
if (now - mark.t > 500 || y > mark.y)
|
|
97
|
+
mark = { y, t: now };
|
|
98
|
+
else if (deepest >= window.innerHeight && mark.y - y >= window.innerHeight * 0.4)
|
|
99
|
+
go();
|
|
100
|
+
};
|
|
101
|
+
document.addEventListener('mouseout', onOut);
|
|
102
|
+
window.addEventListener('scroll', onScroll, { passive: true });
|
|
103
|
+
return stop;
|
|
104
|
+
}
|
|
68
105
|
/**
|
|
69
106
|
* Shows a VeroXM form in the page. Returns a handle to open, close or remove it.
|
|
70
107
|
*
|
|
@@ -100,6 +137,8 @@ export function embedForm(options) {
|
|
|
100
137
|
url.searchParams.set('vx_page', window.location.href.slice(0, 1500));
|
|
101
138
|
if (document.referrer)
|
|
102
139
|
url.searchParams.set('vx_ref', document.referrer.slice(0, 500));
|
|
140
|
+
if (options.survey)
|
|
141
|
+
url.searchParams.set('vx_survey', options.survey);
|
|
103
142
|
const iframe = el('iframe', 'vxe-frame');
|
|
104
143
|
iframe.title = options.title ?? 'Form';
|
|
105
144
|
iframe.allow = 'clipboard-write';
|
|
@@ -223,6 +262,7 @@ export function embedForm(options) {
|
|
|
223
262
|
shell?.classList.add('vxe-in');
|
|
224
263
|
iframe.focus();
|
|
225
264
|
});
|
|
265
|
+
options.onOpen?.();
|
|
226
266
|
},
|
|
227
267
|
close() {
|
|
228
268
|
if (closeTimer)
|
|
@@ -289,6 +329,9 @@ export function embedForm(options) {
|
|
|
289
329
|
cleanups.push(() => window.removeEventListener('scroll', onScroll));
|
|
290
330
|
onScroll();
|
|
291
331
|
}
|
|
332
|
+
else if (auto === 'exit') {
|
|
333
|
+
cleanups.push(onExitIntent(() => handle.open()));
|
|
334
|
+
}
|
|
292
335
|
}
|
|
293
336
|
return handle;
|
|
294
337
|
}
|
|
@@ -328,7 +371,7 @@ export function embedFormsFromPage(baseUrl, root = document) {
|
|
|
328
371
|
autoHeight: d.autoHeight === 'true',
|
|
329
372
|
size: d.size === 'small' || d.size === 'large' ? d.size : undefined,
|
|
330
373
|
position: d.position === 'bottom-left' ? 'bottom-left' : undefined,
|
|
331
|
-
openOn: { on: ['load', 'delay', 'scroll', 'manual'].find((o) => o === d.openOn) ?? 'manual', delaySeconds: num(d.delay), scrollPercent: num(d.scroll) },
|
|
374
|
+
openOn: { on: ['load', 'delay', 'scroll', 'exit', 'manual'].find((o) => o === d.openOn) ?? 'manual', delaySeconds: num(d.delay), scrollPercent: num(d.scroll) },
|
|
332
375
|
trigger: (mode === 'popup' || mode === 'toast') && node.tagName !== 'SCRIPT' ? node : undefined,
|
|
333
376
|
rememberDismissDays: num(d.rememberDays),
|
|
334
377
|
autoCloseSeconds: num(d.autoClose),
|
package/dist/errors.d.ts
CHANGED
|
@@ -15,6 +15,13 @@ export declare class VeroXMApiError extends Error {
|
|
|
15
15
|
* `sign_up_closed`, `too_many_attempts`, `access_denied`, `function_limit`.
|
|
16
16
|
*/
|
|
17
17
|
readonly code: string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The words to show your app's user, in their language (the app's `locale`, else the browser's): the project's own
|
|
20
|
+
* (app translations, `errors.<code>`) or VeroXM's English and Arabic. `message` stays the developer's English.
|
|
21
|
+
*/
|
|
22
|
+
readonly userMessage: string | undefined;
|
|
23
|
+
/** For an entry that didn't validate (`code` "validation_failed"): by field, what to show under it. */
|
|
24
|
+
readonly fieldErrors: Record<string, string> | undefined;
|
|
18
25
|
constructor(status: number, body: unknown);
|
|
19
26
|
private static messageFrom;
|
|
20
27
|
}
|
package/dist/errors.js
CHANGED
|
@@ -13,8 +13,10 @@ export class VeroXMApiError extends Error {
|
|
|
13
13
|
this.name = 'VeroXMApiError';
|
|
14
14
|
this.status = status;
|
|
15
15
|
this.body = body;
|
|
16
|
-
const
|
|
17
|
-
this.code = typeof
|
|
16
|
+
const b = body && typeof body === 'object' ? body : {};
|
|
17
|
+
this.code = typeof b.code === 'string' ? b.code : undefined;
|
|
18
|
+
this.userMessage = typeof b.userMessage === 'string' ? b.userMessage : undefined;
|
|
19
|
+
this.fieldErrors = b.fieldErrors && typeof b.fieldErrors === 'object' && !Array.isArray(b.fieldErrors) ? b.fieldErrors : undefined;
|
|
18
20
|
}
|
|
19
21
|
static messageFrom(status, body) {
|
|
20
22
|
if (body && typeof body === 'object') {
|