@webbyon/promptly-sdk 1.0.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.mjs ADDED
@@ -0,0 +1,737 @@
1
+ // src/http.ts
2
+ var PromptlyError = class extends Error {
3
+ constructor(message, status, errors) {
4
+ super(message);
5
+ this.name = "PromptlyError";
6
+ this.status = status;
7
+ this.errors = errors;
8
+ }
9
+ };
10
+ var HttpClient = class {
11
+ constructor(config) {
12
+ this.token = null;
13
+ this.tenantId = config.tenantId;
14
+ this.baseUrl = (config.baseUrl || "https://promptly.webbyon.com").replace(/\/$/, "");
15
+ this.timeout = config.timeout || 3e4;
16
+ }
17
+ /**
18
+ * Set authentication token
19
+ */
20
+ setToken(token) {
21
+ this.token = token;
22
+ }
23
+ /**
24
+ * Get current token
25
+ */
26
+ getToken() {
27
+ return this.token;
28
+ }
29
+ /**
30
+ * Check if authenticated
31
+ */
32
+ isAuthenticated() {
33
+ return this.token !== null;
34
+ }
35
+ /**
36
+ * Build full URL with query params
37
+ */
38
+ buildUrl(endpoint, params) {
39
+ const url = new URL(`${this.baseUrl}/api/${this.tenantId}${endpoint}`);
40
+ if (params) {
41
+ Object.entries(params).forEach(([key, value]) => {
42
+ if (value !== void 0 && value !== null) {
43
+ url.searchParams.append(key, String(value));
44
+ }
45
+ });
46
+ }
47
+ return url.toString();
48
+ }
49
+ /**
50
+ * Build request headers
51
+ */
52
+ buildHeaders(customHeaders) {
53
+ const headers = {
54
+ "Content-Type": "application/json",
55
+ "Accept": "application/json",
56
+ ...customHeaders
57
+ };
58
+ if (this.token) {
59
+ headers["Authorization"] = `Bearer ${this.token}`;
60
+ }
61
+ return headers;
62
+ }
63
+ /**
64
+ * Make HTTP request
65
+ */
66
+ async request(endpoint, options = {}) {
67
+ const { method = "GET", body, params, headers } = options;
68
+ const url = this.buildUrl(endpoint, params);
69
+ const requestHeaders = this.buildHeaders(headers);
70
+ const controller = new AbortController();
71
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
72
+ try {
73
+ const response = await fetch(url, {
74
+ method,
75
+ headers: requestHeaders,
76
+ body: body ? JSON.stringify(body) : void 0,
77
+ signal: controller.signal
78
+ });
79
+ clearTimeout(timeoutId);
80
+ const data = await response.json();
81
+ if (!response.ok) {
82
+ throw new PromptlyError(
83
+ data.message || "Request failed",
84
+ response.status,
85
+ data.errors
86
+ );
87
+ }
88
+ return data.data !== void 0 ? data.data : data;
89
+ } catch (error) {
90
+ clearTimeout(timeoutId);
91
+ if (error instanceof PromptlyError) {
92
+ throw error;
93
+ }
94
+ if (error instanceof Error) {
95
+ if (error.name === "AbortError") {
96
+ throw new PromptlyError("Request timeout", 408);
97
+ }
98
+ throw new PromptlyError(error.message, 0);
99
+ }
100
+ throw new PromptlyError("Unknown error", 0);
101
+ }
102
+ }
103
+ /**
104
+ * GET request
105
+ */
106
+ get(endpoint, params) {
107
+ return this.request(endpoint, { method: "GET", params });
108
+ }
109
+ /**
110
+ * POST request
111
+ */
112
+ post(endpoint, body) {
113
+ return this.request(endpoint, { method: "POST", body });
114
+ }
115
+ /**
116
+ * PUT request
117
+ */
118
+ put(endpoint, body) {
119
+ return this.request(endpoint, { method: "PUT", body });
120
+ }
121
+ /**
122
+ * PATCH request
123
+ */
124
+ patch(endpoint, body) {
125
+ return this.request(endpoint, { method: "PATCH", body });
126
+ }
127
+ /**
128
+ * DELETE request
129
+ */
130
+ delete(endpoint) {
131
+ return this.request(endpoint, { method: "DELETE" });
132
+ }
133
+ /**
134
+ * Upload file
135
+ */
136
+ async upload(endpoint, file, fieldName = "file") {
137
+ const url = this.buildUrl(endpoint);
138
+ const formData = new FormData();
139
+ formData.append(fieldName, file);
140
+ const headers = {
141
+ "Accept": "application/json"
142
+ };
143
+ if (this.token) {
144
+ headers["Authorization"] = `Bearer ${this.token}`;
145
+ }
146
+ const controller = new AbortController();
147
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
148
+ try {
149
+ const response = await fetch(url, {
150
+ method: "POST",
151
+ headers,
152
+ body: formData,
153
+ signal: controller.signal
154
+ });
155
+ clearTimeout(timeoutId);
156
+ const data = await response.json();
157
+ if (!response.ok) {
158
+ throw new PromptlyError(
159
+ data.message || "Upload failed",
160
+ response.status,
161
+ data.errors
162
+ );
163
+ }
164
+ return data.data !== void 0 ? data.data : data;
165
+ } catch (error) {
166
+ clearTimeout(timeoutId);
167
+ if (error instanceof PromptlyError) {
168
+ throw error;
169
+ }
170
+ if (error instanceof Error) {
171
+ if (error.name === "AbortError") {
172
+ throw new PromptlyError("Upload timeout", 408);
173
+ }
174
+ throw new PromptlyError(error.message, 0);
175
+ }
176
+ throw new PromptlyError("Unknown error", 0);
177
+ }
178
+ }
179
+ };
180
+
181
+ // src/resources/auth.ts
182
+ var AuthResource = class {
183
+ constructor(http) {
184
+ this.http = http;
185
+ }
186
+ /**
187
+ * Login with email and password
188
+ */
189
+ async login(credentials) {
190
+ const response = await this.http.post("/auth/login", credentials);
191
+ if (response.token) {
192
+ this.http.setToken(response.token);
193
+ }
194
+ return response;
195
+ }
196
+ /**
197
+ * Register new member
198
+ */
199
+ async register(data) {
200
+ const response = await this.http.post("/auth/register", data);
201
+ if (response.token) {
202
+ this.http.setToken(response.token);
203
+ }
204
+ return response;
205
+ }
206
+ /**
207
+ * Logout current user
208
+ */
209
+ async logout() {
210
+ try {
211
+ await this.http.post("/auth/logout");
212
+ } finally {
213
+ this.http.setToken(null);
214
+ }
215
+ }
216
+ /**
217
+ * Get current user profile
218
+ */
219
+ async me() {
220
+ return this.http.get("/profile");
221
+ }
222
+ /**
223
+ * Update profile
224
+ */
225
+ async updateProfile(data) {
226
+ return this.http.put("/profile", data);
227
+ }
228
+ /**
229
+ * Send password reset email
230
+ */
231
+ async forgotPassword(data) {
232
+ return this.http.post("/auth/forgot-password", data);
233
+ }
234
+ /**
235
+ * Reset password with token
236
+ */
237
+ async resetPassword(data) {
238
+ return this.http.post("/auth/reset-password", data);
239
+ }
240
+ /**
241
+ * Get available social login providers
242
+ */
243
+ async getSocialProviders() {
244
+ return this.http.get("/auth/social");
245
+ }
246
+ /**
247
+ * Get social login redirect URL
248
+ */
249
+ async getSocialAuthUrl(provider) {
250
+ return this.http.get(`/auth/social/${provider}`);
251
+ }
252
+ /**
253
+ * Handle social login callback
254
+ */
255
+ async socialCallback(provider, code) {
256
+ const response = await this.http.post(`/auth/social/${provider}/callback`, { code });
257
+ if (response.token) {
258
+ this.http.setToken(response.token);
259
+ }
260
+ return response;
261
+ }
262
+ /**
263
+ * Set token manually (e.g., from localStorage)
264
+ */
265
+ setToken(token) {
266
+ this.http.setToken(token);
267
+ }
268
+ /**
269
+ * Get current token
270
+ */
271
+ getToken() {
272
+ return this.http.getToken();
273
+ }
274
+ /**
275
+ * Check if user is authenticated
276
+ */
277
+ isAuthenticated() {
278
+ return this.http.isAuthenticated();
279
+ }
280
+ };
281
+
282
+ // src/resources/boards.ts
283
+ var BoardsResource = class {
284
+ constructor(http) {
285
+ this.http = http;
286
+ }
287
+ // ============================================
288
+ // Boards (Public)
289
+ // ============================================
290
+ /**
291
+ * List all boards
292
+ */
293
+ async list(params) {
294
+ return this.http.get("/public/boards", params);
295
+ }
296
+ /**
297
+ * Get board by ID or slug
298
+ */
299
+ async get(idOrSlug) {
300
+ return this.http.get(`/public/boards/${idOrSlug}`);
301
+ }
302
+ // ============================================
303
+ // Posts (Public)
304
+ // ============================================
305
+ /**
306
+ * List posts in a board
307
+ */
308
+ async listPosts(boardIdOrSlug, params) {
309
+ return this.http.get(`/public/boards/${boardIdOrSlug}/posts`, params);
310
+ }
311
+ /**
312
+ * Get post by ID
313
+ */
314
+ async getPost(postId) {
315
+ return this.http.get(`/public/posts/${postId}`);
316
+ }
317
+ // ============================================
318
+ // Posts (Protected - requires auth)
319
+ // ============================================
320
+ /**
321
+ * Create new post
322
+ */
323
+ async createPost(data) {
324
+ return this.http.post("/posts", data);
325
+ }
326
+ /**
327
+ * Update post
328
+ */
329
+ async updatePost(postId, data) {
330
+ return this.http.put(`/posts/${postId}`, data);
331
+ }
332
+ /**
333
+ * Delete post
334
+ */
335
+ async deletePost(postId) {
336
+ return this.http.delete(`/posts/${postId}`);
337
+ }
338
+ // ============================================
339
+ // Comments
340
+ // ============================================
341
+ /**
342
+ * List comments for a post
343
+ */
344
+ async listComments(postId) {
345
+ return this.http.get(`/public/posts/${postId}/comments`);
346
+ }
347
+ /**
348
+ * Create comment on a post
349
+ */
350
+ async createComment(postId, data) {
351
+ return this.http.post(`/posts/${postId}/comments`, data);
352
+ }
353
+ /**
354
+ * Update comment
355
+ */
356
+ async updateComment(commentId, data) {
357
+ return this.http.put(`/comments/${commentId}`, data);
358
+ }
359
+ /**
360
+ * Delete comment
361
+ */
362
+ async deleteComment(commentId) {
363
+ return this.http.delete(`/comments/${commentId}`);
364
+ }
365
+ };
366
+
367
+ // src/resources/blog.ts
368
+ var BlogResource = class {
369
+ constructor(http) {
370
+ this.http = http;
371
+ }
372
+ /**
373
+ * List blog posts
374
+ */
375
+ async list(params) {
376
+ return this.http.get("/public/blog", params);
377
+ }
378
+ /**
379
+ * Get blog post by slug
380
+ */
381
+ async get(slug) {
382
+ return this.http.get(`/public/blog/${slug}`);
383
+ }
384
+ /**
385
+ * Get blog post by ID
386
+ */
387
+ async getById(id) {
388
+ return this.http.get(`/public/blog/id/${id}`);
389
+ }
390
+ /**
391
+ * Get featured blog posts
392
+ */
393
+ async featured(limit = 5) {
394
+ const response = await this.http.get("/public/blog", {
395
+ per_page: limit,
396
+ featured: true
397
+ });
398
+ return response.data;
399
+ }
400
+ /**
401
+ * Get blog posts by category
402
+ */
403
+ async byCategory(category, params) {
404
+ return this.http.get("/public/blog", {
405
+ ...params,
406
+ category
407
+ });
408
+ }
409
+ /**
410
+ * Get blog posts by tag
411
+ */
412
+ async byTag(tag, params) {
413
+ return this.http.get("/public/blog", {
414
+ ...params,
415
+ tag
416
+ });
417
+ }
418
+ /**
419
+ * Search blog posts
420
+ */
421
+ async search(query, params) {
422
+ return this.http.get("/public/blog", {
423
+ ...params,
424
+ search: query
425
+ });
426
+ }
427
+ /**
428
+ * Get blog categories
429
+ */
430
+ async categories() {
431
+ return this.http.get("/public/blog/categories");
432
+ }
433
+ /**
434
+ * Get blog tags
435
+ */
436
+ async tags() {
437
+ return this.http.get("/public/blog/tags");
438
+ }
439
+ };
440
+
441
+ // src/resources/forms.ts
442
+ var FormsResource = class {
443
+ constructor(http) {
444
+ this.http = http;
445
+ }
446
+ /**
447
+ * List all forms
448
+ */
449
+ async list(params) {
450
+ return this.http.get("/public/forms", params);
451
+ }
452
+ /**
453
+ * Get form by ID or slug
454
+ */
455
+ async get(idOrSlug) {
456
+ return this.http.get(`/public/forms/${idOrSlug}`);
457
+ }
458
+ /**
459
+ * Submit form data
460
+ */
461
+ async submit(formIdOrSlug, data) {
462
+ return this.http.post(`/public/forms/${formIdOrSlug}/submit`, data);
463
+ }
464
+ // ============================================
465
+ // Protected endpoints (requires auth)
466
+ // ============================================
467
+ /**
468
+ * Get my form submissions
469
+ */
470
+ async mySubmissions(params) {
471
+ return this.http.get("/form-submissions", params);
472
+ }
473
+ /**
474
+ * Get specific submission
475
+ */
476
+ async getSubmission(submissionId) {
477
+ return this.http.get(`/form-submissions/${submissionId}`);
478
+ }
479
+ };
480
+
481
+ // src/resources/shop.ts
482
+ var ShopResource = class {
483
+ constructor(http) {
484
+ this.http = http;
485
+ }
486
+ // ============================================
487
+ // Products (Public)
488
+ // ============================================
489
+ /**
490
+ * List products
491
+ */
492
+ async listProducts(params) {
493
+ return this.http.get("/public/products", params);
494
+ }
495
+ /**
496
+ * Get product by ID or slug
497
+ */
498
+ async getProduct(idOrSlug) {
499
+ return this.http.get(`/public/products/${idOrSlug}`);
500
+ }
501
+ /**
502
+ * Get featured products
503
+ */
504
+ async featuredProducts(limit = 8) {
505
+ const response = await this.http.get("/public/products", {
506
+ per_page: limit,
507
+ is_featured: true
508
+ });
509
+ return response.data;
510
+ }
511
+ /**
512
+ * Search products
513
+ */
514
+ async searchProducts(query, params) {
515
+ return this.http.get("/public/products", {
516
+ ...params,
517
+ search: query
518
+ });
519
+ }
520
+ // ============================================
521
+ // Categories (Public)
522
+ // ============================================
523
+ /**
524
+ * List product categories
525
+ */
526
+ async listCategories() {
527
+ return this.http.get("/public/categories");
528
+ }
529
+ /**
530
+ * Get category by ID or slug
531
+ */
532
+ async getCategory(idOrSlug) {
533
+ return this.http.get(`/public/categories/${idOrSlug}`);
534
+ }
535
+ /**
536
+ * Get products in category
537
+ */
538
+ async categoryProducts(categoryIdOrSlug, params) {
539
+ return this.http.get(`/public/categories/${categoryIdOrSlug}/products`, params);
540
+ }
541
+ // ============================================
542
+ // Cart
543
+ // ============================================
544
+ /**
545
+ * Get current cart
546
+ */
547
+ async getCart() {
548
+ return this.http.get("/cart");
549
+ }
550
+ /**
551
+ * Add item to cart
552
+ */
553
+ async addToCart(data) {
554
+ return this.http.post("/cart/items", data);
555
+ }
556
+ /**
557
+ * Update cart item quantity
558
+ */
559
+ async updateCartItem(itemId, data) {
560
+ return this.http.put(`/cart/items/${itemId}`, data);
561
+ }
562
+ /**
563
+ * Remove item from cart
564
+ */
565
+ async removeFromCart(itemId) {
566
+ return this.http.delete(`/cart/items/${itemId}`);
567
+ }
568
+ /**
569
+ * Clear entire cart
570
+ */
571
+ async clearCart() {
572
+ return this.http.delete("/cart");
573
+ }
574
+ // ============================================
575
+ // Orders (Protected)
576
+ // ============================================
577
+ /**
578
+ * List my orders
579
+ */
580
+ async listOrders(params) {
581
+ return this.http.get("/orders", params);
582
+ }
583
+ /**
584
+ * Get order by ID or order number
585
+ */
586
+ async getOrder(idOrNumber) {
587
+ return this.http.get(`/orders/${idOrNumber}`);
588
+ }
589
+ /**
590
+ * Create order from cart
591
+ */
592
+ async createOrder(data) {
593
+ return this.http.post("/orders", data);
594
+ }
595
+ /**
596
+ * Cancel order
597
+ */
598
+ async cancelOrder(orderId) {
599
+ return this.http.post(`/orders/${orderId}/cancel`);
600
+ }
601
+ // ============================================
602
+ // Payments
603
+ // ============================================
604
+ /**
605
+ * Get payment for order
606
+ */
607
+ async getPayment(orderId) {
608
+ return this.http.get(`/orders/${orderId}/payment`);
609
+ }
610
+ /**
611
+ * Prepare payment (get payment key)
612
+ */
613
+ async preparePayment(data) {
614
+ return this.http.post(`/payments/ready`, data);
615
+ }
616
+ /**
617
+ * Confirm payment (after Toss redirect)
618
+ */
619
+ async confirmPayment(data) {
620
+ return this.http.post("/payments/confirm", data);
621
+ }
622
+ /**
623
+ * Cancel payment
624
+ */
625
+ async cancelPayment(paymentId, data) {
626
+ return this.http.post(`/payments/${paymentId}/cancel`, data);
627
+ }
628
+ // ============================================
629
+ // Coupons
630
+ // ============================================
631
+ /**
632
+ * Validate coupon code
633
+ */
634
+ async validateCoupon(code, orderAmount) {
635
+ return this.http.post("/coupons/validate", {
636
+ code,
637
+ order_amount: orderAmount
638
+ });
639
+ }
640
+ /**
641
+ * Get available coupons for current user
642
+ */
643
+ async myCoupons() {
644
+ return this.http.get("/coupons");
645
+ }
646
+ };
647
+
648
+ // src/resources/media.ts
649
+ var MediaResource = class {
650
+ constructor(http) {
651
+ this.http = http;
652
+ }
653
+ /**
654
+ * List my media files
655
+ */
656
+ async list(params) {
657
+ return this.http.get("/media", params);
658
+ }
659
+ /**
660
+ * Get media by ID
661
+ */
662
+ async get(mediaId) {
663
+ return this.http.get(`/media/${mediaId}`);
664
+ }
665
+ /**
666
+ * Upload file
667
+ */
668
+ async upload(file) {
669
+ return this.http.upload("/media", file, "file");
670
+ }
671
+ /**
672
+ * Upload multiple files
673
+ */
674
+ async uploadMultiple(files) {
675
+ const results = [];
676
+ for (const file of files) {
677
+ const media = await this.upload(file);
678
+ results.push(media);
679
+ }
680
+ return results;
681
+ }
682
+ /**
683
+ * Delete media
684
+ */
685
+ async delete(mediaId) {
686
+ return this.http.delete(`/media/${mediaId}`);
687
+ }
688
+ };
689
+
690
+ // src/index.ts
691
+ var Promptly = class {
692
+ constructor(config) {
693
+ this.http = new HttpClient(config);
694
+ this.auth = new AuthResource(this.http);
695
+ this.boards = new BoardsResource(this.http);
696
+ this.blog = new BlogResource(this.http);
697
+ this.forms = new FormsResource(this.http);
698
+ this.shop = new ShopResource(this.http);
699
+ this.media = new MediaResource(this.http);
700
+ }
701
+ /**
702
+ * Get site theme settings
703
+ */
704
+ async getTheme() {
705
+ return this.http.get("/public/theme");
706
+ }
707
+ /**
708
+ * Get site settings
709
+ */
710
+ async getSettings() {
711
+ return this.http.get("/public/settings");
712
+ }
713
+ /**
714
+ * Check if user is authenticated
715
+ */
716
+ isAuthenticated() {
717
+ return this.auth.isAuthenticated();
718
+ }
719
+ /**
720
+ * Set authentication token manually
721
+ */
722
+ setToken(token) {
723
+ this.auth.setToken(token);
724
+ }
725
+ /**
726
+ * Get current authentication token
727
+ */
728
+ getToken() {
729
+ return this.auth.getToken();
730
+ }
731
+ };
732
+ var index_default = Promptly;
733
+ export {
734
+ Promptly,
735
+ PromptlyError,
736
+ index_default as default
737
+ };