@cobaltio/cobalt-js 7.0.0 → 8.0.0

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/cobalt.js CHANGED
@@ -1,56 +1,18 @@
1
+ "use strict";
1
2
  /**
2
3
  * Cobalt Frontend SDK
3
4
  */
4
-
5
- /**
6
- * @typedef {Object} Application An application in Cobalt.
7
- * @property {String} name The application name.
8
- * @property {String} description The application description.
9
- * @property {String} icon The application icon.
10
- * @property {String} type The application slug for native apps.
11
- * @property {String} [slug] The application slug for custom apps.
12
- * @property {"oauth2"|"keybased"} auth_type The type of auth used by application.
13
- * @property {Boolean} [connected] Whether the user has connected the application.
14
- * @property {InputField[]} [auth_input_map] The fields required from the user to connect the application (for `keybased` auth type).
15
- */
16
-
17
- /**
18
- * @typedef {Object} InputField An Input field to take input from the user.
19
- * @property {String} name Key name of the field.
20
- * @property {String} type Input type of the field.
21
- * @property {String} required Whether the field is required.
22
- * @property {String} placeholder The placeholder of the field.
23
- * @property {String} label The label of the field.
24
- */
25
-
26
- /**
27
- * @typedef {Object} ConfigPayload The payload object for config.
28
- * @property {String} slug The application slug.
29
- * @property {String} [config_id] Unique ID for the config.
30
- * @property {Object.<string, Label[]>} labels The dynamic label mappings.
31
- */
32
-
33
- /**
34
- * @typedef {Object} Label Label Mapping
35
- * @property {string} name The label name.
36
- * @property {string | number | boolean} value The label value.
37
- */
38
-
39
- /**
40
- * @typedef {Object} UpdateConfigPayload The configuration data for an application.
41
- * @property {String} slug The application slug.
42
- * @property {String} [config_id] Unique ID for the config.
43
- * @property {Object.<string, string | number | boolean>} fields A map of application fields and their values.
44
- * @property {WorkflowPayload[]} workflows Whether the workflow is enabled.
45
- */
46
-
47
- /**
48
- * @typedef {Object} WorkflowPayload The workflow.
49
- * @property {String} id The ID of the workflow.
50
- * @property {Boolean} enabled Whether the workflow is enabled.
51
- * @property {Object.<string, string | number | boolean>} fields A map of workflow fields and their values.
52
- */
53
-
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Cobalt = void 0;
54
16
  class Cobalt {
55
17
  /**
56
18
  * Cobalt Frontend SDK
@@ -60,37 +22,27 @@ class Cobalt {
60
22
  */
61
23
  constructor(options = {}) {
62
24
  this.baseUrl = options.baseUrl || "https://api.gocobalt.io";
63
- this.token = options.token;
25
+ this.token = options.token || "";
64
26
  }
65
-
66
- get token() {
67
- return this.sessionToken;
68
- };
69
-
70
- set token(token) {
71
- return this.sessionToken = typeof token === "string" ? token : "";
72
- };
73
-
74
27
  /**
75
28
  * Returns the org & customer details for the associated token.
76
29
  * @private
77
30
  * @returns {Promise<unknown>}
78
31
  */
79
- async getAccountDetails() {
80
- const res = await fetch(`${this.baseUrl}/api/v3/org/basics`, {
81
- headers: {
82
- authorization: `Bearer ${this.token}`,
83
- },
32
+ getAccountDetails() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const res = yield fetch(`${this.baseUrl}/api/v3/org/basics`, {
35
+ headers: {
36
+ authorization: `Bearer ${this.token}`,
37
+ },
38
+ });
39
+ if (res.status >= 400 && res.status < 600) {
40
+ throw new Error(res.statusText);
41
+ }
42
+ const data = yield res.json();
43
+ return data;
84
44
  });
85
-
86
- if (res.status >= 400 && res.status < 600) {
87
- throw new Error(res.statusText);
88
- }
89
-
90
- const data = await res.json();
91
- return data;
92
45
  }
93
-
94
46
  /**
95
47
  * Returns the application details for the specified application, provided
96
48
  * the application is enabled in Cobalt. If no application is specified,
@@ -98,232 +50,225 @@ class Cobalt {
98
50
  * @param {String} [slug] The application slug.
99
51
  * @returns {Promise<Application>} The application details.
100
52
  */
101
- async getApp(slug) {
102
- const res = await fetch(`${this.baseUrl}/api/v2/f-sdk/application${slug ? `/${slug}` : ""}`, {
103
- headers: {
104
- authorization: `Bearer ${this.token}`,
105
- },
53
+ getApp(slug) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const res = yield fetch(`${this.baseUrl}/api/v2/f-sdk/application${slug ? `/${slug}` : ""}`, {
56
+ headers: {
57
+ authorization: `Bearer ${this.token}`,
58
+ },
59
+ });
60
+ if (res.status >= 400 && res.status < 600) {
61
+ throw new Error(res.statusText);
62
+ }
63
+ const data = yield res.json();
64
+ return data;
106
65
  });
107
-
108
- if (res.status >= 400 && res.status < 600) {
109
- throw new Error(res.statusText);
110
- }
111
-
112
- const data = await res.json();
113
- return data;
114
66
  }
115
-
116
67
  /**
117
68
  * Returns the auth URL that users can use to authenticate themselves to the
118
69
  * specified application.
119
70
  * @private
120
71
  * @param {String} slug The application slug.
121
- * @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
72
+ * @param {Object.<string, string>} [params] The key value pairs of auth data.
122
73
  * @returns {Promise<String>} The auth URL where users can authenticate themselves.
123
74
  */
124
- async getOAuthUrl(slug, params) {
125
- const res = await fetch(`${this.baseUrl}/api/v1/${slug}/integrate?${new URLSearchParams(params).toString()}`, {
126
- headers: {
127
- authorization: `Bearer ${this.token}`,
128
- },
75
+ getOAuthUrl(slug, params) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const res = yield fetch(`${this.baseUrl}/api/v1/${slug}/integrate?${new URLSearchParams(params).toString()}`, {
78
+ headers: {
79
+ authorization: `Bearer ${this.token}`,
80
+ },
81
+ });
82
+ if (res.status >= 400 && res.status < 600) {
83
+ throw new Error(res.statusText);
84
+ }
85
+ const data = yield res.json();
86
+ return data.auth_url;
129
87
  });
130
-
131
- if (res.status >= 400 && res.status < 600) {
132
- throw new Error(res.statusText);
133
- }
134
-
135
- const data = await res.json();
136
- return data.auth_url;
137
88
  }
138
-
139
89
  /**
140
90
  * Handle OAuth for the specified native application.
141
91
  * @private
142
92
  * @param {String} slug The application slug.
143
- * @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
93
+ * @param {Object.<string, string>} [params] The key value pairs of auth data.
144
94
  * @returns {Promise<Boolean>} Whether the user authenticated.
145
95
  */
146
- async oauth(slug, params) {
147
- return new Promise((resolve, reject) => {
148
- this.getOAuthUrl(slug, params)
149
- .then(oauthUrl => {
150
- const connectWindow = window.open(oauthUrl);
151
-
152
- // keep checking connection status
153
- const interval = setInterval(() => {
154
- this.getApp(slug)
155
- .then(app => {
156
- if (app && app.connected === true) {
157
- // close auth window
158
- connectWindow && connectWindow.close();
159
- // clear interval
160
- clearInterval(interval);
161
- // resovle status
162
- resolve(true);
163
- } else {
164
- // user closed oauth window without authenticating
165
- if (connectWindow && connectWindow.closed) {
96
+ oauth(slug, params) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ return new Promise((resolve, reject) => {
99
+ this.getOAuthUrl(slug, params)
100
+ .then(oauthUrl => {
101
+ const connectWindow = window.open(oauthUrl);
102
+ // keep checking connection status
103
+ const interval = setInterval(() => {
104
+ this.getApp(slug)
105
+ .then(app => {
106
+ if (app && app.connected === true && !app.reauth_required) {
107
+ // close auth window
108
+ connectWindow && connectWindow.close();
166
109
  // clear interval
167
110
  clearInterval(interval);
168
- // resolve status
169
- resolve(false);
111
+ // resovle status
112
+ resolve(true);
170
113
  }
171
- }
172
- })
173
- .catch(e => {
174
- console.error(e);
175
- clearInterval(interval);
176
- reject(e);
177
- });
178
- }, 3e3);
179
- })
180
- .catch(reject);
114
+ else {
115
+ // user closed oauth window without authenticating
116
+ if (connectWindow && connectWindow.closed) {
117
+ // clear interval
118
+ clearInterval(interval);
119
+ // resolve status
120
+ resolve(false);
121
+ }
122
+ }
123
+ })
124
+ .catch(e => {
125
+ console.error(e);
126
+ clearInterval(interval);
127
+ reject(e);
128
+ });
129
+ }, 3e3);
130
+ })
131
+ .catch(reject);
132
+ });
181
133
  });
182
134
  }
183
-
184
135
  /**
185
136
  * Connect the specified application, optionally with the auth data that user provides.
186
137
  * @param {String} slug The application slug.
187
- * @param {Object.<string, string | number | boolean>} [payload={}] The key value pairs of auth data.
138
+ * @param {Object.<string, string>} [payload] The key value pairs of auth data.
188
139
  * @returns {Promise<Boolean>} Whether the connection was successful.
189
140
  */
190
- async connect(slug, payload) {
191
- return new Promise(async (resolve, reject) => {
192
- try {
193
- const app = await this.getApp(slug);
194
-
195
- // oauth
196
- if (app?.auth_type ==="oauth2") {
197
- const connected = await this.oauth(slug, payload);
198
- resolve(connected);
199
- // key based
200
- } else {
201
- const res = await fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
202
- method: "POST",
203
- headers: {
204
- authorization: `Bearer ${this.token}`,
205
- "content-type": "application/json",
206
- },
207
- body: JSON.stringify({
208
- ...payload,
209
- }),
210
- });
211
-
212
- if (res.status >= 400 && res.status < 600) {
213
- reject(new Error(res.statusText));
141
+ connect(slug, payload) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
144
+ try {
145
+ const app = yield this.getApp(slug);
146
+ // oauth
147
+ if (app && app.auth_type === "oauth2") {
148
+ const connected = yield this.oauth(slug, payload);
149
+ resolve(connected);
150
+ // key based
151
+ }
152
+ else {
153
+ const res = yield fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
154
+ method: "POST",
155
+ headers: {
156
+ authorization: `Bearer ${this.token}`,
157
+ "content-type": "application/json",
158
+ },
159
+ body: JSON.stringify(Object.assign({}, payload)),
160
+ });
161
+ if (res.status >= 400 && res.status < 600) {
162
+ reject(new Error(res.statusText));
163
+ }
164
+ const data = yield res.json();
165
+ resolve(data.success);
214
166
  }
215
-
216
- const data = await res.json();
217
- resolve(data.success);
218
167
  }
219
- } catch (error) {
220
- reject(error);
221
- }
168
+ catch (error) {
169
+ reject(error);
170
+ }
171
+ }));
222
172
  });
223
173
  }
224
-
225
174
  /**
226
175
  * Disconnect the specified application and remove any associated data from Cobalt.
227
176
  * @param {String} slug The application slug.
228
177
  * @returns {Promise<void>}
229
178
  */
230
- async disconnect(slug) {
231
- const res = await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${slug}`, {
232
- method: "DELETE",
233
- headers: {
234
- authorization: `Bearer ${this.token}`,
235
- },
179
+ disconnect(slug) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ const res = yield fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${slug}`, {
182
+ method: "DELETE",
183
+ headers: {
184
+ authorization: `Bearer ${this.token}`,
185
+ },
186
+ });
187
+ if (res.status >= 400 && res.status < 600) {
188
+ throw new Error(res.statusText);
189
+ }
236
190
  });
237
-
238
- if (res.status >= 400 && res.status < 600) {
239
- throw new Error(res.statusText);
240
- }
241
191
  }
242
-
243
192
  /**
244
193
  * Returns the specified config, or creates one if it doesn't exist.
245
194
  * @param {ConfigPayload} payload The payload object for config.
246
195
  * @returns {Promise<Config>} The specified config.
247
196
  */
248
- async config(payload) {
249
- const res = await fetch(`${this.baseUrl}/api/v2/f-sdk/config`, {
250
- method: "POST",
251
- headers: {
252
- authorization: `Bearer ${this.token}`,
253
- "content-type": "application/json",
254
- },
255
- body: JSON.stringify(payload),
197
+ config(payload) {
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ const res = yield fetch(`${this.baseUrl}/api/v2/f-sdk/config`, {
200
+ method: "POST",
201
+ headers: {
202
+ authorization: `Bearer ${this.token}`,
203
+ "content-type": "application/json",
204
+ },
205
+ body: JSON.stringify(payload),
206
+ });
207
+ if (res.status >= 400 && res.status < 600) {
208
+ throw new Error(res.statusText);
209
+ }
210
+ return yield res.json();
256
211
  });
257
-
258
- if (res.status >= 400 && res.status < 600) {
259
- throw new Error(res.statusText);
260
- }
261
-
262
- return await res.json();
263
212
  }
264
-
265
213
  /**
266
214
  * Returns the specified config.
267
215
  * @param {String} slug The application slug.
268
216
  * @param {String} [configId] The unique ID of the config.
269
217
  * @returns {Promise<Config>} The specified config.
270
218
  */
271
- async getConfig(slug, configId) {
272
- const res = await fetch(`${this.baseUrl}/api/v2/f-sdk/slug/${slug}/config${configId ? `/${configId}` : ""}`, {
273
- headers: {
274
- authorization: `Bearer ${this.token}`,
275
- },
219
+ getConfig(slug, configId) {
220
+ return __awaiter(this, void 0, void 0, function* () {
221
+ const res = yield fetch(`${this.baseUrl}/api/v2/f-sdk/slug/${slug}/config${configId ? `/${configId}` : ""}`, {
222
+ headers: {
223
+ authorization: `Bearer ${this.token}`,
224
+ },
225
+ });
226
+ if (res.status >= 400 && res.status < 600) {
227
+ throw new Error(res.statusText);
228
+ }
229
+ return yield res.json();
276
230
  });
277
-
278
- if (res.status >= 400 && res.status < 600) {
279
- throw new Error(res.statusText);
280
- }
281
-
282
- return await res.json();
283
231
  }
284
-
285
232
  /**
286
233
  * Update the specified config.
287
234
  * @param {UpdateConfigPayload} payload The update payload.
288
235
  * @returns {Promise<Config>} The specified config.
289
236
  */
290
- async updateConfig(payload) {
291
- const res = await fetch(`${this.baseUrl}/api/v2/f-sdk/config`, {
292
- method: "PUT",
293
- headers: {
294
- authorization: `Bearer ${this.token}`,
295
- "content-type": "application/json",
296
- },
297
- body: JSON.stringify(payload),
237
+ updateConfig(payload) {
238
+ return __awaiter(this, void 0, void 0, function* () {
239
+ const res = yield fetch(`${this.baseUrl}/api/v2/f-sdk/config`, {
240
+ method: "PUT",
241
+ headers: {
242
+ authorization: `Bearer ${this.token}`,
243
+ "content-type": "application/json",
244
+ },
245
+ body: JSON.stringify(payload),
246
+ });
247
+ if (res.status >= 400 && res.status < 600) {
248
+ throw new Error(res.statusText);
249
+ }
250
+ return yield res.json();
298
251
  });
299
-
300
- if (res.status >= 400 && res.status < 600) {
301
- throw new Error(res.statusText);
302
- }
303
-
304
- return await res.json();
305
252
  }
306
-
307
253
  /**
308
254
  * Delete the specified config.
309
255
  * @param {String} slug The application slug.
310
256
  * @param {String} [configId] The unique ID of the config.
311
257
  * @returns {Promise<unknown>}
312
258
  */
313
- async deleteConfig(slug, configId) {
314
- const res = await fetch(`${this.baseUrl}/api/v2/f-sdk/slug/${slug}/config${configId ? `/${configId}` : ""}`, {
315
- method: "DELETE",
316
- headers: {
317
- authorization: `Bearer ${this.token}`,
318
- },
259
+ deleteConfig(slug, configId) {
260
+ return __awaiter(this, void 0, void 0, function* () {
261
+ const res = yield fetch(`${this.baseUrl}/api/v2/f-sdk/slug/${slug}/config${configId ? `/${configId}` : ""}`, {
262
+ method: "DELETE",
263
+ headers: {
264
+ authorization: `Bearer ${this.token}`,
265
+ },
266
+ });
267
+ if (res.status >= 400 && res.status < 600) {
268
+ throw new Error(res.statusText);
269
+ }
270
+ return yield res.json();
319
271
  });
320
-
321
- if (res.status >= 400 && res.status < 600) {
322
- throw new Error(res.statusText);
323
- }
324
-
325
- return await res.json();
326
272
  }
327
273
  }
328
-
329
- module.exports = Cobalt;
274
+ exports.Cobalt = Cobalt;