@crmcom/self-service-sdk 2.1.2 → 3.0.0-build.11

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 ADDED
@@ -0,0 +1,165 @@
1
+ # @crmcom/self-service-sdk
2
+
3
+ Official CRM.COM Self-Service JavaScript SDK for consumer-facing API integration.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @crmcom/self-service-sdk
9
+ ```
10
+
11
+ ### Peer Dependencies
12
+
13
+ ```bash
14
+ npm install jwt-decode js-sha256 xml2js
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ### 1. Initialize the SDK
20
+
21
+ You **must** call `init()` before using any other SDK method. This configures the HTTP layer with your API credentials and session management callbacks.
22
+
23
+ ```js
24
+ import { init } from '@crmcom/self-service-sdk';
25
+
26
+ await init({
27
+ apiKey: 'YOUR_API_KEY',
28
+ host: 'https://api.crm.com',
29
+ storeKVFn: (key, value) => localStorage.setItem(key, value),
30
+ getKVFn: (key) => localStorage.getItem(key),
31
+ sessionInvalidCallback: (shouldLogout) => {
32
+ if (shouldLogout) {
33
+ // redirect to login
34
+ }
35
+ },
36
+ });
37
+ ```
38
+
39
+ ### 2. Use SDK modules
40
+
41
+ Once initialized, import and use any module:
42
+
43
+ ```js
44
+ import { authentication, contacts, orders, wallet } from '@crmcom/self-service-sdk';
45
+
46
+ // Authenticate
47
+ const authResult = await authentication.authenticateEmail({
48
+ username: 'user@example.com',
49
+ password: 'password',
50
+ });
51
+
52
+ // Get contact profile
53
+ const profile = await contacts.getContact();
54
+
55
+ // Browse products
56
+ const products = await orders.getProducts({ page: 1, size: 20 });
57
+
58
+ // Check wallet balance
59
+ const myWallet = await wallet.getWallet();
60
+ ```
61
+
62
+ ### React Example
63
+
64
+ ```jsx
65
+ // App.jsx
66
+ import { useEffect, useState } from 'react';
67
+ import { init } from '@crmcom/self-service-sdk';
68
+
69
+ function App() {
70
+ const [ready, setReady] = useState(false);
71
+
72
+ useEffect(() => {
73
+ init({
74
+ apiKey: process.env.REACT_APP_CRM_API_KEY,
75
+ host: process.env.REACT_APP_CRM_HOST,
76
+ storeKVFn: (key, value) => localStorage.setItem(key, JSON.stringify(value)),
77
+ getKVFn: (key) => JSON.parse(localStorage.getItem(key)),
78
+ sessionInvalidCallback: () => {
79
+ window.location.href = '/login';
80
+ },
81
+ }).then(() => setReady(true));
82
+ }, []);
83
+
84
+ if (!ready) return <div>Loading...</div>;
85
+
86
+ return <YourApp />;
87
+ }
88
+ ```
89
+
90
+ ## Init Options
91
+
92
+ | Parameter | Type | Required | Description |
93
+ |-----------|------|----------|-------------|
94
+ | `apiKey` | `string` | Yes | Your CRM.COM API key |
95
+ | `host` | `string` | Yes | Base API URL (e.g. `https://api.crm.com`) |
96
+ | `storeKVFn` | `(key, value) => void \| Promise<void>` | Yes | Function to persist key-value pairs (tokens, session data) |
97
+ | `getKVFn` | `(key) => any \| Promise<any>` | Yes | Function to retrieve persisted values |
98
+ | `sessionInvalidCallback` | `(shouldLogout) => void` | Yes | Called when session is invalid and refresh fails |
99
+ | `fetchFn` | `typeof fetch` | No | Custom fetch implementation (defaults to global `fetch`) |
100
+ | `enableSslPinning` | `boolean` | No | Enable SSL pinning (mobile apps) |
101
+ | `sslPinningOptions` | `Record<string, any>` | No | SSL pinning configuration |
102
+ | `isBackend` | `boolean` | No | Set `true` for backend/server usage |
103
+ | `middlewareHost` | `string` | No | Middleware service host URL |
104
+ | `middlewareApiKey` | `string` | No | Middleware API key |
105
+ | `mwNodejsHost` | `string` | No | Node.js middleware host URL |
106
+ | `mwNodejsApiKey` | `string` | No | Node.js middleware API key |
107
+
108
+ ## Available Modules
109
+
110
+ | Module | Description |
111
+ |--------|-------------|
112
+ | `authentication` | Phone, email, Facebook, SSO/OIDC auth flows |
113
+ | `contacts` | Contact profile, addresses, preferences |
114
+ | `orders` | Catalogue, products, order estimation and placement |
115
+ | `wallet` | Balance, transactions, top-ups, transfers |
116
+ | `payment` | Payment methods, forms, top-ups |
117
+ | `subscriptions` | Services, devices, billing |
118
+ | `rewards` | Offers, promotions, schemes |
119
+ | `account` | Journals, balances, rewards |
120
+ | `communications` | Messages, notifications |
121
+ | `community` | Members, relations, groups |
122
+ | `config` | App config, languages, address lookup |
123
+ | `organisations` | Organisation search, locations |
124
+ | `servicerequest` | Support tickets |
125
+ | `connectx` | SIM management |
126
+ | `jcccards` | Card operations |
127
+ | `mobilepass` | Mobile wallet passes |
128
+ | `paymentgateway` | Payment gateway client tokens |
129
+ | `payouts` | Payout creation |
130
+
131
+ ## Error Handling
132
+
133
+ All SDK methods return an `SDKResult` object:
134
+
135
+ ```js
136
+ const result = await contacts.getContact();
137
+
138
+ if (result.code === 'OK') {
139
+ console.log(result.data);
140
+ } else {
141
+ console.error('Error:', result.code, result.error);
142
+ }
143
+ ```
144
+
145
+ If the SDK is used before calling `init()`, it throws:
146
+
147
+ ```
148
+ Error: CRM.COM SDK not initialized. Call init() before using any SDK methods.
149
+ ```
150
+
151
+ ## Migration from v2
152
+
153
+ If you were using `setupChannel`, it still works but is deprecated:
154
+
155
+ ```diff
156
+ - import { httpUtil } from '@crmcom/self-service-sdk';
157
+ - await httpUtil.setupChannel({ apiKey, host, ... });
158
+
159
+ + import { init } from '@crmcom/self-service-sdk';
160
+ + await init({ apiKey, host, ... });
161
+ ```
162
+
163
+ ## License
164
+
165
+ MIT
package/authentication.js CHANGED
@@ -88,7 +88,6 @@ async function authenticate(contact, startSession = true) {
88
88
  if (startSession == true && response.code == "OK" && response.data && response.data.access_token) {
89
89
  httpUtil.startSession(response.data);
90
90
  }
91
- //check return code here instead of put as there would be different intepretation for different API
92
91
  if (response.code == "OK") {
93
92
  if (response.data.auth_otp || response.data.access_token) {
94
93
  return createResult(ErrorCodes.OK, response.data);
@@ -96,20 +95,7 @@ async function authenticate(contact, startSession = true) {
96
95
  return createResult(ErrorCodes.INVALID_LOGIN, response.error);
97
96
  }
98
97
  } else {
99
- if (
100
- response.error &&
101
- response.error.message == "COM.CRM.EXCEPTIONS.INVALIDLOGINEXCEPTION"
102
- ) {
103
- return createResult(ErrorCodes.INVALID_LOGIN, response.error);
104
- } else if (response.error &&
105
- response.error.error == "COM.CRM.EXCEPTIONS.EMAILNOTVERIFIEDEXCEPTION") {
106
- return createResult(ErrorCodes.EMAIL_NOT_VERIFIED, response.error);
107
- } else if (response.code == '429' || response.code === 429) {
108
- return createResult(ErrorCodes.TOO_MANY_REQUESTS, response.error);
109
- } else if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION") {
110
- return createResult(ErrorCodes.CIMALREADYEXISTSFORANOTHERCONTACTEXCEPTION, response.error);
111
- }
112
- else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
98
+ return createCommonResult(response);
113
99
  }
114
100
  } catch (e) {
115
101
  logger.error("Exception register:", e);
@@ -165,16 +151,7 @@ async function validateOTP({ auth_otp,otp }, startSession = true) {
165
151
  if (startSession == true && response.code == "OK") {
166
152
  httpUtil.startSession(response.data);
167
153
  }
168
- if (response.code == "OK")
169
- return createResult(ErrorCodes.OK, response.data);
170
- else {
171
- if (
172
- response.error &&
173
- response.error.message == "COM.CRM.EXCEPTIONS.INVALIDLOGINEXCEPTION"
174
- ) {
175
- return createResult(ErrorCodes.INVALID_LOGIN, response.error);
176
- } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
177
- }
154
+ return createCommonResult(response);
178
155
  } catch (e) {
179
156
  logger.error("Exception register:", e);
180
157
  return createResult(ErrorCodes.UNKNOWN, e);
@@ -195,7 +172,8 @@ async function registerByPhone(
195
172
  custom_fields,
196
173
  accept_term_conditions=true,
197
174
  validation_required,
198
- pass_code
175
+ pass_code,
176
+ pass_pin
199
177
  },
200
178
  startSession = false
201
179
  ) {
@@ -216,7 +194,8 @@ async function registerByPhone(
216
194
  language_code,
217
195
  custom_fields,
218
196
  validation_required,
219
- pass_code
197
+ pass_code,
198
+ pass_pin
220
199
  };
221
200
  if (referral_code) {
222
201
  body.referral_code = referral_code;
@@ -239,7 +218,8 @@ async function registerByEmail(
239
218
  custom_fields,
240
219
  accept_term_conditions=true,
241
220
  validation_required,
242
- pass_code
221
+ pass_code,
222
+ pass_pin
243
223
  },
244
224
  startSession = false
245
225
  ) {
@@ -258,7 +238,8 @@ async function registerByEmail(
258
238
  language_code,
259
239
  custom_fields,
260
240
  validation_required,
261
- pass_code
241
+ pass_code,
242
+ pass_pin
262
243
  };
263
244
  if (referral_code) {
264
245
  body.referral_code = referral_code;
@@ -300,43 +281,18 @@ async function register(body,startSession = true) {
300
281
  httpUtil.startSession(response.data);
301
282
  }
302
283
  //check return code here instead of put as there would be different intepretation for different API
303
- if (response.code == "OK")
284
+ if (response.code == "OK") {
304
285
  return createResult(ErrorCodes.OK, response.data);
305
- else {
306
- if (
307
- response.error && (response.error.error == "COM.CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION" || response.error.error == 'CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION')
308
- ) {
309
- return createResult(
310
- ErrorCodes.REGISTRATION_FAIL_CONTACT_EXISTS,
311
- response.error
312
- );
313
- }else if(response.error && response.error.error == "CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION"){
314
- return createResult(
315
- ErrorCodes.INVALIDVALUEEXCEPTION,
316
- response.error
317
- );
318
- }
319
- else if(response.error && response.error.error == "CRM.EXCEPTIONS.NOTFOUNDEXCEPTION"){
320
- return createResult(
321
- ErrorCodes.REDEEM_PASS_INVALID,
322
- response.error
323
- );
324
- }
325
- else if(response.error && response.error.error == "COM.CRM.EXCEPTIONS.THISPASSHASALREADYBEENREDEEMEDEXCEPTION"){
326
- return createResult(
327
- ErrorCodes.REDEEM_PASS_USED,
328
- response.error
329
- );
330
- }
331
- else if(response.error && response.error.error === "COM.CRM.EXCEPTIONS.ONLYACTIVEPASSESCANBEREDEEMEDEXCEPTION"){
332
- return createResult(
333
- ErrorCodes.REDEEM_PASS_NOT_ACTIVE,
334
- response.error
335
- );
336
- }
337
- else
338
- return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
339
286
  }
287
+ // Registration-specific: MORETHANONEENTITYEXISTSEXCEPTION means contact already exists
288
+ if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION" || response.error.error == 'CRM.EXCEPTIONS.MORETHANONEENTITYEXISTSEXCEPTION')) {
289
+ return createResult(ErrorCodes.REGISTRATION_FAIL_CONTACT_EXISTS, response.error);
290
+ }
291
+ // Pass-specific: NOTFOUNDEXCEPTION means invalid pass in this context
292
+ if (response.error && response.error.error == "CRM.EXCEPTIONS.NOTFOUNDEXCEPTION") {
293
+ return createResult(ErrorCodes.REDEEM_PASS_INVALID, response.error);
294
+ }
295
+ return createCommonResult(response);
340
296
  } catch (e) {
341
297
  logger.error("Exception register:", e);
342
298
  return createResult(ErrorCodes.UNKNOWN, e);
@@ -421,28 +377,15 @@ async function addContactIdentity(body,contactId,accessToken) {
421
377
  accessToken: accessToken
422
378
  });
423
379
  logger.debug('Identity add response received')
424
- if (response.code == "OK")
380
+ if (response.code == "OK") {
425
381
  return createResult(ErrorCodes.OK, response.data);
426
- else {
427
- logger.debug("Identity add error:", response.error)
428
- if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")) {
429
- return createResult(
430
- ErrorCodes.REGISTRATION_FAIL_CONTACT_EXISTS,
431
- response.error
432
- );
433
- } else if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.INVALIDPASSWORDEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.INVALIDPASSWORDEXCEPTION")) {
434
- return createResult(
435
- ErrorCodes.INVALID_PASSWORD_EXCEPTION,
436
- response.error
437
- );
438
- } else if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.INVALIDCONTACTPASSWORDEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.INVALIDCONTACTPASSWORDEXCEPTION")) {
439
- return createResult(
440
- "INVALID_CONTACTPASSWORD_EXCEPTION",
441
- response.error
442
- );
443
- }
444
- else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
445
382
  }
383
+ logger.debug("Identity add error:", response.error)
384
+ // Identity-specific: ALREADYEXISTSEXCEPTION means contact already exists
385
+ if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")) {
386
+ return createResult(ErrorCodes.REGISTRATION_FAIL_CONTACT_EXISTS, response.error);
387
+ }
388
+ return createCommonResult(response);
446
389
  } catch (e) {
447
390
  logger.error("Exception register:", e);
448
391
  return createResult(ErrorCodes.UNKNOWN, e);
@@ -458,15 +401,14 @@ async function forgotPassword(email) {
458
401
  },
459
402
  logOutIfSessionInvalid: false
460
403
  });
461
- if (response.code == "OK")
404
+ if (response.code == "OK") {
462
405
  return createResult(ErrorCodes.OK, response.data);
463
- else {
464
- if (response.error && response.error.error == "COM.CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION") {
465
- return createResult(ErrorCodes.FORGOT_EMAIL_NOT_FOUND_EXCEPTION, response.error);
466
- } else if (response.code == '429') {
467
- return createResult(ErrorCodes.TOO_MANY_REQUESTS, response.error);
468
- } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
469
406
  }
407
+ // Forgot password specific: INVALIDVALUEEXCEPTION means email not found
408
+ if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.INVALIDVALUEEXCEPTION")) {
409
+ return createResult(ErrorCodes.FORGOT_EMAIL_NOT_FOUND_EXCEPTION, response.error);
410
+ }
411
+ return createCommonResult(response);
470
412
  } catch (e) {
471
413
  logger.error("Exception update phone:", e);
472
414
  return createResult(ErrorCodes.UNKNOWN, e);
package/config.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { httpUtil } from './httpUtil'
2
2
  import { ErrorCodes, createResult, createCommonResult } from './resultUtil'
3
- import { getData } from '../../utils/common';
4
3
  import { logger } from './logger';
5
4
  export const config = {
6
5
  getAppCfg,
@@ -23,9 +22,6 @@ async function getAppCfg({
23
22
  cname
24
23
  } = {}) {
25
24
  try {
26
- var appId = await getData("APP_ID");
27
- if (!app_id) app_id = appId;
28
-
29
25
  var queryParams = {}
30
26
  if (use_cname) {
31
27
  queryParams.cloud_name = cname;
package/connectx.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { httpUtil } from './httpUtil'
2
2
  import { ErrorCodes, createResult, createCommonResult } from './resultUtil'
3
- import { getData } from '../../utils/common';
4
- import { logger } from './logger';
3
+ import { logger } from './logger';
5
4
  export const connectx = {
6
5
  getBuckets,
7
6
  simConfirmation,
package/contacts.js CHANGED
@@ -123,19 +123,14 @@ async function addContactAddress({
123
123
  },
124
124
  withAccessToken: true,
125
125
  });
126
- if (response.code == "OK")
126
+ if (response.code == "OK") {
127
127
  return createResult(ErrorCodes.OK, response.data);
128
- else {
129
- if (
130
- response.error && (
131
- response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")
132
- ) {
133
- return createResult(
134
- ErrorCodes.ADD_ADDRESS_ALREADY_TYPE,
135
- response.error
136
- );
137
- } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
138
128
  }
129
+ // Address-specific: ALREADYEXISTSEXCEPTION means address type already exists
130
+ if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")) {
131
+ return createResult(ErrorCodes.ADD_ADDRESS_ALREADY_TYPE, response.error);
132
+ }
133
+ return createCommonResult(response);
139
134
  } catch (e) {
140
135
  logger.error("Exception addContactAddress:", e);
141
136
  return createResult(ErrorCodes.UNKNOWN, e);
@@ -176,19 +171,14 @@ async function updateContactAddress({
176
171
  },
177
172
  withAccessToken: true,
178
173
  });
179
- if (response.code == "OK")
174
+ if (response.code == "OK") {
180
175
  return createResult(ErrorCodes.OK, response.data);
181
- else {
182
- if (
183
- response.error && (
184
- response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")
185
- ) {
186
- return createResult(
187
- ErrorCodes.ADD_ADDRESS_ALREADY_TYPE,
188
- response.error
189
- );
190
- } else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
191
176
  }
177
+ // Address-specific: ALREADYEXISTSEXCEPTION means address type already exists
178
+ if (response.error && (response.error.error == "COM.CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION" || response.error.error == "CRM.EXCEPTIONS.ALREADYEXISTSEXCEPTION")) {
179
+ return createResult(ErrorCodes.ADD_ADDRESS_ALREADY_TYPE, response.error);
180
+ }
181
+ return createCommonResult(response);
192
182
  } catch (e) {
193
183
  logger.error("Exception updateContactAddress:", e);
194
184
  return createResult(ErrorCodes.UNKNOWN, e);
@@ -207,9 +197,7 @@ async function updateContactOnboardingCards({
207
197
  },
208
198
  withAccessToken: true,
209
199
  });
210
- if (response.code == "OK")
211
- return createResult(ErrorCodes.OK, response.data);
212
- else return createResult(ErrorCodes.UNCLASSIFIED_ERROR, response.error);
200
+ return createCommonResult(response);
213
201
  } catch (e) {
214
202
  logger.error("Exception updateContactOnboardingCards:", e);
215
203
  return createResult(ErrorCodes.UNKNOWN, e);
package/eventListener.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { logger } from './logger';
2
2
  let events = {};
3
3
 
4
- try {
5
- // Dynamic import for Vite / ES modules
6
- const customConfigModule = import("../../../custom.config");
7
- events = customConfigModule.events || {};
8
- } catch (error) {
9
- if (error.code === 'MODULE_NOT_FOUND') {
10
- events = {};
11
- } else {
12
- throw error;
13
- }
14
- }
4
+ // try {
5
+ // // Dynamic import for Vite / ES modules
6
+ // const customConfigModule = import("../../../custom.config");
7
+ // events = customConfigModule.events || {};
8
+ // } catch (error) {
9
+ // if (error.code === 'MODULE_NOT_FOUND') {
10
+ // events = {};
11
+ // } else {
12
+ // throw error;
13
+ // }
14
+ // }
15
15
 
16
16
  export const eventListener = {
17
17
  handleEvent,
@@ -28,14 +28,14 @@ export async function handleEvent({
28
28
  object_type // object type, e.g., string or object
29
29
  }) {
30
30
  logger.debug("handleEvent type:", type);
31
- logger.debug("handleEvent code:", code);
32
- logger.debug("handleEvent object_type:", object_type);
33
- logger.debug("handleEvent data:", data);
31
+ // logger.debug("handleEvent code:", code);
32
+ // logger.debug("handleEvent object_type:", object_type);
33
+ // logger.debug("handleEvent data:", data);
34
34
 
35
- if (type === eventTypes.kochava) {
36
- const kochava = events?.kochava ?? null;
37
- if (kochava?.sendEvent) {
38
- await kochava.sendEvent(object_type, code, data);
39
- }
40
- }
35
+ // if (type === eventTypes.kochava) {
36
+ // const kochava = events?.kochava ?? null;
37
+ // if (kochava?.sendEvent) {
38
+ // await kochava.sendEvent(object_type, code, data);
39
+ // }
40
+ // }
41
41
  }
@@ -3,12 +3,16 @@
3
3
  */
4
4
 
5
5
  //import querystring from 'querystring';
6
- import { initOptionHeader } from '../../utils/common';
7
6
  import {jwtDecode} from 'jwt-decode';
8
7
  import { logger } from './logger';
9
8
 
9
+ let _initialized = false;
10
+
10
11
  export const httpBackOfficeUtil = {
11
- setupChannel,
12
+ init,
13
+ /** @deprecated Use init() instead */
14
+ setupChannel: init,
15
+ isInitialized: () => _initialized,
12
16
  put,
13
17
  post,
14
18
  get,
@@ -21,9 +25,24 @@ export const httpBackOfficeUtil = {
21
25
  switchSession
22
26
  };
23
27
 
28
+ const initOptionHeader = () => {
29
+ return {
30
+ 'User-Agent': 'request',
31
+ 'Content-Type': 'application/json; charset=utf-8'
32
+ }
33
+ }
24
34
 
25
35
  /** local variables */
26
36
 
37
+ function _assertInitialized() {
38
+ if (!_initialized) {
39
+ throw new Error(
40
+ 'CRM.COM Back-Office SDK not initialized. Call initBackOffice() before using any back-office SDK methods. ' +
41
+ 'Example: import { initBackOffice } from "@crmcom/self-service-sdk"; await initBackOffice({ apiKey, host, ... });'
42
+ );
43
+ }
44
+ }
45
+
27
46
  let _storeKVFn;
28
47
  let _getKVFn;
29
48
  let _sessionInvalidCallback;
@@ -43,7 +62,7 @@ let _backofficePath = 'backoffice/v1/';
43
62
  * when no connection refresh token will not be available -> should not kick user out.
44
63
  */
45
64
 
46
- async function setupChannel({
65
+ async function init({
47
66
  storeKVFn, //function to store key value
48
67
  getKVFn, //function to get value by key from the storage
49
68
  sessionInvalidCallback, //function to call when api_key or token key is invalid and refresh token if available was failed
@@ -75,7 +94,7 @@ async function setupChannel({
75
94
  _sessionData = undefined;
76
95
  }
77
96
  }
78
- //TODO add silent refresh token here
97
+ _initialized = true;
79
98
  }
80
99
 
81
100
  async function refreshToken() {
@@ -179,6 +198,7 @@ async function post({
179
198
  logOutIfSessionInvalid=true,
180
199
  refreshToken
181
200
  }) {
201
+ _assertInitialized();
182
202
  // let serverConfig = getServerConfig();
183
203
  // let uri = getURI(isBackend, resourcePath);
184
204
  let uri = _host + 'backoffice/v1/' + resourcePath;
@@ -242,6 +262,7 @@ async function get({
242
262
  logOutIfSessionInvalid = true,
243
263
  returnText=false
244
264
  }) {
265
+ _assertInitialized();
245
266
  let uri = getURI(isBackend, resourcePath);
246
267
  // let uri = _host + 'backoffice/v1/' + resourcePath;
247
268
  var options = {};
@@ -291,6 +312,7 @@ async function sendDelete({
291
312
  isBackend = false,
292
313
  logOutIfSessionInvalid = true
293
314
  }) {
315
+ _assertInitialized();
294
316
  // let uri = getURI(isBackend, resourcePath);
295
317
  let uri = _host + 'backoffice/v1/' + resourcePath;
296
318
  var options = {};
@@ -339,6 +361,7 @@ async function put({
339
361
  isBackend = false,
340
362
  logOutIfSessionInvalid = true
341
363
  }) {
364
+ _assertInitialized();
342
365
  // let uri = getURI(isBackend, resourcePath);
343
366
  let uri = _host + 'backoffice/v1/' + resourcePath;
344
367
  var options = {};