sdk-kit 0.0.0-stage → 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.
@@ -0,0 +1,69 @@
1
+
2
+ import { nanoid } from "nanoid";
3
+
4
+ import type {
5
+ SurveyIdentity,
6
+ SurveyUser,
7
+ } from "../types";
8
+
9
+ import type {
10
+ SurveyStorage,
11
+ } from "./storage";
12
+
13
+ const VISITOR_ID_KEY = "rls_visitor_id";
14
+
15
+ /**
16
+ * Get existing visitor ID.
17
+ * If unavailable, generate a new one.
18
+ */
19
+ export function getAnonymousIdentity(
20
+ storage: SurveyStorage
21
+ ): SurveyIdentity {
22
+
23
+ let visitorId = storage.getItem(VISITOR_ID_KEY);
24
+
25
+ if (!visitorId) {
26
+ visitorId = `visitor_${nanoid(16)}`;
27
+
28
+ storage.setItem(
29
+ VISITOR_ID_KEY,
30
+ visitorId
31
+ );
32
+ }
33
+
34
+ return {
35
+ type: "anonymous",
36
+ id: visitorId,
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Resolve visitor identity.
42
+ *
43
+ * Authenticated user:
44
+ * Use the application user ID.
45
+ *
46
+ * Anonymous visitor:
47
+ * Generate or reuse visitor UUID.
48
+ */
49
+ export function resolveIdentity(
50
+ user: SurveyUser | undefined,
51
+ storage: SurveyStorage
52
+ ): SurveyIdentity {
53
+
54
+ if (user?.isAuthenticated) {
55
+
56
+ if (!user.id?.trim()) {
57
+ throw new Error(
58
+ "Authenticated user must provide a valid ID."
59
+ );
60
+ }
61
+
62
+ return {
63
+ type: "authenticated",
64
+ id: user.id,
65
+ };
66
+ }
67
+
68
+ return getAnonymousIdentity(storage);
69
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./identity";
2
+ export * from "./storage";
3
+ export * from "./SurveyManager";
4
+ export * from "./eligibility";
5
+ export * from "./duplicate-check";
6
+ export * from "./SurveyEngine";
@@ -0,0 +1,47 @@
1
+
2
+ export interface SurveyStorage {
3
+ getItem(key: string): string | null;
4
+ setItem(key: string, value: string): void;
5
+ }
6
+
7
+ /**
8
+ * In-memory storage fallback.
9
+ */
10
+ export class MemorySurveyStorage
11
+ implements SurveyStorage {
12
+
13
+ private data = new Map<string, string>();
14
+
15
+ getItem(key: string): string | null {
16
+ return this.data.get(key) ?? null;
17
+ }
18
+
19
+ setItem(key: string, value: string): void {
20
+ this.data.set(key, value);
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Browser localStorage adapter.
26
+ */
27
+ export class LocalSurveyStorage
28
+ implements SurveyStorage {
29
+
30
+ private fallback = new MemorySurveyStorage();
31
+
32
+ getItem(key: string): string | null {
33
+ try {
34
+ return globalThis.localStorage.getItem(key);
35
+ } catch {
36
+ return this.fallback.getItem(key);
37
+ }
38
+ }
39
+
40
+ setItem(key: string, value: string): void {
41
+ try {
42
+ globalThis.localStorage.setItem(key, value);
43
+ } catch {
44
+ this.fallback.setItem(key, value);
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./core";
3
+ export * from "./core/storage";
@@ -0,0 +1,103 @@
1
+
2
+ /**
3
+ * Survey identification modes.
4
+ *
5
+ * anonymous:
6
+ * Visitors who have not logged in.
7
+ *
8
+ * operational:
9
+ * Visitors who have logged in.
10
+ */
11
+ export type SurveyMode =
12
+ | "anonymous"
13
+ | "operational";
14
+
15
+ /**
16
+ * Survey status.
17
+ */
18
+ export type SurveyStatus =
19
+ | "active"
20
+ | "inactive"
21
+ | "draft";
22
+
23
+ /**
24
+ * Question types supported by the SDK.
25
+ */
26
+ export type QuestionType =
27
+ | "text"
28
+ | "rating"
29
+ | "multiple-choice"
30
+ | "single-choice"
31
+ | "yes-no";
32
+
33
+ /**
34
+ * Visitor identity.
35
+ */
36
+ export interface SurveyIdentity {
37
+ type: "anonymous" | "authenticated";
38
+ id: string;
39
+ }
40
+
41
+ /**
42
+ * Application user information.
43
+ */
44
+ export interface SurveyUser {
45
+ id: string;
46
+ isAuthenticated: boolean;
47
+ }
48
+
49
+ /**
50
+ * Survey question.
51
+ */
52
+ export interface SurveyQuestion {
53
+ id: string;
54
+ title: string;
55
+ type: QuestionType;
56
+ required: boolean;
57
+ options?: string[];
58
+ }
59
+
60
+ /**
61
+ * Survey definition.
62
+ */
63
+ export interface Survey {
64
+ id: string;
65
+ title: string;
66
+ description?: string;
67
+
68
+ mode: SurveyMode;
69
+ status: SurveyStatus;
70
+
71
+ questions: SurveyQuestion[];
72
+
73
+ createdAt: string;
74
+ }
75
+
76
+ /**
77
+ * Survey execution record.
78
+ *
79
+ * This records when a survey was first displayed.
80
+ */
81
+ export interface SurveyExecution {
82
+ surveyId: string;
83
+
84
+ identityType: SurveyIdentity["type"];
85
+ identityId: string;
86
+
87
+ shownAt: string;
88
+ }
89
+
90
+ /**
91
+ * Survey response.
92
+ */
93
+ export interface SurveyResponse {
94
+ id: string;
95
+ surveyId: string;
96
+
97
+ identityType: SurveyIdentity["type"];
98
+ identityId: string;
99
+
100
+ answers: Record<string, string | string[]>;
101
+
102
+ submittedAt: string;
103
+ }
package/vite.config.js ADDED
@@ -0,0 +1,11 @@
1
+ import react from '@vitejs/plugin-react'
2
+ import { defineConfig } from 'vite'
3
+
4
+ // https://vite.dev/config/
5
+ export default defineConfig({
6
+ devtools: {
7
+ // Remove this `apply` property to enable devtools for build as well
8
+ apply: 'serve',
9
+ },
10
+ plugins: [react()],
11
+ })