@unboundcx/sdk 4.13.152 → 4.13.154

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
@@ -125,6 +125,25 @@ const api = new SDK({
125
125
  });
126
126
  ```
127
127
 
128
+ `tokenDelivery` controls how `token` is attached to outgoing requests:
129
+ `'bearer'` (default, unchanged behavior) sends `Authorization: Bearer
130
+ <token>`; `'cookie'` sends `Cookie: authToken=<token>` instead, merging with
131
+ any `Cookie` header you already pass in `headers`. Use `'cookie'` when
132
+ `token` is a cookie-type token (e.g. forwarded from a browser session) --
133
+ app1-api's auth_v3 middleware rejects those over `Authorization` with
134
+ "Invalid token, authentication method mismatch". `setToken()` works the same
135
+ in either mode, the option is passed through to transport plugins, and
136
+ `autoRefresh`/`refreshToken` are unaffected (cookie clients never need a
137
+ refresh token):
138
+
139
+ ```javascript
140
+ const api = new SDK({
141
+ namespace: 'your-namespace',
142
+ token: cookieAuthToken,
143
+ tokenDelivery: 'cookie', // sends `Cookie: authToken=<token>`
144
+ });
145
+ ```
146
+
128
147
  ### Legacy Positional Constructor (Backwards Compatible)
129
148
 
130
149
  ```javascript
package/base.js CHANGED
@@ -41,6 +41,29 @@ export function internalRequest(sdk, endpoint, method, params = {}, forceFetch)
41
41
  }
42
42
 
43
43
  export class BaseSDK {
44
+ /**
45
+ * @param {object|string} options Object-based options (recommended), or a
46
+ * legacy positional `namespace` string (see constructor body).
47
+ * @param {string} [options.namespace]
48
+ * @param {string} [options.callId]
49
+ * @param {string} [options.token] Auth token; sent per `tokenDelivery`.
50
+ * @param {string} [options.fwRequestId]
51
+ * @param {string} [options.baseURL]
52
+ * @param {boolean} [options.autoRefresh=false] Opt-in 401 refresh+retry.
53
+ * @param {(err: Error) => void} [options.onUnauthorized]
54
+ * @param {string} [options.refreshToken] Bearer-delivery clients only.
55
+ * @param {'bearer'|'cookie'} [options.tokenDelivery='bearer'] How `token`
56
+ * is attached to outgoing requests. 'bearer' (default, unchanged
57
+ * behavior) sends `Authorization: Bearer <token>`. 'cookie' sends
58
+ * `Cookie: authToken=<token>` instead (merged with any caller-supplied
59
+ * Cookie header) -- required for cookie-type tokens, which app1-api's
60
+ * auth_v3 middleware rejects with "Invalid token, authentication
61
+ * method mismatch" when they arrive as a Bearer header. Applies to the
62
+ * built-in HTTP transport and is passed through to transport plugins'
63
+ * options object as `tokenDelivery`. `setToken` keeps working in both
64
+ * modes; `autoRefresh` is unaffected (cookie clients never need
65
+ * `refreshToken`).
66
+ */
44
67
  constructor(options = {}) {
45
68
  // Support both object and legacy positional parameters for backwards compatibility
46
69
  if (typeof options === 'string') {
@@ -49,6 +72,8 @@ export class BaseSDK {
49
72
  this.callId = arguments[1];
50
73
  this.token = arguments[2];
51
74
  this.fwRequestId = arguments[3];
75
+ // Legacy positional constructor has no slot for this; always bearer.
76
+ this._tokenDelivery = 'bearer';
52
77
  } else {
53
78
  // New object-based parameters
54
79
  const {
@@ -60,6 +85,7 @@ export class BaseSDK {
60
85
  autoRefresh,
61
86
  onUnauthorized,
62
87
  refreshToken,
88
+ tokenDelivery,
63
89
  } = options;
64
90
  this.namespace = namespace || process?.env?.namespace;
65
91
  this.callId = callId;
@@ -78,6 +104,15 @@ export class BaseSDK {
78
104
  // Seeded via this option or setRefreshToken(); updated in place on
79
105
  // every rotation by lib/refreshInterceptor.js.
80
106
  this._refreshToken = refreshToken;
107
+ // Additive, opt-in (default 'bearer' -- unchanged behavior: the token
108
+ // goes out as `Authorization: Bearer <token>`). 'cookie' sends it as
109
+ // `Cookie: authToken=<token>` instead -- app1-api auth_v3 rejects a
110
+ // cookie-type token carried as a Bearer header with "Invalid token,
111
+ // authentication method mismatch" (cookie-type tokens must arrive as
112
+ // a cookie). Applies to the built-in HTTP transport and is also
113
+ // passed through to transport plugins' options object. autoRefresh
114
+ // is unaffected: cookie clients never carry a refreshToken.
115
+ this._tokenDelivery = tokenDelivery === 'cookie' ? 'cookie' : 'bearer';
81
116
  }
82
117
  this.baseURL;
83
118
  this.transports = new Map();
@@ -262,7 +297,20 @@ export class BaseSDK {
262
297
 
263
298
  // Add auth headers
264
299
  if (this.token) {
265
- headers.Authorization = `Bearer ${this.token}`;
300
+ if (this._tokenDelivery === 'cookie') {
301
+ // Cookie-type tokens must arrive as a cookie, never Authorization
302
+ // (see constructor doc above). Merge with any caller-supplied
303
+ // Cookie header rather than clobbering it.
304
+ const existingCookie = headers.Cookie ?? headers.cookie;
305
+ if (headers.cookie !== undefined && headers.Cookie === undefined) {
306
+ delete headers.cookie;
307
+ }
308
+ headers.Cookie = existingCookie
309
+ ? `${existingCookie}; authToken=${this.token}`
310
+ : `authToken=${this.token}`;
311
+ } else {
312
+ headers.Authorization = `Bearer ${this.token}`;
313
+ }
266
314
  }
267
315
  if (this.fwRequestId) {
268
316
  headers['x-request-id-fw'] = this.fwRequestId;
@@ -284,6 +332,7 @@ export class BaseSDK {
284
332
  callId: this.callId,
285
333
  fwRequestId: this.fwRequestId,
286
334
  baseURL: this.baseURL || this.fullUrl,
335
+ tokenDelivery: this._tokenDelivery,
287
336
  });
288
337
  } catch (err) {
289
338
  // IMPORTANT: This catch block should ONLY handle transport-level failures
package/index.js CHANGED
@@ -100,9 +100,10 @@ class UnboundSDK extends BaseSDK {
100
100
  url,
101
101
  socketStore,
102
102
  baseURL,
103
+ tokenDelivery,
103
104
  } = options;
104
105
 
105
- super({ namespace, callId, token, fwRequestId, baseURL });
106
+ super({ namespace, callId, token, fwRequestId, baseURL, tokenDelivery });
106
107
 
107
108
  // Handle client-side specific parameters
108
109
  if (url) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unboundcx/sdk",
3
- "version": "4.13.152",
3
+ "version": "4.13.154",
4
4
  "description": "Official JavaScript SDK for the Unbound API - A comprehensive toolkit for integrating with Unbound's communication, AI, and data management services",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -46,6 +46,75 @@ export class DiscountApprovalsService {
46
46
  body,
47
47
  });
48
48
  }
49
+
50
+ /**
51
+ * Round-2 fix 1 (margin-in-discount-popover) -- numbers-free D9 dry-run
52
+ * for one or more lines on an opportunity. Never locks or writes
53
+ * anything server-side. `marginPercent` is included on each returned
54
+ * line ONLY when the calling session holds `crm:margin:view` -- this
55
+ * method never asks for it and can't force it; everyone else gets
56
+ * `belowMarginFloor` only.
57
+ * @param {string} opportunityId
58
+ * @param {Array<{lineId: string, price?: number, discountType?: 'percent'|'amount', discountValue?: number}>} lines
59
+ * @returns {Promise<{lines: Array<{lineId: string, belowMarginFloor: boolean, marginPercent?: number}>}>}
60
+ */
61
+ async marginPreview(opportunityId, lines) {
62
+ this.sdk.validateParams(
63
+ { opportunityId, lines },
64
+ {
65
+ opportunityId: { type: 'string', required: true },
66
+ lines: { type: 'array', required: true },
67
+ },
68
+ );
69
+ return internalRequest(
70
+ this.sdk,
71
+ '/discount-approvals/margin-preview',
72
+ 'POST',
73
+ { body: { opportunityId, lines } },
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Round-2 fix 3 (bulk discount) -- one `discountType`/`discountValue`
79
+ * applied to every line in `lineIds`, through ONE `evaluateLineWrite`
80
+ * call -> at most one `discountApprovals` row for the whole selection,
81
+ * never one request per line. `discountValue` with
82
+ * `discountType='amount'` is a PER-UNIT amount off each selected
83
+ * line's own listPrice (the same semantics a single-line
84
+ * `discountType='amount'` edit already has) -- never prorated across
85
+ * the selection. Lines that are closed/locked, already have a pending
86
+ * discount request, or aren't found are skipped server-side and
87
+ * reported back in `skipped`, never merged or silently dropped.
88
+ * @param {string} opportunityId
89
+ * @param {string[]} lineIds
90
+ * @param {Object} options
91
+ * @param {'percent'|'amount'} options.discountType
92
+ * @param {number} options.discountValue
93
+ * @param {string} [options.reason]
94
+ * @returns {Promise<{opportunityId: string, committed: string[], held: string[], skipped: Array<{lineId: string, reason: string}>, skippedCount: number, rollup: Object}>}
95
+ */
96
+ async bulkDiscount(opportunityId, lineIds, { discountType, discountValue, reason } = {}) {
97
+ this.sdk.validateParams(
98
+ { opportunityId, lineIds, discountType, discountValue },
99
+ {
100
+ opportunityId: { type: 'string', required: true },
101
+ lineIds: { type: 'array', required: true },
102
+ discountType: { type: 'string', required: true },
103
+ discountValue: { type: 'number', required: true },
104
+ },
105
+ );
106
+ if (!['percent', 'amount'].includes(discountType)) {
107
+ throw new Error("discountType must be one of 'percent', 'amount'");
108
+ }
109
+ const body = { opportunityId, lineIds, discountType, discountValue };
110
+ if (reason !== undefined) body.reason = reason;
111
+ return internalRequest(
112
+ this.sdk,
113
+ '/discount-approvals/bulk-discount',
114
+ 'POST',
115
+ { body },
116
+ );
117
+ }
49
118
  }
50
119
 
51
120
  export default DiscountApprovalsService;