@cobaltio/cobalt-js 6.0.0 → 7.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 +4 -4
- package/cobalt.js +37 -26
- 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>
|
package/cobalt.js
CHANGED
|
@@ -118,10 +118,11 @@ class Cobalt {
|
|
|
118
118
|
* specified application.
|
|
119
119
|
* @private
|
|
120
120
|
* @param {String} slug The application slug.
|
|
121
|
+
* @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
|
|
121
122
|
* @returns {Promise<String>} The auth URL where users can authenticate themselves.
|
|
122
123
|
*/
|
|
123
|
-
async getOAuthUrl(slug) {
|
|
124
|
-
const res = await fetch(`${this.baseUrl}/api/v1/${slug}/integrate`, {
|
|
124
|
+
async getOAuthUrl(slug, params) {
|
|
125
|
+
const res = await fetch(`${this.baseUrl}/api/v1/${slug}/integrate?${new URLSearchParams(params).toString()}`, {
|
|
125
126
|
headers: {
|
|
126
127
|
authorization: `Bearer ${this.token}`,
|
|
127
128
|
},
|
|
@@ -139,11 +140,12 @@ class Cobalt {
|
|
|
139
140
|
* Handle OAuth for the specified native application.
|
|
140
141
|
* @private
|
|
141
142
|
* @param {String} slug The application slug.
|
|
143
|
+
* @param {Object.<string, string | number | boolean>} [params={}] The key value pairs of auth data.
|
|
142
144
|
* @returns {Promise<Boolean>} Whether the user authenticated.
|
|
143
145
|
*/
|
|
144
|
-
async oauth(slug) {
|
|
146
|
+
async oauth(slug, params) {
|
|
145
147
|
return new Promise((resolve, reject) => {
|
|
146
|
-
this.getOAuthUrl(slug)
|
|
148
|
+
this.getOAuthUrl(slug, params)
|
|
147
149
|
.then(oauthUrl => {
|
|
148
150
|
const connectWindow = window.open(oauthUrl);
|
|
149
151
|
|
|
@@ -186,29 +188,38 @@ class Cobalt {
|
|
|
186
188
|
* @returns {Promise<Boolean>} Whether the connection was successful.
|
|
187
189
|
*/
|
|
188
190
|
async connect(slug, payload) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
191
|
+
return new Promise(async (resolve, reject) => {
|
|
192
|
+
try {
|
|
193
|
+
const app = await this.getApp(slug);
|
|
194
|
+
|
|
195
|
+
// oauth
|
|
196
|
+
if (app?.auth_type ==="oauth2") {
|
|
197
|
+
const connected = await this.oauth(slug, payload);
|
|
198
|
+
resolve(connected);
|
|
199
|
+
// key based
|
|
200
|
+
} else {
|
|
201
|
+
const res = await fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
|
|
202
|
+
method: "POST",
|
|
203
|
+
headers: {
|
|
204
|
+
authorization: `Bearer ${this.token}`,
|
|
205
|
+
"content-type": "application/json",
|
|
206
|
+
},
|
|
207
|
+
body: JSON.stringify({
|
|
208
|
+
...payload,
|
|
209
|
+
}),
|
|
210
|
+
});
|
|
205
211
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
if (res.status >= 400 && res.status < 600) {
|
|
213
|
+
reject(new Error(res.statusText));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const data = await res.json();
|
|
217
|
+
resolve(data.success);
|
|
218
|
+
}
|
|
219
|
+
} catch (error) {
|
|
220
|
+
reject(error);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
212
223
|
}
|
|
213
224
|
|
|
214
225
|
/**
|