ab-ecosystem-cpass 1.0.12 → 1.0.13

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/README.md CHANGED
@@ -229,7 +229,7 @@ same pages ship inside the package, so they are readable offline at
229
229
  | --- | --- | --- |
230
230
  | `crm.channels` | [channels](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/channels.md) | `list` `get` `updateDetails` `disable` `rotateToken` `getProp` `setProp` |
231
231
  | — | [channel properties](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/channel-props.md) | every `prop` `getProp`/`setProp` accepts |
232
- | `crm.channelOnboarding` | [connecting a channel](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/onboarding.md) | `types` `url` `exchange` `complete` `connect` |
232
+ | `crm.channelOnboarding` | [connecting a channel](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/onboarding.md) | `types` `url` `pending` `complete` `exchange` `connect` |
233
233
  | `crm.templates` | [templates](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/templates.md) | `list` `get` `create` `update` `delete` `sync` |
234
234
  | `crm.channelWebhooks` | [channel webhooks](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/webhooks.md) | `list` `get` `create` `update` `delete` |
235
235
  | `crm.billing` | [conversation billing](https://github.com/udghatan-dev/crm-sdk/blob/main/packages/sdk-js/docs/channels/billing.md) | `list` `stats` |
package/dist/index.d.ts CHANGED
@@ -26,8 +26,8 @@ export { CampaignMessages } from './resources/campaign-messages';
26
26
  export { MAX_BULK_RECEIVERS, buildReceivers, normaliseColumn, normaliseNumbers, parseDelimited, resolveAudience, type BulkAudienceInput, type CampaignReceiver, type ResolvedAudience, } from './resources/campaign-audience';
27
27
  export { Channels, type SetPropOptions } from './resources/channels/channels';
28
28
  export { ChannelOnboarding, type ConnectOptions } from './resources/channels/onboarding';
29
- /** The popup driver on its own, for a console that calls `url()` itself. */
30
- export { openSignup, EmbeddedSignupError, type OpenSignupOptions, } from './internal/embedded-signup';
29
+ /** The popup drivers on their own, for a console that calls `url()` itself. */
30
+ export { awaitSignupReturn, openSignup, EmbeddedSignupError, type OpenSignupOptions, } from './internal/embedded-signup';
31
31
  export { Templates } from './resources/channels/templates';
32
32
  export { ChannelWebhooks } from './resources/channels/webhooks';
33
33
  export { Billing } from './resources/channels/billing';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Activity = exports.Wallet = exports.Billing = exports.ChannelWebhooks = exports.Templates = exports.EmbeddedSignupError = exports.openSignup = exports.ChannelOnboarding = exports.Channels = exports.resolveAudience = exports.parseDelimited = exports.normaliseNumbers = exports.normaliseColumn = exports.buildReceivers = exports.MAX_BULK_RECEIVERS = exports.CampaignMessages = exports.Campaigns = exports.InboxMedia = exports.DataStoreRecords = exports.DataStoreCollections = exports.DataStores = exports.Notifications = exports.Webhooks = exports.Exports = exports.Imports = exports.CrmTriggers = exports.Segments = exports.CustomFields = exports.CannedReplies = exports.ContactNotes = exports.ContactTags = exports.Contacts = exports.CHANNELS = exports.AUDIENCE = void 0;
17
+ exports.Activity = exports.Wallet = exports.Billing = exports.ChannelWebhooks = exports.Templates = exports.EmbeddedSignupError = exports.openSignup = exports.awaitSignupReturn = exports.ChannelOnboarding = exports.Channels = exports.resolveAudience = exports.parseDelimited = exports.normaliseNumbers = exports.normaliseColumn = exports.buildReceivers = exports.MAX_BULK_RECEIVERS = exports.CampaignMessages = exports.Campaigns = exports.InboxMedia = exports.DataStoreRecords = exports.DataStoreCollections = exports.DataStores = exports.Notifications = exports.Webhooks = exports.Exports = exports.Imports = exports.CrmTriggers = exports.Segments = exports.CustomFields = exports.CannedReplies = exports.ContactNotes = exports.ContactTags = exports.Contacts = exports.CHANNELS = exports.AUDIENCE = void 0;
18
18
  __exportStar(require("./client"), exports);
19
19
  __exportStar(require("./http"), exports);
20
20
  __exportStar(require("./errors"), exports);
@@ -73,8 +73,9 @@ var channels_1 = require("./resources/channels/channels");
73
73
  Object.defineProperty(exports, "Channels", { enumerable: true, get: function () { return channels_1.Channels; } });
74
74
  var onboarding_1 = require("./resources/channels/onboarding");
75
75
  Object.defineProperty(exports, "ChannelOnboarding", { enumerable: true, get: function () { return onboarding_1.ChannelOnboarding; } });
76
- /** The popup driver on its own, for a console that calls `url()` itself. */
76
+ /** The popup drivers on their own, for a console that calls `url()` itself. */
77
77
  var embedded_signup_1 = require("./internal/embedded-signup");
78
+ Object.defineProperty(exports, "awaitSignupReturn", { enumerable: true, get: function () { return embedded_signup_1.awaitSignupReturn; } });
78
79
  Object.defineProperty(exports, "openSignup", { enumerable: true, get: function () { return embedded_signup_1.openSignup; } });
79
80
  Object.defineProperty(exports, "EmbeddedSignupError", { enumerable: true, get: function () { return embedded_signup_1.EmbeddedSignupError; } });
80
81
  var templates_1 = require("./resources/channels/templates");
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,yCAAuB;AACvB,2CAAyB;AACzB,0CAAwB;AAExB,2CAAuD;AAA9C,iGAAA,QAAQ,OAAA;AAAE,iGAAA,QAAQ,OAAA;AAE3B,WAAW;AACX,0DAAyD;AAAhD,oGAAA,QAAQ,OAAA;AACjB,kEAAgE;AAAvD,2GAAA,WAAW,OAAA;AACpB,oEAAkE;AAAzD,6GAAA,YAAY,OAAA;AACrB,sEAAoE;AAA3D,+GAAA,aAAa,OAAA;AACtB,oEAAkE;AAAzD,6GAAA,YAAY,OAAA;AACrB,0DAAyD;AAAhD,oGAAA,QAAQ,OAA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ACf,0DAAyD;AAAhD,oGAAA,QAAQ,OAAA"}
@@ -1,7 +1,10 @@
1
- import type { SignupCredentials } from '../types';
1
+ import type { OnboardingReturn, SignupCredentials } from '../types';
2
2
  /**
3
- * The dialog answers in two halves -- the code on the redirect, the ids by
4
- * `postMessage` -- and neither is usable alone, so `openSignup` waits for both.
3
+ * Two drivers for the same popup. {@link awaitSignupReturn} is the one to use:
4
+ * the code goes to the service's callback, so it only waits for the popup to
5
+ * come back to our origin. {@link openSignup} is the older half-and-half (code
6
+ * on the redirect, ids by `postMessage`), for a BSP app whose registered
7
+ * redirect is a page of yours -- what `/onboarding/exchange` exists for.
5
8
  */
6
9
  /** How the popup is opened, and how to follow its progress. */
7
10
  export interface OpenSignupOptions {
@@ -13,17 +16,31 @@ export interface OpenSignupOptions {
13
16
  timeoutMs?: number;
14
17
  /** For a console showing more than a spinner. `step` is Meta's own `current_step`. */
15
18
  onProgress?: (event: {
16
- phase: 'opened' | 'step' | 'code' | 'ids';
19
+ phase: 'opened' | 'step' | 'code' | 'ids' | 'returned';
17
20
  step?: string;
18
21
  }) => void;
19
22
  }
20
23
  /** Thrown for every way the dialog can fail to produce credentials. */
21
24
  export declare class EmbeddedSignupError extends Error {
22
25
  readonly code: 'unsupported_environment' | 'popup_blocked' | 'popup_closed' | 'cancelled' | 'signup_error' | 'timeout';
23
- constructor(code: 'unsupported_environment' | 'popup_blocked' | 'popup_closed' | 'cancelled' | 'signup_error' | 'timeout', message: string);
26
+ /** The service's own code, when the refusal came back on the return URL. */
27
+ readonly reason?: string | undefined;
28
+ constructor(code: 'unsupported_environment' | 'popup_blocked' | 'popup_closed' | 'cancelled' | 'signup_error' | 'timeout', message: string,
29
+ /** The service's own code, when the refusal came back on the return URL. */
30
+ reason?: string | undefined);
24
31
  }
25
32
  /**
26
- * Call this **straight from a click handler**: a `window.open` a browser does not
27
- * attribute to a gesture is blocked, and that looks like the user's popup blocker.
33
+ * The redirect flow: open the dialog, wait for the popup to come back to our
34
+ * origin with the service's outcome on it. Nothing here reads Meta, so the
35
+ * return has to be a page on **this** origin -- a cross-origin one cannot be
36
+ * read, and the wait ends as `popup_closed`.
37
+ *
38
+ * Call it **straight from a click handler**, or the `window.open` is blocked.
39
+ */
40
+ export declare const awaitSignupReturn: (url: string, options?: OpenSignupOptions) => Promise<OnboardingReturn>;
41
+ /**
42
+ * The older driver, for a BSP app whose registered redirect is a page of yours:
43
+ * it waits for the code *and* the ids, which is what `/onboarding/exchange`
44
+ * takes. Call it **straight from a click handler**, as above.
28
45
  */
29
46
  export declare const openSignup: (url: string, options?: OpenSignupOptions) => Promise<SignupCredentials>;
@@ -1,16 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.openSignup = exports.EmbeddedSignupError = void 0;
3
+ exports.openSignup = exports.awaitSignupReturn = exports.EmbeddedSignupError = void 0;
4
4
  /** Thrown for every way the dialog can fail to produce credentials. */
5
5
  class EmbeddedSignupError extends Error {
6
6
  code;
7
- constructor(code, message) {
7
+ reason;
8
+ constructor(code, message,
9
+ /** The service's own code, when the refusal came back on the return URL. */
10
+ reason) {
8
11
  super(message);
9
12
  this.code = code;
13
+ this.reason = reason;
10
14
  this.name = 'EmbeddedSignupError';
11
15
  }
12
16
  }
13
17
  exports.EmbeddedSignupError = EmbeddedSignupError;
18
+ /** The events that carry the ids. The second is a number off the WA Business app. */
19
+ const FINISH_EVENTS = ['FINISH', 'FINISH_WHATSAPP_BUSINESS_APP_ONBOARDING'];
14
20
  /** Origins Meta posts the signup events from. */
15
21
  const META_ORIGINS = ['https://www.facebook.com', 'https://web.facebook.com', 'https://business.facebook.com'];
16
22
  const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
@@ -31,20 +37,144 @@ const parsePayload = (data) => {
31
37
  }
32
38
  return typeof data === 'object' && data !== null ? data : undefined;
33
39
  };
40
+ const str = (value) => (typeof value === 'string' && value ? value : undefined);
41
+ const decodeBase64 = (value) => {
42
+ try {
43
+ const json = globalThis.atob?.(value);
44
+ return json ? parsePayload(json) : undefined;
45
+ }
46
+ catch {
47
+ return undefined;
48
+ }
49
+ };
34
50
  /**
35
- * Call this **straight from a click handler**: a `window.open` a browser does not
36
- * attribute to a gesture is blocked, and that looks like the user's popup blocker.
51
+ * A redirect page may post its whole query string rather than an object, and
52
+ * may wrap it in a base64 `json` param, as our panel does.
37
53
  */
38
- const openSignup = (url, options = {}) => {
39
- const win = browser();
40
- if (!win) {
41
- return Promise.reject(new EmbeddedSignupError('unsupported_environment', 'Embedded signup needs a browser; drive the three onboarding calls yourself outside one'));
54
+ const fromSameOrigin = (data) => {
55
+ if (typeof data === 'string') {
56
+ const text = data.trim();
57
+ if (!text || !/[?&=]/.test(text))
58
+ return undefined;
59
+ const params = new URLSearchParams(text.startsWith('?') ? text.slice(1) : text);
60
+ const wrapped = params.get('json');
61
+ if (wrapped) {
62
+ const payload = decodeBase64(wrapped);
63
+ // `{ success: false, data: '<message>' }` on a refusal.
64
+ if (payload?.success === false)
65
+ return { error: str(payload.data) ?? 'Signup was refused' };
66
+ const inner = (payload?.data ?? payload);
67
+ return inner ?? undefined;
68
+ }
69
+ return Object.fromEntries(params.entries());
42
70
  }
71
+ const payload = parsePayload(data);
72
+ return payload?.type === 'CRM_CHANNEL_ONBOARDING' ? payload : undefined;
73
+ };
74
+ /** Centred, and named so a second call reuses the window rather than opening two. */
75
+ const openPopup = (win, url, options) => {
43
76
  const width = options.width ?? 620;
44
77
  const height = options.height ?? 760;
45
78
  const left = Math.max(0, ((win.screen?.width ?? width) - width) / 2);
46
79
  const top = Math.max(0, ((win.screen?.height ?? height) - height) / 2);
47
- const popup = win.open(url, options.name ?? 'wa_embedded_signup', `popup=1,width=${width},height=${height},left=${left},top=${top},scrollbars=1,resizable=1`);
80
+ return win.open(url, options.name ?? 'wa_embedded_signup', `popup=1,width=${width},height=${height},left=${left},top=${top},scrollbars=1,resizable=1`);
81
+ };
82
+ /** A console's progress callback is never allowed to break the flow. */
83
+ const report = (options, phase, step) => {
84
+ try {
85
+ options.onProgress?.({ phase, step });
86
+ }
87
+ catch {
88
+ // Theirs to fix, not ours to propagate.
89
+ }
90
+ };
91
+ /**
92
+ * The redirect flow: open the dialog, wait for the popup to come back to our
93
+ * origin with the service's outcome on it. Nothing here reads Meta, so the
94
+ * return has to be a page on **this** origin -- a cross-origin one cannot be
95
+ * read, and the wait ends as `popup_closed`.
96
+ *
97
+ * Call it **straight from a click handler**, or the `window.open` is blocked.
98
+ */
99
+ const awaitSignupReturn = (url, options = {}) => {
100
+ const win = browser();
101
+ if (!win) {
102
+ return Promise.reject(new EmbeddedSignupError('unsupported_environment', 'Embedded signup needs a browser; drive the onboarding calls yourself outside one'));
103
+ }
104
+ const popup = openPopup(win, url, options);
105
+ if (!popup) {
106
+ return Promise.reject(new EmbeddedSignupError('popup_blocked', 'The signup window was blocked; allow popups and try again'));
107
+ }
108
+ return new Promise((resolve, reject) => {
109
+ let settled = false;
110
+ const finish = (outcome) => {
111
+ if (settled)
112
+ return;
113
+ settled = true;
114
+ win.removeEventListener('message', onMessage);
115
+ win.clearInterval(poll);
116
+ win.clearTimeout(timer);
117
+ try {
118
+ if (!popup.closed)
119
+ popup.close();
120
+ }
121
+ catch {
122
+ // Cross-origin as it closed; the window is the user's problem now.
123
+ }
124
+ outcome();
125
+ };
126
+ /** True once something carrying a `status` has been dealt with. */
127
+ const take = (returned) => {
128
+ const status = str(returned?.status);
129
+ if (!returned || !status)
130
+ return false;
131
+ report(options, 'returned');
132
+ if (status === 'error') {
133
+ finish(() => reject(new EmbeddedSignupError('signup_error', str(returned.message) ?? 'The number could not be connected', str(returned.code))));
134
+ return true;
135
+ }
136
+ finish(() => resolve(returned));
137
+ return true;
138
+ };
139
+ // For a return page that posts its query and closes itself, which the poll
140
+ // would never get to read.
141
+ const onMessage = (event) => {
142
+ if (event.origin !== win.location.origin)
143
+ return;
144
+ take(fromSameOrigin(event.data));
145
+ };
146
+ const poll = win.setInterval(() => {
147
+ if (popup.closed) {
148
+ finish(() => reject(new EmbeddedSignupError('popup_closed', 'The signup window was closed before it finished')));
149
+ return;
150
+ }
151
+ try {
152
+ // Reading this throws while the window is still on Meta's origin.
153
+ take(popup.location.search ? fromSameOrigin(popup.location.search) : undefined);
154
+ }
155
+ catch {
156
+ // Still at Meta, so there is nothing to read yet.
157
+ }
158
+ }, 400);
159
+ const timer = win.setTimeout(() => {
160
+ finish(() => reject(new EmbeddedSignupError('timeout', 'The signup window was not completed in time')));
161
+ }, options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
162
+ win.addEventListener('message', onMessage);
163
+ report(options, 'opened');
164
+ });
165
+ };
166
+ exports.awaitSignupReturn = awaitSignupReturn;
167
+ /**
168
+ * The older driver, for a BSP app whose registered redirect is a page of yours:
169
+ * it waits for the code *and* the ids, which is what `/onboarding/exchange`
170
+ * takes. Call it **straight from a click handler**, as above.
171
+ */
172
+ const openSignup = (url, options = {}) => {
173
+ const win = browser();
174
+ if (!win) {
175
+ return Promise.reject(new EmbeddedSignupError('unsupported_environment', 'Embedded signup needs a browser; drive the three onboarding calls yourself outside one'));
176
+ }
177
+ const popup = openPopup(win, url, options);
48
178
  if (!popup) {
49
179
  return Promise.reject(new EmbeddedSignupError('popup_blocked', 'The signup window was blocked; allow popups and try again'));
50
180
  }
@@ -87,20 +217,31 @@ const openSignup = (url, options = {}) => {
87
217
  }
88
218
  };
89
219
  const onMessage = (event) => {
220
+ // Our own redirect page posting back, rather than the poll reading its URL.
221
+ // It may hold all three, so it can finish the flow on its own.
222
+ if (event.origin === win.location.origin) {
223
+ const returned = fromSameOrigin(event.data);
224
+ if (returned) {
225
+ const error = str(returned.error) ?? str(returned.error_description);
226
+ if (error) {
227
+ finish(() => reject(new EmbeddedSignupError('signup_error', error)));
228
+ return;
229
+ }
230
+ code = str(returned.code) ?? code;
231
+ state = str(returned.state) ?? state;
232
+ wabaId = str(returned.waba_id) ?? wabaId;
233
+ phoneNumberId = str(returned.phone_number_id) ?? phoneNumberId;
234
+ if (code)
235
+ progress('code');
236
+ if (wabaId && phoneNumberId)
237
+ progress('ids');
238
+ settleIfComplete();
239
+ return;
240
+ }
241
+ }
90
242
  const payload = parsePayload(event.data);
91
243
  if (!payload)
92
244
  return;
93
- // Our own redirect page posting back, rather than the poll reading its URL.
94
- if (event.origin === win.location.origin && payload.type === 'CRM_CHANNEL_ONBOARDING') {
95
- if (typeof payload.code === 'string')
96
- code = payload.code;
97
- if (typeof payload.state === 'string')
98
- state = payload.state;
99
- if (code)
100
- progress('code');
101
- settleIfComplete();
102
- return;
103
- }
104
245
  if (!META_ORIGINS.includes(event.origin))
105
246
  return;
106
247
  if (payload.type !== 'WA_EMBEDDED_SIGNUP')
@@ -118,10 +259,11 @@ const openSignup = (url, options = {}) => {
118
259
  finish(() => reject(new EmbeddedSignupError('signup_error', message)));
119
260
  return;
120
261
  }
121
- if (typeof data.waba_id === 'string')
122
- wabaId = data.waba_id;
123
- if (typeof data.phone_number_id === 'string')
124
- phoneNumberId = data.phone_number_id;
262
+ // Only a finish event carries the ids; the rest are step changes.
263
+ if (FINISH_EVENTS.includes(String(payload.event))) {
264
+ wabaId = str(data.waba_id) ?? wabaId;
265
+ phoneNumberId = str(data.phone_number_id) ?? phoneNumberId;
266
+ }
125
267
  if (wabaId && phoneNumberId)
126
268
  progress('ids');
127
269
  else if (typeof data.current_step === 'string')
@@ -139,24 +281,25 @@ const openSignup = (url, options = {}) => {
139
281
  : 'The signup window was closed')));
140
282
  return;
141
283
  }
142
- if (code)
284
+ if (code && wabaId && phoneNumberId)
143
285
  return;
144
286
  try {
145
- const search = popup.location.search;
146
- if (!search)
287
+ const returned = popup.location.search ? fromSameOrigin(popup.location.search) : undefined;
288
+ if (!returned)
147
289
  return;
148
- const params = new URLSearchParams(search);
149
- const returned = params.get('code');
150
- if (!returned) {
151
- // A refusal arrives here and never as a message; without this the
152
- // popup just sits there.
153
- const error = params.get('error_description') ?? params.get('error');
154
- if (error)
155
- finish(() => reject(new EmbeddedSignupError('signup_error', error)));
290
+ // A refusal arrives here and never as a message; without this the popup
291
+ // just sits there.
292
+ const error = str(returned.error) ?? str(returned.error_description);
293
+ if (error) {
294
+ finish(() => reject(new EmbeddedSignupError('signup_error', error)));
156
295
  return;
157
296
  }
158
- code = returned;
159
- state = params.get('state') ?? state;
297
+ if (!returned.code)
298
+ return;
299
+ code = str(returned.code) ?? code;
300
+ state = str(returned.state) ?? state;
301
+ wabaId = str(returned.waba_id) ?? wabaId;
302
+ phoneNumberId = str(returned.phone_number_id) ?? phoneNumberId;
160
303
  progress('code');
161
304
  settleIfComplete();
162
305
  }
@@ -1 +1 @@
1
- {"version":3,"file":"embedded-signup.js","sourceRoot":"","sources":["../../src/internal/embedded-signup.ts"],"names":[],"mappings":";;;AAmBA,uEAAuE;AACvE,MAAa,mBAAoB,SAAQ,KAAK;IAEjC;IADX,YACW,IAMI,EACb,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QATN,SAAI,GAAJ,IAAI,CAMA;QAIb,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAdD,kDAcC;AAED,iDAAiD;AACjD,MAAM,YAAY,GAAG,CAAC,0BAA0B,EAAE,0BAA0B,EAAE,+BAA+B,CAAC,CAAC;AAE/G,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAoB1C,MAAM,OAAO,GAAG,GAA8B,EAAE;IAC9C,MAAM,SAAS,GAAI,UAAmC,CAAC,MAAmC,CAAC;IAC3F,OAAO,SAAS,IAAI,OAAO,SAAS,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;AACnF,CAAC,CAAC;AAEF,yDAAyD;AACzD,MAAM,YAAY,GAAG,CAAC,IAAa,EAAuC,EAAE;IAC1E,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI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C,IAAI,aAAa,CAAC;gBAC/D,QAAQ,CAAC,MAAM,CAAC,CAAC;gBACjB,gBAAgB,EAAE,CAAC;YACrB,CAAC;YAAC,MAAM,CAAC;gBACP,2DAA2D;YAC7D,CAAC;QACH,CAAC,EAAE,GAAG,CAAC,CAAC;QAER,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,GAAG,EAAE;YAChC,MAAM,CAAC,GAAG,EAAE,CACV,MAAM,CAAC,IAAI,mBAAmB,CAAC,SAAS,EAAE,6CAA6C,CAAC,CAAC,CAC1F,CAAC;QACJ,CAAC,EAAE,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAC,CAAC;QAE5C,GAAG,CAAC,gBAAgB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAC3C,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACrB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AArLW,QAAA,UAAU,cAqLrB"}
@@ -1,4 +1,4 @@
1
- import type { ChannelOnboardingOption, ChannelResponse, CompleteOnboardingInput, OnboardingChannelType, OnboardingExchangeInput, OnboardingExchangeResult, OnboardingUrlInput, OnboardingUrlResult } from '../../types';
1
+ import type { ChannelOnboardingOption, ChannelResponse, CompleteOnboardingInput, OnboardingChannelType, OnboardingExchangeInput, OnboardingExchangeResult, OnboardingPendingResult, OnboardingUrlInput, OnboardingUrlResult } from '../../types';
2
2
  import type { HttpClient } from '../../http';
3
3
  import { type OpenSignupOptions } from '../../internal/embedded-signup';
4
4
  /** Options for the one-call driver. */
@@ -7,20 +7,30 @@ export interface ConnectOptions extends OpenSignupOptions {
7
7
  type?: OnboardingChannelType;
8
8
  /** The inclusion to spend, when the plan carries more than one. */
9
9
  slug?: string;
10
- /** A non-default redirect, which must be one the service is configured for. */
11
- redirectUri?: string;
10
+ /** The BSP to connect through, by provider code. Omit for the active one. */
11
+ provider?: string;
12
+ /**
13
+ * Where the popup is handed back to. **Required**, and it has to be a page on
14
+ * this page's own origin -- that is what makes the outcome readable from here.
15
+ */
16
+ redirectUri: string;
12
17
  }
13
18
  /**
14
- * Connecting a channel. `connect()` does steps 1 to 3 and hands back the number
15
- * to confirm; or call `url()`, open the dialog yourself, then `exchange()`.
16
- * Either way the browser never sees a Meta credential.
19
+ * Connecting a channel. The browser only runs Meta's dialog: Meta returns to
20
+ * the service's callback, which redeems the code and sends the browser back to
21
+ * `redirectUri` with a `status` and a `token` -- no credential reaches this
22
+ * page. `connect()` drives all of it; or call `url()`, open the dialog
23
+ * yourself, and read the return off your own landing page.
17
24
  *
18
25
  * ```ts
19
26
  * // In the click handler for "Connect WhatsApp" -- not after an await.
20
- * const candidate = await crm.channelOnboarding.connect();
21
- * // Show candidate.phoneNumber, prefill a form from candidate.defaults...
27
+ * const candidate = await crm.channelOnboarding.connect({
28
+ * redirectUri: 'https://console.example.com/channels/connected',
29
+ * });
30
+ * // One number, or several to choose between: candidate.numbers
22
31
  * const { data: channel } = await crm.channelOnboarding.complete({
23
32
  * token: candidate.id,
33
+ * phoneNumberId: chosen.id,
24
34
  * name: form.name,
25
35
  * });
26
36
  * ```
@@ -29,22 +39,35 @@ export declare class ChannelOnboarding {
29
39
  private readonly http;
30
40
  constructor(http: HttpClient);
31
41
  /**
32
- * `GET /v1/channels/onboarding/types` -- every family, available or not, so a
33
- * picker greys rows out. Check `available`, show `reason`. Scope: `channels:read`.
42
+ * `GET /v1/channels/onboarding/types` -- the families this caller can connect
43
+ * right now. Anything the plan, the provider record or their role rules out is
44
+ * left out, so an empty list means "nothing to offer". Scope: `channels:read`.
34
45
  */
35
46
  types(): Promise<{
36
47
  data: ChannelOnboardingOption[];
37
48
  }>;
38
49
  /**
39
- * `POST /v1/channels/onboarding/url` -- the dialog URL and the `state` to hand
40
- * back. **Meta refuses to be framed**: use a popup. 402 if the plan has no room.
50
+ * `POST /v1/channels/onboarding/url` -- the page to open. **Meta refuses to
51
+ * be framed**: use a popup, or navigate. 402 if the plan has no room.
52
+ *
53
+ * `url` is a launcher on the app's own domain that forwards to the dialog
54
+ * (`dialogUrl`); `redirectUri` is where *we* return the browser, not what
55
+ * Meta is given -- that is `callbackUri`.
41
56
  */
42
- url(input?: OnboardingUrlInput): Promise<{
57
+ url(input: OnboardingUrlInput): Promise<{
43
58
  data: OnboardingUrlResult;
44
59
  }>;
45
60
  /**
46
- * `POST /v1/channels/onboarding/exchange` -- redeems the code. **Creates
47
- * nothing**: show `phoneNumber`, Meta's own description, and let them confirm.
61
+ * `GET /v1/channels/onboarding/pending/:token` -- what the return URL's
62
+ * `token` refers to: the numbers it covers, and the form's prefill.
63
+ */
64
+ pending(token: string): Promise<{
65
+ data: OnboardingPendingResult;
66
+ }>;
67
+ /**
68
+ * `POST /v1/channels/onboarding/exchange` -- redeems a code you hold yourself,
69
+ * for a BSP app whose registered redirect is a page of yours rather than the
70
+ * service's callback. **Creates nothing**: show `phoneNumber` and confirm.
48
71
  */
49
72
  exchange(input: OnboardingExchangeInput): Promise<{
50
73
  data: OnboardingExchangeResult;
@@ -55,8 +78,12 @@ export declare class ChannelOnboarding {
55
78
  */
56
79
  complete(input: CompleteOnboardingInput): Promise<ChannelResponse>;
57
80
  /**
58
- * The URL, the dialog and the exchange in one call. **Call it straight from the
59
- * click handler**, or the popup is blocked. Rejects with `EmbeddedSignupError`.
81
+ * The URL, the dialog, and the record the callback leaves behind, in one call.
82
+ * **Call it straight from the click handler**, or the popup is blocked.
83
+ * Rejects with `EmbeddedSignupError`; `reason` carries the service's own code.
84
+ *
85
+ * What comes back is a *candidate* -- nothing exists until `complete()`, and
86
+ * if it holds several numbers the person picks one.
60
87
  */
61
- connect(options?: ConnectOptions): Promise<OnboardingExchangeResult>;
88
+ connect(options: ConnectOptions): Promise<OnboardingPendingResult>;
62
89
  }
@@ -5,16 +5,21 @@ const paths_1 = require("../paths");
5
5
  const embedded_signup_1 = require("../../internal/embedded-signup");
6
6
  const ONBOARDING = `${paths_1.CHANNELS}/onboarding`;
7
7
  /**
8
- * Connecting a channel. `connect()` does steps 1 to 3 and hands back the number
9
- * to confirm; or call `url()`, open the dialog yourself, then `exchange()`.
10
- * Either way the browser never sees a Meta credential.
8
+ * Connecting a channel. The browser only runs Meta's dialog: Meta returns to
9
+ * the service's callback, which redeems the code and sends the browser back to
10
+ * `redirectUri` with a `status` and a `token` -- no credential reaches this
11
+ * page. `connect()` drives all of it; or call `url()`, open the dialog
12
+ * yourself, and read the return off your own landing page.
11
13
  *
12
14
  * ```ts
13
15
  * // In the click handler for "Connect WhatsApp" -- not after an await.
14
- * const candidate = await crm.channelOnboarding.connect();
15
- * // Show candidate.phoneNumber, prefill a form from candidate.defaults...
16
+ * const candidate = await crm.channelOnboarding.connect({
17
+ * redirectUri: 'https://console.example.com/channels/connected',
18
+ * });
19
+ * // One number, or several to choose between: candidate.numbers
16
20
  * const { data: channel } = await crm.channelOnboarding.complete({
17
21
  * token: candidate.id,
22
+ * phoneNumberId: chosen.id,
18
23
  * name: form.name,
19
24
  * });
20
25
  * ```
@@ -25,22 +30,35 @@ class ChannelOnboarding {
25
30
  this.http = http;
26
31
  }
27
32
  /**
28
- * `GET /v1/channels/onboarding/types` -- every family, available or not, so a
29
- * picker greys rows out. Check `available`, show `reason`. Scope: `channels:read`.
33
+ * `GET /v1/channels/onboarding/types` -- the families this caller can connect
34
+ * right now. Anything the plan, the provider record or their role rules out is
35
+ * left out, so an empty list means "nothing to offer". Scope: `channels:read`.
30
36
  */
31
37
  types() {
32
38
  return this.http.request('GET', `${ONBOARDING}/types`);
33
39
  }
34
40
  /**
35
- * `POST /v1/channels/onboarding/url` -- the dialog URL and the `state` to hand
36
- * back. **Meta refuses to be framed**: use a popup. 402 if the plan has no room.
41
+ * `POST /v1/channels/onboarding/url` -- the page to open. **Meta refuses to
42
+ * be framed**: use a popup, or navigate. 402 if the plan has no room.
43
+ *
44
+ * `url` is a launcher on the app's own domain that forwards to the dialog
45
+ * (`dialogUrl`); `redirectUri` is where *we* return the browser, not what
46
+ * Meta is given -- that is `callbackUri`.
37
47
  */
38
- url(input = {}) {
48
+ url(input) {
39
49
  return this.http.request('POST', `${ONBOARDING}/url`, { body: input });
40
50
  }
41
51
  /**
42
- * `POST /v1/channels/onboarding/exchange` -- redeems the code. **Creates
43
- * nothing**: show `phoneNumber`, Meta's own description, and let them confirm.
52
+ * `GET /v1/channels/onboarding/pending/:token` -- what the return URL's
53
+ * `token` refers to: the numbers it covers, and the form's prefill.
54
+ */
55
+ pending(token) {
56
+ return this.http.request('GET', `${ONBOARDING}/pending/${encodeURIComponent(token)}`);
57
+ }
58
+ /**
59
+ * `POST /v1/channels/onboarding/exchange` -- redeems a code you hold yourself,
60
+ * for a BSP app whose registered redirect is a page of yours rather than the
61
+ * service's callback. **Creates nothing**: show `phoneNumber` and confirm.
44
62
  */
45
63
  exchange(input) {
46
64
  return this.http.request('POST', `${ONBOARDING}/exchange`, {
@@ -55,20 +73,22 @@ class ChannelOnboarding {
55
73
  return this.http.request('POST', `${ONBOARDING}/complete`, { body: input });
56
74
  }
57
75
  /**
58
- * The URL, the dialog and the exchange in one call. **Call it straight from the
59
- * click handler**, or the popup is blocked. Rejects with `EmbeddedSignupError`.
76
+ * The URL, the dialog, and the record the callback leaves behind, in one call.
77
+ * **Call it straight from the click handler**, or the popup is blocked.
78
+ * Rejects with `EmbeddedSignupError`; `reason` carries the service's own code.
79
+ *
80
+ * What comes back is a *candidate* -- nothing exists until `complete()`, and
81
+ * if it holds several numbers the person picks one.
60
82
  */
61
- async connect(options = {}) {
62
- const { type, slug, redirectUri, ...popup } = options;
63
- const { data: session } = await this.url({ type, slug, redirectUri });
64
- const credentials = await (0, embedded_signup_1.openSignup)(session.url, popup);
65
- const { data } = await this.exchange({
66
- // The one we issued, since the dialog may or may not return it.
67
- state: session.state,
68
- code: credentials.code,
69
- wabaId: credentials.wabaId,
70
- phoneNumberId: credentials.phoneNumberId,
71
- });
83
+ async connect(options) {
84
+ const { type, slug, provider, redirectUri, ...popup } = options;
85
+ const { data: session } = await this.url({ type, slug, provider, redirectUri });
86
+ const returned = await (0, embedded_signup_1.awaitSignupReturn)(session.url, popup);
87
+ if (!returned.token) {
88
+ throw new embedded_signup_1.EmbeddedSignupError('signup_error', 'The signup came back without an onboarding token');
89
+ }
90
+ // The query string carries a summary; this is the whole record.
91
+ const { data } = await this.pending(returned.token);
72
92
  return data;
73
93
  }
74
94
  }