@cobaltio/cobalt-js 0.0.18 → 0.0.19

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/cobalt.js CHANGED
@@ -125,6 +125,49 @@ class Cobalt {
125
125
  return data?.auth_url;
126
126
  }
127
127
 
128
+ /**
129
+ * Handle OAuth for the specified application.
130
+ * @property {string} application The application type.
131
+ * @returns {Promise<boolean>} Whether the user authenticated.
132
+ */
133
+ async oauth(application) {
134
+ return new Promise((resolve, reject) => {
135
+ this.getAppAuthUrl(application)
136
+ .then(oauthUrl => {
137
+ const connectWindow = window.open(oauthUrl);
138
+
139
+ // keep checking connection status
140
+ const interval = setInterval(() => {
141
+ this.getAppAuthStatus(application)
142
+ .then(connected => {
143
+ if (connected === true) {
144
+ // close auth window
145
+ connectWindow?.close();
146
+ // clear interval
147
+ clearInterval(interval);
148
+ // resovle status
149
+ resolve(true);
150
+ } else {
151
+ // user closed oauth window without authenticating
152
+ if (connectWindow?.closed) {
153
+ // clear interval
154
+ clearInterval(interval);
155
+ // resolve status
156
+ resolve(false);
157
+ }
158
+ }
159
+ })
160
+ .catch(e => {
161
+ console.error(e);
162
+ clearInterval(interval);
163
+ reject(e);
164
+ });
165
+ }, 3e3);
166
+ })
167
+ .catch(reject);
168
+ });
169
+ }
170
+
128
171
  /**
129
172
  * Save the auth data that user provides to authenticate themselves to the
130
173
  * specified application.
@@ -250,7 +293,7 @@ class Cobalt {
250
293
  * @property {string} workflowId The ID of the workflow you want to activate.
251
294
  * @returns {Promise<void>}
252
295
  */
253
- async activateWorkflow(workflowId) {
296
+ async activateWorkflow(workflowId) {
254
297
  const res = await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/install/success`, {
255
298
  method: "PUT",
256
299
  headers: {
package/cobalt.min.js CHANGED
@@ -1 +1 @@
1
- class Cobalt{constructor(options){this.apiBaseUrl=options?.baseUrl||"https://api.gocobalt.io";this.token=options?.token}get token(){return this.sessionToken}set token(token){return this.sessionToken=typeof token==="string"?token:""}get baseUrl(){return this.apiBaseUrl}async installTemplate(templateId,udf={}){const res=await fetch(`${this.baseUrl}/api/v1/template/install/${templateId}`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({udf:udf})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async getAppAuthStatus(application){const res=await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/auth?integration_type=${application}`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return!!data?.status}async getAppAuthUrl(application){const res=await fetch(`${this.baseUrl}/api/v1/${application}/integrate`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return data?.auth_url}async setAppAuthData(application,payload,appId){const res=await fetch(appId?`${this.baseUrl}/api/v1/${application}/${appId}/save`:`${this.baseUrl}/api/v1/${application}/save`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({...payload})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return data}async removeAppAuth(application,appId){const res=await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${application}?app_id=${appId}`,{method:"DELETE",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}}async getNodeConfiguration(workflowId,nodeId,fieldName,selectedValues={}){const res=await fetch(`${this.baseUrl}/api/v1/workflow/${workflowId}/node/${nodeId}/configuration`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({fieldName:fieldName,selectedValues:selectedValues})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async saveNode(workflowId,nodeId,inputData={}){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/node/${nodeId}`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({input_data:inputData})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async getWorkflowConfiguration(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/configuration`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async activateWorkflow(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/install/success`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async toggleWorkflowStatus(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/toggle-status`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async deleteWorkflow(workflowId){const res=await fetch(`${this.baseUrl}/api/v1/workflow/${workflowId}`,{method:"DELETE",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}}module.exports=Cobalt;
1
+ class Cobalt{constructor(options){this.apiBaseUrl=options?.baseUrl||"https://api.gocobalt.io";this.token=options?.token}get token(){return this.sessionToken}set token(token){return this.sessionToken=typeof token==="string"?token:""}get baseUrl(){return this.apiBaseUrl}async installTemplate(templateId,udf={}){const res=await fetch(`${this.baseUrl}/api/v1/template/install/${templateId}`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({udf:udf})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async getAppAuthStatus(application){const res=await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/auth?integration_type=${application}`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return!!data?.status}async getAppAuthUrl(application){const res=await fetch(`${this.baseUrl}/api/v1/${application}/integrate`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return data?.auth_url}async oauth(application){return new Promise((resolve,reject)=>{this.getAppAuthUrl(application).then(oauthUrl=>{const connectWindow=window.open(oauthUrl);const interval=setInterval(()=>{this.getAppAuthStatus(application).then(connected=>{if(connected===true){connectWindow?.close();clearInterval(interval);resolve(true)}else{if(connectWindow?.closed){clearInterval(interval);resolve(false)}}}).catch(e=>{console.error(e);clearInterval(interval);reject(e)})},3e3)}).catch(reject)})}async setAppAuthData(application,payload,appId){const res=await fetch(appId?`${this.baseUrl}/api/v1/${application}/${appId}/save`:`${this.baseUrl}/api/v1/${application}/save`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({...payload})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}const data=await res.json();return data}async removeAppAuth(application,appId){const res=await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${application}?app_id=${appId}`,{method:"DELETE",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}}async getNodeConfiguration(workflowId,nodeId,fieldName,selectedValues={}){const res=await fetch(`${this.baseUrl}/api/v1/workflow/${workflowId}/node/${nodeId}/configuration`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({fieldName:fieldName,selectedValues:selectedValues})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async saveNode(workflowId,nodeId,inputData={}){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/node/${nodeId}`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify({input_data:inputData})});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async getWorkflowConfiguration(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/configuration`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async activateWorkflow(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/install/success`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async toggleWorkflowStatus(workflowId){const res=await fetch(`${this.baseUrl}/api/v2/workflow/${workflowId}/toggle-status`,{method:"PUT",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async deleteWorkflow(workflowId){const res=await fetch(`${this.baseUrl}/api/v1/workflow/${workflowId}`,{method:"DELETE",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}}module.exports=Cobalt;
package/docs.md CHANGED
@@ -38,6 +38,7 @@ Cobalt Frontend SDK
38
38
  * [.installTemplate()](#Cobalt+installTemplate) ⇒ [<code>Promise.&lt;Workflow&gt;</code>](#Workflow)
39
39
  * [.getAppAuthStatus()](#Cobalt+getAppAuthStatus) ⇒ <code>Promise.&lt;boolean&gt;</code>
40
40
  * [.getAppAuthUrl()](#Cobalt+getAppAuthUrl) ⇒ <code>Promise.&lt;string&gt;</code>
41
+ * [.oauth()](#Cobalt+oauth) ⇒ <code>Promise.&lt;boolean&gt;</code>
41
42
  * [.setAppAuthData()](#Cobalt+setAppAuthData) ⇒ <code>Promise.&lt;void&gt;</code>
42
43
  * [.removeAppAuth()](#Cobalt+removeAppAuth) ⇒ <code>Promise.&lt;void&gt;</code>
43
44
  * [.getNodeConfiguration()](#Cobalt+getNodeConfiguration) ⇒ <code>Promise.&lt;Array.&lt;Field&gt;&gt;</code>
@@ -114,6 +115,19 @@ specified application.
114
115
  | --- | --- | --- |
115
116
  | application | <code>string</code> | The application type. |
116
117
 
118
+ <a name="Cobalt+oauth"></a>
119
+
120
+ ### cobalt.oauth() ⇒ <code>Promise.&lt;boolean&gt;</code>
121
+ Handle OAuth for the specified application.
122
+
123
+ **Kind**: instance method of [<code>Cobalt</code>](#Cobalt)
124
+ **Returns**: <code>Promise.&lt;boolean&gt;</code> - Whether the user authenticated.
125
+ **Properties**
126
+
127
+ | Name | Type | Description |
128
+ | --- | --- | --- |
129
+ | application | <code>string</code> | The application type. |
130
+
117
131
  <a name="Cobalt+setAppAuthData"></a>
118
132
 
119
133
  ### cobalt.setAppAuthData() ⇒ <code>Promise.&lt;void&gt;</code>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobaltio/cobalt-js",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "Cobalt frontend SDK",
5
5
  "main": "./cobalt.js",
6
6
  "scripts": {
@@ -30,8 +30,8 @@
30
30
  "homepage": "https://github.com/Breakout-Embed/cobalt-js#readme",
31
31
  "devDependencies": {
32
32
  "foodoc": "^0.0.9",
33
- "jsdoc": "^3.6.11",
34
- "jsdoc-to-markdown": "^7.1.1",
35
- "uglify-js": "^3.17.0"
33
+ "jsdoc": "^4.0.0",
34
+ "jsdoc-to-markdown": "^8.0.0",
35
+ "uglify-js": "^3.17.4"
36
36
  }
37
37
  }