This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypetalk.min.js
3 lines (3 loc) · 8.35 KB
/
typetalk.min.js
1
2
3
// typetalk-js v0.2.0
// License: MIT
"use strict";if("undefined"==typeof window)var Promise=Promise||require("promise"),XMLHttpRequest=XMLHttpRequest||require("xmlhttprequest").XMLHttpRequest;(function(){var a=function(){function a(a){b=this,a=a||{},b.accessToken=a.access_token,b.refreshToken=a.refresh_token,b.timeout=a.timeout||3e3,c=a.client_id,d=a.client_secret,e=a.redirect_uri,f=a.scope||f}a.API_BASE_URL="https://typetalk.in/api/v1/",a.OAUTH_BASE_URL="https://typetalk.in/oauth2/";var b,c,d,e,f="topic.read",g=function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(encodeURIComponent(c)+"="+encodeURIComponent(a[c]));return b.join("&")},h=function(c){return new Promise(function(d,e){var f=new XMLHttpRequest;f.onload=function(){200===f.status?d(JSON.parse(f.responseText)):e(JSON.parse(f.responseText))},f.onerror=e,f.open("POST",a.OAUTH_BASE_URL+"access_token"),f.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),f.timeout=b.timeout,f.send(c)})},i=function(a,c,d,e){return new Promise(function(f,g){var h=new XMLHttpRequest;h.onload=function(){if(200===h.status)f(JSON.parse(h.responseText));else if(400===h.status||401===h.status){var a=h.getResponseHeader("WWW-Authenticate")||"",b=a.match(/error="(\w+)"/)||[],c=a.match(/error_description="(\w+)"/)||[],d=b.length>1?b[1]:h.statusText,e=c.length>1?c[1]:"";g({status:h.status,error:d,error_description:e})}else g({status:h.status,error:h.statusText,error_description:"An error has occurred while requesting api"})},h.onerror=g,h.open(c,a),e&&e["Content-Type"]&&h.setRequestHeader("Content-Type",e["Content-Type"]),h.setRequestHeader("Authorization","Bearer "+encodeURIComponent(b.accessToken)),h.timeout=b.timeout,h.send(d)})};return a.prototype.authorizeChromeApp=function(d){return d=d||{},new Promise(function(g,h){if(!chrome||!chrome.identity)return void h(new Error("chrome.identity API is unsupported"));var i=a.OAUTH_BASE_URL+"authorize?client_id="+encodeURIComponent(c||d.client_id)+"&redirect_uri="+encodeURIComponent(e||d.redirect_uri)+"&scope="+encodeURIComponent(f||d.scope)+"&response_type=code";chrome.identity.launchWebAuthFlow({url:i,interactive:!0},function(a){if("undefined"==typeof a)return void h(new Error("Cannot get response url"));var c=a.match(/code=(.+)/)[1];return"undefined"==typeof c?void h(new Error("authorization code is required")):void b.getAccessTokenUsingAuthorizationCode(c).then(function(a){g(a)},function(a){h(a)})})})},a.prototype.hasToken=function(){return!!b.accessToken&&!!b.refreshToken},a.prototype.clearToken=function(){b.accessToken=null,b.refreshToken=null},a.prototype.validateAccessToken=function(){return b.getMyProfile()},a.prototype.getAccessTokenUsingClientCredentials=function(a){a=a||{};var b="client_id="+encodeURIComponent(c||a.client_id)+"&client_secret="+encodeURIComponent(d||a.client_secret)+"&grant_type=client_credentials&scope="+encodeURIComponent(f||a.scope);return h(b)},a.prototype.requestAuthorization=function(b){b=b||{};var d="client_id="+encodeURIComponent(c||b.client_id)+"&redirect_uri="+encodeURIComponent(e||b.redirect_uri)+"&scope="+encodeURIComponent(f||b.scope)+"&response_type=code";location.href=a.OAUTH_BASE_URL+"authorize?"+d},a.prototype.getAccessTokenUsingAuthorizationCode=function(a,b){b=b||{};var f="client_id="+encodeURIComponent(c||b.client_id)+"&client_secret="+encodeURIComponent(d||b.client_secret)+"&redirect_uri="+encodeURIComponent(e||b.redirect_uri)+"&grant_type=authorization_code&code="+encodeURIComponent(a);return h(f)},a.prototype.refreshAccessToken=function(a){a=a||{};var e="client_id="+encodeURIComponent(c||a.client_id)+"&client_secret="+encodeURIComponent(d||a.client_secret)+"&grant_type=refresh_token&refresh_token="+encodeURIComponent(b.refreshToken||a.refresh_token);return h(e)},a.prototype.getMyProfile=function(){return i(a.API_BASE_URL+"profile","GET",null)},a.prototype.getMyTopics=function(){return i(a.API_BASE_URL+"topics","GET",null)},a.prototype.getTopicMessages=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"?"+g(c),"GET",null)},a.prototype.postMessage=function(b,c,d){return d=d||{},d.message=c,i(a.API_BASE_URL+"topics/"+encodeURIComponent(b),"POST",g(d),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.uploadAttachmentFile=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b),"POST",{file:c},{"Content-Type":"multipart/form-data"})},a.prototype.getTopicMembers=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/members/status","GET",null)},a.prototype.getMessage=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/posts/"+encodeURIComponent(c),"GET",null)},a.prototype.removeMessage=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/posts/"+encodeURIComponent(c),"DELETE",null)},a.prototype.likeMessage=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/posts/"+encodeURIComponent(c)+"/like","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.unlikeMessage=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/posts/"+encodeURIComponent(c)+"/like","DELETE",null)},a.prototype.favoriteTopic=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/favorite","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.unfavoriteTopic=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/favorite","DELETE",null)},a.prototype.getNotificationList=function(){return i(a.API_BASE_URL+"notifications","GET",null)},a.prototype.getNotificationCount=function(){return i(a.API_BASE_URL+"notifications/status","GET",null)},a.prototype.readNotification=function(){return i(a.API_BASE_URL+"notifications/open","PUT",null)},a.prototype.readMessagesInTopic=function(b,c){return c=c||{},c.topicId=b,i(a.API_BASE_URL+"bookmarks","PUT",g(c),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.getMentionList=function(b){return b=b||{},i(a.API_BASE_URL+"mentions?"+g(b),"GET",null)},a.prototype.readMention=function(b){return i(a.API_BASE_URL+"mentions/"+encodeURIComponent(b),"PUT",null)},a.prototype.acceptTeamInvitation=function(b,c){return i(a.API_BASE_URL+"teams/"+encodeURIComponent(b)+"/members/invite/"+encodeURIComponent(c)+"/accept","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.declineTeamInvitation=function(b,c){return i(a.API_BASE_URL+"teams/"+encodeURIComponent(b)+"/members/invite/"+encodeURIComponent(c)+"/decline","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.acceptTopicInvitation=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/members/invite/"+encodeURIComponent(c)+"/accept","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.declineTopicInvitation=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/members/invite/"+encodeURIComponent(c)+"/decline","POST",{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.createTopic=function(b,c){return c=c||{},c.name=b,i(a.API_BASE_URL+"topics","POST",g(c),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.updateTopic=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b),"PUT",g(c),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.deleteTopic=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b),"DELETE",null)},a.prototype.getTopicDetails=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/details","GET",null)},a.prototype.inviteTopicMember=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/members/invite","POST",g(c),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.removeTopicMember=function(b,c){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/members/remove","POST",g(c),{"Content-Type":"application/x-www-form-urlencoded"})},a.prototype.getTeams=function(){return i(a.API_BASE_URL+"teams","GET",null)},a.prototype.getFriends=function(){return i(a.API_BASE_URL+"search/friends","GET",null)},a.prototype.searchAccounts=function(b){return i(a.API_BASE_URL+"search/accounts?nameOrEmailAddress="+encodeURIComponent(b),"GET",null)},a.prototype.getTalks=function(b){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/talks","GET",null)},a.prototype.getTalk=function(b,c,d){return i(a.API_BASE_URL+"topics/"+encodeURIComponent(b)+"/talks/"+encodeURIComponent(c)+"/posts","GET",g(d))},a}();"undefined"!=typeof module?module.exports=a:this.Typetalk=a}).call(this);