sdk-kit 0.1.0 → 0.1.2

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.
@@ -1,99 +0,0 @@
1
-
2
- import { describe, it, expect } from "vitest";
3
-
4
- import { SurveyEngine } from "./SurveyEngine";
5
- import { SurveyManager } from "./SurveyManager";
6
- import { MemorySurveyStorage } from "./storage";
7
-
8
- import type { Survey } from "../types";
9
-
10
- const testSurvey: Survey = {
11
- id: "SURVEY_001",
12
- title: "Customer Feedback",
13
- mode: "anonymous",
14
- status: "active",
15
- createdAt: new Date().toISOString(),
16
-
17
- questions: [
18
- {
19
- id: "Q1",
20
- title: "How was your experience?",
21
- type: "rating",
22
- required: true,
23
- },
24
- ],
25
- };
26
-
27
- describe("SurveyEngine", () => {
28
-
29
- it("should display survey first time", () => {
30
- const manager = new SurveyManager();
31
- const storage = new MemorySurveyStorage();
32
-
33
- manager.createSurvey(testSurvey);
34
-
35
- const engine = new SurveyEngine(
36
- manager,
37
- storage
38
- );
39
-
40
- const identity = {
41
- type: "anonymous" as const,
42
- id: "visitor_101",
43
- };
44
-
45
- const survey = engine.getNextSurvey(identity);
46
-
47
- expect(survey?.id).toBe("SURVEY_001");
48
- });
49
-
50
- it("should not display the same survey twice", () => {
51
- const manager = new SurveyManager();
52
- const storage = new MemorySurveyStorage();
53
-
54
- manager.createSurvey(testSurvey);
55
-
56
- const engine = new SurveyEngine(
57
- manager,
58
- storage
59
- );
60
-
61
- const identity = {
62
- type: "anonymous" as const,
63
- id: "visitor_101",
64
- };
65
-
66
- engine.getNextSurvey(identity);
67
-
68
- const secondSurvey =
69
- engine.getNextSurvey(identity);
70
-
71
- expect(secondSurvey).toBeNull();
72
- });
73
-
74
- it("should allow a different visitor", () => {
75
- const manager = new SurveyManager();
76
- const storage = new MemorySurveyStorage();
77
-
78
- manager.createSurvey(testSurvey);
79
-
80
- const engine = new SurveyEngine(
81
- manager,
82
- storage
83
- );
84
-
85
- engine.getNextSurvey({
86
- type: "anonymous",
87
- id: "visitor_101",
88
- });
89
-
90
- const secondVisitor =
91
- engine.getNextSurvey({
92
- type: "anonymous",
93
- id: "visitor_202",
94
- });
95
-
96
- expect(secondVisitor?.id).toBe("SURVEY_001");
97
- });
98
-
99
- });
@@ -1,225 +0,0 @@
1
-
2
- import { nanoid } from "nanoid";
3
-
4
- import type {
5
- Survey,
6
- SurveyIdentity,
7
- SurveyResponse,
8
- SurveyUser,
9
- } from "../types";
10
-
11
- import { SurveyManager } from "./SurveyManager";
12
-
13
- import {
14
- resolveIdentity,
15
- } from "./identity";
16
-
17
- import {
18
- isSurveyEligible,
19
- } from "./eligibility";
20
-
21
- import {
22
- getExecutions,
23
- hasAlreadySeenSurvey,
24
- recordSurveyExecution,
25
- } from "./duplicate-check";
26
-
27
- import {
28
- LocalSurveyStorage,
29
- type SurveyStorage,
30
- } from "./storage";
31
-
32
- const RESPONSE_KEY = "rls_survey_responses";
33
-
34
- export class SurveyEngine {
35
-
36
- private manager: SurveyManager;
37
-
38
- private storage: SurveyStorage;
39
-
40
- constructor(
41
- manager: SurveyManager = new SurveyManager(),
42
- storage: SurveyStorage = new LocalSurveyStorage()
43
- ) {
44
- this.manager = manager;
45
- this.storage = storage;
46
- }
47
-
48
- /**
49
- * Resolve current visitor identity.
50
- */
51
- getIdentity(
52
- user?: SurveyUser
53
- ): SurveyIdentity {
54
-
55
- return resolveIdentity(
56
- user,
57
- this.storage
58
- );
59
- }
60
-
61
- /**
62
- * Access survey manager.
63
- */
64
- getSurveyManager(): SurveyManager {
65
- return this.manager;
66
- }
67
-
68
- /**
69
- * Get next eligible survey.
70
- *
71
- * IMPORTANT:
72
- * We record the display before returning
73
- * the survey to the UI.
74
- *
75
- * This implements the "once shown" policy.
76
- */
77
- getNextSurvey(
78
- identity: SurveyIdentity
79
- ): Survey | null {
80
-
81
- const activeSurveys =
82
- this.manager.getActiveSurveys();
83
-
84
- for (const survey of activeSurveys) {
85
-
86
- const eligible = isSurveyEligible(
87
- survey,
88
- identity
89
- );
90
-
91
- if (!eligible) {
92
- continue;
93
- }
94
-
95
- const alreadySeen =
96
- hasAlreadySeenSurvey(
97
- survey.id,
98
- identity,
99
- this.storage
100
- );
101
-
102
- if (alreadySeen) {
103
- continue;
104
- }
105
-
106
- // Record before displaying the popup.
107
- recordSurveyExecution(
108
- survey.id,
109
- identity,
110
- this.storage
111
- );
112
-
113
- return survey;
114
- }
115
-
116
- return null;
117
- }
118
-
119
- /**
120
- * Submit survey response.
121
- */
122
- submitSurvey(
123
- surveyId: string,
124
- identity: SurveyIdentity,
125
- answers: Record<string, string | string[]>
126
- ): SurveyResponse {
127
-
128
- const survey =
129
- this.manager.getSurveyById(surveyId);
130
-
131
- if (!survey) {
132
- throw new Error("Survey not found.");
133
- }
134
-
135
- const alreadyShown =
136
- hasAlreadySeenSurvey(
137
- surveyId,
138
- identity,
139
- this.storage
140
- );
141
-
142
- if (!alreadyShown) {
143
- throw new Error(
144
- "Survey must be displayed before submission."
145
- );
146
- }
147
-
148
- // Validate required questions.
149
- for (const question of survey.questions) {
150
-
151
- if (!question.required) {
152
- continue;
153
- }
154
-
155
- const answer = answers[question.id];
156
-
157
- const isEmpty =
158
- answer === undefined ||
159
- (typeof answer === "string" && !answer.trim()) ||
160
- (Array.isArray(answer) && answer.length === 0);
161
-
162
- if (isEmpty) {
163
- throw new Error(
164
- `Please answer: ${question.title}`
165
- );
166
- }
167
- }
168
-
169
- const response: SurveyResponse = {
170
- id: `response_${nanoid(12)}`,
171
- surveyId,
172
-
173
- identityType: identity.type,
174
- identityId: identity.id,
175
-
176
- answers,
177
-
178
- submittedAt: new Date().toISOString(),
179
- };
180
-
181
- const existingResponses =
182
- this.getResponses();
183
-
184
- this.storage.setItem(
185
- RESPONSE_KEY,
186
- JSON.stringify([
187
- ...existingResponses,
188
- response,
189
- ])
190
- );
191
-
192
- return response;
193
- }
194
-
195
- /**
196
- * Get all responses.
197
- */
198
- getResponses(): SurveyResponse[] {
199
-
200
- try {
201
- const value =
202
- this.storage.getItem(RESPONSE_KEY);
203
-
204
- if (!value) {
205
- return [];
206
- }
207
-
208
- const parsed: unknown = JSON.parse(value);
209
-
210
- return Array.isArray(parsed)
211
- ? parsed as SurveyResponse[]
212
- : [];
213
-
214
- } catch {
215
- return [];
216
- }
217
- }
218
-
219
- /**
220
- * Get execution history.
221
- */
222
- getExecutionHistory() {
223
- return getExecutions(this.storage);
224
- }
225
- }
@@ -1,82 +0,0 @@
1
-
2
- import type {
3
- Survey,
4
- } from "../types";
5
-
6
- export class SurveyManager {
7
-
8
- private surveys: Survey[] = [];
9
-
10
- /**
11
- * Add a new survey.
12
- */
13
- createSurvey(survey: Survey): void {
14
-
15
- const exists = this.surveys.some(
16
- (item) => item.id === survey.id
17
- );
18
-
19
- if (exists) {
20
- throw new Error(
21
- `Survey already exists: ${survey.id}`
22
- );
23
- }
24
-
25
- this.surveys.push(survey);
26
- }
27
-
28
- /**
29
- * Get all surveys.
30
- */
31
- getSurveys(): Survey[] {
32
- return [...this.surveys];
33
- }
34
-
35
- /**
36
- * Get survey by ID.
37
- */
38
- getSurveyById(
39
- surveyId: string
40
- ): Survey | undefined {
41
-
42
- return this.surveys.find(
43
- (survey) => survey.id === surveyId
44
- );
45
- }
46
-
47
- /**
48
- * Get active surveys.
49
- */
50
- getActiveSurveys(): Survey[] {
51
-
52
- return this.surveys.filter(
53
- (survey) => survey.status === "active"
54
- );
55
- }
56
-
57
- /**
58
- * Update survey.
59
- */
60
- updateSurvey(
61
- surveyId: string,
62
- updates: Partial<Survey>
63
- ): void {
64
-
65
- this.surveys = this.surveys.map(
66
- (survey) =>
67
- survey.id === surveyId
68
- ? { ...survey, ...updates, id: surveyId }
69
- : survey
70
- );
71
- }
72
-
73
- /**
74
- * Delete survey.
75
- */
76
- deleteSurvey(surveyId: string): void {
77
-
78
- this.surveys = this.surveys.filter(
79
- (survey) => survey.id !== surveyId
80
- );
81
- }
82
- }
@@ -1,94 +0,0 @@
1
-
2
- import type {
3
- SurveyExecution,
4
- SurveyIdentity,
5
- } from "../types";
6
-
7
- import type {
8
- SurveyStorage,
9
- } from "./storage";
10
-
11
- const EXECUTION_KEY = "rls_survey_executions";
12
-
13
- /**
14
- * Read survey execution history.
15
- */
16
- export function getExecutions(
17
- storage: SurveyStorage
18
- ): SurveyExecution[] {
19
-
20
- try {
21
- const value = storage.getItem(EXECUTION_KEY);
22
-
23
- if (!value) {
24
- return [];
25
- }
26
-
27
- const parsed: unknown = JSON.parse(value);
28
-
29
- return Array.isArray(parsed)
30
- ? parsed as SurveyExecution[]
31
- : [];
32
-
33
- } catch {
34
- return [];
35
- }
36
- }
37
-
38
- /**
39
- * Check if survey was already displayed
40
- * to this identity.
41
- */
42
- export function hasAlreadySeenSurvey(
43
- surveyId: string,
44
- identity: SurveyIdentity,
45
- storage: SurveyStorage
46
- ): boolean {
47
-
48
- const executions = getExecutions(storage);
49
-
50
- return executions.some(
51
- (execution) =>
52
- execution.surveyId === surveyId &&
53
- execution.identityType === identity.type &&
54
- execution.identityId === identity.id
55
- );
56
- }
57
-
58
- /**
59
- * Record survey display.
60
- */
61
- export function recordSurveyExecution(
62
- surveyId: string,
63
- identity: SurveyIdentity,
64
- storage: SurveyStorage
65
- ): void {
66
-
67
- const executions = getExecutions(storage);
68
-
69
- const alreadySeen = executions.some(
70
- (execution) =>
71
- execution.surveyId === surveyId &&
72
- execution.identityType === identity.type &&
73
- execution.identityId === identity.id
74
- );
75
-
76
- if (alreadySeen) {
77
- return;
78
- }
79
-
80
- const execution: SurveyExecution = {
81
- surveyId,
82
- identityType: identity.type,
83
- identityId: identity.id,
84
- shownAt: new Date().toISOString(),
85
- };
86
-
87
- storage.setItem(
88
- EXECUTION_KEY,
89
- JSON.stringify([
90
- ...executions,
91
- execution,
92
- ])
93
- );
94
- }
@@ -1,35 +0,0 @@
1
-
2
- import type {
3
- Survey,
4
- SurveyIdentity,
5
- } from "../types";
6
-
7
- /**
8
- * Check whether a survey matches
9
- * the visitor's identity type.
10
- */
11
- export function isSurveyEligible(
12
- survey: Survey,
13
- identity: SurveyIdentity
14
- ): boolean {
15
-
16
- if (survey.status !== "active") {
17
- return false;
18
- }
19
-
20
- if (
21
- survey.mode === "anonymous" &&
22
- identity.type !== "anonymous"
23
- ) {
24
- return false;
25
- }
26
-
27
- if (
28
- survey.mode === "operational" &&
29
- identity.type !== "authenticated"
30
- ) {
31
- return false;
32
- }
33
-
34
- return true;
35
- }
@@ -1,69 +0,0 @@
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
- }
@@ -1,6 +0,0 @@
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";
@@ -1,47 +0,0 @@
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
- }
@@ -1,3 +0,0 @@
1
- export * from "./types";
2
- export * from "./core";
3
- export * from "./core/storage";