UNPKG

mailtm-proxy

Version:

TypeScript library for interacting with temporary email services like mail.tm and mail.gw with proxy support

3 lines (2 loc) 2.82 kB
import t from"axios";import{HttpsProxyAgent as e}from"https-proxy-agent";function r(t,e,r,n){return new(r||(r=Promise))((function(i,o){function s(t){try{d(n.next(t))}catch(t){o(t)}}function a(t){try{d(n.throw(t))}catch(t){o(t)}}function d(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(s,a)}d((n=n.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const n=({provider:r="mail.tm",proxy:n})=>{const i=n?new e(n):void 0,o=t.create({baseURL:"mail.tm"===r?"https://api.mail.tm":"https://api.mail.gw",httpsAgent:i});return o.interceptors.response.use((t=>t),(t=>{if(t.response){if(t.response.status>=400&&t.response.status<500)return Promise.resolve(t.response);const e=t.response.data;throw new Error(JSON.stringify(e))}return Promise.reject(t)})),o},i=(t=15)=>{const e="abcdefghijklmnopqrstuvwxyz0123456789";let r="";for(let n=0;n<t;n++){r+=e[Math.floor(36*Math.random())]}return r};class o{constructor({provider:t,proxy:e}={}){try{this.api=n({provider:t,proxy:e})}catch(t){this.api=n({})}}getDomains(){return r(this,void 0,void 0,(function*(){var t;try{return(null===(t=(yield this.api.get("/domains")).data["hydra:member"])||void 0===t?void 0:t.map((t=>t.domain)))||[]}catch(t){return[]}}))}createRandomEmail(){return r(this,arguments,void 0,(function*({emailLength:t=15,emailPrefix:e,passwordLength:r=7}={}){const n={email:void 0,id:void 0,password:void 0};try{const o=yield this.getDomains();if(0===o.length)return n;const s=o[Math.floor(Math.random()*o.length)],a=i(e?Math.max(t-e.length,0):t),d=e?`${e}${a}@${s}`:`${a}@${s}`,u=i(r),c=yield this.api.post("/accounts",{address:d,password:u});return c.data&&c.data.id?{id:c.data.id,email:d,password:u}:n}catch(t){return n}}))}login(t){return r(this,arguments,void 0,(function*({email:t,password:e}){try{const r=yield this.api.post("/token",{address:t,password:e}),{token:n}=r.data;return n?(this.setToken(n),{token:n}):{token:""}}catch(t){return{token:""}}}))}getMessages(){return r(this,void 0,void 0,(function*(){try{return(yield this.api.get("/messages")).data["hydra:member"]||[]}catch(t){return[]}}))}getMessage(t){return r(this,void 0,void 0,(function*(){try{if(!t)return null;return(yield this.api.get(`/messages/${t}`)).data}catch(t){return null}}))}deleteMessage(t){return r(this,void 0,void 0,(function*(){try{return!!t&&(yield this.api.delete(`/messages/${t}`),!0)}catch(t){return!1}}))}deleteAccount(t){return r(this,void 0,void 0,(function*(){try{return!!t&&(yield this.api.delete(`/accounts/${t}`),!0)}catch(t){return!1}}))}me(){return r(this,void 0,void 0,(function*(){try{return(yield this.api.get("/me")).data}catch(t){return null}}))}setToken(t){t&&(this.api.defaults.headers.common.Authorization=`Bearer ${t}`)}}export{o as CustomMailjs,o as default}; //# sourceMappingURL=index.js.map