UNPKG

@libkakashi/porkbunjs

Version:

A comprehensive TypeScript library for the Porkbun API v3

1 lines 4.49 kB
var e=class extends Error{constructor(e,t,n){super(e),this.status=t,this.response=n,this.name=`PorkbunError`}},t=class extends e{constructor(e=`Authentication failed`){super(e,`AUTH_ERROR`),this.name=`PorkbunAuthError`}},n=class extends e{constructor(e=`Rate limit exceeded`,t){super(e,`RATE_LIMIT`),this.limits=t,this.name=`PorkbunRateLimitError`}},r=class{#apiKey;#secretKey;#baseUrl;#timeout;constructor(e){this.#apiKey=e.apiKey,this.#secretKey=e.secretKey,this.#baseUrl=e.baseUrl||`https://api.porkbun.com/api/json/v3`,this.#timeout=e.timeout||3e4}#getAuth(){return{apikey:this.#apiKey,secretapikey:this.#secretKey}}#convertApiResponse(e){if(e==null)return e;if(Array.isArray(e))return e.map(e=>this.#convertApiResponse(e));if(typeof e==`object`){let t={...e};return Object.keys(t).forEach(e=>{t[e]===`yes`?t[e]=!0:t[e]===`no`?t[e]=!1:typeof t[e]==`object`&&t[e]!==null&&(t[e]=this.#convertApiResponse(t[e]))}),t}return e}async#makeRequest(e,r={}){let i=`${this.#baseUrl}${e}`,a={...this.#getAuth(),...r};try{let e=new AbortController,r=setTimeout(()=>e.abort(),this.#timeout),c=await fetch(i,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(a),signal:e.signal});if(clearTimeout(r),!c.ok)throw c.status===403?new t(`Authentication failed or additional authentication required`):Error(`HTTP ${c.status}: ${c.statusText}`);let l=await c.json();if(l.status===`ERROR`){var o,s;throw(o=l.message)?.includes(`rate limit`)||(s=l.message)?.includes(`limit`)?new n(l.message):Error(l.message||`Unknown API error`)}return delete l.status,this.#convertApiResponse(l)}catch(e){throw e instanceof Error?e.name===`AbortError`?Error(`Request timeout`):e instanceof t||e instanceof n?e:Error(`API request failed: ${e.message}`):Error(`Unknown error occurred`)}}async ping(){return this.#makeRequest(`/ping`)}async getPricing(){let e=`/pricing/get`,{pricing:t}=await this.#makeRequest(e);return t}async updateNameServers(e,t){let n=`/domain/updateNs/${e}`;await this.#makeRequest(n,t)}async getNameServers(e){let t=`/domain/getNs/${e}`,{ns:n}=await this.#makeRequest(t);return n}async listDomains(e={}){let t=`/domain/listAll`,n={...e,...e.includeLabels!==void 0&&{includeLabels:e.includeLabels?`yes`:`no`}},{domains:r}=await this.#makeRequest(t,n);return r}async addURLForward(e,t){let n=`/domain/addUrlForward/${e}`,r={...t,includePath:t.includePath?`yes`:`no`,wildcard:t.wildcard?`yes`:`no`};await this.#makeRequest(n,r)}async getURLForwarding(e){let t=`/domain/getUrlForwarding/${e}`,{forwards:n}=await this.#makeRequest(t);return n}async deleteURLForward(e,t){let n=`/domain/deleteUrlForward/${e}/${t}`;await this.#makeRequest(n)}async checkDomain(e){let t=`/domain/checkDomain/${e}`,{response:n,limits:r}=await this.#makeRequest(t);return{response:n,limits:r}}async createGlueRecord(e,t,n){let r=`/domain/createGlue/${e}/${t}`;await this.#makeRequest(r,n)}async updateGlueRecord(e,t,n){let r=`/domain/updateGlue/${e}/${t}`;await this.#makeRequest(r,n)}async deleteGlueRecord(e,t){let n=`/domain/deleteGlue/${e}/${t}`;await this.#makeRequest(n)}async getGlueRecords(e){let t=`/domain/getGlue/${e}`,{hosts:n}=await this.#makeRequest(t);return n}async createDNSRecord(e,t){let n=`/dns/create/${e}`,{id:r}=await this.#makeRequest(n,t);return r}async editDNSRecord(e,t,n){let r=`/dns/edit/${e}/${t}`;await this.#makeRequest(r,n)}async editDNSRecordsByType(e,t,n=``,r){let i=n?`/dns/editByNameType/${e}/${t}/${n}`:`/dns/editByNameType/${e}/${t}`;await this.#makeRequest(i,r)}async deleteDNSRecord(e,t){let n=`/dns/delete/${e}/${t}`;await this.#makeRequest(n)}async deleteDNSRecordsByType(e,t,n=``){let r=n?`/dns/deleteByNameType/${e}/${t}/${n}`:`/dns/deleteByNameType/${e}/${t}`;await this.#makeRequest(r)}async getDNSRecords(e,t){let n=t?`/dns/retrieve/${e}/${t}`:`/dns/retrieve/${e}`,{records:r}=await this.#makeRequest(n);return r}async getDNSRecordsByType(e,t,n=``){let r=n?`/dns/retrieveByNameType/${e}/${t}/${n}`:`/dns/retrieveByNameType/${e}/${t}`,{records:i}=await this.#makeRequest(r);return i}async createDNSSECRecord(e,t){let n=`/dns/createDnssecRecord/${e}`;await this.#makeRequest(n,t)}async getDNSSECRecords(e){let t=`/dns/getDnssecRecords/${e}`,{records:n}=await this.#makeRequest(t);return n}async deleteDNSSECRecord(e,t){let n=`/dns/deleteDnssecRecord/${e}/${t}`;await this.#makeRequest(n)}getSSLBundle(e){let t=`/ssl/retrieve/${e}`;return this.#makeRequest(t)}};export{t as PorkbunAuthError,r as PorkbunClient,e as PorkbunError,n as PorkbunRateLimitError};