onesecmail
Version:
Create and receive email in only 1 second.
111 lines (110 loc) • 4.54 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _OneSecMailAPI_got;
import { got } from "got";
import { z } from "zod";
import { shortMessageSchema, messageSchema } from "./schemas.js";
export const BASE_API_URL = "https://www.1secmail.com/api/v1/";
export const FORBIDDEN_LOGIN = [
"abuse",
"admin",
"contact",
"hostmaster",
"postmaster",
"webmaster",
];
export default class OneSecMailAPI {
constructor(options) {
_OneSecMailAPI_got.set(this, void 0);
__classPrivateFieldSet(this, _OneSecMailAPI_got, got.extend({ prefixUrl: BASE_API_URL }), "f");
this.retry = options?.retry ?? 2;
this.timeout = options?.timeout ?? 10000;
}
async request(searchParams, options) {
try {
return __classPrivateFieldGet(this, _OneSecMailAPI_got, "f").call(this, {
searchParams,
retry: {
limit: options?.retry ?? this.retry,
},
timeout: {
request: options?.timeout ?? this.timeout,
},
});
}
catch (e) {
throw new Error("HTTP request failed");
}
}
async genRandomMailbox(a, b) {
let count = 1;
let options = {};
if (typeof a === "number" && typeof b === "undefined") {
count = a;
}
else if (typeof a === "object" && typeof b === "undefined") {
options = a;
}
else if (typeof a === "number" && typeof b === "object") {
count = a;
options = b;
}
if (count < 1 || count > 500)
throw new RangeError("`count` must be between 1 and 500");
const { body } = await this.request({ action: "genRandomMailbox", count }, options);
const schema = z.array(z.string().email()).length(count);
try {
return schema.parse(JSON.parse(body));
}
catch (e) {
throw new Error("Malformed response");
}
}
async getDomainList(options) {
const { body } = await this.request({ action: "getDomainList" }, options);
const schema = z.array(z.string().regex(/([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}/)).nonempty();
try {
return schema.parse(JSON.parse(body));
}
catch (e) {
throw new Error("Malformed response");
}
}
async getMessages(login, domain, options) {
const { body } = await this.request({ action: "getMessages", login, domain }, options);
const schema = z.array(shortMessageSchema);
try {
return schema.parse(JSON.parse(body));
}
catch (e) {
throw new Error("Malformed response");
}
}
async readMessage(login, domain, id, options) {
const { body } = await this.request({ action: "readMessage", login, domain, id }, options);
if (body === "Messagenotfound")
return null;
try {
return messageSchema.parse(JSON.parse(body));
}
catch (e) {
throw new Error("Malformed response");
}
}
async download(login, domain, id, file, options) {
const { body, headers } = await this.request({ action: "download", login, domain, id, file }, options);
if (headers["content-disposition"] !== `attachment; filename="${file}"`)
return null;
return Buffer.from(body);
}
}
_OneSecMailAPI_got = new WeakMap();