@easyslip/sdk 0.0.0-stage → 1.0.1
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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +595 -2
- package/README.th.md +610 -0
- package/dist/cjs/client.d.ts +59 -0
- package/dist/cjs/client.js +150 -0
- package/dist/cjs/core/call.d.ts +18 -0
- package/dist/cjs/core/call.js +60 -0
- package/dist/cjs/core/error-codes.d.ts +8 -0
- package/dist/cjs/core/error-codes.js +87 -0
- package/dist/cjs/core/errors.d.ts +162 -0
- package/dist/cjs/core/errors.js +210 -0
- package/dist/cjs/core/files.d.ts +20 -0
- package/dist/cjs/core/files.js +131 -0
- package/dist/cjs/core/http.d.ts +65 -0
- package/dist/cjs/core/http.js +242 -0
- package/dist/cjs/core/meta.d.ts +33 -0
- package/dist/cjs/core/meta.js +52 -0
- package/dist/cjs/core/redact.d.ts +43 -0
- package/dist/cjs/core/redact.js +208 -0
- package/dist/cjs/core/runtime.d.ts +16 -0
- package/dist/cjs/core/runtime.js +86 -0
- package/dist/cjs/core/settings.d.ts +19 -0
- package/dist/cjs/core/settings.js +22 -0
- package/dist/cjs/core/sha256.d.ts +6 -0
- package/dist/cjs/core/sha256.js +101 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +55 -0
- package/dist/cjs/models.d.ts +398 -0
- package/dist/cjs/models.js +234 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resources/bank-accounts.d.ts +63 -0
- package/dist/cjs/resources/bank-accounts.js +126 -0
- package/dist/cjs/resources/banks.d.ts +10 -0
- package/dist/cjs/resources/banks.js +22 -0
- package/dist/cjs/resources/jobs.d.ts +64 -0
- package/dist/cjs/resources/jobs.js +177 -0
- package/dist/cjs/resources/v1.d.ts +88 -0
- package/dist/cjs/resources/v1.js +137 -0
- package/dist/cjs/resources/verify.d.ts +39 -0
- package/dist/cjs/resources/verify.js +105 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/webhooks.d.ts +26 -0
- package/dist/cjs/webhooks.js +92 -0
- package/dist/esm/client.d.ts +59 -0
- package/dist/esm/client.js +146 -0
- package/dist/esm/core/call.d.ts +18 -0
- package/dist/esm/core/call.js +52 -0
- package/dist/esm/core/error-codes.d.ts +8 -0
- package/dist/esm/core/error-codes.js +84 -0
- package/dist/esm/core/errors.d.ts +162 -0
- package/dist/esm/core/errors.js +184 -0
- package/dist/esm/core/files.d.ts +20 -0
- package/dist/esm/core/files.js +126 -0
- package/dist/esm/core/http.d.ts +65 -0
- package/dist/esm/core/http.js +235 -0
- package/dist/esm/core/meta.d.ts +33 -0
- package/dist/esm/core/meta.js +45 -0
- package/dist/esm/core/redact.d.ts +43 -0
- package/dist/esm/core/redact.js +198 -0
- package/dist/esm/core/runtime.d.ts +16 -0
- package/dist/esm/core/runtime.js +47 -0
- package/dist/esm/core/settings.d.ts +19 -0
- package/dist/esm/core/settings.js +19 -0
- package/dist/esm/core/sha256.d.ts +6 -0
- package/dist/esm/core/sha256.js +96 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/models.d.ts +398 -0
- package/dist/esm/models.js +228 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/resources/bank-accounts.d.ts +63 -0
- package/dist/esm/resources/bank-accounts.js +122 -0
- package/dist/esm/resources/banks.d.ts +10 -0
- package/dist/esm/resources/banks.js +18 -0
- package/dist/esm/resources/jobs.d.ts +64 -0
- package/dist/esm/resources/jobs.js +173 -0
- package/dist/esm/resources/v1.d.ts +88 -0
- package/dist/esm/resources/v1.js +133 -0
- package/dist/esm/resources/verify.d.ts +39 -0
- package/dist/esm/resources/verify.js +100 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/webhooks.d.ts +26 -0
- package/dist/esm/webhooks.js +86 -0
- package/package.json +80 -5
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BankAccounts = void 0;
|
|
4
|
+
const call_js_1 = require("../core/call.js");
|
|
5
|
+
const CREATE_KEYS = [
|
|
6
|
+
'bankCode',
|
|
7
|
+
'bankNumber',
|
|
8
|
+
'nameTh',
|
|
9
|
+
'nameEn',
|
|
10
|
+
'type',
|
|
11
|
+
'extraVerify',
|
|
12
|
+
'matchMode',
|
|
13
|
+
];
|
|
14
|
+
function listQuery(params) {
|
|
15
|
+
return {
|
|
16
|
+
type: params?.type,
|
|
17
|
+
bankCode: params?.bankCode,
|
|
18
|
+
page: params?.page === undefined ? undefined : String(params.page),
|
|
19
|
+
limit: params?.limit === undefined ? undefined : String(params.limit),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Receiving bank accounts used by `matchAccount` (`client.bankAccounts`). */
|
|
23
|
+
class BankAccounts {
|
|
24
|
+
#t;
|
|
25
|
+
constructor(transport) {
|
|
26
|
+
this.#t = transport;
|
|
27
|
+
}
|
|
28
|
+
/** One page of the accounts linked to this branch. */
|
|
29
|
+
async list(params, options) {
|
|
30
|
+
return (0, call_js_1.call)(this.#t, 'BankAccountPage', {
|
|
31
|
+
...(0, call_js_1.requestOptions)(options),
|
|
32
|
+
api: 'v2',
|
|
33
|
+
method: 'GET',
|
|
34
|
+
path: '/bank-accounts',
|
|
35
|
+
query: listQuery(params),
|
|
36
|
+
retry: 'safe',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** One page of every active account of the service, linked to this branch or not. */
|
|
40
|
+
async listAll(params, options) {
|
|
41
|
+
return (0, call_js_1.call)(this.#t, 'BankAccountPage', {
|
|
42
|
+
...(0, call_js_1.requestOptions)(options),
|
|
43
|
+
api: 'v2',
|
|
44
|
+
method: 'GET',
|
|
45
|
+
path: '/bank-accounts/all',
|
|
46
|
+
query: listQuery(params),
|
|
47
|
+
retry: 'safe',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/** Gets one account linked to this branch. */
|
|
51
|
+
async get(id, options) {
|
|
52
|
+
(0, call_js_1.requireId)(id);
|
|
53
|
+
return (0, call_js_1.call)(this.#t, 'BankAccount', {
|
|
54
|
+
...(0, call_js_1.requestOptions)(options),
|
|
55
|
+
api: 'v2',
|
|
56
|
+
method: 'GET',
|
|
57
|
+
path: `/bank-accounts/${id}`,
|
|
58
|
+
retry: 'safe',
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Registers an account on the service and links it to this branch. */
|
|
62
|
+
async create(params, options) {
|
|
63
|
+
return (0, call_js_1.call)(this.#t, 'BankAccount', {
|
|
64
|
+
...(0, call_js_1.requestOptions)(options),
|
|
65
|
+
api: 'v2',
|
|
66
|
+
method: 'POST',
|
|
67
|
+
path: '/bank-accounts',
|
|
68
|
+
json: (0, call_js_1.defined)(params, CREATE_KEYS),
|
|
69
|
+
retry: 'write',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** Changes the fields you pass. Pass `extraVerify: null` to clear it. */
|
|
73
|
+
async update(id, params, options) {
|
|
74
|
+
(0, call_js_1.requireId)(id);
|
|
75
|
+
return (0, call_js_1.call)(this.#t, 'BankAccount', {
|
|
76
|
+
...(0, call_js_1.requestOptions)(options),
|
|
77
|
+
api: 'v2',
|
|
78
|
+
method: 'PATCH',
|
|
79
|
+
path: `/bank-accounts/${id}`,
|
|
80
|
+
json: (0, call_js_1.defined)(params, CREATE_KEYS),
|
|
81
|
+
retry: 'write',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** Links an existing service account to this branch. Linking twice is fine. */
|
|
85
|
+
async link(id, options) {
|
|
86
|
+
(0, call_js_1.requireId)(id);
|
|
87
|
+
return (0, call_js_1.call)(this.#t, 'LinkResult', {
|
|
88
|
+
...(0, call_js_1.requestOptions)(options),
|
|
89
|
+
api: 'v2',
|
|
90
|
+
method: 'POST',
|
|
91
|
+
path: `/bank-accounts/${id}/link`,
|
|
92
|
+
retry: 'safe',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/** Unlinks an account from this branch. The account stays on the service. */
|
|
96
|
+
async unlink(id, options) {
|
|
97
|
+
(0, call_js_1.requireId)(id);
|
|
98
|
+
return (0, call_js_1.call)(this.#t, 'UnlinkResult', {
|
|
99
|
+
...(0, call_js_1.requestOptions)(options),
|
|
100
|
+
api: 'v2',
|
|
101
|
+
method: 'DELETE',
|
|
102
|
+
path: `/bank-accounts/${id}/link`,
|
|
103
|
+
retry: 'write',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** Walks every page of {@link list}, one request per page (100 accounts by default). */
|
|
107
|
+
iterate(params, options) {
|
|
108
|
+
return this.#walk((p) => this.list(p, options), params);
|
|
109
|
+
}
|
|
110
|
+
/** Walks every page of {@link listAll}. */
|
|
111
|
+
iterateAll(params, options) {
|
|
112
|
+
return this.#walk((p) => this.listAll(p, options), params);
|
|
113
|
+
}
|
|
114
|
+
async *#walk(fetchPage, params) {
|
|
115
|
+
let page = params?.page ?? 1;
|
|
116
|
+
const limit = params?.limit ?? 100;
|
|
117
|
+
while (true) {
|
|
118
|
+
const result = await fetchPage({ ...params, page, limit });
|
|
119
|
+
yield* result.items;
|
|
120
|
+
if (result.items.length === 0 || page * limit >= result.total)
|
|
121
|
+
return;
|
|
122
|
+
page++;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.BankAccounts = BankAccounts;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
2
|
+
import type { WithMeta } from '../core/meta.js';
|
|
3
|
+
import type { BankList } from '../models.js';
|
|
4
|
+
/** Supported banks and receiving kinds (`client.banks`). */
|
|
5
|
+
export declare class Banks {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(transport: Transport);
|
|
8
|
+
/** Lists the bank codes you can register accounts with, and their `extraVerify` options. */
|
|
9
|
+
list(options?: RequestOptions): Promise<WithMeta<BankList>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Banks = void 0;
|
|
4
|
+
const call_js_1 = require("../core/call.js");
|
|
5
|
+
/** Supported banks and receiving kinds (`client.banks`). */
|
|
6
|
+
class Banks {
|
|
7
|
+
#t;
|
|
8
|
+
constructor(transport) {
|
|
9
|
+
this.#t = transport;
|
|
10
|
+
}
|
|
11
|
+
/** Lists the bank codes you can register accounts with, and their `extraVerify` options. */
|
|
12
|
+
async list(options) {
|
|
13
|
+
return (0, call_js_1.call)(this.#t, 'BankList', {
|
|
14
|
+
...(0, call_js_1.requestOptions)(options),
|
|
15
|
+
api: 'v2',
|
|
16
|
+
method: 'GET',
|
|
17
|
+
path: '/banks',
|
|
18
|
+
retry: 'safe',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Banks = Banks;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
2
|
+
import type { WithMeta } from '../core/meta.js';
|
|
3
|
+
import type { BatchSubmission, Job, JobSubmission } from '../models.js';
|
|
4
|
+
import { type VerifyOptions } from './verify.js';
|
|
5
|
+
/** Most slips one batch call accepts. {@link Jobs.submitBankMany} splits larger lists. */
|
|
6
|
+
export declare const BATCH_MAX = 100;
|
|
7
|
+
/** Options of an async verify call. */
|
|
8
|
+
export interface AsyncVerifyOptions extends VerifyOptions {
|
|
9
|
+
/**
|
|
10
|
+
* https URL the result webhook is posted to, up to 255 characters. Required unless the
|
|
11
|
+
* branch has a default webhook URL.
|
|
12
|
+
*/
|
|
13
|
+
callbackUrl?: string;
|
|
14
|
+
}
|
|
15
|
+
/** One slip of a batch. */
|
|
16
|
+
export interface BatchSlip extends VerifyOptions {
|
|
17
|
+
/** QR payload string. */
|
|
18
|
+
payload: string;
|
|
19
|
+
}
|
|
20
|
+
export interface BatchOptions extends RequestOptions {
|
|
21
|
+
/** https URL every job's webhook is posted to. */
|
|
22
|
+
callbackUrl?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface WaitForJobOptions {
|
|
25
|
+
/** Time between polls in milliseconds. Default 2000, minimum 500. */
|
|
26
|
+
interval?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Give up after this many milliseconds. Default 600000 (10 minutes). One deadline for the
|
|
29
|
+
* whole wait: requests still running at the deadline are cancelled, and an answer that
|
|
30
|
+
* arrives after it is not returned.
|
|
31
|
+
*/
|
|
32
|
+
timeout?: number;
|
|
33
|
+
/** Stops waiting; the call then rejects with the signal's reason. */
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Async bank slip verification (`client.jobs`). Submits are billed when the job verifies the
|
|
38
|
+
* slip; like the sync calls they are never retried once the request may have arrived.
|
|
39
|
+
*/
|
|
40
|
+
export declare class Jobs {
|
|
41
|
+
#private;
|
|
42
|
+
constructor(transport: Transport);
|
|
43
|
+
/** Queues one QR payload. The result arrives on the webhook or through {@link get}. */
|
|
44
|
+
submitBank(payload: string, options?: AsyncVerifyOptions & RequestOptions): Promise<WithMeta<JobSubmission>>;
|
|
45
|
+
/** Queues 1 to 100 payloads in one call. Each slip becomes its own job. */
|
|
46
|
+
submitBankBatch(slips: readonly BatchSlip[], options?: BatchOptions): Promise<WithMeta<BatchSubmission>>;
|
|
47
|
+
/**
|
|
48
|
+
* Queues any number of payloads, 100 per call, one call after another. Quota is the same as
|
|
49
|
+
* one big batch: each slip is billed when its job verifies it. If a call fails, the batches
|
|
50
|
+
* already accepted keep running and {@link BatchSubmitError} lists them.
|
|
51
|
+
*/
|
|
52
|
+
submitBankMany(slips: readonly BatchSlip[], options?: BatchOptions): Promise<BatchSubmission[]>;
|
|
53
|
+
/** Gets a job by id. */
|
|
54
|
+
get(jobId: string, options?: RequestOptions): Promise<WithMeta<Job>>;
|
|
55
|
+
/**
|
|
56
|
+
* Polls a job until its status is `done` or `failed` and returns it. A failed job is
|
|
57
|
+
* returned, not thrown. `timeout` is one deadline for the whole wait: each
|
|
58
|
+
* poll and its retries get only the time left, a request still running at the deadline is
|
|
59
|
+
* cancelled, and an answer that arrives after the deadline is not returned. Then it throws
|
|
60
|
+
* {@link JobWaitTimeoutError} with the last job received in time. Aborting `signal` rejects
|
|
61
|
+
* with the signal's reason instead.
|
|
62
|
+
*/
|
|
63
|
+
waitForJob(jobId: string, options?: WaitForJobOptions): Promise<WithMeta<Job>>;
|
|
64
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Jobs = exports.BATCH_MAX = void 0;
|
|
4
|
+
const call_js_1 = require("../core/call.js");
|
|
5
|
+
const errors_js_1 = require("../core/errors.js");
|
|
6
|
+
const verify_js_1 = require("./verify.js");
|
|
7
|
+
/** Most slips one batch call accepts. {@link Jobs.submitBankMany} splits larger lists. */
|
|
8
|
+
exports.BATCH_MAX = 100;
|
|
9
|
+
const SLIP_KEYS = ['payload', 'remark', 'matchAccount', 'matchAmount', 'checkDuplicate'];
|
|
10
|
+
/**
|
|
11
|
+
* Async bank slip verification (`client.jobs`). Submits are billed when the job verifies the
|
|
12
|
+
* slip; like the sync calls they are never retried once the request may have arrived.
|
|
13
|
+
*/
|
|
14
|
+
class Jobs {
|
|
15
|
+
#t;
|
|
16
|
+
#sleep;
|
|
17
|
+
#timeout;
|
|
18
|
+
constructor(transport) {
|
|
19
|
+
this.#t = transport;
|
|
20
|
+
this.#sleep = transport.config.sleep;
|
|
21
|
+
this.#timeout = transport.config.timeout;
|
|
22
|
+
}
|
|
23
|
+
/** Queues one QR payload. The result arrives on the webhook or through {@link get}. */
|
|
24
|
+
async submitBank(payload, options) {
|
|
25
|
+
(0, call_js_1.requireString)(payload, 'payload');
|
|
26
|
+
const json = { payload, ...(0, verify_js_1.verifyFields)(options) };
|
|
27
|
+
if (options?.callbackUrl !== undefined)
|
|
28
|
+
json.callbackUrl = options.callbackUrl;
|
|
29
|
+
return (0, call_js_1.call)(this.#t, 'JobSubmission', {
|
|
30
|
+
...(0, call_js_1.requestOptions)(options),
|
|
31
|
+
api: 'v2',
|
|
32
|
+
method: 'POST',
|
|
33
|
+
path: '/verify/bank/async',
|
|
34
|
+
json,
|
|
35
|
+
retry: 'billed',
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/** Queues 1 to 100 payloads in one call. Each slip becomes its own job. */
|
|
39
|
+
async submitBankBatch(slips, options) {
|
|
40
|
+
if (!Array.isArray(slips) || slips.length === 0) {
|
|
41
|
+
throw new errors_js_1.InputError('EMPTY_BATCH', 'slips must contain at least one slip');
|
|
42
|
+
}
|
|
43
|
+
if (slips.length > exports.BATCH_MAX) {
|
|
44
|
+
throw new errors_js_1.InputError('BATCH_TOO_LARGE', `a batch takes at most ${exports.BATCH_MAX} slips; use submitBankMany for more`);
|
|
45
|
+
}
|
|
46
|
+
for (const slip of slips)
|
|
47
|
+
(0, call_js_1.requireString)(slip?.payload, 'slip payload');
|
|
48
|
+
const json = {};
|
|
49
|
+
if (options?.callbackUrl !== undefined)
|
|
50
|
+
json.callbackUrl = options.callbackUrl;
|
|
51
|
+
json.slips = slips.map((slip) => (0, call_js_1.defined)(slip, SLIP_KEYS));
|
|
52
|
+
return (0, call_js_1.call)(this.#t, 'BatchSubmission', {
|
|
53
|
+
...(0, call_js_1.requestOptions)(options),
|
|
54
|
+
api: 'v2',
|
|
55
|
+
method: 'POST',
|
|
56
|
+
path: '/verify/bank/batch',
|
|
57
|
+
json,
|
|
58
|
+
retry: 'billed',
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Queues any number of payloads, 100 per call, one call after another. Quota is the same as
|
|
63
|
+
* one big batch: each slip is billed when its job verifies it. If a call fails, the batches
|
|
64
|
+
* already accepted keep running and {@link BatchSubmitError} lists them.
|
|
65
|
+
*/
|
|
66
|
+
async submitBankMany(slips, options) {
|
|
67
|
+
if (!Array.isArray(slips) || slips.length === 0) {
|
|
68
|
+
throw new errors_js_1.InputError('EMPTY_BATCH', 'slips must contain at least one slip');
|
|
69
|
+
}
|
|
70
|
+
const submitted = [];
|
|
71
|
+
for (let start = 0, chunk = 0; start < slips.length; start += exports.BATCH_MAX, chunk++) {
|
|
72
|
+
try {
|
|
73
|
+
submitted.push(await this.submitBankBatch(slips.slice(start, start + exports.BATCH_MAX), options));
|
|
74
|
+
}
|
|
75
|
+
catch (cause) {
|
|
76
|
+
throw new errors_js_1.BatchSubmitError(submitted, chunk, cause);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return submitted;
|
|
80
|
+
}
|
|
81
|
+
/** Gets a job by id. */
|
|
82
|
+
async get(jobId, options) {
|
|
83
|
+
(0, call_js_1.requireString)(jobId, 'jobId');
|
|
84
|
+
return (0, call_js_1.call)(this.#t, 'Job', {
|
|
85
|
+
...(0, call_js_1.requestOptions)(options),
|
|
86
|
+
api: 'v2',
|
|
87
|
+
method: 'GET',
|
|
88
|
+
path: `/verify/bank/jobs/${encodeURIComponent(jobId)}`,
|
|
89
|
+
retry: 'safe',
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Polls a job until its status is `done` or `failed` and returns it. A failed job is
|
|
94
|
+
* returned, not thrown. `timeout` is one deadline for the whole wait: each
|
|
95
|
+
* poll and its retries get only the time left, a request still running at the deadline is
|
|
96
|
+
* cancelled, and an answer that arrives after the deadline is not returned. Then it throws
|
|
97
|
+
* {@link JobWaitTimeoutError} with the last job received in time. Aborting `signal` rejects
|
|
98
|
+
* with the signal's reason instead.
|
|
99
|
+
*/
|
|
100
|
+
async waitForJob(jobId, options) {
|
|
101
|
+
(0, call_js_1.requireString)(jobId, 'jobId');
|
|
102
|
+
const interval = Math.max(500, options?.interval ?? 2000);
|
|
103
|
+
const timeout = options?.timeout ?? 600_000;
|
|
104
|
+
if (!Number.isFinite(timeout) || timeout < 0) {
|
|
105
|
+
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'timeout must be a number >= 0');
|
|
106
|
+
}
|
|
107
|
+
const caller = options?.signal;
|
|
108
|
+
caller?.throwIfAborted();
|
|
109
|
+
const deadline = Date.now() + timeout;
|
|
110
|
+
let last = null;
|
|
111
|
+
const expired = () => new errors_js_1.JobWaitTimeoutError(`job ${jobId} ${last ? `still ${last.status}` : 'not read'} after ${timeout} ms`, last);
|
|
112
|
+
// One signal for every request and wait: aborted by the caller or at the deadline.
|
|
113
|
+
const budget = new AbortController();
|
|
114
|
+
const reason = new Error('waitForJob deadline');
|
|
115
|
+
const timer = setTimeout(() => budget.abort(reason), timeout);
|
|
116
|
+
const onAbort = () => budget.abort(caller?.reason);
|
|
117
|
+
caller?.addEventListener('abort', onAbort, { once: true });
|
|
118
|
+
const deadlinePassed = () => budget.signal.reason === reason && !caller?.aborted;
|
|
119
|
+
// Settles when the budget is aborted, so a custom fetch or sleep that ignores the signal
|
|
120
|
+
// cannot hold the call past the deadline.
|
|
121
|
+
const stopped = new Promise((_, reject) => {
|
|
122
|
+
budget.signal.addEventListener('abort', () => reject(budget.signal.reason), {
|
|
123
|
+
once: true,
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
stopped.catch(() => { });
|
|
127
|
+
try {
|
|
128
|
+
while (true) {
|
|
129
|
+
const left = deadline - Date.now();
|
|
130
|
+
if (left <= 0)
|
|
131
|
+
throw expired();
|
|
132
|
+
// The attempt timeout is cut to the time left, so its timeout is the deadline too.
|
|
133
|
+
const capped = left < this.#timeout;
|
|
134
|
+
let job;
|
|
135
|
+
try {
|
|
136
|
+
job = await Promise.race([
|
|
137
|
+
this.get(jobId, {
|
|
138
|
+
signal: budget.signal,
|
|
139
|
+
timeout: Math.max(1, Math.min(this.#timeout, left)),
|
|
140
|
+
}),
|
|
141
|
+
stopped,
|
|
142
|
+
]);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (deadlinePassed())
|
|
146
|
+
throw expired();
|
|
147
|
+
if (capped && error instanceof errors_js_1.RequestTimeoutError && !caller?.aborted) {
|
|
148
|
+
throw expired();
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
// A request that ignored cancellation can still answer late: do not return it.
|
|
153
|
+
if (Date.now() >= deadline)
|
|
154
|
+
throw expired();
|
|
155
|
+
last = job;
|
|
156
|
+
if (job.status === 'done' || job.status === 'failed')
|
|
157
|
+
return job;
|
|
158
|
+
const wait = Math.min(interval, deadline - Date.now());
|
|
159
|
+
if (wait <= 0)
|
|
160
|
+
throw expired();
|
|
161
|
+
try {
|
|
162
|
+
await Promise.race([this.#sleep(wait, budget.signal), stopped]);
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
if (deadlinePassed())
|
|
166
|
+
throw expired();
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
clearTimeout(timer);
|
|
173
|
+
caller?.removeEventListener('abort', onAbort);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.Jobs = Jobs;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type FileInput } from '../core/files.js';
|
|
2
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
3
|
+
import type { WithMeta } from '../core/meta.js';
|
|
4
|
+
import type { BankSlip, QrCode, TrueWalletSlip, V1Me } from '../models.js';
|
|
5
|
+
/** Options of the v1 verify calls. */
|
|
6
|
+
export interface V1VerifyOptions extends RequestOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Reject a slip this application verified before with `DuplicateSlipError`, whose `slip`
|
|
9
|
+
* is the original slip. v1 does not charge for a duplicate.
|
|
10
|
+
*/
|
|
11
|
+
checkDuplicate?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** Shared fields of every QR request. */
|
|
14
|
+
interface QrBase {
|
|
15
|
+
/** Amount in baht. Leave out for a QR the payer fills in. */
|
|
16
|
+
amount?: number;
|
|
17
|
+
}
|
|
18
|
+
/** PromptPay QR: exactly one of `msisdn`, `natId`, `eWalletId`, `billerId`. */
|
|
19
|
+
export interface PromptPayQr extends QrBase {
|
|
20
|
+
type: 'PROMPTPAY';
|
|
21
|
+
msisdn?: string;
|
|
22
|
+
natId?: string;
|
|
23
|
+
eWalletId?: string;
|
|
24
|
+
/** Biller id (bill payment). `ref1` and `ref2` are only allowed with it. */
|
|
25
|
+
billerId?: string;
|
|
26
|
+
/** Reference 1, with `billerId` only. Optional: leave it out for a QR without Ref1. */
|
|
27
|
+
ref1?: string;
|
|
28
|
+
/** Reference 2, with `billerId` and `ref1` only. */
|
|
29
|
+
ref2?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Bill payment QR, the older name for PromptPay with `billerId`. */
|
|
32
|
+
export interface BillPaymentQr extends QrBase {
|
|
33
|
+
type: 'BILL_PAYMENT';
|
|
34
|
+
billerId: string;
|
|
35
|
+
/** Reference 1. Optional: leave it out for a QR without Ref1. */
|
|
36
|
+
ref1?: string;
|
|
37
|
+
/** Reference 2, only together with `ref1`. */
|
|
38
|
+
ref2?: string;
|
|
39
|
+
}
|
|
40
|
+
/** K SHOP or Mae Manee merchant QR. */
|
|
41
|
+
export interface MerchantQr extends QrBase {
|
|
42
|
+
type: 'KSHOP' | 'MAE_MANEE';
|
|
43
|
+
/**
|
|
44
|
+
* The shop id. K SHOP: `KB` and 12 digits (the API accepts any case and removes a leading
|
|
45
|
+
* `KPS004`). Mae Manee: 15 digits.
|
|
46
|
+
*/
|
|
47
|
+
ref1: string;
|
|
48
|
+
}
|
|
49
|
+
/** Thung Ngern merchant QR. */
|
|
50
|
+
export interface TungNgernQr extends QrBase {
|
|
51
|
+
type: 'TUNGNGERN';
|
|
52
|
+
/** Merchant code, 1 to 20 letters or digits. */
|
|
53
|
+
ref1: string;
|
|
54
|
+
/**
|
|
55
|
+
* The English shop name shown in the Tung Ngern app. The API keeps A-Z and 0-9, uppercased
|
|
56
|
+
* and cut to 20 characters, and rejects a name with none of them (a Thai-only name).
|
|
57
|
+
*/
|
|
58
|
+
merchantName: string;
|
|
59
|
+
}
|
|
60
|
+
/** Body of `v1.generateQr`. The API checks the field combinations. */
|
|
61
|
+
export type QrRequest = PromptPayQr | BillPaymentQr | MerchantQr | TungNgernQr;
|
|
62
|
+
/** The QR types `v1.generateQr` accepts. */
|
|
63
|
+
export type QrType = QrRequest['type'];
|
|
64
|
+
/** A QR code from `generateQr`, with a helper that decodes the PNG. */
|
|
65
|
+
export type QrCodeResult = WithMeta<QrCode> & {
|
|
66
|
+
/** The PNG image as bytes. */
|
|
67
|
+
imageBytes(): Uint8Array;
|
|
68
|
+
};
|
|
69
|
+
/** The v1 API (`client.v1`). Verify calls are billed; `generateQr` is free. */
|
|
70
|
+
export declare class V1 {
|
|
71
|
+
#private;
|
|
72
|
+
constructor(transport: Transport);
|
|
73
|
+
/** Quota and credit of the application. */
|
|
74
|
+
getMe(options?: RequestOptions): Promise<WithMeta<V1Me>>;
|
|
75
|
+
/** Verifies a bank slip from its QR payload (`GET /verify`). */
|
|
76
|
+
verifyBankByPayload(payload: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
77
|
+
/** Verifies a bank slip image (multipart field `file`). */
|
|
78
|
+
verifyBankByImage(file: FileInput, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
79
|
+
/** Verifies a base64 bank slip image. Sent as the JSON field `image`. */
|
|
80
|
+
verifyBankByBase64(base64: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
81
|
+
/** Verifies a bank slip image the API downloads from a public URL. */
|
|
82
|
+
verifyBankByUrl(url: string, options?: V1VerifyOptions): Promise<WithMeta<BankSlip>>;
|
|
83
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
84
|
+
verifyTruemoneyByImage(file: FileInput, options?: V1VerifyOptions): Promise<WithMeta<TrueWalletSlip>>;
|
|
85
|
+
/** Generates a payment QR image. No quota is used. */
|
|
86
|
+
generateQr(request: QrRequest, options?: RequestOptions): Promise<QrCodeResult>;
|
|
87
|
+
}
|
|
88
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.V1 = void 0;
|
|
4
|
+
const call_js_1 = require("../core/call.js");
|
|
5
|
+
const errors_js_1 = require("../core/errors.js");
|
|
6
|
+
const files_js_1 = require("../core/files.js");
|
|
7
|
+
function decodeBase64(text) {
|
|
8
|
+
const binary = atob(text);
|
|
9
|
+
const out = new Uint8Array(binary.length);
|
|
10
|
+
for (let i = 0; i < binary.length; i++)
|
|
11
|
+
out[i] = binary.charCodeAt(i);
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function boolString(value) {
|
|
15
|
+
return value ? 'true' : 'false';
|
|
16
|
+
}
|
|
17
|
+
/** The v1 API (`client.v1`). Verify calls are billed; `generateQr` is free. */
|
|
18
|
+
class V1 {
|
|
19
|
+
#t;
|
|
20
|
+
constructor(transport) {
|
|
21
|
+
this.#t = transport;
|
|
22
|
+
}
|
|
23
|
+
/** Quota and credit of the application. */
|
|
24
|
+
async getMe(options) {
|
|
25
|
+
return (0, call_js_1.call)(this.#t, 'V1Me', {
|
|
26
|
+
...(0, call_js_1.requestOptions)(options),
|
|
27
|
+
api: 'v1',
|
|
28
|
+
method: 'GET',
|
|
29
|
+
path: '/me',
|
|
30
|
+
retry: 'safe',
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** Verifies a bank slip from its QR payload (`GET /verify`). */
|
|
34
|
+
async verifyBankByPayload(payload, options) {
|
|
35
|
+
(0, call_js_1.requireString)(payload, 'payload');
|
|
36
|
+
return (0, call_js_1.call)(this.#t, 'BankSlip', {
|
|
37
|
+
...(0, call_js_1.requestOptions)(options),
|
|
38
|
+
api: 'v1',
|
|
39
|
+
method: 'GET',
|
|
40
|
+
path: '/verify',
|
|
41
|
+
query: {
|
|
42
|
+
payload,
|
|
43
|
+
checkDuplicate: options?.checkDuplicate === undefined
|
|
44
|
+
? undefined
|
|
45
|
+
: boolString(options.checkDuplicate),
|
|
46
|
+
},
|
|
47
|
+
retry: 'billed',
|
|
48
|
+
duplicateSlip: 'BankSlip',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/** Verifies a bank slip image (multipart field `file`). */
|
|
52
|
+
async verifyBankByImage(file, options) {
|
|
53
|
+
const prepared = await (0, files_js_1.prepareFile)(file);
|
|
54
|
+
const fields = {};
|
|
55
|
+
if (options?.checkDuplicate !== undefined) {
|
|
56
|
+
fields.checkDuplicate = boolString(options.checkDuplicate);
|
|
57
|
+
}
|
|
58
|
+
return (0, call_js_1.call)(this.#t, 'BankSlip', {
|
|
59
|
+
...(0, call_js_1.requestOptions)(options),
|
|
60
|
+
api: 'v1',
|
|
61
|
+
method: 'POST',
|
|
62
|
+
path: '/verify',
|
|
63
|
+
form: (0, call_js_1.imageForm)('file', prepared, fields),
|
|
64
|
+
retry: 'billed',
|
|
65
|
+
duplicateSlip: 'BankSlip',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Verifies a base64 bank slip image. Sent as the JSON field `image`. */
|
|
69
|
+
async verifyBankByBase64(base64, options) {
|
|
70
|
+
(0, call_js_1.requireString)(base64, 'base64');
|
|
71
|
+
const json = { image: base64 };
|
|
72
|
+
if (options?.checkDuplicate !== undefined)
|
|
73
|
+
json.checkDuplicate = options.checkDuplicate;
|
|
74
|
+
return (0, call_js_1.call)(this.#t, 'BankSlip', {
|
|
75
|
+
...(0, call_js_1.requestOptions)(options),
|
|
76
|
+
api: 'v1',
|
|
77
|
+
method: 'POST',
|
|
78
|
+
path: '/verify',
|
|
79
|
+
json,
|
|
80
|
+
retry: 'billed',
|
|
81
|
+
duplicateSlip: 'BankSlip',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** Verifies a bank slip image the API downloads from a public URL. */
|
|
85
|
+
async verifyBankByUrl(url, options) {
|
|
86
|
+
(0, call_js_1.requireString)(url, 'url');
|
|
87
|
+
const json = { url };
|
|
88
|
+
if (options?.checkDuplicate !== undefined)
|
|
89
|
+
json.checkDuplicate = options.checkDuplicate;
|
|
90
|
+
return (0, call_js_1.call)(this.#t, 'BankSlip', {
|
|
91
|
+
...(0, call_js_1.requestOptions)(options),
|
|
92
|
+
api: 'v1',
|
|
93
|
+
method: 'POST',
|
|
94
|
+
path: '/verify',
|
|
95
|
+
json,
|
|
96
|
+
retry: 'billed',
|
|
97
|
+
duplicateSlip: 'BankSlip',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
101
|
+
async verifyTruemoneyByImage(file, options) {
|
|
102
|
+
const prepared = await (0, files_js_1.prepareFile)(file);
|
|
103
|
+
const fields = {};
|
|
104
|
+
if (options?.checkDuplicate !== undefined) {
|
|
105
|
+
fields.checkDuplicate = boolString(options.checkDuplicate);
|
|
106
|
+
}
|
|
107
|
+
return (0, call_js_1.call)(this.#t, 'TrueWalletSlip', {
|
|
108
|
+
...(0, call_js_1.requestOptions)(options),
|
|
109
|
+
api: 'v1',
|
|
110
|
+
method: 'POST',
|
|
111
|
+
path: '/verify/truewallet',
|
|
112
|
+
form: (0, call_js_1.imageForm)('file', prepared, fields),
|
|
113
|
+
retry: 'billed',
|
|
114
|
+
duplicateSlip: 'TrueWalletSlip',
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/** Generates a payment QR image. No quota is used. */
|
|
118
|
+
async generateQr(request, options) {
|
|
119
|
+
if (!request || typeof request !== 'object') {
|
|
120
|
+
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'request must be an object with a type');
|
|
121
|
+
}
|
|
122
|
+
const result = await (0, call_js_1.call)(this.#t, 'QrCode', {
|
|
123
|
+
...(0, call_js_1.requestOptions)(options),
|
|
124
|
+
api: 'v1',
|
|
125
|
+
method: 'POST',
|
|
126
|
+
path: '/qr/generate',
|
|
127
|
+
json: request,
|
|
128
|
+
retry: 'safe',
|
|
129
|
+
});
|
|
130
|
+
Object.defineProperty(result, 'imageBytes', {
|
|
131
|
+
value: () => decodeBase64(result.image),
|
|
132
|
+
enumerable: false,
|
|
133
|
+
});
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.V1 = V1;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type FileInput } from '../core/files.js';
|
|
2
|
+
import type { RequestOptions, Transport } from '../core/http.js';
|
|
3
|
+
import type { WithMeta } from '../core/meta.js';
|
|
4
|
+
import type { BankSlipResult, TrueWalletResult } from '../models.js';
|
|
5
|
+
/** Options of every v2 verify call. Options you leave out are not sent. */
|
|
6
|
+
export interface VerifyOptions {
|
|
7
|
+
/** Your own note, 1 to 255 characters. Echoed back as `remark`. */
|
|
8
|
+
remark?: string;
|
|
9
|
+
/** Match the receiver against your registered bank accounts. Adds `matchedAccount`. */
|
|
10
|
+
matchAccount?: boolean;
|
|
11
|
+
/** Expected amount. Adds `amountInOrder` and `isAmountMatched`. */
|
|
12
|
+
matchAmount?: number;
|
|
13
|
+
/** Check whether this branch verified the slip before. Adds `isDuplicate`. */
|
|
14
|
+
checkDuplicate?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type VerifyCallOptions = VerifyOptions & RequestOptions;
|
|
17
|
+
export declare function verifyFields(options: VerifyOptions | undefined): VerifyOptions;
|
|
18
|
+
/**
|
|
19
|
+
* v2 slip verification (`client.verify`). Every call here is billed: it can spend quota, so
|
|
20
|
+
* the SDK only retries it when the request never reached the API (or on 429).
|
|
21
|
+
*/
|
|
22
|
+
export declare class Verify {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(transport: Transport);
|
|
25
|
+
/** Verifies a bank slip from its QR payload string. */
|
|
26
|
+
bankByPayload(payload: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
27
|
+
/** Verifies a bank slip image (JPEG, PNG, GIF or WebP, up to 4 MB). */
|
|
28
|
+
bankByImage(file: FileInput, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
29
|
+
/** Verifies a base64 bank slip image. A `data:image/...;base64,` prefix is fine. */
|
|
30
|
+
bankByBase64(base64: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
31
|
+
/** Verifies a bank slip image the API downloads from a public http(s) URL. */
|
|
32
|
+
bankByUrl(url: string, options?: VerifyCallOptions): Promise<WithMeta<BankSlipResult>>;
|
|
33
|
+
/** Verifies a TrueMoney Wallet slip image. */
|
|
34
|
+
truewalletByImage(file: FileInput, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
35
|
+
/** Verifies a base64 TrueMoney Wallet slip image. */
|
|
36
|
+
truewalletByBase64(base64: string, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
37
|
+
/** Verifies a TrueMoney Wallet slip image from a public URL (up to 255 characters). */
|
|
38
|
+
truewalletByUrl(url: string, options?: VerifyCallOptions): Promise<WithMeta<TrueWalletResult>>;
|
|
39
|
+
}
|