ReOTP.
개발자 패키지/ReOTP Node.js 라이브러리
1 분 분량

ReOTP Node.js 라이브러리

Node.js/Next.js 애플리케이션에서 ReOTP를 쉽게 사용할 수 있는 준비된 라이브러리.

수동으로 HTTP 요청을 작성하는 대신, 간단한 SDK 코드를 사용하여 ReOTP를 몇 초 안에 통합할 수 있습니다.

이 라이브러리는 프로젝트에 바로 복사하여 reotp.ts 또는 reotp.js 파일로 사용할 수 있는 준비된 Wrapper입니다.

기능

  • 자동으로 Init 전송 및 오류 처리.
  • 서버-클라이언트 이벤트(SSE) 통합 상태 확인 기능.
  • 강력한 TypeScript 타입 제공.
연동 코드 (SDK)
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> {
    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(false);
      }, timeoutMs);

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

      evtSource.onerror = () => {
        // Fallback or retry logic can be added here
      };
    });
  }
}