ReOTP.
डेवलपर पैकेज/ReOTP लाइब्रेरी Node.js के लिए
1 पढ़ने का समय

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

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

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

विशेषताएँ

  • आरंभ भेजना स्वचालित रूप से और त्रुटियों के साथ संभालना।
  • एक फ़ंक्शन प्रदान करना जो SSE (Server-Sent Events) (निरंतर स्थिति जाँच) के लिए एम्बेडेड है।
  • शक्तिशाली TypeScript प्रकार प्रदान करना।
एकीकरण कोड (SDKs)
export class ReOTPClient {
  private apiKey: string;
  private baseUrl: string;

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

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

  async waitForVerification(requestId: string, timeoutMs: number = 120000): Promise<{ verified: boolean; phoneNumber?: string }> {
    return new Promise((resolve, reject) => {
      const EventSource = require('eventsource');
      const evtSource = new EventSource(`${this.baseUrl}/api/verify/stream?id=${requestId}`);

      const timeout = setTimeout(() => {
        evtSource.close();
        resolve({ verified: false });
      }, timeoutMs);

      evtSource.onmessage = (event: any) => {
        const data = JSON.parse(event.data);
        if (data.status === "VERIFIED") {
          clearTimeout(timeout);
          evtSource.close();
          resolve({ verified: true, phoneNumber: data.phoneNumber });
        } else if (data.status === "EXPIRED") {
          clearTimeout(timeout);
          evtSource.close();
          reject(new Error("Session expired"));
        }
      };

      evtSource.onerror = () => {
        // Fallback or retry
      };
    });
  }
}