ReOTP.
डेवलपर पैकेजेस/Node.js के लिए ReOTP लाइब्रेरी
1 min read

Node.js के लिए ReOTP लाइब्रेरी

Node.js/Next.js अनुप्रयोगों में ReOTP के उपयोग को आसान बनाने के लिए तैयार लाइब्रेरी।

HTTP अनुरोधों को मैन्युअल रूप से लिखने के बजाय, हमारे SDK कोड का उपयोग करके ReOTP को सेकंडों में एकीकृत करें।

यह लाइब्रेरी एक तैयार Wrapper है जिसे आप सीधे अपनी परियोजना में reotp.ts या reotp.js फ़ाइल के रूप में कॉपी कर सकते हैं और इसका उपयोग कर सकते हैं।

मुख्य विशेषताएँ

  • स्वचालित Init संदेश भेजना और त्रुटि संभालना।
  • सतत स्थिति जाँच (Polling) के लिए एक फ़ंक्शन जैसे Polling का समर्थन।
  • मज़बूत Typescript प्रकार प्रदान करना।
Integration Code (SDKs)
export class ReOTPClient {
  private apiKey: string;
  private baseUrl: string;

  constructor(apiKey: string, baseUrl: string = "https://your-domain.com") {
    this.apiKey = apiKey;
    this.baseUrl = baseUrl;
  }

  async verify(phoneNumber: string, method: "WHATSAPP" | "TELEGRAM" = "WHATSAPP") {
    const res = await fetch(`${this.baseUrl}/api/verify/init`, {
      method: "POST",
      headers: { 
        "Content-Type": "application/json",
        "Authorization": `Bearer ${this.apiKey}`
      },
      body: JSON.stringify({ phoneNumber, method })
    });
    
    if (!res.ok) throw new Error(await res.text());
    return await res.json(); // { requestId, waLink, tgLink, expectedText }
  }

  async waitForVerification(requestId: string, timeoutMs: number = 120000): Promise<boolean> {
    const startTime = Date.now();
    
    return new Promise((resolve, reject) => {
      const interval = setInterval(async () => {
        if (Date.now() - startTime > timeoutMs) {
          clearInterval(interval);
          resolve(false);
        }

        try {
          const res = await fetch(`${this.baseUrl}/api/verify/status?id=${requestId}`);
          const data = await res.json();
          
          if (data.status === "VERIFIED") {
            clearInterval(interval);
            resolve(true);
          } else if (data.status === "EXPIRED") {
            clearInterval(interval);
            reject(new Error("Session expired"));
          } else if (data.status === "REJECTED") {
            clearInterval(interval);
            reject(new Error(data.failureReason || "Session rejected"));
          }
        } catch (e) {
          // Keep polling
        }
      }, 3000);
    });
  }
}