@tungoai/sdk 0.0.0-stage → 0.2.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 +40 -0
- package/LICENSE +21 -0
- package/README.kk.md +327 -0
- package/README.md +346 -2
- package/README.ru.md +327 -0
- package/dist/cjs/chat.d.ts +85 -0
- package/dist/cjs/chat.js +151 -0
- package/dist/cjs/client.d.ts +116 -0
- package/dist/cjs/client.js +223 -0
- package/dist/cjs/core.d.ts +210 -0
- package/dist/cjs/core.js +605 -0
- package/dist/cjs/errors.d.ts +212 -0
- package/dist/cjs/errors.js +410 -0
- package/dist/cjs/generated/resources.d.ts +1059 -0
- package/dist/cjs/generated/resources.js +1064 -0
- package/dist/cjs/generated/types.d.ts +4927 -0
- package/dist/cjs/generated/types.js +11 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +80 -0
- package/dist/cjs/json.d.ts +10 -0
- package/dist/cjs/json.js +120 -0
- package/dist/cjs/money.d.ts +32 -0
- package/dist/cjs/money.js +141 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pagination.d.ts +47 -0
- package/dist/cjs/pagination.js +113 -0
- package/dist/cjs/streaming.d.ts +74 -0
- package/dist/cjs/streaming.js +253 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +6 -0
- package/dist/cjs/webhooks.d.ts +74 -0
- package/dist/cjs/webhooks.js +248 -0
- package/dist/esm/chat.d.ts +85 -0
- package/dist/esm/chat.js +143 -0
- package/dist/esm/client.d.ts +116 -0
- package/dist/esm/client.js +219 -0
- package/dist/esm/core.d.ts +210 -0
- package/dist/esm/core.js +589 -0
- package/dist/esm/errors.d.ts +212 -0
- package/dist/esm/errors.js +381 -0
- package/dist/esm/generated/resources.d.ts +1059 -0
- package/dist/esm/generated/resources.js +1048 -0
- package/dist/esm/generated/types.d.ts +4927 -0
- package/dist/esm/generated/types.js +10 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/json.d.ts +10 -0
- package/dist/esm/json.js +116 -0
- package/dist/esm/money.d.ts +32 -0
- package/dist/esm/money.js +137 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/pagination.d.ts +47 -0
- package/dist/esm/pagination.js +107 -0
- package/dist/esm/streaming.d.ts +74 -0
- package/dist/esm/streaming.js +247 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/webhooks.d.ts +74 -0
- package/dist/esm/webhooks.js +239 -0
- package/package.json +67 -4
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/** One entry of `error.fields` in the API error envelope. */
|
|
2
|
+
export interface APIFieldError {
|
|
3
|
+
/** Path of the invalid field, e.g. `lines.0.amount`. */
|
|
4
|
+
path: string;
|
|
5
|
+
/** Machine-readable reason. */
|
|
6
|
+
code: string;
|
|
7
|
+
/** Human-readable explanation. */
|
|
8
|
+
message: string;
|
|
9
|
+
}
|
|
10
|
+
/** Rate-limit information taken from the `x-ratelimit-*` headers of one response. */
|
|
11
|
+
export interface RateLimit {
|
|
12
|
+
/** Requests allowed in the current window. */
|
|
13
|
+
limit: number;
|
|
14
|
+
/** Requests left in the current window. */
|
|
15
|
+
remaining: number;
|
|
16
|
+
/** Seconds until the window resets. */
|
|
17
|
+
reset: number;
|
|
18
|
+
}
|
|
19
|
+
/** The values of `error.type` in the API error envelope. */
|
|
20
|
+
export type APIErrorType = 'invalid_request_error' | 'authentication_error' | 'payment_required_error' | 'permission_error' | 'not_found_error' | 'conflict_error' | 'gone_error' | 'limit_error' | 'api_error' | 'unavailable_error';
|
|
21
|
+
/** Base class of every error thrown by the SDK. */
|
|
22
|
+
export declare class TungoError extends Error {
|
|
23
|
+
constructor(message: string, options?: {
|
|
24
|
+
cause?: unknown;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** The client is misconfigured (for example no API key): raised before any request is made. */
|
|
28
|
+
export declare class ConfigError extends TungoError {
|
|
29
|
+
constructor(message: string, options?: {
|
|
30
|
+
cause?: unknown;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** An argument is invalid (for example an empty path parameter): raised before any request is made. */
|
|
34
|
+
export declare class ValidationError extends TungoError {
|
|
35
|
+
constructor(message: string, options?: {
|
|
36
|
+
cause?: unknown;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** The request could not be sent or the connection broke before a complete response arrived. */
|
|
40
|
+
export declare class ConnectionError extends TungoError {
|
|
41
|
+
constructor(message: string, options?: {
|
|
42
|
+
cause?: unknown;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A deadline passed: the request timeout (which covers reading the body), the idle timeout of a
|
|
47
|
+
* stream, or the deadline of a wait helper. It is a local error, never an HTTP 408.
|
|
48
|
+
*/
|
|
49
|
+
export declare class TimeoutError extends TungoError {
|
|
50
|
+
/** The deadline that passed, in milliseconds. */
|
|
51
|
+
readonly timeout: number;
|
|
52
|
+
constructor(message: string, timeout: number, options?: {
|
|
53
|
+
cause?: unknown;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/** The caller cancelled the operation through an `AbortSignal`. The signal's reason is the `cause`. */
|
|
57
|
+
export declare class AbortError extends TungoError {
|
|
58
|
+
constructor(message?: string, options?: {
|
|
59
|
+
cause?: unknown;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A successful response whose body is not what the contract promises: not JSON, truncated, empty,
|
|
64
|
+
* not an event stream where one was requested, or a chat stream that ended without its `done` event.
|
|
65
|
+
*/
|
|
66
|
+
export declare class DecodeError extends TungoError {
|
|
67
|
+
/** HTTP status of the response; undefined when the data did not come from a response of this SDK. */
|
|
68
|
+
readonly status: number | undefined;
|
|
69
|
+
/** Request id from the `x-request-id` header, when present. */
|
|
70
|
+
readonly requestId: string | null;
|
|
71
|
+
/** The body as received (possibly truncated to 2000 characters). */
|
|
72
|
+
readonly body: string;
|
|
73
|
+
/** For a chat stream that ended before completion: the turn as accumulated so far. */
|
|
74
|
+
partialResult: unknown;
|
|
75
|
+
constructor(message: string, init: {
|
|
76
|
+
status?: number | undefined;
|
|
77
|
+
requestId: string | null;
|
|
78
|
+
body: string;
|
|
79
|
+
cause?: unknown;
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** Why a webhook was rejected. */
|
|
83
|
+
export type WebhookVerificationReason = 'invalid_tolerance' | 'invalid_secret' | 'missing_header' | 'malformed_timestamp' | 'timestamp_too_old' | 'timestamp_too_new' | 'signature_mismatch' | 'invalid_payload';
|
|
84
|
+
/** A webhook failed verification. Never trust the payload when this is thrown. */
|
|
85
|
+
export declare class WebhookVerificationError extends TungoError {
|
|
86
|
+
readonly reason: WebhookVerificationReason;
|
|
87
|
+
constructor(reason: WebhookVerificationReason, message: string, options?: {
|
|
88
|
+
cause?: unknown;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/** A money value cannot be converted (malformed text, NaN, out of the 64-bit range). */
|
|
92
|
+
export declare class MoneyError extends TungoError {
|
|
93
|
+
constructor(message: string);
|
|
94
|
+
}
|
|
95
|
+
/** Everything an {@link APIError} is built from. */
|
|
96
|
+
export interface APIErrorInit {
|
|
97
|
+
/** HTTP status; undefined for an error reported inside a successful response (a chat turn). */
|
|
98
|
+
status: number | undefined;
|
|
99
|
+
type: string;
|
|
100
|
+
code: string | null;
|
|
101
|
+
/** The message sent by the API (or a generic text when the body is not the error envelope). */
|
|
102
|
+
detail: string;
|
|
103
|
+
requestId: string | null;
|
|
104
|
+
fields: APIFieldError[];
|
|
105
|
+
retryAfter: number | null;
|
|
106
|
+
rateLimit: RateLimit | null;
|
|
107
|
+
headers: Headers;
|
|
108
|
+
body: string;
|
|
109
|
+
extra: Record<string, unknown>;
|
|
110
|
+
envelope: boolean;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The API answered with an error status, or a chat turn reported an error (an `error` event of a
|
|
114
|
+
* chat stream, or `errors` of a non-streaming turn). Subclasses are chosen by `error.type`,
|
|
115
|
+
* falling back to the HTTP status when the body is not the API's error envelope.
|
|
116
|
+
*/
|
|
117
|
+
export declare class APIError extends TungoError {
|
|
118
|
+
/**
|
|
119
|
+
* HTTP status code. Undefined when the error was reported inside a successful (2xx) response:
|
|
120
|
+
* the chat helpers throw the `error` of a turn with the class that matches its `error.type`,
|
|
121
|
+
* and no HTTP error status exists for it.
|
|
122
|
+
*/
|
|
123
|
+
readonly status: number | undefined;
|
|
124
|
+
/** `error.type` as sent by the API (possibly a value this SDK version does not know), or the status fallback. */
|
|
125
|
+
readonly type: string;
|
|
126
|
+
/** `error.code`, a stable machine-readable code; null when absent. */
|
|
127
|
+
readonly code: string | null;
|
|
128
|
+
/** `error.message` exactly as sent by the API. */
|
|
129
|
+
readonly detail: string;
|
|
130
|
+
/** Request id: `request_id` of the body, else the `x-request-id` header. Quote it to support. */
|
|
131
|
+
readonly requestId: string | null;
|
|
132
|
+
/** Per-field validation errors (`error.fields`); empty when absent. */
|
|
133
|
+
readonly fields: APIFieldError[];
|
|
134
|
+
/** Seconds the server asked to wait (`retry-after-ms` / `Retry-After`), or null. */
|
|
135
|
+
readonly retryAfter: number | null;
|
|
136
|
+
/** Rate-limit headers of this response, or null. */
|
|
137
|
+
readonly rateLimit: RateLimit | null;
|
|
138
|
+
/** Response headers. */
|
|
139
|
+
readonly headers: Headers;
|
|
140
|
+
/** Raw response body. */
|
|
141
|
+
readonly body: string;
|
|
142
|
+
/** Every other property of `error` (for example `resets_at`, `permission`, `limit_kind`). */
|
|
143
|
+
readonly extra: Record<string, unknown>;
|
|
144
|
+
/** True when the body was the API's error envelope. */
|
|
145
|
+
readonly envelope: boolean;
|
|
146
|
+
/** For errors raised by chat helpers: what had been accumulated before the error. */
|
|
147
|
+
partialResult: unknown;
|
|
148
|
+
constructor(init: APIErrorInit);
|
|
149
|
+
}
|
|
150
|
+
/** `invalid_request_error` (usually 400): the request is malformed or failed validation. */
|
|
151
|
+
export declare class InvalidRequestError extends APIError {
|
|
152
|
+
constructor(init: APIErrorInit);
|
|
153
|
+
}
|
|
154
|
+
/** `authentication_error` (401): the API key is missing, wrong, expired or revoked. */
|
|
155
|
+
export declare class AuthenticationError extends APIError {
|
|
156
|
+
constructor(init: APIErrorInit);
|
|
157
|
+
}
|
|
158
|
+
/** `payment_required_error` (402): the balance is exhausted. */
|
|
159
|
+
export declare class PaymentRequiredError extends APIError {
|
|
160
|
+
constructor(init: APIErrorInit);
|
|
161
|
+
}
|
|
162
|
+
/** `permission_error` (403): the key lacks a permission. */
|
|
163
|
+
export declare class PermissionError extends APIError {
|
|
164
|
+
constructor(init: APIErrorInit);
|
|
165
|
+
}
|
|
166
|
+
/** `not_found_error` (404). */
|
|
167
|
+
export declare class NotFoundError extends APIError {
|
|
168
|
+
constructor(init: APIErrorInit);
|
|
169
|
+
}
|
|
170
|
+
/** `conflict_error` (409). */
|
|
171
|
+
export declare class ConflictError extends APIError {
|
|
172
|
+
constructor(init: APIErrorInit);
|
|
173
|
+
}
|
|
174
|
+
/** `gone_error` (410): the resource existed and was removed. */
|
|
175
|
+
export declare class GoneError extends APIError {
|
|
176
|
+
constructor(init: APIErrorInit);
|
|
177
|
+
}
|
|
178
|
+
/** `limit_error` (429): a rate limit or a plan limit was reached. See `retryAfter` and `extra.resets_at`. */
|
|
179
|
+
export declare class LimitError extends APIError {
|
|
180
|
+
constructor(init: APIErrorInit);
|
|
181
|
+
}
|
|
182
|
+
/** `api_error` (500): an unexpected error on the server. */
|
|
183
|
+
export declare class InternalServerError extends APIError {
|
|
184
|
+
constructor(init: APIErrorInit);
|
|
185
|
+
}
|
|
186
|
+
/** `unavailable_error` (502, 503, 504): the service is temporarily unavailable. */
|
|
187
|
+
export declare class UnavailableError extends APIError {
|
|
188
|
+
constructor(init: APIErrorInit);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Parses the delay the server asked for: `retry-after-ms` (milliseconds) wins over `Retry-After`
|
|
192
|
+
* (seconds, possibly fractional, or an HTTP-date). Returns seconds, or null when there is no usable value.
|
|
193
|
+
*/
|
|
194
|
+
export declare function parseRetryAfter(headers: Headers, nowMs?: number): number | null;
|
|
195
|
+
/** Reads the `x-ratelimit-*` headers of one response. */
|
|
196
|
+
export declare function parseRateLimit(headers: Headers): RateLimit | null;
|
|
197
|
+
/** The `error.type` used when the body is not the API's error envelope. */
|
|
198
|
+
export declare function errorTypeForStatus(status: number): APIErrorType;
|
|
199
|
+
/** The pieces of an error object `{ type, message, code, fields, ... }` of the API. */
|
|
200
|
+
export interface ParsedErrorDetail {
|
|
201
|
+
type: string;
|
|
202
|
+
detail: string;
|
|
203
|
+
code: string | null;
|
|
204
|
+
fields: APIFieldError[];
|
|
205
|
+
extra: Record<string, unknown>;
|
|
206
|
+
}
|
|
207
|
+
/** Splits an `error` object of the envelope (or of a chat `error` event); null when it is not one. */
|
|
208
|
+
export declare function parseErrorDetail(value: unknown): ParsedErrorDetail | null;
|
|
209
|
+
/** Builds the error of the class that matches `init.type`, falling back to the status. */
|
|
210
|
+
export declare function makeAPIError(init: APIErrorInit): APIError;
|
|
211
|
+
/** Turns an HTTP error response into the matching {@link APIError}. Never throws, whatever the body is. */
|
|
212
|
+
export declare function apiErrorFromResponse(status: number, headers: Headers, body: string, nowMs?: number): APIError;
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// Code generated by tungo-sdkgen. DO NOT EDIT.
|
|
2
|
+
/** Base class of every error thrown by the SDK. */
|
|
3
|
+
export class TungoError extends Error {
|
|
4
|
+
constructor(message, options) {
|
|
5
|
+
super(message, options !== undefined && options.cause !== undefined ? { cause: options.cause } : undefined);
|
|
6
|
+
this.name = 'TungoError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** The client is misconfigured (for example no API key): raised before any request is made. */
|
|
10
|
+
export class ConfigError extends TungoError {
|
|
11
|
+
constructor(message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = 'ConfigError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** An argument is invalid (for example an empty path parameter): raised before any request is made. */
|
|
17
|
+
export class ValidationError extends TungoError {
|
|
18
|
+
constructor(message, options) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
this.name = 'ValidationError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** The request could not be sent or the connection broke before a complete response arrived. */
|
|
24
|
+
export class ConnectionError extends TungoError {
|
|
25
|
+
constructor(message, options) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.name = 'ConnectionError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A deadline passed: the request timeout (which covers reading the body), the idle timeout of a
|
|
32
|
+
* stream, or the deadline of a wait helper. It is a local error, never an HTTP 408.
|
|
33
|
+
*/
|
|
34
|
+
export class TimeoutError extends TungoError {
|
|
35
|
+
/** The deadline that passed, in milliseconds. */
|
|
36
|
+
timeout;
|
|
37
|
+
constructor(message, timeout, options) {
|
|
38
|
+
super(message, options);
|
|
39
|
+
this.name = 'TimeoutError';
|
|
40
|
+
this.timeout = timeout;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The caller cancelled the operation through an `AbortSignal`. The signal's reason is the `cause`. */
|
|
44
|
+
export class AbortError extends TungoError {
|
|
45
|
+
constructor(message = 'The operation was aborted', options) {
|
|
46
|
+
super(message, options);
|
|
47
|
+
this.name = 'AbortError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A successful response whose body is not what the contract promises: not JSON, truncated, empty,
|
|
52
|
+
* not an event stream where one was requested, or a chat stream that ended without its `done` event.
|
|
53
|
+
*/
|
|
54
|
+
export class DecodeError extends TungoError {
|
|
55
|
+
/** HTTP status of the response; undefined when the data did not come from a response of this SDK. */
|
|
56
|
+
status;
|
|
57
|
+
/** Request id from the `x-request-id` header, when present. */
|
|
58
|
+
requestId;
|
|
59
|
+
/** The body as received (possibly truncated to 2000 characters). */
|
|
60
|
+
body;
|
|
61
|
+
/** For a chat stream that ended before completion: the turn as accumulated so far. */
|
|
62
|
+
partialResult;
|
|
63
|
+
constructor(message, init) {
|
|
64
|
+
super(message, { cause: init.cause });
|
|
65
|
+
this.name = 'DecodeError';
|
|
66
|
+
this.status = init.status;
|
|
67
|
+
this.requestId = init.requestId;
|
|
68
|
+
this.body = init.body.length > 2000 ? init.body.slice(0, 2000) : init.body;
|
|
69
|
+
this.partialResult = undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** A webhook failed verification. Never trust the payload when this is thrown. */
|
|
73
|
+
export class WebhookVerificationError extends TungoError {
|
|
74
|
+
reason;
|
|
75
|
+
constructor(reason, message, options) {
|
|
76
|
+
super(message, options);
|
|
77
|
+
this.name = 'WebhookVerificationError';
|
|
78
|
+
this.reason = reason;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** A money value cannot be converted (malformed text, NaN, out of the 64-bit range). */
|
|
82
|
+
export class MoneyError extends TungoError {
|
|
83
|
+
constructor(message) {
|
|
84
|
+
super(message);
|
|
85
|
+
this.name = 'MoneyError';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function describe(init) {
|
|
89
|
+
let text = init.status === undefined ? 'Tungo chat turn error ' + init.type : 'Tungo API error ' + init.status + ' ' + init.type;
|
|
90
|
+
if (init.code !== null)
|
|
91
|
+
text += ' (' + init.code + ')';
|
|
92
|
+
text += ': ' + init.detail;
|
|
93
|
+
if (init.requestId !== null)
|
|
94
|
+
text += ' [request id ' + init.requestId + ']';
|
|
95
|
+
return text;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The API answered with an error status, or a chat turn reported an error (an `error` event of a
|
|
99
|
+
* chat stream, or `errors` of a non-streaming turn). Subclasses are chosen by `error.type`,
|
|
100
|
+
* falling back to the HTTP status when the body is not the API's error envelope.
|
|
101
|
+
*/
|
|
102
|
+
export class APIError extends TungoError {
|
|
103
|
+
/**
|
|
104
|
+
* HTTP status code. Undefined when the error was reported inside a successful (2xx) response:
|
|
105
|
+
* the chat helpers throw the `error` of a turn with the class that matches its `error.type`,
|
|
106
|
+
* and no HTTP error status exists for it.
|
|
107
|
+
*/
|
|
108
|
+
status;
|
|
109
|
+
/** `error.type` as sent by the API (possibly a value this SDK version does not know), or the status fallback. */
|
|
110
|
+
type;
|
|
111
|
+
/** `error.code`, a stable machine-readable code; null when absent. */
|
|
112
|
+
code;
|
|
113
|
+
/** `error.message` exactly as sent by the API. */
|
|
114
|
+
detail;
|
|
115
|
+
/** Request id: `request_id` of the body, else the `x-request-id` header. Quote it to support. */
|
|
116
|
+
requestId;
|
|
117
|
+
/** Per-field validation errors (`error.fields`); empty when absent. */
|
|
118
|
+
fields;
|
|
119
|
+
/** Seconds the server asked to wait (`retry-after-ms` / `Retry-After`), or null. */
|
|
120
|
+
retryAfter;
|
|
121
|
+
/** Rate-limit headers of this response, or null. */
|
|
122
|
+
rateLimit;
|
|
123
|
+
/** Response headers. */
|
|
124
|
+
headers;
|
|
125
|
+
/** Raw response body. */
|
|
126
|
+
body;
|
|
127
|
+
/** Every other property of `error` (for example `resets_at`, `permission`, `limit_kind`). */
|
|
128
|
+
extra;
|
|
129
|
+
/** True when the body was the API's error envelope. */
|
|
130
|
+
envelope;
|
|
131
|
+
/** For errors raised by chat helpers: what had been accumulated before the error. */
|
|
132
|
+
partialResult;
|
|
133
|
+
constructor(init) {
|
|
134
|
+
super(describe(init));
|
|
135
|
+
this.name = 'APIError';
|
|
136
|
+
this.status = init.status;
|
|
137
|
+
this.type = init.type;
|
|
138
|
+
this.code = init.code;
|
|
139
|
+
this.detail = init.detail;
|
|
140
|
+
this.requestId = init.requestId;
|
|
141
|
+
this.fields = init.fields;
|
|
142
|
+
this.retryAfter = init.retryAfter;
|
|
143
|
+
this.rateLimit = init.rateLimit;
|
|
144
|
+
this.headers = init.headers;
|
|
145
|
+
this.body = init.body;
|
|
146
|
+
this.extra = init.extra;
|
|
147
|
+
this.envelope = init.envelope;
|
|
148
|
+
this.partialResult = undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** `invalid_request_error` (usually 400): the request is malformed or failed validation. */
|
|
152
|
+
export class InvalidRequestError extends APIError {
|
|
153
|
+
constructor(init) {
|
|
154
|
+
super(init);
|
|
155
|
+
this.name = 'InvalidRequestError';
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** `authentication_error` (401): the API key is missing, wrong, expired or revoked. */
|
|
159
|
+
export class AuthenticationError extends APIError {
|
|
160
|
+
constructor(init) {
|
|
161
|
+
super(init);
|
|
162
|
+
this.name = 'AuthenticationError';
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** `payment_required_error` (402): the balance is exhausted. */
|
|
166
|
+
export class PaymentRequiredError extends APIError {
|
|
167
|
+
constructor(init) {
|
|
168
|
+
super(init);
|
|
169
|
+
this.name = 'PaymentRequiredError';
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** `permission_error` (403): the key lacks a permission. */
|
|
173
|
+
export class PermissionError extends APIError {
|
|
174
|
+
constructor(init) {
|
|
175
|
+
super(init);
|
|
176
|
+
this.name = 'PermissionError';
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** `not_found_error` (404). */
|
|
180
|
+
export class NotFoundError extends APIError {
|
|
181
|
+
constructor(init) {
|
|
182
|
+
super(init);
|
|
183
|
+
this.name = 'NotFoundError';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** `conflict_error` (409). */
|
|
187
|
+
export class ConflictError extends APIError {
|
|
188
|
+
constructor(init) {
|
|
189
|
+
super(init);
|
|
190
|
+
this.name = 'ConflictError';
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** `gone_error` (410): the resource existed and was removed. */
|
|
194
|
+
export class GoneError extends APIError {
|
|
195
|
+
constructor(init) {
|
|
196
|
+
super(init);
|
|
197
|
+
this.name = 'GoneError';
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** `limit_error` (429): a rate limit or a plan limit was reached. See `retryAfter` and `extra.resets_at`. */
|
|
201
|
+
export class LimitError extends APIError {
|
|
202
|
+
constructor(init) {
|
|
203
|
+
super(init);
|
|
204
|
+
this.name = 'LimitError';
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/** `api_error` (500): an unexpected error on the server. */
|
|
208
|
+
export class InternalServerError extends APIError {
|
|
209
|
+
constructor(init) {
|
|
210
|
+
super(init);
|
|
211
|
+
this.name = 'InternalServerError';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/** `unavailable_error` (502, 503, 504): the service is temporarily unavailable. */
|
|
215
|
+
export class UnavailableError extends APIError {
|
|
216
|
+
constructor(init) {
|
|
217
|
+
super(init);
|
|
218
|
+
this.name = 'UnavailableError';
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const NON_NEGATIVE_NUMBER = /^[0-9]+(\.[0-9]+)?$/;
|
|
222
|
+
const IMF_FIXDATE = /^[A-Za-z]{3}, [0-9]{2} [A-Za-z]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} GMT$/;
|
|
223
|
+
/**
|
|
224
|
+
* Parses the delay the server asked for: `retry-after-ms` (milliseconds) wins over `Retry-After`
|
|
225
|
+
* (seconds, possibly fractional, or an HTTP-date). Returns seconds, or null when there is no usable value.
|
|
226
|
+
*/
|
|
227
|
+
export function parseRetryAfter(headers, nowMs = Date.now()) {
|
|
228
|
+
const ms = headers.get('retry-after-ms');
|
|
229
|
+
if (ms !== null && NON_NEGATIVE_NUMBER.test(ms))
|
|
230
|
+
return Number(ms) / 1000;
|
|
231
|
+
const value = headers.get('retry-after');
|
|
232
|
+
if (value === null)
|
|
233
|
+
return null;
|
|
234
|
+
if (NON_NEGATIVE_NUMBER.test(value))
|
|
235
|
+
return Number(value);
|
|
236
|
+
if (IMF_FIXDATE.test(value)) {
|
|
237
|
+
const at = Date.parse(value);
|
|
238
|
+
if (!Number.isNaN(at))
|
|
239
|
+
return Math.max(0, (at - nowMs) / 1000);
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
/** Reads the `x-ratelimit-*` headers of one response. */
|
|
244
|
+
export function parseRateLimit(headers) {
|
|
245
|
+
const limit = Number(headers.get('x-ratelimit-limit') ?? 'x');
|
|
246
|
+
const remaining = Number(headers.get('x-ratelimit-remaining') ?? 'x');
|
|
247
|
+
const reset = Number(headers.get('x-ratelimit-reset') ?? 'x');
|
|
248
|
+
if (Number.isNaN(limit) || Number.isNaN(remaining) || Number.isNaN(reset))
|
|
249
|
+
return null;
|
|
250
|
+
return { limit, remaining, reset };
|
|
251
|
+
}
|
|
252
|
+
/** The `error.type` used when the body is not the API's error envelope. */
|
|
253
|
+
export function errorTypeForStatus(status) {
|
|
254
|
+
switch (status) {
|
|
255
|
+
case 401:
|
|
256
|
+
return 'authentication_error';
|
|
257
|
+
case 402:
|
|
258
|
+
return 'payment_required_error';
|
|
259
|
+
case 403:
|
|
260
|
+
return 'permission_error';
|
|
261
|
+
case 404:
|
|
262
|
+
return 'not_found_error';
|
|
263
|
+
case 409:
|
|
264
|
+
return 'conflict_error';
|
|
265
|
+
case 410:
|
|
266
|
+
return 'gone_error';
|
|
267
|
+
case 429:
|
|
268
|
+
return 'limit_error';
|
|
269
|
+
case 502:
|
|
270
|
+
case 503:
|
|
271
|
+
case 504:
|
|
272
|
+
return 'unavailable_error';
|
|
273
|
+
default:
|
|
274
|
+
return status >= 500 ? 'api_error' : 'invalid_request_error';
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const ERROR_CLASSES = {
|
|
278
|
+
invalid_request_error: InvalidRequestError,
|
|
279
|
+
authentication_error: AuthenticationError,
|
|
280
|
+
payment_required_error: PaymentRequiredError,
|
|
281
|
+
permission_error: PermissionError,
|
|
282
|
+
not_found_error: NotFoundError,
|
|
283
|
+
conflict_error: ConflictError,
|
|
284
|
+
gone_error: GoneError,
|
|
285
|
+
limit_error: LimitError,
|
|
286
|
+
api_error: InternalServerError,
|
|
287
|
+
unavailable_error: UnavailableError,
|
|
288
|
+
};
|
|
289
|
+
function isKnownType(type) {
|
|
290
|
+
return Object.prototype.hasOwnProperty.call(ERROR_CLASSES, type);
|
|
291
|
+
}
|
|
292
|
+
function isRecord(value) {
|
|
293
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
294
|
+
}
|
|
295
|
+
function parseFields(value) {
|
|
296
|
+
if (!Array.isArray(value))
|
|
297
|
+
return [];
|
|
298
|
+
const out = [];
|
|
299
|
+
for (const item of value) {
|
|
300
|
+
if (!isRecord(item))
|
|
301
|
+
continue;
|
|
302
|
+
out.push({
|
|
303
|
+
path: typeof item['path'] === 'string' ? item['path'] : '',
|
|
304
|
+
code: typeof item['code'] === 'string' ? item['code'] : '',
|
|
305
|
+
message: typeof item['message'] === 'string' ? item['message'] : '',
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
/** Splits an `error` object of the envelope (or of a chat `error` event); null when it is not one. */
|
|
311
|
+
export function parseErrorDetail(value) {
|
|
312
|
+
if (!isRecord(value))
|
|
313
|
+
return null;
|
|
314
|
+
const type = value['type'];
|
|
315
|
+
const message = value['message'];
|
|
316
|
+
if (typeof type !== 'string' || typeof message !== 'string')
|
|
317
|
+
return null;
|
|
318
|
+
const extra = {};
|
|
319
|
+
let code = null;
|
|
320
|
+
let fields = [];
|
|
321
|
+
for (const key of Object.keys(value)) {
|
|
322
|
+
if (key === 'type' || key === 'message')
|
|
323
|
+
continue;
|
|
324
|
+
if (key === 'code') {
|
|
325
|
+
if (typeof value[key] === 'string')
|
|
326
|
+
code = value[key];
|
|
327
|
+
}
|
|
328
|
+
else if (key === 'fields') {
|
|
329
|
+
fields = parseFields(value[key]);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
extra[key] = value[key];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return { type, detail: message, code, fields, extra };
|
|
336
|
+
}
|
|
337
|
+
/** Builds the error of the class that matches `init.type`, falling back to the status. */
|
|
338
|
+
export function makeAPIError(init) {
|
|
339
|
+
const kind = isKnownType(init.type) ? init.type : init.status === undefined ? 'api_error' : errorTypeForStatus(init.status);
|
|
340
|
+
return new ERROR_CLASSES[kind](init);
|
|
341
|
+
}
|
|
342
|
+
/** Turns an HTTP error response into the matching {@link APIError}. Never throws, whatever the body is. */
|
|
343
|
+
export function apiErrorFromResponse(status, headers, body, nowMs = Date.now()) {
|
|
344
|
+
const headerRequestId = headers.get('x-request-id');
|
|
345
|
+
const init = {
|
|
346
|
+
status,
|
|
347
|
+
type: errorTypeForStatus(status),
|
|
348
|
+
code: null,
|
|
349
|
+
detail: 'the response body is not an API error envelope',
|
|
350
|
+
requestId: headerRequestId !== null && headerRequestId !== '' ? headerRequestId : null,
|
|
351
|
+
fields: [],
|
|
352
|
+
retryAfter: parseRetryAfter(headers, nowMs),
|
|
353
|
+
rateLimit: parseRateLimit(headers),
|
|
354
|
+
headers,
|
|
355
|
+
body,
|
|
356
|
+
extra: {},
|
|
357
|
+
envelope: false,
|
|
358
|
+
};
|
|
359
|
+
let parsed;
|
|
360
|
+
try {
|
|
361
|
+
parsed = JSON.parse(body);
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
parsed = undefined;
|
|
365
|
+
}
|
|
366
|
+
if (isRecord(parsed)) {
|
|
367
|
+
const detail = parseErrorDetail(parsed['error']);
|
|
368
|
+
if (detail !== null) {
|
|
369
|
+
init.envelope = true;
|
|
370
|
+
init.type = detail.type;
|
|
371
|
+
init.detail = detail.detail;
|
|
372
|
+
init.code = detail.code;
|
|
373
|
+
init.fields = detail.fields;
|
|
374
|
+
init.extra = detail.extra;
|
|
375
|
+
const bodyRequestId = parsed['request_id'];
|
|
376
|
+
if (typeof bodyRequestId === 'string' && bodyRequestId !== '')
|
|
377
|
+
init.requestId = bodyRequestId;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return makeAPIError(init);
|
|
381
|
+
}
|