@veroxm/sdk 1.2.0 → 1.4.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 +22 -0
- package/README.md +42 -1
- package/dist/auth.d.ts +67 -1
- package/dist/auth.js +89 -1
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +23 -0
- package/dist/generated/api.d.ts +1158 -119
- package/dist/http.js +2 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +2 -1
- package/dist/passkeys.d.ts +32 -0
- package/dist/passkeys.js +84 -0
- package/dist/tracker.d.ts +9 -0
- package/dist/tracker.js +22 -1
- package/package.json +1 -1
package/dist/http.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { apiError } from './errors.js';
|
|
2
2
|
function safeJsonParse(text) {
|
|
3
3
|
try {
|
|
4
4
|
return JSON.parse(text);
|
|
@@ -36,7 +36,7 @@ export async function rawRequest(apiBase, opts) {
|
|
|
36
36
|
const text = await res.text();
|
|
37
37
|
const parsed = text ? safeJsonParse(text) : null;
|
|
38
38
|
if (!res.ok) {
|
|
39
|
-
throw
|
|
39
|
+
throw apiError(res.status, parsed ?? text);
|
|
40
40
|
}
|
|
41
41
|
return parsed;
|
|
42
42
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { VeroXMApp } from './client.js';
|
|
2
2
|
export type { VeroXMAppOptions } from './client.js';
|
|
3
3
|
export { VeroXMAuth } from './auth.js';
|
|
4
|
-
export type { Account, EmailCode, PendingStore, SocialProvider, SocialSignInStart } from './auth.js';
|
|
4
|
+
export type { Account, AuthenticatorSetup, EmailCode, Passkey, PendingStore, SecondStep, SocialProvider, SocialSignInStart, TwoStepStatus, WithTicket, } from './auth.js';
|
|
5
|
+
export { browserPasskeys, creationOptionsFromJSON, credentialToJSON, requestOptionsFromJSON } from './passkeys.js';
|
|
6
|
+
export type { PasskeyOptionsJSON, PasskeyProvider } from './passkeys.js';
|
|
5
7
|
export { createPkce } from './pkce.js';
|
|
6
8
|
export { ContentResource } from './content.js';
|
|
7
9
|
export type { ListOptions } from './content.js';
|
|
@@ -14,7 +16,8 @@ export { ProcessesResource } from './processes.js';
|
|
|
14
16
|
export type { ProcessRun, ProcessRunDetail, ProcessRunListOptions } from './processes.js';
|
|
15
17
|
export type { Schemas } from './schemas.js';
|
|
16
18
|
export type { paths as ApiPaths, components as ApiComponents, operations as ApiOperations } from './generated/api.js';
|
|
17
|
-
export { VeroXMApiError, VeroXMNotAuthenticatedError } from './errors.js';
|
|
19
|
+
export { VeroXMApiError, VeroXMNotAuthenticatedError, VeroXMTwoStepRequiredError } from './errors.js';
|
|
20
|
+
export type { TwoStepMethod } from './errors.js';
|
|
18
21
|
export { MemoryTokenStorage, BrowserLocalStorageTokenStorage } from './token-storage.js';
|
|
19
22
|
export type { TokenStorage, StoredSession, AuthUser } from './token-storage.js';
|
|
20
23
|
export { VeroxmTracker, createTracker } from './tracker.js';
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export { VeroXMApp } from './client.js';
|
|
2
2
|
export { VeroXMAuth } from './auth.js';
|
|
3
|
+
export { browserPasskeys, creationOptionsFromJSON, credentialToJSON, requestOptionsFromJSON } from './passkeys.js';
|
|
3
4
|
export { createPkce } from './pkce.js';
|
|
4
5
|
export { ContentResource } from './content.js';
|
|
5
6
|
export { MediaResource } from './media.js';
|
|
6
7
|
export { FunctionsResource } from './functions.js';
|
|
7
8
|
export { TasksResource } from './tasks.js';
|
|
8
9
|
export { ProcessesResource } from './processes.js';
|
|
9
|
-
export { VeroXMApiError, VeroXMNotAuthenticatedError } from './errors.js';
|
|
10
|
+
export { VeroXMApiError, VeroXMNotAuthenticatedError, VeroXMTwoStepRequiredError } from './errors.js';
|
|
10
11
|
export { MemoryTokenStorage, BrowserLocalStorageTokenStorage } from './token-storage.js';
|
|
11
12
|
// Experience OS visitor tracking (page views, actions, consent, identify).
|
|
12
13
|
export { VeroxmTracker, createTracker } from './tracker.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Options from VeroXM, as JSON (PublicKeyCredentialCreationOptionsJSON or ...RequestOptionsJSON). */
|
|
2
|
+
export type PasskeyOptionsJSON = Record<string, unknown> & {
|
|
3
|
+
challenge: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Makes and uses passkeys: the browser's navigator.credentials by default. In React Native or another runtime, pass
|
|
7
|
+
* one that calls the platform's passkey API (most take and return these same JSON forms).
|
|
8
|
+
*/
|
|
9
|
+
export interface PasskeyProvider {
|
|
10
|
+
create(options: PasskeyOptionsJSON): Promise<Record<string, unknown>>;
|
|
11
|
+
get(options: PasskeyOptionsJSON): Promise<Record<string, unknown>>;
|
|
12
|
+
}
|
|
13
|
+
/** JSON creation options as the browser takes them (binary fields as ArrayBuffers). */
|
|
14
|
+
export declare function creationOptionsFromJSON(o: PasskeyOptionsJSON): Record<string, unknown>;
|
|
15
|
+
/** JSON request options as the browser takes them. */
|
|
16
|
+
export declare function requestOptionsFromJSON(o: PasskeyOptionsJSON): Record<string, unknown>;
|
|
17
|
+
interface CredentialLike {
|
|
18
|
+
id: string;
|
|
19
|
+
rawId: ArrayBuffer;
|
|
20
|
+
type: string;
|
|
21
|
+
authenticatorAttachment?: string | null;
|
|
22
|
+
response: Record<string, unknown> & {
|
|
23
|
+
getTransports?: () => string[];
|
|
24
|
+
};
|
|
25
|
+
getClientExtensionResults?: () => Record<string, unknown>;
|
|
26
|
+
toJSON?: () => Record<string, unknown>;
|
|
27
|
+
}
|
|
28
|
+
/** A browser PublicKeyCredential as the JSON VeroXM takes. */
|
|
29
|
+
export declare function credentialToJSON(cred: CredentialLike): Record<string, unknown>;
|
|
30
|
+
/** The browser's passkeys (navigator.credentials). Throws where there are none. */
|
|
31
|
+
export declare const browserPasskeys: PasskeyProvider;
|
|
32
|
+
export {};
|
package/dist/passkeys.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Passkeys in the browser: VeroXM sends WebAuthn options as JSON and takes the browser's answer as JSON. Browsers
|
|
2
|
+
// that have PublicKeyCredential.parseCreationOptionsFromJSON do the conversion themselves; for the rest, the binary
|
|
3
|
+
// fields are converted here.
|
|
4
|
+
const toBytes = (b64url) => {
|
|
5
|
+
const b64 = b64url.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(b64url.length / 4) * 4, '=');
|
|
6
|
+
const bin = atob(b64);
|
|
7
|
+
const out = new Uint8Array(bin.length);
|
|
8
|
+
for (let i = 0; i < bin.length; i++)
|
|
9
|
+
out[i] = bin.charCodeAt(i);
|
|
10
|
+
return out.buffer;
|
|
11
|
+
};
|
|
12
|
+
const toB64url = (buf) => {
|
|
13
|
+
const bytes = buf instanceof ArrayBuffer ? new Uint8Array(buf) : new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
14
|
+
let bin = '';
|
|
15
|
+
for (const b of bytes)
|
|
16
|
+
bin += String.fromCharCode(b);
|
|
17
|
+
return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
18
|
+
};
|
|
19
|
+
/** JSON creation options as the browser takes them (binary fields as ArrayBuffers). */
|
|
20
|
+
export function creationOptionsFromJSON(o) {
|
|
21
|
+
const user = o.user;
|
|
22
|
+
return {
|
|
23
|
+
...o,
|
|
24
|
+
challenge: toBytes(o.challenge),
|
|
25
|
+
user: { ...user, id: toBytes(user.id) },
|
|
26
|
+
excludeCredentials: (o.excludeCredentials ?? []).map((c) => ({ ...c, type: c.type ?? 'public-key', id: toBytes(c.id) })),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** JSON request options as the browser takes them. */
|
|
30
|
+
export function requestOptionsFromJSON(o) {
|
|
31
|
+
return {
|
|
32
|
+
...o,
|
|
33
|
+
challenge: toBytes(o.challenge),
|
|
34
|
+
allowCredentials: (o.allowCredentials ?? []).map((c) => ({ ...c, type: c.type ?? 'public-key', id: toBytes(c.id) })),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** A browser PublicKeyCredential as the JSON VeroXM takes. */
|
|
38
|
+
export function credentialToJSON(cred) {
|
|
39
|
+
if (typeof cred.toJSON === 'function')
|
|
40
|
+
return cred.toJSON();
|
|
41
|
+
const r = cred.response;
|
|
42
|
+
const response = {};
|
|
43
|
+
for (const key of ['clientDataJSON', 'attestationObject', 'authenticatorData', 'signature', 'userHandle']) {
|
|
44
|
+
const v = r[key];
|
|
45
|
+
if (v instanceof ArrayBuffer || ArrayBuffer.isView(v))
|
|
46
|
+
response[key] = toB64url(v);
|
|
47
|
+
}
|
|
48
|
+
if (typeof r.getTransports === 'function')
|
|
49
|
+
response.transports = r.getTransports();
|
|
50
|
+
return {
|
|
51
|
+
id: cred.id,
|
|
52
|
+
rawId: toB64url(cred.rawId),
|
|
53
|
+
type: cred.type,
|
|
54
|
+
authenticatorAttachment: cred.authenticatorAttachment ?? undefined,
|
|
55
|
+
clientExtensionResults: cred.getClientExtensionResults?.() ?? {},
|
|
56
|
+
response,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** The browser's passkeys (navigator.credentials). Throws where there are none. */
|
|
60
|
+
export const browserPasskeys = {
|
|
61
|
+
async create(options) {
|
|
62
|
+
const { credentials, PKC } = webauthn();
|
|
63
|
+
const publicKey = PKC.parseCreationOptionsFromJSON ? PKC.parseCreationOptionsFromJSON(options) : creationOptionsFromJSON(options);
|
|
64
|
+
const cred = (await credentials.create({ publicKey }));
|
|
65
|
+
if (!cred)
|
|
66
|
+
throw Object.assign(new Error('No passkey was made.'), { code: 'passkey_cancelled' });
|
|
67
|
+
return credentialToJSON(cred);
|
|
68
|
+
},
|
|
69
|
+
async get(options) {
|
|
70
|
+
const { credentials, PKC } = webauthn();
|
|
71
|
+
const publicKey = PKC.parseRequestOptionsFromJSON ? PKC.parseRequestOptionsFromJSON(options) : requestOptionsFromJSON(options);
|
|
72
|
+
const cred = (await credentials.get({ publicKey }));
|
|
73
|
+
if (!cred)
|
|
74
|
+
throw Object.assign(new Error('No passkey was chosen.'), { code: 'passkey_cancelled' });
|
|
75
|
+
return credentialToJSON(cred);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
function webauthn() {
|
|
79
|
+
const g = globalThis;
|
|
80
|
+
if (!g.navigator?.credentials || !g.PublicKeyCredential) {
|
|
81
|
+
throw Object.assign(new Error("Passkeys aren't available here: use a browser that has them, or pass a passkey provider."), { code: 'passkeys_unavailable' });
|
|
82
|
+
}
|
|
83
|
+
return { credentials: g.navigator.credentials, PKC: g.PublicKeyCredential };
|
|
84
|
+
}
|
package/dist/tracker.d.ts
CHANGED
|
@@ -84,6 +84,8 @@ export declare class VeroxmTracker {
|
|
|
84
84
|
private backoffMs;
|
|
85
85
|
private retryAt;
|
|
86
86
|
private decisions;
|
|
87
|
+
/** The page this visit started on, and what linked to it: /decide reads its campaign (campaign.* rules). */
|
|
88
|
+
private landing;
|
|
87
89
|
private readonly listeners;
|
|
88
90
|
constructor(options: TrackerOptions);
|
|
89
91
|
/** The anonymous id for this browser/app install. */
|
|
@@ -107,6 +109,13 @@ export declare class VeroxmTracker {
|
|
|
107
109
|
identifyAccount(accessToken: string, traits?: Record<string, unknown>): Promise<void>;
|
|
108
110
|
/** Call on sign-out: starts a new anonymous visitor on this device. Consent answers are kept. */
|
|
109
111
|
reset(): void;
|
|
112
|
+
/**
|
|
113
|
+
* The address the visit started from, for campaign targeting (campaign.* rules) when it isn't the browser's own
|
|
114
|
+
* page: an app opened by a link (a deep link or universal link with utm tags), or a server rendering for a visitor.
|
|
115
|
+
* Call it before getExperience; decisions already made are asked again. Record the visit itself with
|
|
116
|
+
* `page({ url })`, so the visitor's profile keeps the campaign too.
|
|
117
|
+
*/
|
|
118
|
+
setLandingPage(url: string, referrer?: string): void;
|
|
110
119
|
/**
|
|
111
120
|
* Which variant of a placement this visitor sees, e.g.
|
|
112
121
|
* `const d = await tracker.getExperience('home.hero')` -- then render
|
package/dist/tracker.js
CHANGED
|
@@ -112,6 +112,8 @@ export class VeroxmTracker {
|
|
|
112
112
|
this.backoffMs = 0;
|
|
113
113
|
this.retryAt = 0;
|
|
114
114
|
this.decisions = new Map();
|
|
115
|
+
/** The page this visit started on, and what linked to it: /decide reads its campaign (campaign.* rules). */
|
|
116
|
+
this.landing = null;
|
|
115
117
|
this.listeners = new Set();
|
|
116
118
|
this.apiBase = `${options.baseUrl.replace(/\/$/, '')}/public/v2/projects/${options.projectId}`;
|
|
117
119
|
this.key = options.eventsKey;
|
|
@@ -125,6 +127,11 @@ export class VeroxmTracker {
|
|
|
125
127
|
};
|
|
126
128
|
this.consentState = this.readConsent();
|
|
127
129
|
this.visitorId = this.store().get(this.keys.visitor) ?? uuid();
|
|
130
|
+
// Campaign attribution: in a browser, the page the visitor arrived on (with its utm tags and ad click ids) and the
|
|
131
|
+
// page that linked to it. Kept in memory for this page load only; a single-page app keeps it as people move around.
|
|
132
|
+
if (typeof window !== 'undefined' && window.location) {
|
|
133
|
+
this.landing = { url: window.location.href, referrer: typeof document !== 'undefined' ? document.referrer : '' };
|
|
134
|
+
}
|
|
128
135
|
if (this.canPersist()) {
|
|
129
136
|
this.store().set(this.keys.visitor, this.visitorId);
|
|
130
137
|
setVisitorCookie(this.visitorId);
|
|
@@ -241,6 +248,16 @@ export class VeroxmTracker {
|
|
|
241
248
|
}
|
|
242
249
|
this.changed();
|
|
243
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* The address the visit started from, for campaign targeting (campaign.* rules) when it isn't the browser's own
|
|
253
|
+
* page: an app opened by a link (a deep link or universal link with utm tags), or a server rendering for a visitor.
|
|
254
|
+
* Call it before getExperience; decisions already made are asked again. Record the visit itself with
|
|
255
|
+
* `page({ url })`, so the visitor's profile keeps the campaign too.
|
|
256
|
+
*/
|
|
257
|
+
setLandingPage(url, referrer) {
|
|
258
|
+
this.landing = { url, referrer: referrer ?? '' };
|
|
259
|
+
this.decisions.clear();
|
|
260
|
+
}
|
|
244
261
|
/**
|
|
245
262
|
* Which variant of a placement this visitor sees, e.g.
|
|
246
263
|
* `const d = await tracker.getExperience('home.hero')` -- then render
|
|
@@ -266,7 +283,11 @@ export class VeroxmTracker {
|
|
|
266
283
|
const timeoutMs = options.timeoutMs ?? 1500;
|
|
267
284
|
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
|
|
268
285
|
const timer = setTimeout(() => controller?.abort(), timeoutMs);
|
|
269
|
-
|
|
286
|
+
let qs = `placement=${encodeURIComponent(placement)}&visitorId=${encodeURIComponent(this.visitorId)}`;
|
|
287
|
+
if (this.landing?.url)
|
|
288
|
+
qs += `&url=${encodeURIComponent(this.landing.url.slice(0, 2000))}`;
|
|
289
|
+
if (this.landing?.referrer)
|
|
290
|
+
qs += `&referrer=${encodeURIComponent(this.landing.referrer.slice(0, 2000))}`;
|
|
270
291
|
const request = fetch(`${this.apiBase}/decide?${qs}`, {
|
|
271
292
|
headers: { Authorization: `Bearer ${this.key}` },
|
|
272
293
|
...(controller ? { signal: controller.signal } : {}),
|