@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/README.md +8 -246
- package/cobalt.d.ts +162 -0
- package/cobalt.js +178 -234
- package/cobalt.ts +360 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +113 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1394 -0
- package/docs/classes/Cobalt.html +50 -0
- package/docs/index.html +15 -0
- package/docs/interfaces/Application.html +20 -0
- package/docs/interfaces/CobaltOptions.html +5 -0
- package/docs/interfaces/ConfigPayload.html +8 -0
- package/docs/interfaces/InputField.html +12 -0
- package/docs/interfaces/Label.html +6 -0
- package/docs/interfaces/UpdateConfigPayload.html +10 -0
- package/docs/interfaces/WorkflowPayload.html +8 -0
- package/docs/modules.html +9 -0
- package/package.json +5 -4
- package/tsconfig.json +12 -0
- package/docs.md +0 -236
package/cobalt.js
CHANGED
|
@@ -1,57 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* Cobalt Frontend SDK
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
|
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
|
-
|
|
148
|
-
return
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
//
|
|
170
|
-
resolve(
|
|
111
|
+
// resovle status
|
|
112
|
+
resolve(true);
|
|
171
113
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
|
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
|
-
|
|
192
|
-
return
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
|
|
221
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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;
|