@easyslip/sdk 1.0.1 → 1.1.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/CHANGELOG.md +7 -0
- package/README.md +8 -6
- package/README.th.md +8 -6
- package/dist/cjs/index.js +2039 -54
- package/dist/cjs/models.d.ts +16 -7
- package/dist/cjs/resources/v1.d.ts +2 -0
- package/dist/cjs/resources/verify.d.ts +2 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/esm/index.js +1997 -7
- package/dist/esm/models.d.ts +16 -7
- package/dist/esm/resources/v1.d.ts +2 -0
- package/dist/esm/resources/verify.d.ts +2 -0
- package/dist/esm/version.d.ts +1 -1
- package/package.json +12 -14
- package/dist/cjs/client.js +0 -150
- package/dist/cjs/core/call.js +0 -60
- package/dist/cjs/core/error-codes.js +0 -87
- package/dist/cjs/core/errors.js +0 -210
- package/dist/cjs/core/files.js +0 -131
- package/dist/cjs/core/http.js +0 -242
- package/dist/cjs/core/meta.js +0 -52
- package/dist/cjs/core/redact.js +0 -208
- package/dist/cjs/core/runtime.js +0 -86
- package/dist/cjs/core/settings.js +0 -22
- package/dist/cjs/core/sha256.js +0 -101
- package/dist/cjs/models.js +0 -234
- package/dist/cjs/resources/bank-accounts.js +0 -126
- package/dist/cjs/resources/banks.js +0 -22
- package/dist/cjs/resources/jobs.js +0 -177
- package/dist/cjs/resources/v1.js +0 -137
- package/dist/cjs/resources/verify.js +0 -105
- package/dist/cjs/version.js +0 -5
- package/dist/cjs/webhooks.js +0 -92
- package/dist/esm/client.js +0 -146
- package/dist/esm/core/call.js +0 -52
- package/dist/esm/core/error-codes.js +0 -84
- package/dist/esm/core/errors.js +0 -184
- package/dist/esm/core/files.js +0 -126
- package/dist/esm/core/http.js +0 -235
- package/dist/esm/core/meta.js +0 -45
- package/dist/esm/core/redact.js +0 -198
- package/dist/esm/core/runtime.js +0 -47
- package/dist/esm/core/settings.js +0 -19
- package/dist/esm/core/sha256.js +0 -96
- package/dist/esm/models.js +0 -228
- package/dist/esm/resources/bank-accounts.js +0 -122
- package/dist/esm/resources/banks.js +0 -18
- package/dist/esm/resources/jobs.js +0 -173
- package/dist/esm/resources/v1.js +0 -133
- package/dist/esm/resources/verify.js +0 -100
- package/dist/esm/version.js +0 -2
- package/dist/esm/webhooks.js +0 -86
|
@@ -1,177 +0,0 @@
|
|
|
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;
|
package/dist/cjs/resources/v1.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
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;
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Verify = void 0;
|
|
4
|
-
exports.verifyFields = verifyFields;
|
|
5
|
-
const call_js_1 = require("../core/call.js");
|
|
6
|
-
const files_js_1 = require("../core/files.js");
|
|
7
|
-
const OPTION_KEYS = ['remark', 'matchAccount', 'matchAmount', 'checkDuplicate'];
|
|
8
|
-
function verifyFields(options) {
|
|
9
|
-
return (0, call_js_1.defined)(options, OPTION_KEYS);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* v2 slip verification (`client.verify`). Every call here is billed: it can spend quota, so
|
|
13
|
-
* the SDK only retries it when the request never reached the API (or on 429).
|
|
14
|
-
*/
|
|
15
|
-
class Verify {
|
|
16
|
-
#t;
|
|
17
|
-
constructor(transport) {
|
|
18
|
-
this.#t = transport;
|
|
19
|
-
}
|
|
20
|
-
/** Verifies a bank slip from its QR payload string. */
|
|
21
|
-
async bankByPayload(payload, options) {
|
|
22
|
-
(0, call_js_1.requireString)(payload, 'payload');
|
|
23
|
-
return (0, call_js_1.call)(this.#t, 'BankSlipResult', {
|
|
24
|
-
...(0, call_js_1.requestOptions)(options),
|
|
25
|
-
api: 'v2',
|
|
26
|
-
method: 'POST',
|
|
27
|
-
path: '/verify/bank',
|
|
28
|
-
json: { payload, ...verifyFields(options) },
|
|
29
|
-
retry: 'billed',
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
/** Verifies a bank slip image (JPEG, PNG, GIF or WebP, up to 4 MB). */
|
|
33
|
-
async bankByImage(file, options) {
|
|
34
|
-
const prepared = await (0, files_js_1.prepareFile)(file);
|
|
35
|
-
return (0, call_js_1.call)(this.#t, 'BankSlipResult', {
|
|
36
|
-
...(0, call_js_1.requestOptions)(options),
|
|
37
|
-
api: 'v2',
|
|
38
|
-
method: 'POST',
|
|
39
|
-
path: '/verify/bank',
|
|
40
|
-
form: (0, call_js_1.imageForm)('image', prepared, verifyFields(options)),
|
|
41
|
-
retry: 'billed',
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
/** Verifies a base64 bank slip image. A `data:image/...;base64,` prefix is fine. */
|
|
45
|
-
async bankByBase64(base64, options) {
|
|
46
|
-
(0, call_js_1.requireString)(base64, 'base64');
|
|
47
|
-
return (0, call_js_1.call)(this.#t, 'BankSlipResult', {
|
|
48
|
-
...(0, call_js_1.requestOptions)(options),
|
|
49
|
-
api: 'v2',
|
|
50
|
-
method: 'POST',
|
|
51
|
-
path: '/verify/bank',
|
|
52
|
-
json: { base64, ...verifyFields(options) },
|
|
53
|
-
retry: 'billed',
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
/** Verifies a bank slip image the API downloads from a public http(s) URL. */
|
|
57
|
-
async bankByUrl(url, options) {
|
|
58
|
-
(0, call_js_1.requireString)(url, 'url');
|
|
59
|
-
return (0, call_js_1.call)(this.#t, 'BankSlipResult', {
|
|
60
|
-
...(0, call_js_1.requestOptions)(options),
|
|
61
|
-
api: 'v2',
|
|
62
|
-
method: 'POST',
|
|
63
|
-
path: '/verify/bank',
|
|
64
|
-
json: { url, ...verifyFields(options) },
|
|
65
|
-
retry: 'billed',
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
/** Verifies a TrueMoney Wallet slip image. */
|
|
69
|
-
async truewalletByImage(file, options) {
|
|
70
|
-
const prepared = await (0, files_js_1.prepareFile)(file);
|
|
71
|
-
return (0, call_js_1.call)(this.#t, 'TrueWalletResult', {
|
|
72
|
-
...(0, call_js_1.requestOptions)(options),
|
|
73
|
-
api: 'v2',
|
|
74
|
-
method: 'POST',
|
|
75
|
-
path: '/verify/truewallet',
|
|
76
|
-
form: (0, call_js_1.imageForm)('image', prepared, verifyFields(options)),
|
|
77
|
-
retry: 'billed',
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
/** Verifies a base64 TrueMoney Wallet slip image. */
|
|
81
|
-
async truewalletByBase64(base64, options) {
|
|
82
|
-
(0, call_js_1.requireString)(base64, 'base64');
|
|
83
|
-
return (0, call_js_1.call)(this.#t, 'TrueWalletResult', {
|
|
84
|
-
...(0, call_js_1.requestOptions)(options),
|
|
85
|
-
api: 'v2',
|
|
86
|
-
method: 'POST',
|
|
87
|
-
path: '/verify/truewallet',
|
|
88
|
-
json: { base64, ...verifyFields(options) },
|
|
89
|
-
retry: 'billed',
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/** Verifies a TrueMoney Wallet slip image from a public URL (up to 255 characters). */
|
|
93
|
-
async truewalletByUrl(url, options) {
|
|
94
|
-
(0, call_js_1.requireString)(url, 'url');
|
|
95
|
-
return (0, call_js_1.call)(this.#t, 'TrueWalletResult', {
|
|
96
|
-
...(0, call_js_1.requestOptions)(options),
|
|
97
|
-
api: 'v2',
|
|
98
|
-
method: 'POST',
|
|
99
|
-
path: '/verify/truewallet',
|
|
100
|
-
json: { url, ...verifyFields(options) },
|
|
101
|
-
retry: 'billed',
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
exports.Verify = Verify;
|
package/dist/cjs/version.js
DELETED
package/dist/cjs/webhooks.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WEBHOOK_SIGNATURE_HEADER = void 0;
|
|
4
|
-
exports.verifyWebhookSignature = verifyWebhookSignature;
|
|
5
|
-
exports.parseWebhookEvent = parseWebhookEvent;
|
|
6
|
-
exports.constructWebhookEvent = constructWebhookEvent;
|
|
7
|
-
const errors_js_1 = require("./core/errors.js");
|
|
8
|
-
const runtime_js_1 = require("./core/runtime.js");
|
|
9
|
-
const settings_js_1 = require("./core/settings.js");
|
|
10
|
-
const sha256_js_1 = require("./core/sha256.js");
|
|
11
|
-
const models_js_1 = require("./models.js");
|
|
12
|
-
/** Name of the header that carries the webhook signature. */
|
|
13
|
-
exports.WEBHOOK_SIGNATURE_HEADER = settings_js_1.settings.webhook.signatureHeader;
|
|
14
|
-
const PREFIX = settings_js_1.settings.webhook.signaturePrefix;
|
|
15
|
-
const SIGNATURE = /^[0-9a-f]{64}$/;
|
|
16
|
-
function toBytes(body) {
|
|
17
|
-
if (typeof body === 'string')
|
|
18
|
-
return new TextEncoder().encode(body);
|
|
19
|
-
if (body instanceof ArrayBuffer)
|
|
20
|
-
return new Uint8Array(body);
|
|
21
|
-
if (body instanceof Uint8Array)
|
|
22
|
-
return body;
|
|
23
|
-
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'webhook body must be a string or bytes');
|
|
24
|
-
}
|
|
25
|
-
function secretOrEnv(secret) {
|
|
26
|
-
const value = secret ?? (0, runtime_js_1.readEnv)(settings_js_1.settings.env.webhookSecret);
|
|
27
|
-
if (!value) {
|
|
28
|
-
throw new errors_js_1.InputError('INVALID_ARGUMENT', `no webhook secret: pass one or set ${settings_js_1.settings.env.webhookSecret}`);
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
}
|
|
32
|
-
function equalConstantTime(a, b) {
|
|
33
|
-
if (a.length !== b.length)
|
|
34
|
-
return false;
|
|
35
|
-
let diff = 0;
|
|
36
|
-
for (let i = 0; i < a.length; i++)
|
|
37
|
-
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
38
|
-
return diff === 0;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Checks a webhook signature: `sha256=` plus the lower-case hex HMAC-SHA256 of the raw body
|
|
42
|
-
* under your webhook secret, compared in constant time. Pass the body exactly as received,
|
|
43
|
-
* before any JSON parsing. A missing header gives false.
|
|
44
|
-
*
|
|
45
|
-
* @param secret Your branch's webhook secret. Defaults to the webhook secret env variable.
|
|
46
|
-
*/
|
|
47
|
-
function verifyWebhookSignature(rawBody, signatureHeader, secret) {
|
|
48
|
-
const key = secretOrEnv(secret);
|
|
49
|
-
const header = Array.isArray(signatureHeader) ? signatureHeader[0] : signatureHeader;
|
|
50
|
-
if (typeof header !== 'string' || !header.startsWith(PREFIX))
|
|
51
|
-
return false;
|
|
52
|
-
const given = header.slice(PREFIX.length);
|
|
53
|
-
if (!SIGNATURE.test(given))
|
|
54
|
-
return false;
|
|
55
|
-
const expected = (0, sha256_js_1.toHex)((0, sha256_js_1.hmacSha256)(new TextEncoder().encode(key), toBytes(rawBody)));
|
|
56
|
-
return equalConstantTime(given, expected);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Parses a webhook body into a {@link WebhookEvent} without checking the signature. Throws
|
|
60
|
-
* {@link InputError} when the body is not a JSON object with string `jobId`, `status` and
|
|
61
|
-
* `timestamp` fields.
|
|
62
|
-
*/
|
|
63
|
-
function parseWebhookEvent(rawBody) {
|
|
64
|
-
const text = typeof rawBody === 'string' ? rawBody : new TextDecoder().decode(toBytes(rawBody));
|
|
65
|
-
let value;
|
|
66
|
-
try {
|
|
67
|
-
value = JSON.parse(text);
|
|
68
|
-
}
|
|
69
|
-
catch (cause) {
|
|
70
|
-
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'webhook body is not JSON', { cause });
|
|
71
|
-
}
|
|
72
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
73
|
-
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'webhook body is not a JSON object');
|
|
74
|
-
}
|
|
75
|
-
const fields = value;
|
|
76
|
-
if (typeof fields.jobId !== 'string' ||
|
|
77
|
-
typeof fields.status !== 'string' ||
|
|
78
|
-
typeof fields.timestamp !== 'string') {
|
|
79
|
-
throw new errors_js_1.InputError('INVALID_ARGUMENT', 'webhook body must contain jobId, status and timestamp');
|
|
80
|
-
}
|
|
81
|
-
return (0, models_js_1.parseModel)('WebhookEvent', value);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Verifies the signature, then parses the body. Throws {@link WebhookSignatureError} when the
|
|
85
|
-
* signature is missing or wrong, and {@link InputError} when a signed body is not a webhook event.
|
|
86
|
-
*/
|
|
87
|
-
function constructWebhookEvent(rawBody, signatureHeader, secret) {
|
|
88
|
-
if (!verifyWebhookSignature(rawBody, signatureHeader, secret)) {
|
|
89
|
-
throw new errors_js_1.WebhookSignatureError(`webhook signature in ${exports.WEBHOOK_SIGNATURE_HEADER} is missing or does not match`);
|
|
90
|
-
}
|
|
91
|
-
return parseWebhookEvent(rawBody);
|
|
92
|
-
}
|
package/dist/esm/client.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { call, requestOptions } from './core/call.js';
|
|
2
|
-
import { InputError } from './core/errors.js';
|
|
3
|
-
import { Transport } from './core/http.js';
|
|
4
|
-
import { loadFs, readEnv, runtimeToken, sleep } from './core/runtime.js';
|
|
5
|
-
import { settings } from './core/settings.js';
|
|
6
|
-
import { BankAccounts } from './resources/bank-accounts.js';
|
|
7
|
-
import { Banks } from './resources/banks.js';
|
|
8
|
-
import { Jobs } from './resources/jobs.js';
|
|
9
|
-
import { V1 } from './resources/v1.js';
|
|
10
|
-
import { Verify } from './resources/verify.js';
|
|
11
|
-
import { VERSION } from './version.js';
|
|
12
|
-
function keyFromFile(path) {
|
|
13
|
-
let cached;
|
|
14
|
-
return () => {
|
|
15
|
-
cached ??= (async () => {
|
|
16
|
-
const fs = await loadFs();
|
|
17
|
-
if (!fs) {
|
|
18
|
-
throw new InputError('FILE_NOT_READABLE', `${settings.env.apiKeyFile} needs a runtime with a file system`);
|
|
19
|
-
}
|
|
20
|
-
let text;
|
|
21
|
-
try {
|
|
22
|
-
text = new TextDecoder().decode(await fs.readFile(path));
|
|
23
|
-
}
|
|
24
|
-
catch (cause) {
|
|
25
|
-
throw new InputError('FILE_NOT_READABLE', `cannot read ${settings.env.apiKeyFile}`, {
|
|
26
|
-
cause,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
const key = text.trim();
|
|
30
|
-
if (!key)
|
|
31
|
-
throw new InputError('INVALID_ARGUMENT', `${settings.env.apiKeyFile} is empty`);
|
|
32
|
-
return key;
|
|
33
|
-
})();
|
|
34
|
-
cached.catch(() => {
|
|
35
|
-
cached = undefined;
|
|
36
|
-
});
|
|
37
|
-
return cached;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function checkNumber(value, name, min) {
|
|
41
|
-
if (value !== undefined && (!Number.isFinite(value) || value < min)) {
|
|
42
|
-
throw new InputError('INVALID_ARGUMENT', `${name} must be a number >= ${min}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* API client. Create one and reuse it.
|
|
47
|
-
*
|
|
48
|
-
* ```ts
|
|
49
|
-
* const client = new Client({ apiKey: 'YOUR_API_KEY' })
|
|
50
|
-
* const result = await client.verify.bankByPayload(payload, { checkDuplicate: true })
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
export class Client {
|
|
54
|
-
/** v2 slip verification. */
|
|
55
|
-
verify;
|
|
56
|
-
/** Supported banks. */
|
|
57
|
-
banks;
|
|
58
|
-
/** Registered receiving accounts. */
|
|
59
|
-
bankAccounts;
|
|
60
|
-
/** Async verification jobs. */
|
|
61
|
-
jobs;
|
|
62
|
-
/** The v1 API. */
|
|
63
|
-
v1;
|
|
64
|
-
/** v2 base URL in use. */
|
|
65
|
-
v2BaseUrl;
|
|
66
|
-
/** v1 base URL in use. */
|
|
67
|
-
v1BaseUrl;
|
|
68
|
-
/** Per-attempt timeout in milliseconds. */
|
|
69
|
-
timeout;
|
|
70
|
-
/** Retries after the first attempt. */
|
|
71
|
-
maxRetries;
|
|
72
|
-
#transport;
|
|
73
|
-
constructor(options = {}) {
|
|
74
|
-
checkNumber(options.timeout, 'timeout', 1);
|
|
75
|
-
checkNumber(options.maxRetries, 'maxRetries', 0);
|
|
76
|
-
let apiKey;
|
|
77
|
-
const key = options.apiKey ?? readEnv(settings.env.apiKey);
|
|
78
|
-
if (key !== undefined) {
|
|
79
|
-
if (typeof key !== 'string' || key.trim() === '') {
|
|
80
|
-
throw new InputError('INVALID_ARGUMENT', 'apiKey must be a non-empty string');
|
|
81
|
-
}
|
|
82
|
-
const value = key.trim();
|
|
83
|
-
apiKey = () => Promise.resolve(value);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const file = readEnv(settings.env.apiKeyFile);
|
|
87
|
-
if (!file) {
|
|
88
|
-
throw new InputError('INVALID_ARGUMENT', `no API key: pass apiKey or set ${settings.env.apiKey} or ${settings.env.apiKeyFile}`);
|
|
89
|
-
}
|
|
90
|
-
apiKey = keyFromFile(file);
|
|
91
|
-
}
|
|
92
|
-
if (!options.fetch && typeof globalThis.fetch !== 'function') {
|
|
93
|
-
throw new InputError('INVALID_ARGUMENT', 'no fetch: pass options.fetch');
|
|
94
|
-
}
|
|
95
|
-
this.v2BaseUrl =
|
|
96
|
-
options.v2BaseUrl ?? readEnv(settings.env.v2BaseUrl) ?? settings.baseUrls.v2;
|
|
97
|
-
this.v1BaseUrl =
|
|
98
|
-
options.v1BaseUrl ?? readEnv(settings.env.v1BaseUrl) ?? settings.baseUrls.v1;
|
|
99
|
-
this.timeout = options.timeout ?? 30_000;
|
|
100
|
-
this.maxRetries = Math.floor(options.maxRetries ?? 2);
|
|
101
|
-
this.#transport = new Transport({
|
|
102
|
-
v1BaseUrl: this.v1BaseUrl,
|
|
103
|
-
v2BaseUrl: this.v2BaseUrl,
|
|
104
|
-
timeout: this.timeout,
|
|
105
|
-
maxRetries: this.maxRetries,
|
|
106
|
-
userAgent: `${settings.userAgentProduct}/${VERSION} ${runtimeToken()}`,
|
|
107
|
-
fetch: options.fetch ?? ((input, init) => globalThis.fetch(input, init)),
|
|
108
|
-
sleep: options.sleep ?? sleep,
|
|
109
|
-
random: options.random ?? Math.random,
|
|
110
|
-
apiKey,
|
|
111
|
-
});
|
|
112
|
-
this.verify = new Verify(this.#transport);
|
|
113
|
-
this.banks = new Banks(this.#transport);
|
|
114
|
-
this.bankAccounts = new BankAccounts(this.#transport);
|
|
115
|
-
this.jobs = new Jobs(this.#transport);
|
|
116
|
-
this.v1 = new V1(this.#transport);
|
|
117
|
-
}
|
|
118
|
-
/** The User-Agent header this client sends. */
|
|
119
|
-
get userAgent() {
|
|
120
|
-
return this.#transport.config.userAgent;
|
|
121
|
-
}
|
|
122
|
-
/** Application, branch, quota and account details of the API key. */
|
|
123
|
-
async getInfo(options) {
|
|
124
|
-
return call(this.#transport, 'Info', {
|
|
125
|
-
...requestOptions(options),
|
|
126
|
-
api: 'v2',
|
|
127
|
-
method: 'GET',
|
|
128
|
-
path: '/info',
|
|
129
|
-
retry: 'safe',
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
toJSON() {
|
|
133
|
-
return {
|
|
134
|
-
v2BaseUrl: this.v2BaseUrl,
|
|
135
|
-
v1BaseUrl: this.v1BaseUrl,
|
|
136
|
-
timeout: this.timeout,
|
|
137
|
-
maxRetries: this.maxRetries,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
toString() {
|
|
141
|
-
return `Client(${this.v2BaseUrl})`;
|
|
142
|
-
}
|
|
143
|
-
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
144
|
-
return `Client ${JSON.stringify(this.toJSON())}`;
|
|
145
|
-
}
|
|
146
|
-
}
|