@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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/cobalt.js +37 -26
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # cobalt.js
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@3.0.1"></script>
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@3"></script>
28
- <script src="https://cdn.jsdelivr.net/npm/@cobaltio/cobalt-js@3.0"></script>
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
- if (payload) {
190
- // save auth
191
- const res = await fetch(`${this.baseUrl}/api/v2/app/${slug}/save`, {
192
- method: "POST",
193
- headers: {
194
- authorization: `Bearer ${this.token}`,
195
- "content-type": "application/json",
196
- },
197
- body: JSON.stringify({
198
- ...payload,
199
- }),
200
- });
201
-
202
- if (res.status >= 400 && res.status < 600) {
203
- throw new Error(res.statusText);
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
- const data = await res.json();
207
- return data.success;
208
- } else {
209
- // oauth
210
- return this.oauth(slug);
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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobaltio/cobalt-js",
3
- "version": "6.0.0",
3
+ "version": "7.0.0",
4
4
  "description": "Cobalt frontend SDK",
5
5
  "main": "./cobalt.js",
6
6
  "scripts": {