rentdynamics
Version:
Package to help facilitate communicating with the Rent Dynamics API
1 lines • 3.49 kB
JavaScript
var RentDynamics;(()=>{"use strict";var t,e={d:(t,s)=>{for(var a in s)e.o(s,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:s[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},s={};e.r(s),e.d(s,{BASE_URL:()=>t,Client:()=>a,ClientHelpers:()=>n,ClientOptions:()=>o});class a{constructor(t){this.helpers=new n(t)}async get(t){const e=this.helpers.baseUrl+t,s=await this.helpers.getHeaders(t,void 0,this.authToken);return fetch(e.replace(/\|/g,"%7C"),{method:"GET",headers:Object.assign(Object.assign({},s),{"Content-Type":"application/json"})})}async put(t,e){const s=this.helpers.baseUrl+t,a=await this.helpers.getHeaders(t,e,this.authToken);return fetch(s,{method:"PUT",headers:Object.assign(Object.assign({},a),{"Content-Type":"application/json"}),body:JSON.stringify(e)})}async post(t,e){const s=this.helpers.baseUrl+t,a=await this.helpers.getHeaders(t,e,this.authToken);return fetch(s,{method:"POST",headers:Object.assign(Object.assign({},a),{"Content-Type":"application/json"}),body:JSON.stringify(e)})}async delete(t){const e=this.helpers.baseUrl+t,s=await this.helpers.getHeaders(t,void 0,this.authToken);return fetch(e,{method:"DELETE",headers:Object.assign(Object.assign({},s),{"Content-Type":"application/json"})})}async login(t,e){const s=await this.helpers.encryptPassword(e),a=await this.post("/auth/login",{username:t,password:s});if(!a.ok)return a;const{token:o}=await a.json();return this.authToken=o,a}async logout(){const t=await this.post("/auth/logout",{authToken:this.authToken});return t.ok?(this.authToken=void 0,t):t}}!function(t){t.DEV_RD="https://api.rentdynamics.dev",t.PROD_RD="https://api.rentdynamics.com",t.DEV_RP="https://api-dev.rentplus.com",t.PROD_RP="https://api.rentplus.com"}(t||(t={}));class o{constructor(){this.baseUrl=t.DEV_RD,this.getEncoder=async()=>new TextEncoder,this.getCryptographer=async()=>crypto.subtle}}class n{constructor(t){this.options=t}get baseUrl(){return this.options.baseUrl}getTimestamp(){return Date.now()}async getHeaders(t,e,s){const a={};if(this.options.apiKey&&this.options.apiSecretKey){e&&Object.keys(e).length||(e=void 0),void 0!==e&&(e=JSON.stringify(this.formatPayload(e)));const o=this.getTimestamp(),n=await this.getNonce(o,t,e);return s&&(a.Authorization="TOKEN "+s),a["x-rd-api-key"]=this.options.apiKey,a["x-rd-api-nonce"]=n,a["x-rd-timestamp"]=o.toString(),a}return a}formatPayload(t){let e={};return null==t?e=null:t!==Object(t)?e=t:Array.isArray(t)?(e=[],t.forEach(((s,a)=>{e[a]=this.formatPayload(t[a])}))):Object.keys(t).sort().forEach((s=>{"object"==typeof t[s]?e[s]=this.formatPayload(t[s]):"string"==typeof t[s]?e[s]=t[s].replace(/ /g,""):e[s]=t[s]})),e}async getNonce(t,e,s){if(!this.options.apiSecretKey)return Promise.resolve("");const a=encodeURI(e).replace(/%7[Cc]/g,"|").replace(/%20/g," "),o=void 0!==s?t+a+s:t+a,n=await this.options.getCryptographer(),i=await this.options.getEncoder(),c=i.encode(this.options.apiSecretKey),p=i.encode(o),h={name:"HMAC",hash:"SHA-1"},l=await n.importKey("raw",c,h,!1,["sign"]),y=await n.sign(h.name,l,p);return r(y)}async encryptPassword(t){const e=await this.options.getCryptographer(),s=(await this.options.getEncoder()).encode(t),a=await e.digest("SHA-1",s);return r(a)}}const r=t=>Array.from(new Uint8Array(t)).map((t=>t.toString(16).padStart(2,"0"))).join("");RentDynamics=s})();