@wdft/micropayments-sdk 0.0.5 → 0.0.7
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/package.json +1 -1
- package/src/client.ts +19 -2
- package/src/service.test.ts +3 -0
- package/src/service.ts +34 -1
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -26,6 +26,8 @@ export type ICommitmentPayloadSimple = Omit<
|
|
|
26
26
|
"status" | "message"
|
|
27
27
|
>;
|
|
28
28
|
|
|
29
|
+
const DEFAULT_RETRY_TIME_MS = 5000;
|
|
30
|
+
|
|
29
31
|
class Commitment {
|
|
30
32
|
private readonly payload: ICommitmentPayload;
|
|
31
33
|
|
|
@@ -57,6 +59,10 @@ class Commitment {
|
|
|
57
59
|
return this.getStatus() === "resolved";
|
|
58
60
|
}
|
|
59
61
|
|
|
62
|
+
getMessage(): string {
|
|
63
|
+
return this.payload.message || '';
|
|
64
|
+
}
|
|
65
|
+
|
|
60
66
|
getStatus(): CommitmentStatus {
|
|
61
67
|
return this.payload.status;
|
|
62
68
|
}
|
|
@@ -224,6 +230,10 @@ class Commitment {
|
|
|
224
230
|
try {
|
|
225
231
|
const status = await this.service.checkCommitment(this.getCommitmentId());
|
|
226
232
|
|
|
233
|
+
if (status.retry_after) {
|
|
234
|
+
this.options.retry_ms = Math.max(this.options.retry_ms || DEFAULT_RETRY_TIME_MS, status.retry_after)
|
|
235
|
+
}
|
|
236
|
+
|
|
227
237
|
switch (status.status) {
|
|
228
238
|
case "duplicated": {
|
|
229
239
|
this.markAsDuplicated();
|
|
@@ -316,7 +326,7 @@ export class Client {
|
|
|
316
326
|
reference_id: reference_id,
|
|
317
327
|
expires_at: response.expires_at,
|
|
318
328
|
click_url: response.click_url,
|
|
319
|
-
retry_ms: this.options.retry_ms ??
|
|
329
|
+
retry_ms: this.options.retry_ms ?? DEFAULT_RETRY_TIME_MS,
|
|
320
330
|
},
|
|
321
331
|
options,
|
|
322
332
|
);
|
|
@@ -339,6 +349,7 @@ export class Client {
|
|
|
339
349
|
commitment.startChecking();
|
|
340
350
|
return on(null, {
|
|
341
351
|
status: commitment.getStatus(),
|
|
352
|
+
message: commitment.getMessage(),
|
|
342
353
|
reference_id,
|
|
343
354
|
commitment_id: commitment.getCommitmentId(),
|
|
344
355
|
click_url: commitment.getClickUrl(),
|
|
@@ -347,6 +358,7 @@ export class Client {
|
|
|
347
358
|
.onWaiting((commitment) =>
|
|
348
359
|
on(null, {
|
|
349
360
|
status: commitment.getStatus(),
|
|
361
|
+
message: commitment.getMessage(),
|
|
350
362
|
reference_id,
|
|
351
363
|
commitment_id: commitment.getCommitmentId(),
|
|
352
364
|
click_url: commitment.getClickUrl(),
|
|
@@ -355,6 +367,7 @@ export class Client {
|
|
|
355
367
|
.onDuplicated((commitment) =>
|
|
356
368
|
on(null, {
|
|
357
369
|
status: commitment.getStatus(),
|
|
370
|
+
message: commitment.getMessage(),
|
|
358
371
|
reference_id,
|
|
359
372
|
commitment_id: commitment.getCommitmentId(),
|
|
360
373
|
click_url: commitment.getClickUrl(),
|
|
@@ -363,6 +376,7 @@ export class Client {
|
|
|
363
376
|
.onExpired((commitment) =>
|
|
364
377
|
on(null, {
|
|
365
378
|
status: commitment.getStatus(),
|
|
379
|
+
message: commitment.getMessage(),
|
|
366
380
|
reference_id,
|
|
367
381
|
commitment_id: commitment.getCommitmentId(),
|
|
368
382
|
click_url: commitment.getClickUrl(),
|
|
@@ -371,6 +385,7 @@ export class Client {
|
|
|
371
385
|
.onRejected((commitment) =>
|
|
372
386
|
on(null, {
|
|
373
387
|
status: commitment.getStatus(),
|
|
388
|
+
message: commitment.getMessage(),
|
|
374
389
|
reference_id,
|
|
375
390
|
commitment_id: commitment.getCommitmentId(),
|
|
376
391
|
click_url: commitment.getClickUrl(),
|
|
@@ -379,6 +394,7 @@ export class Client {
|
|
|
379
394
|
.onResolved((commitment) =>
|
|
380
395
|
on(null, {
|
|
381
396
|
status: commitment.getStatus(),
|
|
397
|
+
message: commitment.getMessage(),
|
|
382
398
|
reference_id,
|
|
383
399
|
commitment_id: commitment.getCommitmentId(),
|
|
384
400
|
click_url: commitment.getClickUrl(),
|
|
@@ -400,6 +416,7 @@ export interface CallbackPayload {
|
|
|
400
416
|
status: CommitmentStatus;
|
|
401
417
|
reference_id: string;
|
|
402
418
|
commitment_id: string;
|
|
419
|
+
message: string | null;
|
|
403
420
|
click_url: string;
|
|
404
421
|
}
|
|
405
422
|
|
|
@@ -410,7 +427,7 @@ export type ICallback = OnStatusCallback & OnStatusErrorCallback;
|
|
|
410
427
|
class CommitmentObservabileBuilder {
|
|
411
428
|
private _reference_id: string | undefined;
|
|
412
429
|
private _amount: number | undefined;
|
|
413
|
-
private _retry_ms =
|
|
430
|
+
private _retry_ms = DEFAULT_RETRY_TIME_MS;
|
|
414
431
|
private _currency: string | undefined;
|
|
415
432
|
private _metadata: ICommitmentMetadata = { title: "", url: "" };
|
|
416
433
|
private _onCreatedFn: (commitment: Commitment) => void = () => {};
|
package/src/service.test.ts
CHANGED
|
@@ -41,6 +41,8 @@ it("service.checkCommitment - 200 - resolved good data", async () => {
|
|
|
41
41
|
message: "waiting for sms",
|
|
42
42
|
status: "waiting",
|
|
43
43
|
commitment_id: "sample_commitment_id",
|
|
44
|
+
}, {
|
|
45
|
+
'Retry-After': '5'
|
|
44
46
|
});
|
|
45
47
|
|
|
46
48
|
const status = await my.checkCommitment("sample_commitment_id");
|
|
@@ -48,6 +50,7 @@ it("service.checkCommitment - 200 - resolved good data", async () => {
|
|
|
48
50
|
expect(status).toMatchObject({
|
|
49
51
|
message: "waiting for sms",
|
|
50
52
|
status: "waiting",
|
|
53
|
+
retry_after: 5000,
|
|
51
54
|
commitment_id: "sample_commitment_id",
|
|
52
55
|
});
|
|
53
56
|
});
|
package/src/service.ts
CHANGED
|
@@ -5,6 +5,7 @@ interface CustomResponse {
|
|
|
5
5
|
|
|
6
6
|
interface SuccessResponse<T> extends CustomResponse {
|
|
7
7
|
error: false;
|
|
8
|
+
retry_after: number;
|
|
8
9
|
message: null;
|
|
9
10
|
data: T;
|
|
10
11
|
}
|
|
@@ -41,6 +42,28 @@ export type Fetcher = (
|
|
|
41
42
|
init?: RequestInit,
|
|
42
43
|
) => Promise<Response>;
|
|
43
44
|
|
|
45
|
+
function randomBetween(min: number, max: number) {
|
|
46
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseRetryAfter(retryAfter: string | undefined | null | number): number {
|
|
50
|
+
let retryAfterParsed = 0;
|
|
51
|
+
|
|
52
|
+
if (!retryAfter) {
|
|
53
|
+
return retryAfterParsed;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const retryAfterSeconds = typeof retryAfter === 'number' ? retryAfter : parseInt(retryAfter, 10);
|
|
57
|
+
|
|
58
|
+
if (!isNaN(retryAfterSeconds) && isFinite(retryAfterSeconds)) {
|
|
59
|
+
retryAfterParsed = retryAfterSeconds * 1000;
|
|
60
|
+
} else {
|
|
61
|
+
retryAfterParsed = 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return retryAfterParsed;
|
|
65
|
+
}
|
|
66
|
+
|
|
44
67
|
async function request<T = unknown>(
|
|
45
68
|
path: string,
|
|
46
69
|
req: RequestInit,
|
|
@@ -49,9 +72,11 @@ async function request<T = unknown>(
|
|
|
49
72
|
try {
|
|
50
73
|
const response = await fetcher(new Request(path, req));
|
|
51
74
|
if (response.ok) {
|
|
75
|
+
const retryAfter = response.headers.get('Retry-After');
|
|
52
76
|
return {
|
|
53
77
|
status: response.status,
|
|
54
78
|
error: false,
|
|
79
|
+
retry_after: parseRetryAfter(response.headers.get('Retry-After')),
|
|
55
80
|
data: (await parseResponse(response)) as T,
|
|
56
81
|
} as ResponseDTO<T>;
|
|
57
82
|
}
|
|
@@ -68,6 +93,7 @@ async function request<T = unknown>(
|
|
|
68
93
|
"message" in errorPayload
|
|
69
94
|
? errorPayload.message
|
|
70
95
|
: response.statusText,
|
|
96
|
+
retry_after: parseRetryAfter(response.headers.get('Retry-After')),
|
|
71
97
|
data: null,
|
|
72
98
|
} as ResponseDTO<T>;
|
|
73
99
|
} catch (error) {
|
|
@@ -75,6 +101,7 @@ async function request<T = unknown>(
|
|
|
75
101
|
status: 500,
|
|
76
102
|
error: true,
|
|
77
103
|
message: error instanceof Error ? error.message : `${error}`,
|
|
104
|
+
retry_after: randomBetween(10_000, 30_000),
|
|
78
105
|
data: null,
|
|
79
106
|
} as ErrorResponse;
|
|
80
107
|
}
|
|
@@ -98,6 +125,7 @@ export interface ICommitmentStatus {
|
|
|
98
125
|
commitment_id: string;
|
|
99
126
|
status: string;
|
|
100
127
|
message: string;
|
|
128
|
+
retry_after?: number;
|
|
101
129
|
}
|
|
102
130
|
|
|
103
131
|
export class Service {
|
|
@@ -177,6 +205,11 @@ export class Service {
|
|
|
177
205
|
throw new Error("Bad response");
|
|
178
206
|
}
|
|
179
207
|
|
|
180
|
-
return
|
|
208
|
+
return {
|
|
209
|
+
commitment_id: response.data.commitment_id,
|
|
210
|
+
message: response.data.message,
|
|
211
|
+
status: response.data.status,
|
|
212
|
+
retry_after: response.retry_after
|
|
213
|
+
};
|
|
181
214
|
}
|
|
182
215
|
}
|