@cobaltio/cobalt-js 2.1.1 → 2.1.2
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 +4 -4
- package/cobalt.min.js +1 -1
- package/package.json +1 -1
package/cobalt.js
CHANGED
|
@@ -41,7 +41,7 @@ class Cobalt {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
const data = await res.json();
|
|
44
|
-
return data
|
|
44
|
+
return data.auth_url;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
@@ -61,14 +61,14 @@ class Cobalt {
|
|
|
61
61
|
.then(connected => {
|
|
62
62
|
if (connected === true) {
|
|
63
63
|
// close auth window
|
|
64
|
-
connectWindow
|
|
64
|
+
connectWindow && connectWindow.close();
|
|
65
65
|
// clear interval
|
|
66
66
|
clearInterval(interval);
|
|
67
67
|
// resovle status
|
|
68
68
|
resolve(true);
|
|
69
69
|
} else {
|
|
70
70
|
// user closed oauth window without authenticating
|
|
71
|
-
if (connectWindow
|
|
71
|
+
if (connectWindow && connectWindow.closed) {
|
|
72
72
|
// clear interval
|
|
73
73
|
clearInterval(interval);
|
|
74
74
|
// resolve status
|
|
@@ -158,7 +158,7 @@ class Cobalt {
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
const data = await res.json();
|
|
161
|
-
return !!data
|
|
161
|
+
return !!data.status;
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|
package/cobalt.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class Cobalt{constructor(options={}){this.baseUrl=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:""}async getOAuthUrl(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
|
|
1
|
+
class Cobalt{constructor(options={}){this.baseUrl=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:""}async getOAuthUrl(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.getOAuthUrl(application).then(oauthUrl=>{const connectWindow=window.open(oauthUrl);const interval=setInterval(()=>{this.checkAuth(application).then(connected=>{if(connected===true){connectWindow&&connectWindow.close();clearInterval(interval);resolve(true)}else{if(connectWindow&&connectWindow.closed){clearInterval(interval);resolve(false)}}}).catch(e=>{console.error(e);clearInterval(interval);reject(e)})},3e3)}).catch(reject)})}async auth(application,payload){const res=await fetch(`${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 authCustom(applicationId,payload){const res=await fetch(`${this.baseUrl}/api/v1/custom/${applicationId}/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 checkAuth(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 removeAuth(application,applicationId){const res=await fetch(`${this.baseUrl}/api/v1/linked-acc/integration/${application}?app_id=${applicationId}`,{method:"DELETE",headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}}async config(applicationId,configId,fields={}){const res=await fetch(`${this.baseUrl}/api/v2/application/${applicationId}/installation/${configId}`,{method:"POST",headers:{authorization:`Bearer ${this.token}`,"content-type":"application/json"},body:JSON.stringify(fields)});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async getConfig(application){const res=await fetch(`${this.baseUrl}/api/v1/application/${application}/config`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async saveConfig(applicationId,payload={}){const res=await fetch(`${this.baseUrl}/api/v1/application/${applicationId}/install`,{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)}return await res.json()}async getSavedConfig(applicationId,configId){const res=await fetch(`${this.baseUrl}/api/v1/application/${applicationId}/installation/${configId}`,{headers:{authorization:`Bearer ${this.token}`}});if(res.status>=400&&res.status<600){throw new Error(res.statusText)}return await res.json()}async updateSavedConfig(applicationId,configId,payload={}){const res=await fetch(`${this.baseUrl}/api/v1/application/${applicationId}/installation/${configId}`,{method:"PUT",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)}return await res.json()}async deleteSavedConfig(applicationId,configId){const res=await fetch(`${this.baseUrl}/api/v1/application/${applicationId}/installation/${configId}`,{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;
|