@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/README.md +8 -246
- package/cobalt.d.ts +162 -0
- package/cobalt.js +175 -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,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cobalt Frontend SDK
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
|
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
|
-
|
|
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) {
|
|
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
|
-
//
|
|
170
|
-
resolve(
|
|
108
|
+
// resovle status
|
|
109
|
+
resolve(true);
|
|
171
110
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
|
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
|
-
|
|
192
|
-
return
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
|
|
221
|
-
|
|
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
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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 };
|