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