@workfront/workfront-api
Version:
A Workfront API for the Node.js and the Web
3 lines (2 loc) • 6.88 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Wokfront={})}(this,(function(e){"use strict";class t{constructor(e){let t;this.serverAcceptsJSON=!0,this._uriGenerationMode=!1,this._httpOptions={url:e.url,alwaysUseGet:e.alwaysUseGet,headers:e.headers||{}},e.apiKey&&(this._httpOptions.headers.apiKey=e.apiKey);const{version:s="internal"}=e;["internal","unsupported","asp"].indexOf(s)>=0?t="/attask/api-"+s:(t="/attask/api/v"+s,"2.0"!==s&&"3.0"!==s&&"4.0"!==s||(this.serverAcceptsJSON=!1)),this._httpOptions.path=t}getApiKey(e,t,s){const r={username:e,password:t};return void 0!==s&&(r.subdomain=s),new Promise(((e,t)=>{if(void 0!==this._httpOptions.headers.apiKey)e(this._httpOptions.headers.apiKey);else{this.execute("USER",null,"getApiKey",r).then((s=>{if(""===s.result){this.execute("USER",null,"generateApiKey",r).then((t=>{this._httpOptions.headers.apiKey=t.result,e(this._httpOptions.headers.apiKey)}),t)}else this._httpOptions.headers.apiKey=s.result,e(this._httpOptions.headers.apiKey)}),t)}}))}copy(e,s,r,n,o){const i={copySourceID:s};return r&&(i.updates=JSON.stringify(r)),o&&(i.options=JSON.stringify(o)),this.request(e,i,n,t.Methods.POST)}count(e,s){const r=this.request(e+"/count",s,null,t.Methods.GET);return this._uriGenerationMode?r:r.then((function(e){return e.count}))}clearApiKey(){return new Promise(((e,t)=>{this.execute("USER",null,"clearApiKey").then((s=>{s?(delete this._httpOptions.headers.apiKey,e()):t()}))}))}create(e,s,r){return s.hasOwnProperty("updates")?this.request(e,s,r,t.Methods.POST):this.request(e,{updates:s},r,t.Methods.POST)}edit(e,s,r,n){return r.hasOwnProperty("updates")?this.request(e+"/"+s,r,n,t.Methods.PUT):this.request(e+"/"+s,{updates:r},n,t.Methods.PUT)}editMultiple(e,s,r){return this.request(e,{updates:s},r,t.Methods.PUT)}execute(e,s,r,n){let o=e,i={method:t.Methods.PUT};return s?o+="/"+s+"/"+r:i.action=r,n&&(i={...i,...n}),this.request(o,i,null,t.Methods.POST)}get(e,s,r){"string"==typeof s&&(s=[s]);let n=e,o=null;return 1===s.length?0===s[0].indexOf("$$")?o={id:s[0]}:n+="/"+s[0]:o={id:s},this.request(n,o,r,t.Methods.GET)}login(e,s,r){const n={username:e,password:s};void 0!==r&&(n.subdomain=r);return this.request("login",n,null,t.Methods.POST).then((e=>(this.setSessionID(e.sessionID),e)))}logout(){return new Promise(((e,s)=>{this.request("logout",null,null,t.Methods.GET).then((t=>{t&&t.success?(delete this._httpOptions.headers["X-XSRF-TOKEN"],delete this._httpOptions.headers.sessionID,e()):s()}))}))}metadata(e,s){let r="/metadata";return e&&(r=e+r),this.request(r,null,s,t.Methods.GET)}namedQuery(e,s,r,n){return this.request(e+"/"+s,r,n,t.Methods.GET)}remove(e,s,r){const n=r?{force:!0}:null,o=this.request(e+"/"+s,n,null,t.Methods.DELETE);return this._uriGenerationMode?o:new Promise(((e,t)=>{o.then((s=>{s&&s.success?e():t()}),t)}))}report(e,s,r=!1){let n,o;return r?(n={...s,method:t.Methods.GET},o=t.Methods.POST):(n=s,o=t.Methods.GET),this.request(e+"/report",n,null,o)}request(e,s,n=[],o=t.Methods.GET){const i={...s},h=this.getOptions(e,i,this._uriGenerationMode?t.Methods.GET:o),a=this.getFields(n);a&&(i.fields=a);const d=this.getHeaders(),{bodyParams:u,queryString:p,contentType:l}=this.populateQueryStringAndBodyParams(i,h);if(l&&d.append("Content-Type",l),this._uriGenerationMode){let s="";return-1===p.indexOf("method=")&&(s=(""===p?"?":"&")+"method="+t.Methods.GET),e+p+s}return r(h.url+h.path+p,{headers:d,body:u,method:h.method})}search(e,s,r,n=!1){let o,i;return n?(o={...s,method:t.Methods.GET},i=t.Methods.POST):(o=s,i=t.Methods.GET),this.request(e+"/search",o,r,i)}batch(e,s,r){const n=e(function(e){const s=Object.create(e);return s._uriGenerationMode=!0,{copy:(e,t,r,n,o)=>s.copy(e,t,r,n,o),count:(e,t)=>s.count(e,t),create:(e,t,r)=>s.create(e,t,r),edit:(e,t,r,n)=>s.edit(e,t,r,n),editMultiple:(e,t,r)=>s.editMultiple(e,t,r),execute:(e,t,r,n)=>s.execute(e,t,r,n),get:(e,t,r)=>s.get(e,t,r),metadata:(e,t)=>s.metadata(e,t),namedQuery:(e,t,r,n)=>s.namedQuery(e,t,r,n),remove:(e,t,r)=>s.remove(e,t,r),report:(e,t)=>s.report(e,t),request:(e,r,n,o=t.Methods.GET)=>s.request(e,r,n,o),search:(e,t,r)=>s.search(e,t,r,!1)}}(this));if(0===n.length)return Promise.resolve(s?void 0:[]);const o=this.request("/batch",{atomic:!!s,uri:n,concurrent:!!r},void 0,t.Methods.POST);return s?o.then((e=>{if(!e||!e.success)throw new Error})):o.then((e=>e.map((e=>e.data))))}setApiKey(e){return this._httpOptions.headers.apiKey=e}setSessionID(e){e?this._httpOptions.headers.sessionID=e:delete this._httpOptions.headers.sessionID}setXSRFToken(e){e?this._httpOptions.headers["X-XSRF-TOKEN"]=e:delete this._httpOptions.headers["X-XSRF-TOKEN"]}uploadFileContent(e,s){const r=new FormData;return r.append("uploadedFile",e,s),this.request("upload",r,null,t.Methods.POST)}getHeaders(){const e=new Headers;return e.append("X-Requested-With","XMLHttpRequest"),this._httpOptions.headers.sessionID?e.append("sessionID",this._httpOptions.headers.sessionID):this._httpOptions.headers["X-XSRF-TOKEN"]?e.append("X-XSRF-TOKEN",this._httpOptions.headers["X-XSRF-TOKEN"]):this._httpOptions.headers.apiKey&&e.append("apiKey",this._httpOptions.headers.apiKey),e}getFields(e){return"string"==typeof e?e:Array.isArray(e)?e.join(","):void 0}getOptions(e,s,r){const n={...this._httpOptions};return n.alwaysUseGet&&"login"!==e?(s.method=r,n.method=t.Methods.GET):n.method=r,0!==e.indexOf("/")&&(e="/"+e),n.path=this._httpOptions.path+e,n}populateQueryStringAndBodyParams(e,r){let n=null,o="",i=null;if("undefined"!=typeof FormData&&e instanceof FormData)n=e;else if(!this.serverAcceptsJSON||"object"!=typeof e.updates||r.method!==t.Methods.POST&&r.method!==t.Methods.PUT)i="application/x-www-form-urlencoded",e.hasOwnProperty("updates")&&"string"!=typeof e.updates&&(e.updates=JSON.stringify(e.updates)),n=s(e),r.method!==t.Methods.GET&&r.method!==t.Methods.DELETE||(n&&(o="?"+n),n=null);else{i="application/json",n=JSON.stringify(e.updates),delete e.updates;const t=s(e);t&&(o="?"+t)}return{bodyParams:n,queryString:o,contentType:i}}}t.Methods={GET:"GET",PUT:"PUT",DELETE:"DELETE",POST:"POST"};const s=function(e){return Object.keys(e).reduce((function(t,s){return Array.isArray(e[s])?e[s].forEach((function(e){t.push(s+"="+encodeURIComponent(e))})):t.push(s+"="+encodeURIComponent(e[s])),t}),[]).join("&")};function r(e,t){return fetch(e,{...t,credentials:"same-origin"}).then(n.success,n.failure)}const n={success:e=>e.ok?e.json().then((t=>{if(t.error)throw{status:e.status,message:t.error.message};return t.data})):e.json().then((t=>{throw{status:e.status,message:t.error.message}}),(()=>{throw{status:e.status,message:e.statusText}})),failure:e=>{throw{message:e.message||e.statusText}}};e.Api=t,e.ResponseHandler=n,e.makeFetchCall=r}));
//# sourceMappingURL=workfront-api.umd.min.js.map