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