@back23/promptly-sdk 2.14.2 → 2.18.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/dist/index.js CHANGED
@@ -72,19 +72,60 @@ var HttpClient = class {
72
72
  this.token = null;
73
73
  this.apiKey = null;
74
74
  this.cartSessionId = null;
75
+ // Token persistence options
76
+ this.persistToken = false;
77
+ this.storageType = "localStorage";
75
78
  this.tenantId = config.tenantId;
76
79
  this.baseUrl = (config.baseUrl || "https://promptly.webbyon.com").replace(/\/$/, "");
77
80
  this.timeout = config.timeout || 3e4;
78
81
  this.apiKey = config.apiKey || null;
82
+ this.persistToken = config.persistToken ?? false;
83
+ this.storageType = config.storageType ?? "localStorage";
84
+ this.storageKey = config.storageKey ?? `promptly_auth_token_${this.tenantId}`;
85
+ this.onAuthStateChange = config.onAuthStateChange;
79
86
  if (typeof window !== "undefined" && window.localStorage) {
80
87
  this.cartSessionId = localStorage.getItem(`promptly_cart_session_${this.tenantId}`);
81
88
  }
89
+ if (this.persistToken && typeof window !== "undefined") {
90
+ const storage = this.getStorage();
91
+ if (storage) {
92
+ const savedToken = storage.getItem(this.storageKey);
93
+ if (savedToken) {
94
+ this.token = savedToken;
95
+ this.onAuthStateChange?.(savedToken, void 0);
96
+ }
97
+ }
98
+ }
99
+ if (config.token) {
100
+ this.token = config.token;
101
+ }
102
+ }
103
+ /**
104
+ * Get the storage object based on config
105
+ */
106
+ getStorage() {
107
+ if (typeof window === "undefined") return null;
108
+ return this.storageType === "sessionStorage" ? window.sessionStorage : window.localStorage;
82
109
  }
83
110
  /**
84
111
  * Set authentication token
112
+ * If persistToken is enabled, automatically saves/removes from storage
113
+ * @param token - The auth token or null to clear
114
+ * @param user - The logged-in user (Member) for the callback
85
115
  */
86
- setToken(token) {
116
+ setToken(token, user) {
87
117
  this.token = token;
118
+ if (this.persistToken) {
119
+ const storage = this.getStorage();
120
+ if (storage) {
121
+ if (token) {
122
+ storage.setItem(this.storageKey, token);
123
+ } else {
124
+ storage.removeItem(this.storageKey);
125
+ }
126
+ }
127
+ }
128
+ this.onAuthStateChange?.(token, user ?? null);
88
129
  }
89
130
  /**
90
131
  * Get current token
@@ -300,21 +341,23 @@ var AuthResource = class {
300
341
  }
301
342
  /**
302
343
  * Login with email and password
344
+ * Token is automatically saved if persistToken is enabled
303
345
  */
304
346
  async login(credentials) {
305
347
  const response = await this.http.post("/auth/login", credentials);
306
348
  if (response.token) {
307
- this.http.setToken(response.token);
349
+ this.http.setToken(response.token, response.user);
308
350
  }
309
351
  return response;
310
352
  }
311
353
  /**
312
354
  * Register new member
355
+ * Token is automatically saved if persistToken is enabled
313
356
  */
314
357
  async register(data) {
315
358
  const response = await this.http.post("/auth/register", data);
316
359
  if (response.token) {
317
- this.http.setToken(response.token);
360
+ this.http.setToken(response.token, response.user);
318
361
  }
319
362
  return response;
320
363
  }
@@ -366,11 +409,12 @@ var AuthResource = class {
366
409
  }
367
410
  /**
368
411
  * Handle social login callback
412
+ * Token is automatically saved if persistToken is enabled
369
413
  */
370
414
  async socialCallback(provider, code) {
371
415
  const response = await this.http.post(`/auth/social/${provider}/callback`, { code });
372
416
  if (response.token) {
373
- this.http.setToken(response.token);
417
+ this.http.setToken(response.token, response.user);
374
418
  }
375
419
  return response;
376
420
  }
package/dist/index.mjs CHANGED
@@ -44,19 +44,60 @@ var HttpClient = class {
44
44
  this.token = null;
45
45
  this.apiKey = null;
46
46
  this.cartSessionId = null;
47
+ // Token persistence options
48
+ this.persistToken = false;
49
+ this.storageType = "localStorage";
47
50
  this.tenantId = config.tenantId;
48
51
  this.baseUrl = (config.baseUrl || "https://promptly.webbyon.com").replace(/\/$/, "");
49
52
  this.timeout = config.timeout || 3e4;
50
53
  this.apiKey = config.apiKey || null;
54
+ this.persistToken = config.persistToken ?? false;
55
+ this.storageType = config.storageType ?? "localStorage";
56
+ this.storageKey = config.storageKey ?? `promptly_auth_token_${this.tenantId}`;
57
+ this.onAuthStateChange = config.onAuthStateChange;
51
58
  if (typeof window !== "undefined" && window.localStorage) {
52
59
  this.cartSessionId = localStorage.getItem(`promptly_cart_session_${this.tenantId}`);
53
60
  }
61
+ if (this.persistToken && typeof window !== "undefined") {
62
+ const storage = this.getStorage();
63
+ if (storage) {
64
+ const savedToken = storage.getItem(this.storageKey);
65
+ if (savedToken) {
66
+ this.token = savedToken;
67
+ this.onAuthStateChange?.(savedToken, void 0);
68
+ }
69
+ }
70
+ }
71
+ if (config.token) {
72
+ this.token = config.token;
73
+ }
74
+ }
75
+ /**
76
+ * Get the storage object based on config
77
+ */
78
+ getStorage() {
79
+ if (typeof window === "undefined") return null;
80
+ return this.storageType === "sessionStorage" ? window.sessionStorage : window.localStorage;
54
81
  }
55
82
  /**
56
83
  * Set authentication token
84
+ * If persistToken is enabled, automatically saves/removes from storage
85
+ * @param token - The auth token or null to clear
86
+ * @param user - The logged-in user (Member) for the callback
57
87
  */
58
- setToken(token) {
88
+ setToken(token, user) {
59
89
  this.token = token;
90
+ if (this.persistToken) {
91
+ const storage = this.getStorage();
92
+ if (storage) {
93
+ if (token) {
94
+ storage.setItem(this.storageKey, token);
95
+ } else {
96
+ storage.removeItem(this.storageKey);
97
+ }
98
+ }
99
+ }
100
+ this.onAuthStateChange?.(token, user ?? null);
60
101
  }
61
102
  /**
62
103
  * Get current token
@@ -272,21 +313,23 @@ var AuthResource = class {
272
313
  }
273
314
  /**
274
315
  * Login with email and password
316
+ * Token is automatically saved if persistToken is enabled
275
317
  */
276
318
  async login(credentials) {
277
319
  const response = await this.http.post("/auth/login", credentials);
278
320
  if (response.token) {
279
- this.http.setToken(response.token);
321
+ this.http.setToken(response.token, response.user);
280
322
  }
281
323
  return response;
282
324
  }
283
325
  /**
284
326
  * Register new member
327
+ * Token is automatically saved if persistToken is enabled
285
328
  */
286
329
  async register(data) {
287
330
  const response = await this.http.post("/auth/register", data);
288
331
  if (response.token) {
289
- this.http.setToken(response.token);
332
+ this.http.setToken(response.token, response.user);
290
333
  }
291
334
  return response;
292
335
  }
@@ -338,11 +381,12 @@ var AuthResource = class {
338
381
  }
339
382
  /**
340
383
  * Handle social login callback
384
+ * Token is automatically saved if persistToken is enabled
341
385
  */
342
386
  async socialCallback(provider, code) {
343
387
  const response = await this.http.post(`/auth/social/${provider}/callback`, { code });
344
388
  if (response.token) {
345
- this.http.setToken(response.token);
389
+ this.http.setToken(response.token, response.user);
346
390
  }
347
391
  return response;
348
392
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@back23/promptly-sdk",
3
- "version": "2.14.2",
3
+ "version": "2.18.0",
4
4
  "description": "Promptly AI CMS SDK for JavaScript/TypeScript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -19,6 +19,7 @@
19
19
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",
20
20
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
21
21
  "typecheck": "tsc --noEmit",
22
+ "generate:types": "openapi-typescript $(grep '^APP_URL=' ../../.env | cut -d '=' -f2)/docs/api.json -o src/types/generated.ts",
22
23
  "prepublishOnly": "npm run build"
23
24
  },
24
25
  "keywords": [
@@ -34,7 +35,8 @@
34
35
  "type": "git",
35
36
  "url": "https://github.com/kingofecommerce/back23-promptly-sdk.git"
36
37
  },
37
- "devDependencies": {
38
+ "devDependencies": {
39
+ "openapi-typescript": "^7.10.1",
38
40
  "tsup": "^8.0.0",
39
41
  "typescript": "^5.3.0"
40
42
  },