@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,162 @@
|
|
|
1
|
+
import type { BankSlip, BatchSubmission, Job, TrueWalletSlip } from '../models.js';
|
|
2
|
+
import type { RateLimit } from './meta.js';
|
|
3
|
+
/** Base class of every error this SDK throws. */
|
|
4
|
+
export declare class EasySlipError extends Error {
|
|
5
|
+
constructor(message: string, options?: ErrorOptions);
|
|
6
|
+
toString(): string;
|
|
7
|
+
}
|
|
8
|
+
/** v1 `details`: a message, or one entry per rejected field. */
|
|
9
|
+
export type ErrorDetails = string | ReadonlyArray<{
|
|
10
|
+
readonly field: string;
|
|
11
|
+
readonly message: string;
|
|
12
|
+
}>;
|
|
13
|
+
/** Everything known about an HTTP error response. */
|
|
14
|
+
export interface ApiErrorInit {
|
|
15
|
+
status: number;
|
|
16
|
+
code: string | null;
|
|
17
|
+
message: string;
|
|
18
|
+
requestId: string | null;
|
|
19
|
+
/** The body as it may be shown: with the API key redacted. */
|
|
20
|
+
rawBody: string;
|
|
21
|
+
/** The body exactly as received, for {@link ApiError.sensitiveRawBody}. Defaults to `rawBody`. */
|
|
22
|
+
sensitiveRawBody?: string;
|
|
23
|
+
headers: Readonly<Record<string, string>>;
|
|
24
|
+
details?: ErrorDetails | null;
|
|
25
|
+
retryAfter?: number | null;
|
|
26
|
+
rateLimit?: RateLimit | null;
|
|
27
|
+
slip?: BankSlip | TrueWalletSlip | null;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The API answered with an error status. Subclasses tell the kind of error; `code` holds the
|
|
31
|
+
* exact API error code (for example `SLIP_NOT_FOUND` on v2, `slip_not_found` on v1).
|
|
32
|
+
*
|
|
33
|
+
* If the server echoed your API key back, the SDK has replaced it with `[REDACTED]` in the
|
|
34
|
+
* message, `rawBody`, `headers` and `details`, and `Authorization` header values are always
|
|
35
|
+
* `[REDACTED]`. Only {@link sensitiveRawBody} returns the body as received.
|
|
36
|
+
*/
|
|
37
|
+
export declare class ApiError extends EasySlipError {
|
|
38
|
+
#private;
|
|
39
|
+
/** HTTP status code. */
|
|
40
|
+
readonly status: number;
|
|
41
|
+
/** API error code, or null when the body was not JSON. */
|
|
42
|
+
readonly code: string | null;
|
|
43
|
+
/** `x-request-id` header, else `cf-ray`, else null. */
|
|
44
|
+
readonly requestId: string | null;
|
|
45
|
+
/** The response body, with the API key replaced by `[REDACTED]` if the server echoed it. */
|
|
46
|
+
readonly rawBody: string;
|
|
47
|
+
/** Response headers with lower-case names, redacted like `rawBody`. */
|
|
48
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
49
|
+
/** v1 `details`, when present. */
|
|
50
|
+
readonly details: ErrorDetails | null;
|
|
51
|
+
/** Seconds from the `Retry-After` header, when present. */
|
|
52
|
+
readonly retryAfter: number | null;
|
|
53
|
+
/** Rate limit counters, when the API sent them. */
|
|
54
|
+
readonly rateLimit: RateLimit | null;
|
|
55
|
+
constructor(init: ApiErrorInit);
|
|
56
|
+
/**
|
|
57
|
+
* The response body exactly as received, without redaction. It can contain your API key if
|
|
58
|
+
* the server echoed the request: treat it as a secret and never log it.
|
|
59
|
+
*/
|
|
60
|
+
sensitiveRawBody(): string;
|
|
61
|
+
toString(): string;
|
|
62
|
+
/** The redacted fields, for `JSON.stringify(error)`. */
|
|
63
|
+
toJSON(): Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
/** The API key is missing, malformed or unknown. */
|
|
66
|
+
export declare class AuthenticationError extends ApiError {
|
|
67
|
+
}
|
|
68
|
+
/** The key is valid but the account, branch or IP may not call this endpoint. */
|
|
69
|
+
export declare class PermissionDeniedError extends ApiError {
|
|
70
|
+
}
|
|
71
|
+
/** The service quota is used up. Nothing was charged. */
|
|
72
|
+
export declare class QuotaExceededError extends PermissionDeniedError {
|
|
73
|
+
}
|
|
74
|
+
/** The API rejected the request body, query or parameters. */
|
|
75
|
+
export declare class InvalidRequestError extends ApiError {
|
|
76
|
+
}
|
|
77
|
+
/** The image is missing, too large, not an image, or not a slip of the right kind. */
|
|
78
|
+
export declare class InvalidImageError extends InvalidRequestError {
|
|
79
|
+
}
|
|
80
|
+
/** An image URL or callback URL was refused. */
|
|
81
|
+
export declare class InvalidUrlError extends InvalidRequestError {
|
|
82
|
+
}
|
|
83
|
+
/** v1 only: the slip was verified before. `slip` holds the original slip. */
|
|
84
|
+
export declare class DuplicateSlipError extends InvalidRequestError {
|
|
85
|
+
/** The original slip the API sent with the error. */
|
|
86
|
+
readonly slip: BankSlip | TrueWalletSlip | null;
|
|
87
|
+
constructor(init: ApiErrorInit);
|
|
88
|
+
}
|
|
89
|
+
/** The resource does not exist or is not visible to this branch. */
|
|
90
|
+
export declare class NotFoundError extends ApiError {
|
|
91
|
+
}
|
|
92
|
+
/** The bank or wallet could not find the slip, or the image has no QR code. */
|
|
93
|
+
export declare class SlipNotFoundError extends NotFoundError {
|
|
94
|
+
}
|
|
95
|
+
/** Bangkok Bank slip from the last few minutes. Try again later. */
|
|
96
|
+
export declare class SlipPendingError extends NotFoundError {
|
|
97
|
+
}
|
|
98
|
+
/** The request conflicts with existing data, such as a duplicate bank account. */
|
|
99
|
+
export declare class ConflictError extends ApiError {
|
|
100
|
+
}
|
|
101
|
+
/** Too many requests. `retryAfter` says how long to wait. */
|
|
102
|
+
export declare class RateLimitError extends ApiError {
|
|
103
|
+
}
|
|
104
|
+
/** 5xx from the API or an upstream bank service. */
|
|
105
|
+
export declare class ServerError extends ApiError {
|
|
106
|
+
}
|
|
107
|
+
/** 503: a dependency is briefly unavailable. Usually sent with `retryAfter`. */
|
|
108
|
+
export declare class ServiceUnavailableError extends ServerError {
|
|
109
|
+
}
|
|
110
|
+
/** No HTTP response arrived (DNS, connect, TLS, connection reset). */
|
|
111
|
+
export declare class NetworkError extends EasySlipError {
|
|
112
|
+
}
|
|
113
|
+
/** The client timeout fired before a response arrived. */
|
|
114
|
+
export declare class RequestTimeoutError extends NetworkError {
|
|
115
|
+
}
|
|
116
|
+
/** Codes of {@link InputError}. */
|
|
117
|
+
export type InputErrorCode = 'FILE_TOO_LARGE' | 'UNSUPPORTED_FILE_TYPE' | 'EMPTY_FILE' | 'FILE_NOT_READABLE' | 'BATCH_TOO_LARGE' | 'EMPTY_BATCH' | 'INVALID_ARGUMENT';
|
|
118
|
+
/** The SDK rejected an argument before sending anything. */
|
|
119
|
+
export declare class InputError extends EasySlipError {
|
|
120
|
+
/** Why the argument was rejected. */
|
|
121
|
+
readonly code: InputErrorCode;
|
|
122
|
+
constructor(code: InputErrorCode, message: string, options?: ErrorOptions);
|
|
123
|
+
toString(): string;
|
|
124
|
+
}
|
|
125
|
+
/** A webhook signature header is missing, malformed or does not match the raw body. */
|
|
126
|
+
export declare class WebhookSignatureError extends EasySlipError {
|
|
127
|
+
}
|
|
128
|
+
/** `waitForJob` gave up before the job finished. */
|
|
129
|
+
export declare class JobWaitTimeoutError extends EasySlipError {
|
|
130
|
+
/** The last job state seen. */
|
|
131
|
+
readonly lastJob: Job | null;
|
|
132
|
+
constructor(message: string, lastJob: Job | null);
|
|
133
|
+
}
|
|
134
|
+
/** `submitBankMany` failed part way. Batches already accepted keep running. */
|
|
135
|
+
export declare class BatchSubmitError extends EasySlipError {
|
|
136
|
+
/** Batches the API accepted before the failure. */
|
|
137
|
+
readonly submitted: readonly BatchSubmission[];
|
|
138
|
+
/** 0-based index of the chunk of 100 that failed. */
|
|
139
|
+
readonly failedChunk: number;
|
|
140
|
+
constructor(submitted: readonly BatchSubmission[], failedChunk: number, cause: unknown);
|
|
141
|
+
}
|
|
142
|
+
declare const apiErrorClasses: {
|
|
143
|
+
ApiError: typeof ApiError;
|
|
144
|
+
AuthenticationError: typeof AuthenticationError;
|
|
145
|
+
PermissionDeniedError: typeof PermissionDeniedError;
|
|
146
|
+
QuotaExceededError: typeof QuotaExceededError;
|
|
147
|
+
InvalidRequestError: typeof InvalidRequestError;
|
|
148
|
+
InvalidImageError: typeof InvalidImageError;
|
|
149
|
+
InvalidUrlError: typeof InvalidUrlError;
|
|
150
|
+
DuplicateSlipError: typeof DuplicateSlipError;
|
|
151
|
+
NotFoundError: typeof NotFoundError;
|
|
152
|
+
SlipNotFoundError: typeof SlipNotFoundError;
|
|
153
|
+
SlipPendingError: typeof SlipPendingError;
|
|
154
|
+
ConflictError: typeof ConflictError;
|
|
155
|
+
RateLimitError: typeof RateLimitError;
|
|
156
|
+
ServerError: typeof ServerError;
|
|
157
|
+
ServiceUnavailableError: typeof ServiceUnavailableError;
|
|
158
|
+
};
|
|
159
|
+
/** Names of the error classes an HTTP error can map to. */
|
|
160
|
+
export type ApiErrorClassName = keyof typeof apiErrorClasses;
|
|
161
|
+
export declare function apiErrorClass(name: ApiErrorClassName): typeof ApiError;
|
|
162
|
+
export {};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** Base class of every error this SDK throws. */
|
|
2
|
+
export class EasySlipError extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = new.target.name;
|
|
6
|
+
}
|
|
7
|
+
toString() {
|
|
8
|
+
return `${this.name}: ${this.message}`;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The API answered with an error status. Subclasses tell the kind of error; `code` holds the
|
|
13
|
+
* exact API error code (for example `SLIP_NOT_FOUND` on v2, `slip_not_found` on v1).
|
|
14
|
+
*
|
|
15
|
+
* If the server echoed your API key back, the SDK has replaced it with `[REDACTED]` in the
|
|
16
|
+
* message, `rawBody`, `headers` and `details`, and `Authorization` header values are always
|
|
17
|
+
* `[REDACTED]`. Only {@link sensitiveRawBody} returns the body as received.
|
|
18
|
+
*/
|
|
19
|
+
export class ApiError extends EasySlipError {
|
|
20
|
+
/** HTTP status code. */
|
|
21
|
+
status;
|
|
22
|
+
/** API error code, or null when the body was not JSON. */
|
|
23
|
+
code;
|
|
24
|
+
/** `x-request-id` header, else `cf-ray`, else null. */
|
|
25
|
+
requestId;
|
|
26
|
+
/** The response body, with the API key replaced by `[REDACTED]` if the server echoed it. */
|
|
27
|
+
rawBody;
|
|
28
|
+
/** Response headers with lower-case names, redacted like `rawBody`. */
|
|
29
|
+
headers;
|
|
30
|
+
/** v1 `details`, when present. */
|
|
31
|
+
details;
|
|
32
|
+
/** Seconds from the `Retry-After` header, when present. */
|
|
33
|
+
retryAfter;
|
|
34
|
+
/** Rate limit counters, when the API sent them. */
|
|
35
|
+
rateLimit;
|
|
36
|
+
#sensitiveRawBody;
|
|
37
|
+
constructor(init) {
|
|
38
|
+
super(init.message);
|
|
39
|
+
this.status = init.status;
|
|
40
|
+
this.code = init.code;
|
|
41
|
+
this.requestId = init.requestId;
|
|
42
|
+
this.rawBody = init.rawBody;
|
|
43
|
+
this.#sensitiveRawBody = init.sensitiveRawBody ?? init.rawBody;
|
|
44
|
+
this.headers = init.headers;
|
|
45
|
+
this.details = init.details ?? null;
|
|
46
|
+
this.retryAfter = init.retryAfter ?? null;
|
|
47
|
+
this.rateLimit = init.rateLimit ?? null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The response body exactly as received, without redaction. It can contain your API key if
|
|
51
|
+
* the server echoed the request: treat it as a secret and never log it.
|
|
52
|
+
*/
|
|
53
|
+
sensitiveRawBody() {
|
|
54
|
+
return this.#sensitiveRawBody;
|
|
55
|
+
}
|
|
56
|
+
toString() {
|
|
57
|
+
return `${this.name}: [${this.status}${this.code ? ` ${this.code}` : ''}] ${this.message}`;
|
|
58
|
+
}
|
|
59
|
+
/** The redacted fields, for `JSON.stringify(error)`. */
|
|
60
|
+
toJSON() {
|
|
61
|
+
return {
|
|
62
|
+
name: this.name,
|
|
63
|
+
message: this.message,
|
|
64
|
+
status: this.status,
|
|
65
|
+
code: this.code,
|
|
66
|
+
requestId: this.requestId,
|
|
67
|
+
details: this.details,
|
|
68
|
+
retryAfter: this.retryAfter,
|
|
69
|
+
rateLimit: this.rateLimit,
|
|
70
|
+
headers: this.headers,
|
|
71
|
+
rawBody: this.rawBody,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** The API key is missing, malformed or unknown. */
|
|
76
|
+
export class AuthenticationError extends ApiError {
|
|
77
|
+
}
|
|
78
|
+
/** The key is valid but the account, branch or IP may not call this endpoint. */
|
|
79
|
+
export class PermissionDeniedError extends ApiError {
|
|
80
|
+
}
|
|
81
|
+
/** The service quota is used up. Nothing was charged. */
|
|
82
|
+
export class QuotaExceededError extends PermissionDeniedError {
|
|
83
|
+
}
|
|
84
|
+
/** The API rejected the request body, query or parameters. */
|
|
85
|
+
export class InvalidRequestError extends ApiError {
|
|
86
|
+
}
|
|
87
|
+
/** The image is missing, too large, not an image, or not a slip of the right kind. */
|
|
88
|
+
export class InvalidImageError extends InvalidRequestError {
|
|
89
|
+
}
|
|
90
|
+
/** An image URL or callback URL was refused. */
|
|
91
|
+
export class InvalidUrlError extends InvalidRequestError {
|
|
92
|
+
}
|
|
93
|
+
/** v1 only: the slip was verified before. `slip` holds the original slip. */
|
|
94
|
+
export class DuplicateSlipError extends InvalidRequestError {
|
|
95
|
+
/** The original slip the API sent with the error. */
|
|
96
|
+
slip;
|
|
97
|
+
constructor(init) {
|
|
98
|
+
super(init);
|
|
99
|
+
this.slip = init.slip ?? null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** The resource does not exist or is not visible to this branch. */
|
|
103
|
+
export class NotFoundError extends ApiError {
|
|
104
|
+
}
|
|
105
|
+
/** The bank or wallet could not find the slip, or the image has no QR code. */
|
|
106
|
+
export class SlipNotFoundError extends NotFoundError {
|
|
107
|
+
}
|
|
108
|
+
/** Bangkok Bank slip from the last few minutes. Try again later. */
|
|
109
|
+
export class SlipPendingError extends NotFoundError {
|
|
110
|
+
}
|
|
111
|
+
/** The request conflicts with existing data, such as a duplicate bank account. */
|
|
112
|
+
export class ConflictError extends ApiError {
|
|
113
|
+
}
|
|
114
|
+
/** Too many requests. `retryAfter` says how long to wait. */
|
|
115
|
+
export class RateLimitError extends ApiError {
|
|
116
|
+
}
|
|
117
|
+
/** 5xx from the API or an upstream bank service. */
|
|
118
|
+
export class ServerError extends ApiError {
|
|
119
|
+
}
|
|
120
|
+
/** 503: a dependency is briefly unavailable. Usually sent with `retryAfter`. */
|
|
121
|
+
export class ServiceUnavailableError extends ServerError {
|
|
122
|
+
}
|
|
123
|
+
/** No HTTP response arrived (DNS, connect, TLS, connection reset). */
|
|
124
|
+
export class NetworkError extends EasySlipError {
|
|
125
|
+
}
|
|
126
|
+
/** The client timeout fired before a response arrived. */
|
|
127
|
+
export class RequestTimeoutError extends NetworkError {
|
|
128
|
+
}
|
|
129
|
+
/** The SDK rejected an argument before sending anything. */
|
|
130
|
+
export class InputError extends EasySlipError {
|
|
131
|
+
/** Why the argument was rejected. */
|
|
132
|
+
code;
|
|
133
|
+
constructor(code, message, options) {
|
|
134
|
+
super(message, options);
|
|
135
|
+
this.code = code;
|
|
136
|
+
}
|
|
137
|
+
toString() {
|
|
138
|
+
return `${this.name}: [${this.code}] ${this.message}`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/** A webhook signature header is missing, malformed or does not match the raw body. */
|
|
142
|
+
export class WebhookSignatureError extends EasySlipError {
|
|
143
|
+
}
|
|
144
|
+
/** `waitForJob` gave up before the job finished. */
|
|
145
|
+
export class JobWaitTimeoutError extends EasySlipError {
|
|
146
|
+
/** The last job state seen. */
|
|
147
|
+
lastJob;
|
|
148
|
+
constructor(message, lastJob) {
|
|
149
|
+
super(message);
|
|
150
|
+
this.lastJob = lastJob;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** `submitBankMany` failed part way. Batches already accepted keep running. */
|
|
154
|
+
export class BatchSubmitError extends EasySlipError {
|
|
155
|
+
/** Batches the API accepted before the failure. */
|
|
156
|
+
submitted;
|
|
157
|
+
/** 0-based index of the chunk of 100 that failed. */
|
|
158
|
+
failedChunk;
|
|
159
|
+
constructor(submitted, failedChunk, cause) {
|
|
160
|
+
super(`batch chunk ${failedChunk} failed after ${submitted.length} accepted: ${String(cause instanceof Error ? cause.message : cause)}`, { cause });
|
|
161
|
+
this.submitted = submitted;
|
|
162
|
+
this.failedChunk = failedChunk;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const apiErrorClasses = {
|
|
166
|
+
ApiError,
|
|
167
|
+
AuthenticationError,
|
|
168
|
+
PermissionDeniedError,
|
|
169
|
+
QuotaExceededError,
|
|
170
|
+
InvalidRequestError,
|
|
171
|
+
InvalidImageError,
|
|
172
|
+
InvalidUrlError,
|
|
173
|
+
DuplicateSlipError,
|
|
174
|
+
NotFoundError,
|
|
175
|
+
SlipNotFoundError,
|
|
176
|
+
SlipPendingError,
|
|
177
|
+
ConflictError,
|
|
178
|
+
RateLimitError,
|
|
179
|
+
ServerError,
|
|
180
|
+
ServiceUnavailableError,
|
|
181
|
+
};
|
|
182
|
+
export function apiErrorClass(name) {
|
|
183
|
+
return apiErrorClasses[name];
|
|
184
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Largest image the API accepts, in bytes (4 MB). */
|
|
2
|
+
export declare const MAX_IMAGE_BYTES: number;
|
|
3
|
+
/**
|
|
4
|
+
* An image to upload: a file path (Node, Bun, Deno), a `file:` URL, bytes (`Uint8Array`,
|
|
5
|
+
* `Buffer`, `ArrayBuffer`), a `Blob` or `File`, a web `ReadableStream`, or any async iterable
|
|
6
|
+
* of byte chunks such as a Node readable stream.
|
|
7
|
+
*/
|
|
8
|
+
export type FileInput = string | URL | Uint8Array | ArrayBuffer | Blob | ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
|
|
9
|
+
export interface PreparedFile {
|
|
10
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
11
|
+
contentType: string;
|
|
12
|
+
filename: string;
|
|
13
|
+
}
|
|
14
|
+
/** Image type from the first bytes, or null when it is not JPEG, PNG, GIF or WebP. */
|
|
15
|
+
export declare function sniffImageType(bytes: Uint8Array): {
|
|
16
|
+
type: string;
|
|
17
|
+
ext: string;
|
|
18
|
+
} | null;
|
|
19
|
+
/** Reads an image input, checks its size and type, and names it `slip.<ext>`. */
|
|
20
|
+
export declare function prepareFile(input: FileInput, maxBytes?: number): Promise<PreparedFile>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { InputError } from './errors.js';
|
|
2
|
+
import { loadFs } from './runtime.js';
|
|
3
|
+
/** Largest image the API accepts, in bytes (4 MB). */
|
|
4
|
+
export const MAX_IMAGE_BYTES = 4 * 1024 * 1024;
|
|
5
|
+
const types = [
|
|
6
|
+
{
|
|
7
|
+
type: 'image/jpeg',
|
|
8
|
+
ext: 'jpg',
|
|
9
|
+
test: (b) => b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff,
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
type: 'image/png',
|
|
13
|
+
ext: 'png',
|
|
14
|
+
test: (b) => b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
type: 'image/gif',
|
|
18
|
+
ext: 'gif',
|
|
19
|
+
test: (b) => b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 && b[3] === 0x38,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
type: 'image/webp',
|
|
23
|
+
ext: 'webp',
|
|
24
|
+
test: (b) => ascii(b, 0, 4) === 'RIFF' && ascii(b, 8, 12) === 'WEBP' && b.length >= 12,
|
|
25
|
+
},
|
|
26
|
+
];
|
|
27
|
+
function ascii(bytes, start, end) {
|
|
28
|
+
return String.fromCharCode(...bytes.subarray(start, end));
|
|
29
|
+
}
|
|
30
|
+
/** Image type from the first bytes, or null when it is not JPEG, PNG, GIF or WebP. */
|
|
31
|
+
export function sniffImageType(bytes) {
|
|
32
|
+
const hit = types.find((t) => t.test(bytes));
|
|
33
|
+
return hit ? { type: hit.type, ext: hit.ext } : null;
|
|
34
|
+
}
|
|
35
|
+
function tooLarge(size) {
|
|
36
|
+
return new InputError('FILE_TOO_LARGE', `file is ${size} bytes; the API accepts at most ${MAX_IMAGE_BYTES} bytes`);
|
|
37
|
+
}
|
|
38
|
+
async function collect(chunks, maxBytes) {
|
|
39
|
+
const parts = [];
|
|
40
|
+
let size = 0;
|
|
41
|
+
const encoder = new TextEncoder();
|
|
42
|
+
for await (const chunk of chunks) {
|
|
43
|
+
const part = typeof chunk === 'string' ? encoder.encode(chunk) : chunk;
|
|
44
|
+
size += part.byteLength;
|
|
45
|
+
if (size > maxBytes)
|
|
46
|
+
throw tooLarge(size);
|
|
47
|
+
parts.push(part);
|
|
48
|
+
}
|
|
49
|
+
const out = new Uint8Array(size);
|
|
50
|
+
let offset = 0;
|
|
51
|
+
for (const part of parts) {
|
|
52
|
+
out.set(part, offset);
|
|
53
|
+
offset += part.byteLength;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
async function* readStream(stream) {
|
|
58
|
+
const reader = stream.getReader();
|
|
59
|
+
try {
|
|
60
|
+
while (true) {
|
|
61
|
+
const { done, value } = await reader.read();
|
|
62
|
+
if (done)
|
|
63
|
+
return;
|
|
64
|
+
yield value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
reader.releaseLock();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function readPath(path, maxBytes) {
|
|
72
|
+
const fs = await loadFs();
|
|
73
|
+
if (!fs) {
|
|
74
|
+
throw new InputError('FILE_NOT_READABLE', 'file paths need a runtime with a file system; pass bytes, a Blob or a stream');
|
|
75
|
+
}
|
|
76
|
+
let info;
|
|
77
|
+
try {
|
|
78
|
+
info = await fs.stat(path);
|
|
79
|
+
}
|
|
80
|
+
catch (cause) {
|
|
81
|
+
throw new InputError('FILE_NOT_READABLE', `cannot read ${String(path)}`, { cause });
|
|
82
|
+
}
|
|
83
|
+
if (!info.isFile())
|
|
84
|
+
throw new InputError('FILE_NOT_READABLE', `not a file: ${String(path)}`);
|
|
85
|
+
if (info.size > maxBytes)
|
|
86
|
+
throw tooLarge(info.size);
|
|
87
|
+
try {
|
|
88
|
+
return await fs.readFile(path);
|
|
89
|
+
}
|
|
90
|
+
catch (cause) {
|
|
91
|
+
throw new InputError('FILE_NOT_READABLE', `cannot read ${String(path)}`, { cause });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function readBytes(input, maxBytes) {
|
|
95
|
+
if (typeof input === 'string' || input instanceof URL)
|
|
96
|
+
return readPath(input, maxBytes);
|
|
97
|
+
if (input instanceof Uint8Array)
|
|
98
|
+
return input;
|
|
99
|
+
if (input instanceof ArrayBuffer)
|
|
100
|
+
return new Uint8Array(input);
|
|
101
|
+
if (typeof Blob !== 'undefined' && input instanceof Blob) {
|
|
102
|
+
if (input.size > maxBytes)
|
|
103
|
+
throw tooLarge(input.size);
|
|
104
|
+
return new Uint8Array(await input.arrayBuffer());
|
|
105
|
+
}
|
|
106
|
+
if (typeof ReadableStream !== 'undefined' && input instanceof ReadableStream) {
|
|
107
|
+
return collect(readStream(input), maxBytes);
|
|
108
|
+
}
|
|
109
|
+
if (input && typeof input[Symbol.asyncIterator] === 'function') {
|
|
110
|
+
return collect(input, maxBytes);
|
|
111
|
+
}
|
|
112
|
+
throw new InputError('INVALID_ARGUMENT', 'file must be a path, URL, Uint8Array, ArrayBuffer, Blob or stream');
|
|
113
|
+
}
|
|
114
|
+
/** Reads an image input, checks its size and type, and names it `slip.<ext>`. */
|
|
115
|
+
export async function prepareFile(input, maxBytes = MAX_IMAGE_BYTES) {
|
|
116
|
+
const raw = await readBytes(input, maxBytes);
|
|
117
|
+
if (raw.byteLength === 0)
|
|
118
|
+
throw new InputError('EMPTY_FILE', 'file is empty');
|
|
119
|
+
if (raw.byteLength > maxBytes)
|
|
120
|
+
throw tooLarge(raw.byteLength);
|
|
121
|
+
const kind = sniffImageType(raw);
|
|
122
|
+
if (!kind) {
|
|
123
|
+
throw new InputError('UNSUPPORTED_FILE_TYPE', 'file is not a JPEG, PNG, GIF or WebP image');
|
|
124
|
+
}
|
|
125
|
+
return { bytes: new Uint8Array(raw), contentType: kind.type, filename: `slip.${kind.ext}` };
|
|
126
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type ResponseMeta } from './meta.js';
|
|
2
|
+
import { type Redactor } from './redact.js';
|
|
3
|
+
/** Which API version a call goes to. */
|
|
4
|
+
export type ApiVersion = 'v1' | 'v2';
|
|
5
|
+
/**
|
|
6
|
+
* How a call may be retried.
|
|
7
|
+
* - `safe`: reads and idempotent calls. Retried on network errors, 429 and 5xx.
|
|
8
|
+
* - `write`: creates and changes. Retried only when the request never left, and on 429.
|
|
9
|
+
* - `billed`: calls that can spend quota. Same rule as `write`, so a slip is never billed twice.
|
|
10
|
+
*/
|
|
11
|
+
export type RetryClass = 'safe' | 'write' | 'billed';
|
|
12
|
+
/** Options every call accepts. */
|
|
13
|
+
export interface RequestOptions {
|
|
14
|
+
/** Cancels the call, including any wait between retries. */
|
|
15
|
+
signal?: AbortSignal;
|
|
16
|
+
/** Per-attempt timeout in milliseconds for this call. */
|
|
17
|
+
timeout?: number;
|
|
18
|
+
/** Retries for this call. */
|
|
19
|
+
maxRetries?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface RequestSpec extends RequestOptions {
|
|
22
|
+
api: ApiVersion;
|
|
23
|
+
method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
24
|
+
path: string;
|
|
25
|
+
query?: Record<string, string | undefined>;
|
|
26
|
+
json?: unknown;
|
|
27
|
+
form?: FormData;
|
|
28
|
+
retry: RetryClass;
|
|
29
|
+
/** Which slip model a v1 `duplicate_slip` error carries. */
|
|
30
|
+
duplicateSlip?: 'BankSlip' | 'TrueWalletSlip';
|
|
31
|
+
}
|
|
32
|
+
export interface RawResponse {
|
|
33
|
+
data: unknown;
|
|
34
|
+
meta: ResponseMeta;
|
|
35
|
+
}
|
|
36
|
+
/** The part of `fetch` the SDK uses. */
|
|
37
|
+
export type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
|
|
38
|
+
export interface TransportConfig {
|
|
39
|
+
v1BaseUrl: string;
|
|
40
|
+
v2BaseUrl: string;
|
|
41
|
+
timeout: number;
|
|
42
|
+
maxRetries: number;
|
|
43
|
+
userAgent: string;
|
|
44
|
+
fetch: FetchLike;
|
|
45
|
+
sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
46
|
+
random: () => number;
|
|
47
|
+
apiKey: () => Promise<string>;
|
|
48
|
+
}
|
|
49
|
+
/** True when a fetch failure happened before the request was sent (DNS, connect, TLS). */
|
|
50
|
+
export declare function isConnectFailure(error: unknown): boolean;
|
|
51
|
+
/** Backoff before retry number `attempt` (0-based), in milliseconds. */
|
|
52
|
+
export declare function backoffDelay(attempt: number, random?: () => number): number;
|
|
53
|
+
/**
|
|
54
|
+
* Turns one HTTP response into `{ data, meta }`, or throws the mapped {@link ApiError}.
|
|
55
|
+
* `redact` removes the API key from everything taken from the response.
|
|
56
|
+
* Exported for the conformance tests; SDK users never need it.
|
|
57
|
+
*/
|
|
58
|
+
export declare function decodeResponse(api: ApiVersion, status: number, rawHeaders: Headers | Record<string, string>, text: string, duplicateSlip?: RequestSpec['duplicateSlip'], redact?: Redactor): RawResponse;
|
|
59
|
+
/** Sends requests with auth, timeouts and the retry rules. Holds the API key privately. */
|
|
60
|
+
export declare class Transport {
|
|
61
|
+
#private;
|
|
62
|
+
constructor(config: TransportConfig);
|
|
63
|
+
get config(): Readonly<Omit<TransportConfig, 'apiKey'>>;
|
|
64
|
+
request(spec: RequestSpec): Promise<RawResponse>;
|
|
65
|
+
}
|