bypilot-business-signup-sdk 0.1.0-beta.1

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,805 @@
1
+ // src/core/TokenManager.ts
2
+ var TokenManager = class {
3
+ constructor(providerName, strategy = "memory") {
4
+ this.memoryStorage = /* @__PURE__ */ new Map();
5
+ this.strategy = strategy;
6
+ this.storageKey = `bypilot_${providerName}_token`;
7
+ }
8
+ /**
9
+ * Token'ı storage'a kaydet
10
+ */
11
+ save(token) {
12
+ const serialized = JSON.stringify(token);
13
+ switch (this.strategy) {
14
+ case "localStorage":
15
+ if (typeof localStorage !== "undefined") {
16
+ localStorage.setItem(this.storageKey, serialized);
17
+ }
18
+ break;
19
+ case "sessionStorage":
20
+ if (typeof sessionStorage !== "undefined") {
21
+ sessionStorage.setItem(this.storageKey, serialized);
22
+ }
23
+ break;
24
+ case "memory":
25
+ default:
26
+ this.memoryStorage.set(this.storageKey, serialized);
27
+ break;
28
+ }
29
+ }
30
+ /**
31
+ * Token'ı storage'dan getir
32
+ */
33
+ get() {
34
+ let serialized = null;
35
+ switch (this.strategy) {
36
+ case "localStorage":
37
+ if (typeof localStorage !== "undefined") {
38
+ serialized = localStorage.getItem(this.storageKey);
39
+ }
40
+ break;
41
+ case "sessionStorage":
42
+ if (typeof sessionStorage !== "undefined") {
43
+ serialized = sessionStorage.getItem(this.storageKey);
44
+ }
45
+ break;
46
+ case "memory":
47
+ default:
48
+ serialized = this.memoryStorage.get(this.storageKey) ?? null;
49
+ break;
50
+ }
51
+ if (!serialized) return null;
52
+ try {
53
+ return JSON.parse(serialized);
54
+ } catch {
55
+ return null;
56
+ }
57
+ }
58
+ /**
59
+ * Token'ı sil
60
+ */
61
+ clear() {
62
+ switch (this.strategy) {
63
+ case "localStorage":
64
+ if (typeof localStorage !== "undefined") {
65
+ localStorage.removeItem(this.storageKey);
66
+ }
67
+ break;
68
+ case "sessionStorage":
69
+ if (typeof sessionStorage !== "undefined") {
70
+ sessionStorage.removeItem(this.storageKey);
71
+ }
72
+ break;
73
+ case "memory":
74
+ default:
75
+ this.memoryStorage.delete(this.storageKey);
76
+ break;
77
+ }
78
+ }
79
+ /**
80
+ * Token'ın geçerli olup olmadığını kontrol et
81
+ */
82
+ isValid() {
83
+ const token = this.get();
84
+ if (!token) return false;
85
+ if (token.expiresAt) {
86
+ return Date.now() < token.expiresAt;
87
+ }
88
+ return true;
89
+ }
90
+ /**
91
+ * Token'ın süresinin dolmasına kalan süre (ms)
92
+ */
93
+ getTimeUntilExpiry() {
94
+ const token = this.get();
95
+ if (!token?.expiresAt) return null;
96
+ return Math.max(0, token.expiresAt - Date.now());
97
+ }
98
+ };
99
+
100
+ // src/core/EventEmitter.ts
101
+ var EventEmitter = class {
102
+ constructor() {
103
+ this.listeners = /* @__PURE__ */ new Map();
104
+ }
105
+ /**
106
+ * Event listener ekle
107
+ */
108
+ on(event, listener) {
109
+ if (!this.listeners.has(event)) {
110
+ this.listeners.set(event, /* @__PURE__ */ new Set());
111
+ }
112
+ this.listeners.get(event).add(listener);
113
+ return () => this.off(event, listener);
114
+ }
115
+ /**
116
+ * Event listener kaldır
117
+ */
118
+ off(event, listener) {
119
+ const eventListeners = this.listeners.get(event);
120
+ if (eventListeners) {
121
+ eventListeners.delete(listener);
122
+ }
123
+ }
124
+ /**
125
+ * Event tetikle
126
+ */
127
+ emit(event, data) {
128
+ const eventListeners = this.listeners.get(event);
129
+ if (eventListeners) {
130
+ eventListeners.forEach((listener) => {
131
+ try {
132
+ listener(data);
133
+ } catch (error) {
134
+ console.error(`[ByPilot SDK] Event listener error for ${event}:`, error);
135
+ }
136
+ });
137
+ }
138
+ }
139
+ /**
140
+ * Tek seferlik event listener
141
+ */
142
+ once(event, listener) {
143
+ const onceListener = (data) => {
144
+ this.off(event, onceListener);
145
+ listener(data);
146
+ };
147
+ return this.on(event, onceListener);
148
+ }
149
+ /**
150
+ * Tüm listener'ları temizle
151
+ */
152
+ removeAllListeners(event) {
153
+ if (event) {
154
+ this.listeners.delete(event);
155
+ } else {
156
+ this.listeners.clear();
157
+ }
158
+ }
159
+ };
160
+
161
+ // src/core/PopupManager.ts
162
+ var PopupManager = class {
163
+ constructor() {
164
+ this.popup = null;
165
+ this.checkInterval = null;
166
+ }
167
+ /**
168
+ * Popup aç ve URL'e yönlendir
169
+ */
170
+ open(url, config = {}) {
171
+ const {
172
+ width = 600,
173
+ height = 700,
174
+ left = window.screenX + (window.outerWidth - width) / 2,
175
+ top = window.screenY + (window.outerHeight - height) / 2
176
+ } = config;
177
+ const features = [
178
+ `width=${width}`,
179
+ `height=${height}`,
180
+ `left=${left}`,
181
+ `top=${top}`,
182
+ "toolbar=no",
183
+ "menubar=no",
184
+ "scrollbars=yes",
185
+ "resizable=yes"
186
+ ].join(",");
187
+ this.popup = window.open(url, "bypilot_oauth", features);
188
+ return this.popup;
189
+ }
190
+ /**
191
+ * Popup'ın kapanmasını bekle
192
+ */
193
+ waitForClose() {
194
+ return new Promise((resolve) => {
195
+ if (!this.popup) {
196
+ resolve();
197
+ return;
198
+ }
199
+ this.checkInterval = window.setInterval(() => {
200
+ if (!this.popup || this.popup.closed) {
201
+ this.cleanup();
202
+ resolve();
203
+ }
204
+ }, 100);
205
+ });
206
+ }
207
+ /**
208
+ * Popup'tan callback al (postMessage ile)
209
+ */
210
+ waitForCallback(origin, timeout = 3e5) {
211
+ return new Promise((resolve, reject) => {
212
+ const timeoutId = window.setTimeout(() => {
213
+ cleanup();
214
+ reject(new Error("OAuth callback timeout"));
215
+ }, timeout);
216
+ const messageHandler = (event) => {
217
+ if (event.origin !== origin) return;
218
+ if (event.data?.type === "bypilot_oauth_callback") {
219
+ cleanup();
220
+ resolve(event.data.payload);
221
+ }
222
+ };
223
+ const cleanup = () => {
224
+ window.clearTimeout(timeoutId);
225
+ window.removeEventListener("message", messageHandler);
226
+ this.cleanup();
227
+ };
228
+ window.addEventListener("message", messageHandler);
229
+ this.checkInterval = window.setInterval(() => {
230
+ if (!this.popup || this.popup.closed) {
231
+ cleanup();
232
+ reject(new Error("OAuth popup closed by user"));
233
+ }
234
+ }, 100);
235
+ });
236
+ }
237
+ /**
238
+ * Popup'ı kapat ve temizle
239
+ */
240
+ close() {
241
+ if (this.popup && !this.popup.closed) {
242
+ this.popup.close();
243
+ }
244
+ this.cleanup();
245
+ }
246
+ cleanup() {
247
+ if (this.checkInterval) {
248
+ window.clearInterval(this.checkInterval);
249
+ this.checkInterval = null;
250
+ }
251
+ this.popup = null;
252
+ }
253
+ /**
254
+ * Popup aktif mi?
255
+ */
256
+ isOpen() {
257
+ return this.popup !== null && !this.popup.closed;
258
+ }
259
+ };
260
+
261
+ // src/providers/BaseProvider.ts
262
+ var BaseProvider = class extends EventEmitter {
263
+ constructor(config) {
264
+ super();
265
+ this.config = config;
266
+ this.tokenManager = new TokenManager(
267
+ this.constructor.name.toLowerCase(),
268
+ config.storage ?? "memory"
269
+ );
270
+ this.popupManager = new PopupManager();
271
+ }
272
+ /**
273
+ * Random state oluştur
274
+ */
275
+ generateState() {
276
+ const array = new Uint8Array(32);
277
+ crypto.getRandomValues(array);
278
+ return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
279
+ }
280
+ /**
281
+ * Popup ile login başlat
282
+ */
283
+ async loginWithPopup(popupConfig) {
284
+ this.emit("auth:start");
285
+ const state = this.config.state ?? this.generateState();
286
+ const authUrl = this.buildAuthorizationUrl(state);
287
+ sessionStorage.setItem(`bypilot_state_${this.name}`, state);
288
+ try {
289
+ this.popupManager.open(authUrl, popupConfig);
290
+ const callbackData = await this.popupManager.waitForCallback(
291
+ window.location.origin
292
+ );
293
+ const savedState = sessionStorage.getItem(`bypilot_state_${this.name}`);
294
+ if (callbackData.state !== savedState) {
295
+ throw new Error("State mismatch - possible CSRF attack");
296
+ }
297
+ const result = await this.handleCallback(callbackData);
298
+ if (result.success && result.token) {
299
+ this.tokenManager.save(result.token);
300
+ this.emit("auth:success", result);
301
+ } else {
302
+ this.emit("auth:error", result);
303
+ }
304
+ return result;
305
+ } catch (error) {
306
+ const errorResult = {
307
+ success: false,
308
+ error: error instanceof Error ? error.message : "Unknown error"
309
+ };
310
+ if (error instanceof Error && error.message.includes("closed by user")) {
311
+ this.emit("auth:cancel");
312
+ } else {
313
+ this.emit("auth:error", errorResult);
314
+ }
315
+ return errorResult;
316
+ } finally {
317
+ sessionStorage.removeItem(`bypilot_state_${this.name}`);
318
+ }
319
+ }
320
+ /**
321
+ * Redirect ile login başlat
322
+ */
323
+ loginWithRedirect() {
324
+ this.emit("auth:start");
325
+ const state = this.config.state ?? this.generateState();
326
+ const authUrl = this.buildAuthorizationUrl(state);
327
+ sessionStorage.setItem(`bypilot_state_${this.name}`, state);
328
+ window.location.href = authUrl;
329
+ }
330
+ /**
331
+ * Redirect callback'ini işle (sayfa yüklendiğinde çağrılmalı)
332
+ */
333
+ async handleRedirectCallback() {
334
+ const urlParams = new URLSearchParams(window.location.search);
335
+ const code = urlParams.get("code");
336
+ const state = urlParams.get("state");
337
+ const error = urlParams.get("error");
338
+ if (!code && !error) {
339
+ return null;
340
+ }
341
+ const savedState = sessionStorage.getItem(`bypilot_state_${this.name}`);
342
+ if (state !== savedState) {
343
+ const result = {
344
+ success: false,
345
+ error: "state_mismatch",
346
+ errorDescription: "State mismatch - possible CSRF attack"
347
+ };
348
+ this.emit("auth:error", result);
349
+ return result;
350
+ }
351
+ sessionStorage.removeItem(`bypilot_state_${this.name}`);
352
+ if (error) {
353
+ const result = {
354
+ success: false,
355
+ error,
356
+ errorDescription: urlParams.get("error_description") ?? void 0
357
+ };
358
+ this.emit("auth:error", result);
359
+ return result;
360
+ }
361
+ try {
362
+ const callbackData = {};
363
+ urlParams.forEach((value, key) => {
364
+ callbackData[key] = value;
365
+ });
366
+ const result = await this.handleCallback(callbackData);
367
+ if (result.success && result.token) {
368
+ this.tokenManager.save(result.token);
369
+ this.emit("auth:success", result);
370
+ } else {
371
+ this.emit("auth:error", result);
372
+ }
373
+ window.history.replaceState({}, document.title, window.location.pathname);
374
+ return result;
375
+ } catch (error2) {
376
+ const errorResult = {
377
+ success: false,
378
+ error: error2 instanceof Error ? error2.message : "Unknown error"
379
+ };
380
+ this.emit("auth:error", errorResult);
381
+ return errorResult;
382
+ }
383
+ }
384
+ /**
385
+ * Mevcut token'ı getir
386
+ */
387
+ getToken() {
388
+ return this.tokenManager.get();
389
+ }
390
+ /**
391
+ * Access token'ı getir
392
+ */
393
+ getAccessToken() {
394
+ return this.tokenManager.get()?.accessToken ?? null;
395
+ }
396
+ /**
397
+ * Token geçerli mi?
398
+ */
399
+ isAuthenticated() {
400
+ return this.tokenManager.isValid();
401
+ }
402
+ /**
403
+ * Logout - token'ı temizle
404
+ */
405
+ logout() {
406
+ this.tokenManager.clear();
407
+ this.emit("token:expire");
408
+ }
409
+ /**
410
+ * Storage stratejisini değiştir
411
+ */
412
+ setStorageStrategy(strategy) {
413
+ const currentToken = this.tokenManager.get();
414
+ this.tokenManager = new TokenManager(this.name, strategy);
415
+ if (currentToken) {
416
+ this.tokenManager.save(currentToken);
417
+ }
418
+ }
419
+ };
420
+
421
+ // src/providers/whatsapp/WhatsAppProvider.ts
422
+ var WhatsAppProvider = class extends BaseProvider {
423
+ constructor(config) {
424
+ super(config);
425
+ this.name = "whatsapp";
426
+ this.authorizationEndpoint = "https://www.facebook.com/v24.0/dialog/oauth";
427
+ this.fbSDKLoaded = false;
428
+ this.fbSDKLoadPromise = null;
429
+ /**
430
+ * Son alınan session info
431
+ */
432
+ this.lastSessionInfo = null;
433
+ this.whatsappConfig = config;
434
+ }
435
+ /**
436
+ * Facebook SDK'yı yükle
437
+ */
438
+ async loadFacebookSDK() {
439
+ if (this.fbSDKLoaded && window.FB) {
440
+ return;
441
+ }
442
+ if (this.fbSDKLoadPromise) {
443
+ return this.fbSDKLoadPromise;
444
+ }
445
+ this.fbSDKLoadPromise = new Promise((resolve, reject) => {
446
+ if (window.FB) {
447
+ this.fbSDKLoaded = true;
448
+ this.initFacebookSDK();
449
+ resolve();
450
+ return;
451
+ }
452
+ const script = document.createElement("script");
453
+ script.id = "facebook-jssdk";
454
+ script.src = `https://connect.facebook.net/en_US/sdk.js`;
455
+ script.async = true;
456
+ script.defer = true;
457
+ window.fbAsyncInit = () => {
458
+ this.initFacebookSDK();
459
+ this.fbSDKLoaded = true;
460
+ resolve();
461
+ };
462
+ script.onerror = () => {
463
+ reject(new Error("Failed to load Facebook SDK"));
464
+ };
465
+ const existingScript = document.getElementById("facebook-jssdk");
466
+ if (existingScript) {
467
+ existingScript.remove();
468
+ }
469
+ document.head.appendChild(script);
470
+ setTimeout(() => {
471
+ if (!this.fbSDKLoaded) {
472
+ reject(new Error("Facebook SDK load timeout"));
473
+ }
474
+ }, 1e4);
475
+ });
476
+ return this.fbSDKLoadPromise;
477
+ }
478
+ /**
479
+ * Facebook SDK'yı başlat
480
+ */
481
+ initFacebookSDK() {
482
+ if (!window.FB) return;
483
+ window.FB.init({
484
+ appId: this.whatsappConfig.clientId,
485
+ cookie: true,
486
+ xfbml: true,
487
+ version: this.whatsappConfig.sdkVersion ?? "v24.0"
488
+ });
489
+ }
490
+ /**
491
+ * Authorization URL oluştur (redirect flow için)
492
+ */
493
+ buildAuthorizationUrl(state) {
494
+ const params = new URLSearchParams({
495
+ client_id: this.whatsappConfig.clientId,
496
+ redirect_uri: this.whatsappConfig.redirectUri,
497
+ state,
498
+ response_type: "code",
499
+ config_id: this.whatsappConfig.configId,
500
+ scope: this.whatsappConfig.scope ?? "whatsapp_business_management,whatsapp_business_messaging"
501
+ });
502
+ return `${this.authorizationEndpoint}?${params.toString()}`;
503
+ }
504
+ /**
505
+ * Callback'i işle
506
+ */
507
+ async handleCallback(callbackData) {
508
+ const code = callbackData.code;
509
+ const error = callbackData.error;
510
+ if (error) {
511
+ return {
512
+ success: false,
513
+ error,
514
+ errorDescription: callbackData.error_description
515
+ };
516
+ }
517
+ if (!code) {
518
+ return {
519
+ success: false,
520
+ error: "missing_code",
521
+ errorDescription: "Authorization code not found in callback"
522
+ };
523
+ }
524
+ return {
525
+ success: true,
526
+ raw: { code },
527
+ token: {
528
+ accessToken: code,
529
+ // Bu aslında code, backend'de token'a çevrilmeli
530
+ tokenType: "authorization_code"
531
+ }
532
+ };
533
+ }
534
+ /**
535
+ * WhatsApp Embedded Signup popup ile başlat
536
+ *
537
+ * Facebook SDK kullanarak native popup deneyimi sağlar.
538
+ * Session info message event'i ile WABA ID, Phone Number ID ve access token alır.
539
+ */
540
+ async loginWithPopup(_popupConfig) {
541
+ this.emit("auth:start");
542
+ try {
543
+ await this.loadFacebookSDK();
544
+ if (!window.FB) {
545
+ throw new Error("Facebook SDK not available");
546
+ }
547
+ const sessionInfoPromise = this.waitForSessionInfo();
548
+ const response = await this.launchEmbeddedSignup();
549
+ if (response.status === "not_authorized") {
550
+ this.emit("auth:cancel");
551
+ return {
552
+ success: false,
553
+ error: "not_authorized",
554
+ errorDescription: "User did not authorize the app"
555
+ };
556
+ }
557
+ const authResponse = response.authResponse;
558
+ let sessionInfo = null;
559
+ try {
560
+ sessionInfo = await Promise.race([
561
+ sessionInfoPromise,
562
+ new Promise((resolve) => setTimeout(() => resolve(null), 5e3))
563
+ ]);
564
+ } catch {
565
+ }
566
+ let token;
567
+ if (sessionInfo?.accessToken) {
568
+ token = {
569
+ accessToken: sessionInfo.accessToken,
570
+ tokenType: "bearer",
571
+ scope: this.whatsappConfig.scope
572
+ };
573
+ this.lastSessionInfo = sessionInfo;
574
+ } else if (authResponse?.code) {
575
+ token = {
576
+ accessToken: authResponse.code,
577
+ tokenType: "authorization_code"
578
+ };
579
+ } else if (authResponse?.accessToken) {
580
+ token = this.buildToken(authResponse);
581
+ } else {
582
+ return {
583
+ success: false,
584
+ error: "no_token",
585
+ errorDescription: "No access token or authorization code received"
586
+ };
587
+ }
588
+ this.tokenManager.save(token);
589
+ const result = {
590
+ success: true,
591
+ token,
592
+ raw: {
593
+ ...authResponse,
594
+ sessionInfo
595
+ }
596
+ };
597
+ this.emit("auth:success", result);
598
+ return result;
599
+ } catch (error) {
600
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
601
+ if (errorMessage.includes("closed") || errorMessage.includes("cancel")) {
602
+ this.emit("auth:cancel");
603
+ } else {
604
+ this.emit("auth:error", { error: errorMessage });
605
+ }
606
+ return {
607
+ success: false,
608
+ error: errorMessage
609
+ };
610
+ }
611
+ }
612
+ /**
613
+ * Session info için message event bekle
614
+ */
615
+ waitForSessionInfo() {
616
+ return new Promise((resolve) => {
617
+ const handler = (event) => {
618
+ if (event.origin !== "https://www.facebook.com" && event.origin !== "https://web.facebook.com") {
619
+ return;
620
+ }
621
+ try {
622
+ const data = JSON.parse(event.data);
623
+ if (data.type === "WA_EMBEDDED_SIGNUP") {
624
+ window.removeEventListener("message", handler);
625
+ const sessionInfo = {
626
+ accessToken: data.data?.access_token ?? "",
627
+ phoneNumberId: data.data?.phone_number_id,
628
+ wabaId: data.data?.waba_id,
629
+ phoneNumber: data.data?.phone_number,
630
+ businessId: data.data?.business_id
631
+ };
632
+ resolve(sessionInfo);
633
+ }
634
+ } catch {
635
+ }
636
+ };
637
+ window.addEventListener("message", handler);
638
+ });
639
+ }
640
+ /**
641
+ * Son session info'yu al
642
+ */
643
+ getLastSessionInfo() {
644
+ return this.lastSessionInfo;
645
+ }
646
+ /**
647
+ * Embedded Signup akışını başlat
648
+ */
649
+ launchEmbeddedSignup() {
650
+ return new Promise((resolve) => {
651
+ const extras = {
652
+ ...this.whatsappConfig.extras ?? {},
653
+ featureType: this.whatsappConfig.extras?.featureType ?? "whatsapp_embedded_signup",
654
+ sessionInfoVersion: this.whatsappConfig.extras?.sessionInfoVersion ?? 3
655
+ };
656
+ if (this.whatsappConfig.solutionId) {
657
+ extras.solutionID = this.whatsappConfig.solutionId;
658
+ }
659
+ window.FB.login(
660
+ (response) => resolve(response),
661
+ {
662
+ config_id: this.whatsappConfig.configId,
663
+ response_type: "code",
664
+ override_default_response_type: true,
665
+ extras
666
+ }
667
+ );
668
+ });
669
+ }
670
+ /**
671
+ * Auth response'dan token oluştur
672
+ */
673
+ buildToken(authResponse) {
674
+ return {
675
+ accessToken: authResponse.accessToken ?? "",
676
+ tokenType: "bearer",
677
+ expiresAt: authResponse.expiresIn ? Date.now() + authResponse.expiresIn * 1e3 : void 0,
678
+ scope: this.whatsappConfig.scope
679
+ };
680
+ }
681
+ /**
682
+ * Session info al (Embedded Signup sonrası)
683
+ *
684
+ * Facebook SDK message event'i ile session bilgilerini alır
685
+ */
686
+ getSessionInfoListener(callback) {
687
+ const handler = (event) => {
688
+ if (event.origin !== "https://www.facebook.com" && event.origin !== "https://web.facebook.com") {
689
+ return;
690
+ }
691
+ try {
692
+ const data = JSON.parse(event.data);
693
+ if (data.type === "WA_EMBEDDED_SIGNUP") {
694
+ const sessionInfo = {
695
+ accessToken: data.data?.access_token ?? "",
696
+ phoneNumberId: data.data?.phone_number_id,
697
+ wabaId: data.data?.waba_id,
698
+ phoneNumber: data.data?.phone_number
699
+ };
700
+ callback(sessionInfo);
701
+ }
702
+ } catch {
703
+ }
704
+ };
705
+ window.addEventListener("message", handler);
706
+ return () => window.removeEventListener("message", handler);
707
+ }
708
+ /**
709
+ * Login durumunu kontrol et
710
+ */
711
+ async checkLoginStatus() {
712
+ try {
713
+ await this.loadFacebookSDK();
714
+ if (!window.FB) return null;
715
+ return new Promise((resolve) => {
716
+ window.FB.getLoginStatus((response) => {
717
+ resolve(response);
718
+ });
719
+ });
720
+ } catch {
721
+ return null;
722
+ }
723
+ }
724
+ /**
725
+ * Facebook'tan logout
726
+ */
727
+ logout() {
728
+ super.logout();
729
+ if (window.FB) {
730
+ try {
731
+ window.FB.logout();
732
+ } catch {
733
+ }
734
+ }
735
+ }
736
+ /**
737
+ * Graph API çağrısı yap (Facebook SDK kullanarak)
738
+ *
739
+ * @param path - API endpoint (örn: 'me' veya 'v24.0/me')
740
+ * @param method - HTTP method
741
+ * @param params - Query/body parametreleri
742
+ */
743
+ async graphAPI(path, method = "GET", params) {
744
+ await this.loadFacebookSDK();
745
+ if (!window.FB) {
746
+ throw new Error("Facebook SDK not available");
747
+ }
748
+ const version = this.whatsappConfig.graphApiVersion ?? "v24.0";
749
+ const versionedPath = path.startsWith("v") || path.startsWith("/v") ? path : `${version}/${path}`;
750
+ return new Promise((resolve, reject) => {
751
+ const accessToken = this.getAccessToken();
752
+ window.FB.api(
753
+ versionedPath,
754
+ method,
755
+ { ...params, access_token: accessToken },
756
+ (response) => {
757
+ const resp = response;
758
+ if (resp.error) {
759
+ reject(new Error(resp.error.message ?? "Graph API error"));
760
+ } else {
761
+ resolve(response);
762
+ }
763
+ }
764
+ );
765
+ });
766
+ }
767
+ /**
768
+ * WhatsApp Cloud API'ye doğrudan HTTP çağrısı yap
769
+ *
770
+ * @param endpoint - API endpoint (örn: '123456/messages')
771
+ * @param method - HTTP method
772
+ * @param body - Request body
773
+ */
774
+ async whatsappAPI(endpoint, method = "GET", body) {
775
+ const accessToken = this.getAccessToken();
776
+ const version = this.whatsappConfig.graphApiVersion ?? "v24.0";
777
+ const url = `https://graph.facebook.com/${version}/${endpoint}`;
778
+ const options = {
779
+ method,
780
+ headers: {
781
+ "Authorization": `Bearer ${accessToken}`,
782
+ "Content-Type": "application/json"
783
+ }
784
+ };
785
+ if (body && (method === "POST" || method === "DELETE")) {
786
+ options.body = JSON.stringify(body);
787
+ }
788
+ const response = await fetch(url, options);
789
+ if (!response.ok) {
790
+ const error = await response.json().catch(() => ({ message: "Unknown error" }));
791
+ throw new Error(error.message || `HTTP ${response.status}`);
792
+ }
793
+ return response.json();
794
+ }
795
+ /**
796
+ * Graph API version'unu al
797
+ */
798
+ getGraphApiVersion() {
799
+ return this.whatsappConfig.graphApiVersion ?? "v24.0";
800
+ }
801
+ };
802
+
803
+ export { BaseProvider, EventEmitter, PopupManager, TokenManager, WhatsAppProvider };
804
+ //# sourceMappingURL=chunk-B3CUULGV.js.map
805
+ //# sourceMappingURL=chunk-B3CUULGV.js.map