UNPKG

@chemicalluck/recharge-api-node

Version:
2 lines 6.15 kB
var c=class extends Error{constructor(e){super(`HTTP Error Response: ${e.status} ${e.statusText}`),this.response=e}},a=class extends Error{constructor(e){super(`Recharge API Error: ${e}`)}},h=class extends Error{constructor(e){super(`Not Implemented: ${e}`)}};var b=(o=>(o.GET="GET",o.POST="POST",o.PUT="PUT",o.DELETE="DELETE",o))(b||{});var i=(r=>(r.v1="2021-01",r.v2="2021-11",r))(i||{});var I=class{constructor(e){this.base_url="https://api.rechargeapps.com";this.resource=null;this.recharge_version="2021-01";this.client=e}get url(){if(!this.resource)throw new Error("Resource not set");return`${this.base_url}/${this.recharge_version}/${this.resource}`}_get(e,r){return this.client.get(e,this.recharge_version,r)}_post(e,r){return this.client.post(e,this.recharge_version,r)}_put(e,r){return this.client.put(e,this.recharge_version,r)}_delete(e,r){return this.client.delete(e,this.recharge_version,r)}_paginate(e,r){return this.client.paginate(e,this.recharge_version,r)}},t=I;var u=class extends t{constructor(e){super(e),this.resource="addresses"}create(e,r){return this.client.post(`${this.base_url}/customers/${e}/${this.resource}`,this.recharge_version,r)}get(e){return this._get(`${this.url}/${e}`)}update(e,r){return this._put(`${this.url}/${e}`,r)}delete(e){return this._delete(`${this.url}/${e}`)}list(e,r){return this._get(`${this.base_url}/customers/${e}/${this.resource}`,r)}count(e){return this._get(`${this.url}/count`,e)}validate(e){return this._post(`${this.url}/validate`,e)}applyDiscount(e,r){return this._post(`${this.url}/${e}/apply_discount`,r)}removeDiscount(e){return this._post(`${this.url}/${e}/remove_discount`)}};var p=class extends t{constructor(e){super(e),this.resource="charges",this.recharge_version="2021-01"}};var m=class extends t{constructor(e){super(e),this.resource="checkouts",this.recharge_version="2021-01"}};var g=class extends t{constructor(e){super(e),this.resource="customers",this.recharge_version="2021-01"}};var R=class extends t{constructor(e){super(e),this.resource="orders",this.recharge_version="2021-01"}};var d=class extends t{constructor(e){super(e),this.resource="subscriptions",this.recharge_version="2021-01"}};var l=class extends t{constructor(e){super(e),this.resource="onetimes",this.recharge_version="2021-01"}};var f=class extends t{constructor(e){super(e),this.resource="discounts",this.recharge_version="2021-01"}};var _=class extends t{constructor(e){super(e),this.resource="webhooks",this.recharge_version="2021-01"}};var v=class extends t{constructor(e){super(e),this.resource="metafields",this.recharge_version="2021-01"}};var x=class extends t{constructor(e){super(e),this.resource="shop",this.recharge_version="2021-01"}};var P=class extends t{constructor(e){super(e),this.resource="products",this.recharge_version="2021-01"}};var T=class extends t{constructor(e){super(e),this.resource="async_batch",this.recharge_version="2021-01"}};var w=class extends t{constructor(e){super(e),this.resource="notifications",this.recharge_version="2021-01"}};var y=class extends t{constructor(e){super(e),this.resource="token_information",this.recharge_version="2021-01"}};import N,{Request as O}from"node-fetch";var V=class{constructor(e){this._headers={};this._retries=0;this._maxRetries=3;this._retryDelay=3e3;this.headers={"X-Recharge-Access-Token":e,"X-Recharge-API-Version":"2021-01","Content-Type":"application/json"}}get retries(){return this._retries}set retries(e){this._retries=e}get maxRetries(){return this._maxRetries}get retryDelay(){return this._retryDelay}get headers(){return this._headers}set headers(e){this._headers=e}_setVersion(e){this.headers["X-Recharge-API-Version"]=e}async _retry(e){if(this._retries>=this._maxRetries)throw console.error("Max retries reached"),new a("Max retries reached");return this.retries++,console.info(`Retrying: ${this.retries}/${this.maxRetries} retries, waiting ${this.retryDelay}ms...`),await this._delay(this._retryDelay),this._send(e)}async _delay(e){return new Promise(r=>setTimeout(r,e))}_constructURL(e,r){let n=new URL(e);return r&&Object.keys(r).forEach(o=>n.searchParams.append(o,r[o])),n.toString()}_constructRequestOptions(e,r){return{method:e,headers:this.headers,body:["POST","PUT","PATCH"].includes(e.toUpperCase())&&r?JSON.stringify(r):void 0}}async _request(e,r,n,o){let C=this._constructURL(r,n),A=this._constructRequestOptions(e,o),E=new O(C,A);return this._send(E)}async _send(e){let r=await N(e);if(r.status===429||r.status>=500)return console.warn(r.status===429?"Rate limited, retrying...":"Server error, retrying..."),this._retry(e);if(this.retries=0,!r.ok)throw new c(r);return r}async _extractData(e){return e.json()}_getNextPageV1(e){let n=(e.headers.get("link")||"").match(/<([^>]+)>; rel="next"/);if(n)return n[1]}_getNextPageV2(e){throw new h("getNextPageV2")}_getNextPage(e,r){switch(r){case"2021-01":return this._getNextPageV1(e);case"2021-11":return this._getNextPageV2(e)}}async paginate(e,r,n){this._setVersion(r);let o=[];for(;e;){let C=await this._request("GET",e,n),A=await this._extractData(C);o=o.concat(A),e=this._getNextPage(C,r)||""}return o}async get(e,r,n){return this._setVersion(r),this._request("GET",e,n).then(this._extractData)}async post(e,r,n){this._setVersion(r);let o=await this._request("POST",e,void 0,n);return this._extractData(o)}async put(e,r,n){this._setVersion(r);let o=await this._request("PUT",e,void 0,n);return this._extractData(o)}async delete(e,r,n){this._setVersion(r);let o=await this._request("DELETE",e,void 0,n);return this._extractData(o)}};var k=class{constructor(e){this.address=new u(e),this.charge=new p(e),this.checkout=new m(e),this.customer=new g(e),this.order=new R(e),this.subscription=new d(e),this.onetime=new l(e),this.discount=new f(e),this.webhook=new _(e),this.metafield=new v(e),this.shop=new x(e),this.product=new P(e),this.asyncBatch=new T(e),this.notification=new w(e),this.token=new y(e)}},$=class{constructor(e,r){this.client=r||new V(e),this.v1=new k(this.client)}};export{c as HTTPResponseError,h as NotImplementedError,$ as Recharge,a as RechargeAPIError,i as RechargeAPIVersion,b as RequestMethod}; //# sourceMappingURL=index.js.map