@cobaltio/cobalt-js 8.9.2-beta.2 → 9.0.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.
Files changed (4) hide show
  1. package/cobalt.d.ts +12 -4
  2. package/cobalt.js +37 -33
  3. package/cobalt.ts +42 -37
  4. package/package.json +1 -1
package/cobalt.d.ts CHANGED
@@ -346,26 +346,34 @@ declare class Cobalt {
346
346
  */
347
347
  private getOAuthUrl;
348
348
  /**
349
- * Handle OAuth for the specified native application.
349
+ * Handle OAuth for the specified application.
350
350
  * @private
351
351
  * @param {String} slug The application slug.
352
352
  * @param {Object.<string, string>} [params] The key value pairs of auth data.
353
353
  * @returns {Promise<Boolean>} Whether the user authenticated.
354
354
  */
355
355
  private oauth;
356
+ /**
357
+ * Save auth data for the specified keybased application.
358
+ * @param {String} slug The application slug.
359
+ * @param {Object.<string, string>} [payload] The key value pairs of auth data.
360
+ * @returns {Promise<Boolean>} Whether the auth data was saved successfully.
361
+ */
362
+ private keybased;
356
363
  /**
357
364
  * Connect the specified application, optionally with the auth data that user provides.
358
365
  * @param {String} slug The application slug.
366
+ * @param {AuthType} authType The auth type to use.
359
367
  * @param {Object.<string, string>} [payload] The key value pairs of auth data.
360
368
  * @returns {Promise<Boolean>} Whether the connection was successful.
361
369
  */
362
- connect(slug: string, payload?: Record<string, string>): Promise<boolean>;
370
+ connect(slug: string, authType: AuthType, payload?: Record<string, string>): Promise<boolean>;
363
371
  /**
364
372
  * Disconnect the specified application and remove any associated data from Cobalt.
365
373
  * @param {String} slug The application slug.
366
- * @returns {Promise<void>}
374
+ * @returns {Promise<unknown>}
367
375
  */
368
- disconnect(slug: string): Promise<void>;
376
+ disconnect(slug: string): Promise<unknown>;
369
377
  /**
370
378
  * Returns the specified config, or creates one if it doesn't exist.
371
379
  * @param {ConfigPayload} payload The payload object for config.
package/cobalt.js CHANGED
@@ -146,7 +146,7 @@ class Cobalt {
146
146
  });
147
147
  }
148
148
  /**
149
- * Handle OAuth for the specified native application.
149
+ * Handle OAuth for the specified application.
150
150
  * @private
151
151
  * @param {String} slug The application slug.
152
152
  * @param {Object.<string, string>} [params] The key value pairs of auth data.
@@ -193,50 +193,53 @@ class Cobalt {
193
193
  });
194
194
  });
195
195
  }
196
+ /**
197
+ * Save auth data for the specified keybased application.
198
+ * @param {String} slug The application slug.
199
+ * @param {Object.<string, string>} [payload] The key value pairs of auth data.
200
+ * @returns {Promise<Boolean>} Whether the auth data was saved successfully.
201
+ */
202
+ keybased(slug, payload) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ const res = yield fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
205
+ method: "POST",
206
+ headers: {
207
+ authorization: `Bearer ${this.token}`,
208
+ "content-type": "application/json",
209
+ },
210
+ body: JSON.stringify(Object.assign({}, payload)),
211
+ });
212
+ if (res.status >= 400 && res.status < 600) {
213
+ const error = yield res.json();
214
+ throw error;
215
+ }
216
+ const data = yield res.json();
217
+ return data.success;
218
+ });
219
+ }
196
220
  /**
197
221
  * Connect the specified application, optionally with the auth data that user provides.
198
222
  * @param {String} slug The application slug.
223
+ * @param {AuthType} authType The auth type to use.
199
224
  * @param {Object.<string, string>} [payload] The key value pairs of auth data.
200
225
  * @returns {Promise<Boolean>} Whether the connection was successful.
201
226
  */
202
- connect(slug, payload) {
227
+ connect(slug, authType, payload) {
203
228
  return __awaiter(this, void 0, void 0, function* () {
204
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
205
- try {
206
- const app = yield this.getApp(slug);
207
- // oauth
208
- if (app && app.auth_type === AuthType.OAuth2) {
209
- const connected = yield this.oauth(slug, payload);
210
- resolve(connected);
211
- // key based
212
- }
213
- else {
214
- const res = yield fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
215
- method: "POST",
216
- headers: {
217
- authorization: `Bearer ${this.token}`,
218
- "content-type": "application/json",
219
- },
220
- body: JSON.stringify(Object.assign({}, payload)),
221
- });
222
- if (res.status >= 400 && res.status < 600) {
223
- const error = yield res.json();
224
- reject(error);
225
- }
226
- const data = yield res.json();
227
- resolve(data.success);
228
- }
229
- }
230
- catch (error) {
231
- reject(error);
232
- }
233
- }));
229
+ switch (authType) {
230
+ case AuthType.OAuth2:
231
+ return this.oauth(slug, payload);
232
+ case AuthType.KeyBased:
233
+ return this.keybased(slug, payload);
234
+ default:
235
+ throw new Error(`Invalid auth type: ${authType}`);
236
+ }
234
237
  });
235
238
  }
236
239
  /**
237
240
  * Disconnect the specified application and remove any associated data from Cobalt.
238
241
  * @param {String} slug The application slug.
239
- * @returns {Promise<void>}
242
+ * @returns {Promise<unknown>}
240
243
  */
241
244
  disconnect(slug) {
242
245
  return __awaiter(this, void 0, void 0, function* () {
@@ -250,6 +253,7 @@ class Cobalt {
250
253
  const error = yield res.json();
251
254
  throw error;
252
255
  }
256
+ return yield res.json();
253
257
  });
254
258
  }
255
259
  /**
package/cobalt.ts CHANGED
@@ -468,7 +468,7 @@ class Cobalt {
468
468
  }
469
469
 
470
470
  /**
471
- * Handle OAuth for the specified native application.
471
+ * Handle OAuth for the specified application.
472
472
  * @private
473
473
  * @param {String} slug The application slug.
474
474
  * @param {Object.<string, string>} [params] The key value pairs of auth data.
@@ -513,54 +513,57 @@ class Cobalt {
513
513
  });
514
514
  }
515
515
 
516
+ /**
517
+ * Save auth data for the specified keybased application.
518
+ * @param {String} slug The application slug.
519
+ * @param {Object.<string, string>} [payload] The key value pairs of auth data.
520
+ * @returns {Promise<Boolean>} Whether the auth data was saved successfully.
521
+ */
522
+ private async keybased(slug: string, payload?: Record<string, string>): Promise<boolean> {
523
+ const res = await fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
524
+ method: "POST",
525
+ headers: {
526
+ authorization: `Bearer ${this.token}`,
527
+ "content-type": "application/json",
528
+ },
529
+ body: JSON.stringify({
530
+ ...payload,
531
+ }),
532
+ });
533
+
534
+ if (res.status >= 400 && res.status < 600) {
535
+ const error = await res.json();
536
+ throw error;
537
+ }
538
+
539
+ const data = await res.json();
540
+ return data.success;
541
+ }
542
+
516
543
  /**
517
544
  * Connect the specified application, optionally with the auth data that user provides.
518
545
  * @param {String} slug The application slug.
546
+ * @param {AuthType} authType The auth type to use.
519
547
  * @param {Object.<string, string>} [payload] The key value pairs of auth data.
520
548
  * @returns {Promise<Boolean>} Whether the connection was successful.
521
549
  */
522
- public async connect(slug: string, payload?: Record<string, string>): Promise<boolean> {
523
- return new Promise(async (resolve, reject) => {
524
- try {
525
- const app = await this.getApp(slug);
526
-
527
- // oauth
528
- if (app && app.auth_type === AuthType.OAuth2) {
529
- const connected = await this.oauth(slug, payload);
530
- resolve(connected);
531
- // key based
532
- } else {
533
- const res = await fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
534
- method: "POST",
535
- headers: {
536
- authorization: `Bearer ${this.token}`,
537
- "content-type": "application/json",
538
- },
539
- body: JSON.stringify({
540
- ...payload,
541
- }),
542
- });
543
-
544
- if (res.status >= 400 && res.status < 600) {
545
- const error = await res.json();
546
- reject(error);
547
- }
548
-
549
- const data = await res.json();
550
- resolve(data.success);
551
- }
552
- } catch (error) {
553
- reject(error);
554
- }
555
- });
550
+ public async connect(slug: string, authType: AuthType, payload?: Record<string, string>): Promise<boolean> {
551
+ switch (authType) {
552
+ case AuthType.OAuth2:
553
+ return this.oauth(slug, payload);
554
+ case AuthType.KeyBased:
555
+ return this.keybased(slug, payload);
556
+ default:
557
+ throw new Error(`Invalid auth type: ${authType}`);
558
+ }
556
559
  }
557
560
 
558
561
  /**
559
562
  * Disconnect the specified application and remove any associated data from Cobalt.
560
563
  * @param {String} slug The application slug.
561
- * @returns {Promise<void>}
564
+ * @returns {Promise<unknown>}
562
565
  */
563
- public async disconnect(slug: string): Promise<void> {
566
+ public async disconnect(slug: string): Promise<unknown> {
564
567
  const res = await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${slug}`, {
565
568
  method: "DELETE",
566
569
  headers: {
@@ -572,6 +575,8 @@ class Cobalt {
572
575
  const error = await res.json();
573
576
  throw error;
574
577
  }
578
+
579
+ return await res.json();
575
580
  }
576
581
 
577
582
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobaltio/cobalt-js",
3
- "version": "8.9.2-beta.2",
3
+ "version": "9.0.0-beta.1",
4
4
  "description": "Cobalt frontend SDK",
5
5
  "main": "./cobalt.js",
6
6
  "typings": "./cobalt.d.ts",