@smart-pay-chain/otp 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +581 -0
- package/dist/errors.d.ts +101 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +161 -0
- package/dist/http-client.d.ts +33 -0
- package/dist/http-client.d.ts.map +1 -0
- package/dist/http-client.js +136 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/otp-client.d.ts +184 -0
- package/dist/otp-client.d.ts.map +1 -0
- package/dist/otp-client.js +314 -0
- package/dist/types.d.ts +275 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +72 -0
- package/package.json +69 -0
package/dist/errors.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiKeyRevokedError = exports.IdempotencyConflictError = exports.BrandPendingApprovalError = exports.BrandNotConfiguredError = exports.InsufficientBalanceError = exports.ServiceUnavailableError = exports.InvalidOtpError = exports.OtpExpiredError = exports.OtpNotFoundError = exports.RateLimitError = exports.ValidationError = exports.AuthenticationError = exports.OtpError = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
/**
|
|
6
|
+
* Base error class for all OTP SDK errors
|
|
7
|
+
*/
|
|
8
|
+
class OtpError extends Error {
|
|
9
|
+
constructor(message, code, statusCode, retryable, details, requestId) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'OtpError';
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
this.retryable = retryable;
|
|
15
|
+
this.details = details;
|
|
16
|
+
this.requestId = requestId;
|
|
17
|
+
// Maintains proper stack trace for where our error was thrown
|
|
18
|
+
Error.captureStackTrace(this, this.constructor);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create an OtpError from an API error response
|
|
22
|
+
*/
|
|
23
|
+
static fromApiError(response) {
|
|
24
|
+
return new OtpError(response.error.message, response.error.code, response.error.statusCode, response.error.retryable, response.error.details, response.meta.requestId);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Convert error to a plain object
|
|
28
|
+
*/
|
|
29
|
+
toJSON() {
|
|
30
|
+
return {
|
|
31
|
+
name: this.name,
|
|
32
|
+
message: this.message,
|
|
33
|
+
code: this.code,
|
|
34
|
+
statusCode: this.statusCode,
|
|
35
|
+
retryable: this.retryable,
|
|
36
|
+
details: this.details,
|
|
37
|
+
requestId: this.requestId,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.OtpError = OtpError;
|
|
42
|
+
/**
|
|
43
|
+
* Error thrown when authentication fails
|
|
44
|
+
*/
|
|
45
|
+
class AuthenticationError extends OtpError {
|
|
46
|
+
constructor(message, requestId) {
|
|
47
|
+
super(message, types_1.ErrorCode.AUTHENTICATION_FAILED, 401, false, undefined, requestId);
|
|
48
|
+
this.name = 'AuthenticationError';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.AuthenticationError = AuthenticationError;
|
|
52
|
+
/**
|
|
53
|
+
* Error thrown when validation fails
|
|
54
|
+
*/
|
|
55
|
+
class ValidationError extends OtpError {
|
|
56
|
+
constructor(message, details, requestId) {
|
|
57
|
+
super(message, types_1.ErrorCode.VALIDATION_ERROR, 400, false, details, requestId);
|
|
58
|
+
this.name = 'ValidationError';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.ValidationError = ValidationError;
|
|
62
|
+
/**
|
|
63
|
+
* Error thrown when rate limit is exceeded
|
|
64
|
+
*/
|
|
65
|
+
class RateLimitError extends OtpError {
|
|
66
|
+
constructor(message, requestId) {
|
|
67
|
+
super(message, types_1.ErrorCode.RATE_LIMIT_EXCEEDED, 429, true, undefined, requestId);
|
|
68
|
+
this.name = 'RateLimitError';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.RateLimitError = RateLimitError;
|
|
72
|
+
/**
|
|
73
|
+
* Error thrown when OTP is not found
|
|
74
|
+
*/
|
|
75
|
+
class OtpNotFoundError extends OtpError {
|
|
76
|
+
constructor(message, requestId) {
|
|
77
|
+
super(message, types_1.ErrorCode.OTP_NOT_FOUND, 404, false, undefined, requestId);
|
|
78
|
+
this.name = 'OtpNotFoundError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.OtpNotFoundError = OtpNotFoundError;
|
|
82
|
+
/**
|
|
83
|
+
* Error thrown when OTP has expired
|
|
84
|
+
*/
|
|
85
|
+
class OtpExpiredError extends OtpError {
|
|
86
|
+
constructor(message, requestId) {
|
|
87
|
+
super(message, types_1.ErrorCode.OTP_EXPIRED, 400, false, undefined, requestId);
|
|
88
|
+
this.name = 'OtpExpiredError';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.OtpExpiredError = OtpExpiredError;
|
|
92
|
+
/**
|
|
93
|
+
* Error thrown when OTP code is invalid
|
|
94
|
+
*/
|
|
95
|
+
class InvalidOtpError extends OtpError {
|
|
96
|
+
constructor(message, requestId) {
|
|
97
|
+
super(message, types_1.ErrorCode.INVALID_OTP_CODE, 400, false, undefined, requestId);
|
|
98
|
+
this.name = 'InvalidOtpError';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.InvalidOtpError = InvalidOtpError;
|
|
102
|
+
/**
|
|
103
|
+
* Error thrown when service is unavailable
|
|
104
|
+
*/
|
|
105
|
+
class ServiceUnavailableError extends OtpError {
|
|
106
|
+
constructor(message, requestId) {
|
|
107
|
+
super(message, types_1.ErrorCode.SERVICE_UNAVAILABLE, 503, true, undefined, requestId);
|
|
108
|
+
this.name = 'ServiceUnavailableError';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.ServiceUnavailableError = ServiceUnavailableError;
|
|
112
|
+
/**
|
|
113
|
+
* Error thrown when account has insufficient balance
|
|
114
|
+
*/
|
|
115
|
+
class InsufficientBalanceError extends OtpError {
|
|
116
|
+
constructor(message, requestId) {
|
|
117
|
+
super(message, types_1.ErrorCode.INSUFFICIENT_BALANCE, 402, false, undefined, requestId);
|
|
118
|
+
this.name = 'InsufficientBalanceError';
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.InsufficientBalanceError = InsufficientBalanceError;
|
|
122
|
+
/**
|
|
123
|
+
* Error thrown when brand is not configured (Georgian numbers)
|
|
124
|
+
*/
|
|
125
|
+
class BrandNotConfiguredError extends OtpError {
|
|
126
|
+
constructor(message, requestId) {
|
|
127
|
+
super(message, types_1.ErrorCode.NO_BRAND_CONFIGURED, 400, false, undefined, requestId);
|
|
128
|
+
this.name = 'BrandNotConfiguredError';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.BrandNotConfiguredError = BrandNotConfiguredError;
|
|
132
|
+
/**
|
|
133
|
+
* Error thrown when brand is pending approval (Georgian numbers)
|
|
134
|
+
*/
|
|
135
|
+
class BrandPendingApprovalError extends OtpError {
|
|
136
|
+
constructor(message, requestId) {
|
|
137
|
+
super(message, types_1.ErrorCode.BRAND_PENDING_APPROVAL, 403, false, undefined, requestId);
|
|
138
|
+
this.name = 'BrandPendingApprovalError';
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
exports.BrandPendingApprovalError = BrandPendingApprovalError;
|
|
142
|
+
/**
|
|
143
|
+
* Error thrown when idempotency key conflict occurs
|
|
144
|
+
*/
|
|
145
|
+
class IdempotencyConflictError extends OtpError {
|
|
146
|
+
constructor(message, requestId) {
|
|
147
|
+
super(message, types_1.ErrorCode.IDEMPOTENCY_KEY_CONFLICT, 409, false, undefined, requestId);
|
|
148
|
+
this.name = 'IdempotencyConflictError';
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.IdempotencyConflictError = IdempotencyConflictError;
|
|
152
|
+
/**
|
|
153
|
+
* Error thrown when API key has been revoked
|
|
154
|
+
*/
|
|
155
|
+
class ApiKeyRevokedError extends OtpError {
|
|
156
|
+
constructor(message, requestId) {
|
|
157
|
+
super(message, types_1.ErrorCode.API_KEY_REVOKED, 401, false, undefined, requestId);
|
|
158
|
+
this.name = 'ApiKeyRevokedError';
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.ApiKeyRevokedError = ApiKeyRevokedError;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { OtpClientConfig } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* HTTP client for making requests to the OTP API
|
|
5
|
+
*/
|
|
6
|
+
export declare class HttpClient {
|
|
7
|
+
private client;
|
|
8
|
+
private maxRetries;
|
|
9
|
+
private platform;
|
|
10
|
+
private language;
|
|
11
|
+
constructor(config: OtpClientConfig);
|
|
12
|
+
/**
|
|
13
|
+
* Make a GET request
|
|
14
|
+
*/
|
|
15
|
+
get<T>(path: string, config?: AxiosRequestConfig): Promise<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Make a POST request with retry logic
|
|
18
|
+
*/
|
|
19
|
+
post<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Make a PUT request
|
|
22
|
+
*/
|
|
23
|
+
put<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Make a DELETE request
|
|
26
|
+
*/
|
|
27
|
+
delete<T>(path: string, config?: AxiosRequestConfig): Promise<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Helper method to delay execution
|
|
30
|
+
*/
|
|
31
|
+
private delay;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=http-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-client.d.ts","sourceRoot":"","sources":["../src/http-client.ts"],"names":[],"mappings":"AAAA,OAAc,EAA6B,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAkE,MAAM,SAAS,CAAC;AAmD1G;;GAEG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,QAAQ,CAAc;IAC9B,OAAO,CAAC,QAAQ,CAAc;gBAElB,MAAM,EAAE,eAAe;IA+BnC;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;IAKnE;;OAEG;IACG,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;IA0BhF;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;IAK/E;;OAEG;IACG,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,CAAC,CAAC;IAKtE;;OAEG;IACH,OAAO,CAAC,KAAK;CAGd"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HttpClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const errors_1 = require("./errors");
|
|
9
|
+
// SDK version - update this with each release
|
|
10
|
+
const SDK_VERSION = '2.0.0';
|
|
11
|
+
/**
|
|
12
|
+
* Detect the current platform
|
|
13
|
+
*/
|
|
14
|
+
function detectPlatform() {
|
|
15
|
+
try {
|
|
16
|
+
// Check for React Native
|
|
17
|
+
// @ts-ignore - navigator may not exist in Node.js
|
|
18
|
+
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
19
|
+
return 'react-native';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// navigator not available
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
// Check for Node.js
|
|
27
|
+
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
|
|
28
|
+
return 'node';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// process not available
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
// Check for browser
|
|
36
|
+
// @ts-ignore - window/document may not exist in Node.js
|
|
37
|
+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
38
|
+
return 'browser';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// window/document not available
|
|
43
|
+
}
|
|
44
|
+
return 'unknown';
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Detect the current language
|
|
48
|
+
*/
|
|
49
|
+
function detectLanguage() {
|
|
50
|
+
// TypeScript files will have TS in the stack trace in dev mode
|
|
51
|
+
// In production, both are JavaScript
|
|
52
|
+
return 'typescript'; // Default to TypeScript since SDK is TS-first
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* HTTP client for making requests to the OTP API
|
|
56
|
+
*/
|
|
57
|
+
class HttpClient {
|
|
58
|
+
constructor(config) {
|
|
59
|
+
this.maxRetries = config.maxRetries || 3;
|
|
60
|
+
this.platform = config.platform || detectPlatform();
|
|
61
|
+
this.language = config.language || detectLanguage();
|
|
62
|
+
this.client = axios_1.default.create({
|
|
63
|
+
baseURL: config.baseUrl || 'https://otp.smartpaychain.com',
|
|
64
|
+
timeout: config.timeout || 30000,
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
'X-API-Key': config.apiKey,
|
|
68
|
+
'X-OTP-SDK-Version': SDK_VERSION,
|
|
69
|
+
'X-OTP-SDK-Platform': this.platform,
|
|
70
|
+
'X-OTP-SDK-Language': this.language,
|
|
71
|
+
...config.headers,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
// Response interceptor for error handling
|
|
75
|
+
this.client.interceptors.response.use((response) => response, (error) => {
|
|
76
|
+
if (error.response?.data) {
|
|
77
|
+
const apiError = error.response.data;
|
|
78
|
+
throw errors_1.OtpError.fromApiError(apiError);
|
|
79
|
+
}
|
|
80
|
+
throw error;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Make a GET request
|
|
85
|
+
*/
|
|
86
|
+
async get(path, config) {
|
|
87
|
+
const response = await this.client.get(path, config);
|
|
88
|
+
return response.data.data;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Make a POST request with retry logic
|
|
92
|
+
*/
|
|
93
|
+
async post(path, data, config) {
|
|
94
|
+
let lastError;
|
|
95
|
+
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
|
|
96
|
+
try {
|
|
97
|
+
const response = await this.client.post(path, data, config);
|
|
98
|
+
return response.data.data;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
lastError = error;
|
|
102
|
+
// Only retry if the error is retryable and we have attempts left
|
|
103
|
+
if (error instanceof errors_1.OtpError && error.retryable && attempt < this.maxRetries) {
|
|
104
|
+
// Exponential backoff: 1s, 2s, 4s
|
|
105
|
+
const delayMs = Math.pow(2, attempt - 1) * 1000;
|
|
106
|
+
await this.delay(delayMs);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
// Non-retryable error or max retries reached
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw lastError;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Make a PUT request
|
|
117
|
+
*/
|
|
118
|
+
async put(path, data, config) {
|
|
119
|
+
const response = await this.client.put(path, data, config);
|
|
120
|
+
return response.data.data;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Make a DELETE request
|
|
124
|
+
*/
|
|
125
|
+
async delete(path, config) {
|
|
126
|
+
const response = await this.client.delete(path, config);
|
|
127
|
+
return response.data.data;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Helper method to delay execution
|
|
131
|
+
*/
|
|
132
|
+
delay(ms) {
|
|
133
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.HttpClient = HttpClient;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @smartpaychain/otp-sdk
|
|
3
|
+
*
|
|
4
|
+
* Official TypeScript SDK for Smart Pay Chain OTP Verification Service
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
export { OtpClient } from './otp-client';
|
|
9
|
+
export { HttpClient } from './http-client';
|
|
10
|
+
export { OtpError, AuthenticationError, ValidationError, RateLimitError, OtpNotFoundError, OtpExpiredError, InvalidOtpError, ServiceUnavailableError, InsufficientBalanceError, BrandNotConfiguredError, BrandPendingApprovalError, IdempotencyConflictError, ApiKeyRevokedError, } from './errors';
|
|
11
|
+
export { OtpChannel, ErrorCode, OtpClientConfig, SendOtpOptions, SendOtpResponse, VerifyOtpOptions, VerifyOtpResponse, ResendOtpOptions, ApiErrorResponse, ApiSuccessResponse, OtpStatus, OtpStatusWithCode, SdkConfiguration, SdkPlatform, SdkLanguage, TEST_PHONE_NUMBERS, TEST_OTP_CODE, } from './types';
|
|
12
|
+
export { OtpClient as default } from './otp-client';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EACL,QAAQ,EACR,mBAAmB,EACnB,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,uBAAuB,EACvB,wBAAwB,EACxB,uBAAuB,EACvB,yBAAyB,EACzB,wBAAwB,EACxB,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,UAAU,EACV,SAAS,EACT,eAAe,EACf,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,SAAS,EACT,iBAAiB,EACjB,gBAAgB,EAChB,WAAW,EACX,WAAW,EACX,kBAAkB,EAClB,aAAa,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,SAAS,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @smartpaychain/otp-sdk
|
|
4
|
+
*
|
|
5
|
+
* Official TypeScript SDK for Smart Pay Chain OTP Verification Service
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.default = exports.TEST_OTP_CODE = exports.TEST_PHONE_NUMBERS = exports.ErrorCode = exports.OtpChannel = exports.ApiKeyRevokedError = exports.IdempotencyConflictError = exports.BrandPendingApprovalError = exports.BrandNotConfiguredError = exports.InsufficientBalanceError = exports.ServiceUnavailableError = exports.InvalidOtpError = exports.OtpExpiredError = exports.OtpNotFoundError = exports.RateLimitError = exports.ValidationError = exports.AuthenticationError = exports.OtpError = exports.HttpClient = exports.OtpClient = void 0;
|
|
11
|
+
var otp_client_1 = require("./otp-client");
|
|
12
|
+
Object.defineProperty(exports, "OtpClient", { enumerable: true, get: function () { return otp_client_1.OtpClient; } });
|
|
13
|
+
var http_client_1 = require("./http-client");
|
|
14
|
+
Object.defineProperty(exports, "HttpClient", { enumerable: true, get: function () { return http_client_1.HttpClient; } });
|
|
15
|
+
var errors_1 = require("./errors");
|
|
16
|
+
Object.defineProperty(exports, "OtpError", { enumerable: true, get: function () { return errors_1.OtpError; } });
|
|
17
|
+
Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthenticationError; } });
|
|
18
|
+
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
|
|
19
|
+
Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
|
|
20
|
+
Object.defineProperty(exports, "OtpNotFoundError", { enumerable: true, get: function () { return errors_1.OtpNotFoundError; } });
|
|
21
|
+
Object.defineProperty(exports, "OtpExpiredError", { enumerable: true, get: function () { return errors_1.OtpExpiredError; } });
|
|
22
|
+
Object.defineProperty(exports, "InvalidOtpError", { enumerable: true, get: function () { return errors_1.InvalidOtpError; } });
|
|
23
|
+
Object.defineProperty(exports, "ServiceUnavailableError", { enumerable: true, get: function () { return errors_1.ServiceUnavailableError; } });
|
|
24
|
+
Object.defineProperty(exports, "InsufficientBalanceError", { enumerable: true, get: function () { return errors_1.InsufficientBalanceError; } });
|
|
25
|
+
Object.defineProperty(exports, "BrandNotConfiguredError", { enumerable: true, get: function () { return errors_1.BrandNotConfiguredError; } });
|
|
26
|
+
Object.defineProperty(exports, "BrandPendingApprovalError", { enumerable: true, get: function () { return errors_1.BrandPendingApprovalError; } });
|
|
27
|
+
Object.defineProperty(exports, "IdempotencyConflictError", { enumerable: true, get: function () { return errors_1.IdempotencyConflictError; } });
|
|
28
|
+
Object.defineProperty(exports, "ApiKeyRevokedError", { enumerable: true, get: function () { return errors_1.ApiKeyRevokedError; } });
|
|
29
|
+
var types_1 = require("./types");
|
|
30
|
+
Object.defineProperty(exports, "OtpChannel", { enumerable: true, get: function () { return types_1.OtpChannel; } });
|
|
31
|
+
Object.defineProperty(exports, "ErrorCode", { enumerable: true, get: function () { return types_1.ErrorCode; } });
|
|
32
|
+
Object.defineProperty(exports, "TEST_PHONE_NUMBERS", { enumerable: true, get: function () { return types_1.TEST_PHONE_NUMBERS; } });
|
|
33
|
+
Object.defineProperty(exports, "TEST_OTP_CODE", { enumerable: true, get: function () { return types_1.TEST_OTP_CODE; } });
|
|
34
|
+
// Default export
|
|
35
|
+
var otp_client_2 = require("./otp-client");
|
|
36
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return otp_client_2.OtpClient; } });
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { OtpClientConfig, SendOtpOptions, SendOtpResponse, VerifyOtpOptions, VerifyOtpResponse, ResendOtpOptions, OtpStatus, OtpStatusWithCode, SdkConfiguration } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Main OTP client for interacting with the OTP verification service
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const client = new OtpClient({ apiKey: 'your-api-key' });
|
|
8
|
+
*
|
|
9
|
+
* // Send an OTP
|
|
10
|
+
* const result = await client.sendOtp({
|
|
11
|
+
* phoneNumber: '+995555123456',
|
|
12
|
+
* channel: OtpChannel.SMS,
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* // Verify the OTP
|
|
16
|
+
* const verification = await client.verifyOtp({
|
|
17
|
+
* requestId: result.requestId,
|
|
18
|
+
* code: '123456',
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare class OtpClient {
|
|
23
|
+
private http;
|
|
24
|
+
private serverConfig;
|
|
25
|
+
private serverConfigFetchedAt;
|
|
26
|
+
private readonly CONFIG_CACHE_TTL;
|
|
27
|
+
/**
|
|
28
|
+
* Create a new OTP client
|
|
29
|
+
*
|
|
30
|
+
* @param config - Configuration options
|
|
31
|
+
*/
|
|
32
|
+
constructor(config: OtpClientConfig);
|
|
33
|
+
/**
|
|
34
|
+
* Send an OTP to a phone number
|
|
35
|
+
*
|
|
36
|
+
* @param options - Options for sending the OTP
|
|
37
|
+
* @returns Promise resolving to the OTP request details
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const result = await client.sendOtp({
|
|
42
|
+
* phoneNumber: '+995555123456',
|
|
43
|
+
* channel: OtpChannel.SMS,
|
|
44
|
+
* ttl: 300,
|
|
45
|
+
* length: 6,
|
|
46
|
+
* metadata: { userId: '12345' }
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log(result.requestId); // Save this for verification
|
|
50
|
+
* console.log(result.expiresAt);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
sendOtp(options: SendOtpOptions): Promise<SendOtpResponse>;
|
|
54
|
+
/**
|
|
55
|
+
* Verify an OTP code
|
|
56
|
+
*
|
|
57
|
+
* @param options - Options for verifying the OTP
|
|
58
|
+
* @returns Promise resolving to the verification result
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const result = await client.verifyOtp({
|
|
63
|
+
* requestId: 'req_123456',
|
|
64
|
+
* code: '123456',
|
|
65
|
+
* ipAddress: '192.168.1.1',
|
|
66
|
+
* userAgent: 'Mozilla/5.0...'
|
|
67
|
+
* });
|
|
68
|
+
*
|
|
69
|
+
* if (result.success) {
|
|
70
|
+
* console.log('OTP verified successfully!');
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
verifyOtp(options: VerifyOtpOptions): Promise<VerifyOtpResponse>;
|
|
75
|
+
/**
|
|
76
|
+
* Resend an OTP (generates a new code for the same phone number)
|
|
77
|
+
*
|
|
78
|
+
* @param options - Options for resending the OTP
|
|
79
|
+
* @returns Promise resolving to the new OTP request details
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const result = await client.resendOtp({
|
|
84
|
+
* requestId: 'req_123456'
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
* console.log('New OTP sent:', result.requestId);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
resendOtp(options: ResendOtpOptions): Promise<SendOtpResponse>;
|
|
91
|
+
/**
|
|
92
|
+
* Get OTP status (authenticated endpoint)
|
|
93
|
+
*
|
|
94
|
+
* @param requestId - The request ID from sendOtp()
|
|
95
|
+
* @returns Promise resolving to the OTP status
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const status = await client.getStatus('req_123456');
|
|
100
|
+
* console.log(status.status); // 'PENDING' | 'SENT' | 'VERIFIED' | 'EXPIRED' | 'FAILED'
|
|
101
|
+
* console.log(status.attempts); // Number of verification attempts
|
|
102
|
+
* console.log(status.isExpired); // Whether OTP is expired
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
getStatus(requestId: string): Promise<OtpStatus>;
|
|
106
|
+
/**
|
|
107
|
+
* Get OTP status with code (public endpoint for testing/development)
|
|
108
|
+
*
|
|
109
|
+
* ⚠️ WARNING: This endpoint returns the actual OTP code and should ONLY be used
|
|
110
|
+
* in development/testing environments! Never use in production client code.
|
|
111
|
+
*
|
|
112
|
+
* This method polls for up to 30 seconds waiting for SMS delivery.
|
|
113
|
+
*
|
|
114
|
+
* @param requestId - The request ID from sendOtp()
|
|
115
|
+
* @returns Promise resolving to the OTP status with code
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // For automated testing only!
|
|
120
|
+
* const status = await client.getStatusWithCode('req_123456');
|
|
121
|
+
* console.log(status.otpCode); // The actual OTP code
|
|
122
|
+
* console.log(status.smsProvider); // Which provider was used
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
getStatusWithCode(requestId: string): Promise<OtpStatusWithCode>;
|
|
126
|
+
/**
|
|
127
|
+
* Get SDK configuration from server
|
|
128
|
+
*
|
|
129
|
+
* Fetches and caches configuration from the server. The configuration includes
|
|
130
|
+
* rate limits, supported features, test mode status, and more. Results are
|
|
131
|
+
* cached for 1 hour.
|
|
132
|
+
*
|
|
133
|
+
* @param forceRefresh - Force refresh the cached configuration
|
|
134
|
+
* @returns Promise resolving to the SDK configuration
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const config = await client.getConfig();
|
|
139
|
+
* console.log(config.features.testMode); // Check if test mode is enabled
|
|
140
|
+
* console.log(config.otpConfig.length); // Default OTP length
|
|
141
|
+
* console.log(config.rateLimits); // Rate limit information
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
getConfig(forceRefresh?: boolean): Promise<SdkConfiguration>;
|
|
145
|
+
/**
|
|
146
|
+
* Test connectivity to the OTP service
|
|
147
|
+
*
|
|
148
|
+
* @returns Promise resolving to true if connection is successful
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const isConnected = await client.testConnection();
|
|
153
|
+
* if (isConnected) {
|
|
154
|
+
* console.log('✓ Connected to OTP service');
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
testConnection(): Promise<boolean>;
|
|
159
|
+
/**
|
|
160
|
+
* Check if server is in test mode
|
|
161
|
+
*
|
|
162
|
+
* @returns Promise resolving to true if test mode is enabled
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const testMode = await client.isTestMode();
|
|
167
|
+
* if (testMode) {
|
|
168
|
+
* console.log('Server is in test mode - use test phone numbers');
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
isTestMode(): Promise<boolean>;
|
|
173
|
+
/**
|
|
174
|
+
* Generate a unique idempotency key
|
|
175
|
+
*
|
|
176
|
+
* @returns A unique idempotency key in the format `{timestamp}-{random}`
|
|
177
|
+
*/
|
|
178
|
+
private generateIdempotencyKey;
|
|
179
|
+
/**
|
|
180
|
+
* Validate phone number format (E.164)
|
|
181
|
+
*/
|
|
182
|
+
private validatePhoneNumber;
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=otp-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"otp-client.d.ts","sourceRoot":"","sources":["../src/otp-client.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EACf,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAEhB,SAAS,EACT,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,IAAI,CAAa;IACzB,OAAO,CAAC,YAAY,CAAiC;IACrD,OAAO,CAAC,qBAAqB,CAAuB;IACpD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAW;IAE5C;;;;OAIG;gBACS,MAAM,EAAE,eAAe;IAgBnC;;;;;;;;;;;;;;;;;;;OAmBG;IACG,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,eAAe,CAAC;IA0BhE;;;;;;;;;;;;;;;;;;;OAmBG;IACG,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAmBtE;;;;;;;;;;;;;;OAcG;IACG,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;IAuBpE;;;;;;;;;;;;;OAaG;IACG,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC;IAgBtD;;;;;;;;;;;;;;;;;;OAkBG;IACG,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAkBtE;;;;;;;;;;;;;;;;;OAiBG;IACG,SAAS,CAAC,YAAY,UAAQ,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA6BhE;;;;;;;;;;;;OAYG;IACG,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;IASxC;;;;;;;;;;;;OAYG;IACG,UAAU,IAAI,OAAO,CAAC,OAAO,CAAC;IASpC;;;;OAIG;IACH,OAAO,CAAC,sBAAsB;IAM9B;;OAEG;IACH,OAAO,CAAC,mBAAmB;CAQ5B"}
|