@cobaltio/cobalt-js 6.0.0 → 7.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 +5 -4
- package/cobalt.js +39 -27
- package/docs.md +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cobalt Javascript SDK
|
|
2
2
|
Cobalt frontend SDK.
|
|
3
3
|
|
|
4
4
|
## Install
|
|
@@ -22,10 +22,10 @@ yarn add @cobaltio/cobalt-js
|
|
|
22
22
|
#### Browser
|
|
23
23
|
```html
|
|
24
24
|
<!-- use a specific version -->
|
|
25
|
-
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@
|
|
25
|
+
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@7.0.0"></script>
|
|
26
26
|
<!-- use a version range instead of a specific version -->
|
|
27
|
-
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@
|
|
28
|
-
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@
|
|
27
|
+
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@7"></script>
|
|
28
|
+
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@7.0"></script>
|
|
29
29
|
<!-- omit the version completely to use the latest one -->
|
|
30
30
|
<!-- you should NOT use this in production -->
|
|
31
31
|
<script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js"></script>
|
|
@@ -217,6 +217,7 @@ An application in Cobalt.
|
|
|
217
217
|
| [slug] | <code>String</code> | The application slug for custom apps. |
|
|
218
218
|
| auth_type | <code>"oauth2"</code> \| <code>"keybased"</code> | The type of auth used by application. |
|
|
219
219
|
| [connected] | <code>Boolean</code> | Whether the user has connected the application. |
|
|
220
|
+
| [reauth_required] | <code>Boolean</code> | Whether the connection has expired and re-auth is required. |
|
|
220
221
|
| [auth_input_map] | [<code>Array.<InputField></code>](#InputField) | The fields required from the user to connect the application (for `keybased` auth type). |
|
|
221
222
|
|
|
222
223
|
<a name="InputField"></a>
|
package/cobalt.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* @property {String} [slug] The application slug for custom apps.
|
|
12
12
|
* @property {"oauth2"|"keybased"} auth_type The type of auth used by application.
|
|
13
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.
|
|
14
15
|
* @property {InputField[]} [auth_input_map] The fields required from the user to connect the application (for `keybased` auth type).
|
|
15
16
|
*/
|
|
16
17
|
|
|
@@ -118,10 +119,11 @@ class Cobalt {
|
|
|
118
119
|
* specified application.
|
|
119
120
|
* @private
|
|
120
121
|
* @param {String} slug The application slug.
|
|
122
|
+
* @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
|
|
121
123
|
* @returns {Promise<String>} The auth URL where users can authenticate themselves.
|
|
122
124
|
*/
|
|
123
|
-
async getOAuthUrl(slug) {
|
|
124
|
-
const res = await fetch(`${this.baseUrl}/api/v1/${slug}/integrate`, {
|
|
125
|
+
async getOAuthUrl(slug, params) {
|
|
126
|
+
const res = await fetch(`${this.baseUrl}/api/v1/${slug}/integrate?${new URLSearchParams(params).toString()}`, {
|
|
125
127
|
headers: {
|
|
126
128
|
authorization: `Bearer ${this.token}`,
|
|
127
129
|
},
|
|
@@ -139,11 +141,12 @@ class Cobalt {
|
|
|
139
141
|
* Handle OAuth for the specified native application.
|
|
140
142
|
* @private
|
|
141
143
|
* @param {String} slug The application slug.
|
|
144
|
+
* @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
|
|
142
145
|
* @returns {Promise<Boolean>} Whether the user authenticated.
|
|
143
146
|
*/
|
|
144
|
-
async oauth(slug) {
|
|
147
|
+
async oauth(slug, params) {
|
|
145
148
|
return new Promise((resolve, reject) => {
|
|
146
|
-
this.getOAuthUrl(slug)
|
|
149
|
+
this.getOAuthUrl(slug, params)
|
|
147
150
|
.then(oauthUrl => {
|
|
148
151
|
const connectWindow = window.open(oauthUrl);
|
|
149
152
|
|
|
@@ -151,7 +154,7 @@ class Cobalt {
|
|
|
151
154
|
const interval = setInterval(() => {
|
|
152
155
|
this.getApp(slug)
|
|
153
156
|
.then(app => {
|
|
154
|
-
if (app && app.connected === true) {
|
|
157
|
+
if (app && app.connected === true && !app.reauth_required) {
|
|
155
158
|
// close auth window
|
|
156
159
|
connectWindow && connectWindow.close();
|
|
157
160
|
// clear interval
|
|
@@ -186,29 +189,38 @@ class Cobalt {
|
|
|
186
189
|
* @returns {Promise<Boolean>} Whether the connection was successful.
|
|
187
190
|
*/
|
|
188
191
|
async connect(slug, payload) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
+
});
|
|
205
212
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
213
|
+
if (res.status >= 400 && res.status < 600) {
|
|
214
|
+
reject(new Error(res.statusText));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const data = await res.json();
|
|
218
|
+
resolve(data.success);
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
reject(error);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
212
224
|
}
|
|
213
225
|
|
|
214
226
|
/**
|
package/docs.md
CHANGED
|
@@ -159,6 +159,7 @@ An application in Cobalt.
|
|
|
159
159
|
| [slug] | <code>String</code> | The application slug for custom apps. |
|
|
160
160
|
| auth_type | <code>"oauth2"</code> \| <code>"keybased"</code> | The type of auth used by application. |
|
|
161
161
|
| [connected] | <code>Boolean</code> | Whether the user has connected the application. |
|
|
162
|
+
| [reauth_required] | <code>Boolean</code> | Whether the connection has expired and re-auth is required. |
|
|
162
163
|
| [auth_input_map] | [<code>Array.<InputField></code>](#InputField) | The fields required from the user to connect the application (for `keybased` auth type). |
|
|
163
164
|
|
|
164
165
|
<a name="InputField"></a>
|