UNPKG

onesecmail

Version:

Create and receive email in only 1 second.

100 lines (99 loc) 3.3 kB
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 { #got; retry; timeout; constructor(options) { this.#got = got.extend({ prefixUrl: BASE_API_URL }); this.retry = options?.retry ?? 2; this.timeout = options?.timeout ?? 10000; } async request(searchParams, options) { try { return await this.#got({ 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); } }