Back to Home
Gistify Snippet
kyiov
3d ago·1 file(s)
Public

Klien API Email Sementara temporarymail.com

Mengimplementasikan klien API asinkron untuk temporarymail.com, memfasilitasi pembuatan alamat email acak, pengecekan kotak masuk, dan ekstraksi konten pesan email secara real-time tanpa autentikasi kompleks.

#temporary-email#disposable-email#email-api#nodejs#fetch#inbox
0
Raw
tempmail.jsjavascript
118 lines
1/**
2 * @fileoverview Modul Layanan Temporary Email (Disposable Email Inbox).
3 *
4 * Mengimplementasikan klien API asinkron untuk penyedia email sementara (temporarymail.com).
5 * Memfasilitasi pembuatan alamat email acak, pengecekan berkala kotak masuk (inbox polling),
6 * serta ekstraksi konten pesan email secara real-time tanpa memerlukan autentikasi kompleks.
7 *
8 * @module services/tempmail
9 * @version 1.0.0
10 * @author kyiov
11 * @license MIT
12 */
13
14import { pathToFileURL } from 'node:url'
15
16/**
17 * @typedef {Object} NewEmailResponse
18 * @property {string} address - Alamat email sementara yang dibuat
19 * @property {string} secretKey - Kunci otorisasi sesi untuk memeriksa kotak masuk
20 */
21
22/**
23 * @typedef {Object} EmailMessage
24 * @property {string} id - Pengenal unik email
25 * @property {string} from - Pengirim email
26 * @property {string} subject - Judul subjek email
27 * @property {string} date - Waktu pengiriman
28 */
29
30/**
31 * Kelas klien pengelola email sementara (TempMail).
32 */
33export class TempMail {
34 /**
35 * Menginisialisasi instance TempMail dengan konfigurasi header HTTP standar browser.
36 */
37 constructor() {
38 this.baseHeaders = {
39 'User-Agent':
40 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
41 'Accept': '*/*',
42 'x-requested-with': 'XMLHttpRequest',
43 'referer': 'https://temporarymail.com/',
44 'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7'
45 }
46 }
47
48 /**
49 * Membuat alamat email sementara baru secara acak.
50 *
51 * @returns {Promise<NewEmailResponse>} Objek berisi alamat email dan secretKey sesi
52 * @throws {Error} Jika server merespons dengan status HTTP gagal
53 */
54 async getNewEmail() {
55 const url = 'https://temporarymail.com/api/?action=requestEmailAccess&key=&value=random&r=https://www.google.com/'
56 const res = await fetch(url, { headers: this.baseHeaders })
57 if (!res.ok) throw new Error(`HTTP Error: ${res.status}`)
58 return await res.json()
59 }
60
61 /**
62 * Memeriksa daftar pesan masuk dalam inbox berdasarkan secretKey.
63 *
64 * @param {string} secretKey - Kunci rahasia sesi email yang diperoleh dari getNewEmail
65 * @returns {Promise<EmailMessage[]>} Daftar pesan email dalam kotak masuk
66 * @throws {Error} Jika secretKey kosong atau permintaan HTTP gagal
67 */
68 async checkInbox(secretKey) {
69 if (!secretKey) throw new Error('Secret Key wajib disertakan.')
70 const url = `https://temporarymail.com/api/?action=checkInbox&value=${encodeURIComponent(secretKey)}`
71 const res = await fetch(url, { headers: this.baseHeaders })
72 if (!res.ok) throw new Error(`HTTP Error: ${res.status}`)
73 const data = await res.json()
74 return Object.values(data)
75 }
76
77 /**
78 * Mengambil detail dan isi lengkap dari suatu pesan email berdasarkan ID.
79 *
80 * @param {string} emailId - ID email yang ingin dibaca
81 * @returns {Promise<Object>} Konten pesan lengkap (HTML / teks / lampiran)
82 * @throws {Error} Jika emailId kosong atau permintaan HTTP gagal
83 */
84 async getEmailContent(emailId) {
85 if (!emailId) throw new Error('Email ID wajib disertakan.')
86 const url = `https://temporarymail.com/api/?action=getEmail&value=${encodeURIComponent(emailId)}`
87 const res = await fetch(url, {
88 method: 'POST',
89 headers: {
90 ...this.baseHeaders,
91 origin: 'https://temporarymail.com',
92 'content-length': '0'
93 }
94 })
95 if (!res.ok) throw new Error(`HTTP Error: ${res.status}`)
96 const data = await res.json()
97 return data[emailId]
98 }
99}
100
101export default TempMail
102
103// Eksekusi mandiri untuk pengujian langsung tanpa argumen CLI
104if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
105 try {
106 const client = new TempMail()
107 console.log('Menguji pembuatan email sementara...')
108 const account = await client.getNewEmail()
109 console.log('Hasil Pembuatan Akun:', account)
110
111 console.log('Memeriksa inbox awal...')
112 const inbox = await client.checkInbox(account.secretKey)
113 console.log('Jumlah email di kotak masuk:', inbox.length)
114 } catch (err) {
115 console.error('Galat eksekusi TempMail:', err.message || err)
116 }
117}
118
Discussion & Comments0

No comments yet — be the first to leave feedback or start a code discussion.

Sign in with your GitHub account to write comments and join the discussion.