@meistrari/tela-sdk-js 0.0.2 → 0.0.4
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/dist/index.cjs +1312 -0
- package/dist/index.d.cts +897 -0
- package/dist/index.d.mts +897 -0
- package/dist/index.d.ts +808 -27
- package/dist/index.mjs +1291 -0
- package/package.json +14 -6
- package/dist/base-client.d.ts +0 -49
- package/dist/base-client.js +0 -299
- package/dist/errors.d.ts +0 -67
- package/dist/errors.js +0 -133
- package/dist/file.d.ts +0 -123
- package/dist/file.js +0 -161
- package/dist/index.js +0 -115
- package/dist/resource.d.ts +0 -8
- package/dist/resource.js +0 -9
- package/dist/resources/chat-completions.d.ts +0 -482
- package/dist/resources/chat-completions.js +0 -86
- package/dist/resources/index.d.ts +0 -1
- package/dist/resources/index.js +0 -1
- package/dist/streaming.d.ts +0 -81
- package/dist/streaming.js +0 -462
- package/dist/utils/constants.d.ts +0 -1
- package/dist/utils/constants.js +0 -21
- package/dist/utils/data-transformation.d.ts +0 -2
- package/dist/utils/data-transformation.js +0 -79
- package/dist/utils/stream.d.ts +0 -2
- package/dist/utils/stream.js +0 -11
package/package.json
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meistrari/tela-sdk-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/meistrari/tela-sdk-js.git"
|
|
8
8
|
},
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"main": "dist/index.mjs",
|
|
11
|
+
"module": "dist/index.mjs",
|
|
9
12
|
"exports": {
|
|
10
|
-
".":
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
}
|
|
11
18
|
},
|
|
12
|
-
"main": "dist/index.js",
|
|
13
19
|
"files": [
|
|
14
20
|
"dist"
|
|
15
21
|
],
|
|
@@ -17,9 +23,10 @@
|
|
|
17
23
|
"node": "^20.15.1"
|
|
18
24
|
},
|
|
19
25
|
"scripts": {
|
|
20
|
-
"
|
|
26
|
+
"prepublishOnly": "bun run build",
|
|
27
|
+
"release": "bunx bumpp & npm publish",
|
|
28
|
+
"build": "bunx unbuild",
|
|
21
29
|
"test": "bun test",
|
|
22
|
-
"prepare": "husky",
|
|
23
30
|
"lint": "eslint . --fix",
|
|
24
31
|
"lint:fix": "eslint . --fix",
|
|
25
32
|
"check": "eslint . --quiet && tsc --noEmit",
|
|
@@ -35,7 +42,8 @@
|
|
|
35
42
|
"@types/micromatch": "4.0.9",
|
|
36
43
|
"@types/node": "22.5.5",
|
|
37
44
|
"husky": "9.1.6",
|
|
38
|
-
"lint-staged": "15.2.10"
|
|
45
|
+
"lint-staged": "15.2.10",
|
|
46
|
+
"unbuild": "^2.0.0"
|
|
39
47
|
},
|
|
40
48
|
"peerDependencies": {
|
|
41
49
|
"typescript": "5.6.0"
|
package/dist/base-client.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
type HTTPMethods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
2
|
-
export type RequestOptions<Req = unknown | Record<string, unknown>> = {
|
|
3
|
-
method?: HTTPMethods;
|
|
4
|
-
path?: string;
|
|
5
|
-
query?: Req | undefined;
|
|
6
|
-
body?: Req | null | undefined;
|
|
7
|
-
headers?: Headers | undefined;
|
|
8
|
-
maxRetries?: number;
|
|
9
|
-
stream?: boolean | undefined;
|
|
10
|
-
timeout?: number;
|
|
11
|
-
signal?: AbortSignal | undefined | null;
|
|
12
|
-
};
|
|
13
|
-
export interface BaseClientOptions {
|
|
14
|
-
baseURL: string;
|
|
15
|
-
maxRetries?: number;
|
|
16
|
-
timeout?: number;
|
|
17
|
-
}
|
|
18
|
-
export declare abstract class BaseClient {
|
|
19
|
-
baseURL: string;
|
|
20
|
-
maxRetries: number;
|
|
21
|
-
timeout: number;
|
|
22
|
-
fetch: typeof fetch;
|
|
23
|
-
constructor({ baseURL, maxRetries, timeout }: BaseClientOptions);
|
|
24
|
-
protected createHeaders(opts?: Record<string, string>): Headers;
|
|
25
|
-
protected getUserAgent(): string;
|
|
26
|
-
protected getAuthHeaders(): Record<string, string>;
|
|
27
|
-
get<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
28
|
-
post<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
29
|
-
put<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
30
|
-
patch<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
31
|
-
delete<Req, Res>(path: string, opts?: RequestOptions<Req>): Promise<Res>;
|
|
32
|
-
private methodRequest;
|
|
33
|
-
private request;
|
|
34
|
-
private makeRequest;
|
|
35
|
-
private shouldRetry;
|
|
36
|
-
private retryRequest;
|
|
37
|
-
private calculateDefaultRetryTimeoutMillis;
|
|
38
|
-
fetchWithTimeout(url: string, _options: RequestInit, timeout: number, controller: AbortController): Promise<Response>;
|
|
39
|
-
protected buildRequest<Req>(options: RequestOptions<Req>): {
|
|
40
|
-
req: RequestInit;
|
|
41
|
-
url: string;
|
|
42
|
-
timeout: number;
|
|
43
|
-
};
|
|
44
|
-
protected buildURL<Req>(path: string, query: Req | null | undefined): string;
|
|
45
|
-
protected stringifyQuery(query: Record<string, unknown>): string;
|
|
46
|
-
}
|
|
47
|
-
export declare function createResponseHeaders(headers: Awaited<ReturnType<Fetch>>['headers']): Headers;
|
|
48
|
-
export declare function debug(action: string, ...args: any[]): void;
|
|
49
|
-
export {};
|
package/dist/base-client.js
DELETED
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import { version } from '../package.json';
|
|
2
|
-
import { APIError, ConnectionError, ConnectionTimeout, TelaError, UserAbortError,
|
|
3
|
-
// eslint-disable-next-line perfectionist/sort-named-imports
|
|
4
|
-
toError, } from './errors';
|
|
5
|
-
import { Stream } from './streaming';
|
|
6
|
-
import { DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS } from './utils/constants';
|
|
7
|
-
import { transformObjectFromCamelCaseToSnakeCase, transformObjectFromSnakeCaseToCamelCase } from './utils/data-transformation';
|
|
8
|
-
async function defaultParseResponse(props) {
|
|
9
|
-
const { response } = props;
|
|
10
|
-
if (props.options.stream) {
|
|
11
|
-
debug('response', response.status, response.url, response.headers, response.body);
|
|
12
|
-
return Stream.fromSSEResponse(response, props.controller);
|
|
13
|
-
}
|
|
14
|
-
if (response.status === 204) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
const contentType = response.headers.get('content-type');
|
|
18
|
-
const isJSON = contentType?.includes('application/json') || contentType?.includes('application/vnd.api+json');
|
|
19
|
-
if (isJSON) {
|
|
20
|
-
const json = await response.json();
|
|
21
|
-
debug('response', response.status, response.url, response.headers, json);
|
|
22
|
-
return transformObjectFromSnakeCaseToCamelCase(json, DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS);
|
|
23
|
-
}
|
|
24
|
-
const text = await response.text();
|
|
25
|
-
debug('response', response.status, response.url, response.headers, text);
|
|
26
|
-
return text;
|
|
27
|
-
}
|
|
28
|
-
export class BaseClient {
|
|
29
|
-
baseURL;
|
|
30
|
-
maxRetries;
|
|
31
|
-
timeout;
|
|
32
|
-
fetch;
|
|
33
|
-
constructor({ baseURL, maxRetries = 5, timeout = 0 }) {
|
|
34
|
-
this.baseURL = baseURL;
|
|
35
|
-
this.maxRetries = validateMaxRetries(maxRetries);
|
|
36
|
-
this.timeout = validateTimeout(timeout);
|
|
37
|
-
this.fetch = fetch;
|
|
38
|
-
}
|
|
39
|
-
createHeaders(opts) {
|
|
40
|
-
const headers = new Headers();
|
|
41
|
-
headers.set('Accept', 'application/json');
|
|
42
|
-
headers.set('Content-Type', 'application/json');
|
|
43
|
-
headers.set('User-Agent', this.getUserAgent());
|
|
44
|
-
for (const [key, value] of Object.entries(this.getAuthHeaders())) {
|
|
45
|
-
headers.set(key, value);
|
|
46
|
-
}
|
|
47
|
-
for (const [key, value] of Object.entries(opts ?? {})) {
|
|
48
|
-
headers.set(key, value);
|
|
49
|
-
}
|
|
50
|
-
return headers;
|
|
51
|
-
}
|
|
52
|
-
getUserAgent() {
|
|
53
|
-
return `tela-sdk-node/${version}`;
|
|
54
|
-
}
|
|
55
|
-
getAuthHeaders() {
|
|
56
|
-
return {};
|
|
57
|
-
}
|
|
58
|
-
get(path, opts) {
|
|
59
|
-
return this.methodRequest('GET', path, opts);
|
|
60
|
-
}
|
|
61
|
-
post(path, opts) {
|
|
62
|
-
return this.methodRequest('POST', path, opts);
|
|
63
|
-
}
|
|
64
|
-
put(path, opts) {
|
|
65
|
-
return this.methodRequest('PUT', path, opts);
|
|
66
|
-
}
|
|
67
|
-
patch(path, opts) {
|
|
68
|
-
return this.methodRequest('PATCH', path, opts);
|
|
69
|
-
}
|
|
70
|
-
delete(path, opts) {
|
|
71
|
-
return this.methodRequest('DELETE', path, opts);
|
|
72
|
-
}
|
|
73
|
-
async methodRequest(method, path, opts) {
|
|
74
|
-
const headers = this.createHeaders();
|
|
75
|
-
debug('methodRequest', method, path, opts);
|
|
76
|
-
const transformedQuery = opts?.query ? transformObjectFromCamelCaseToSnakeCase(opts.query, DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS) : undefined;
|
|
77
|
-
const transformedBody = opts?.body ? transformObjectFromCamelCaseToSnakeCase(opts.body, DEFAULT_FIELDS_TRANSFORMATION_EXCLUSIONS) : undefined;
|
|
78
|
-
const { response, options, controller } = await this.request({
|
|
79
|
-
method,
|
|
80
|
-
path,
|
|
81
|
-
headers,
|
|
82
|
-
...opts,
|
|
83
|
-
query: transformedQuery,
|
|
84
|
-
body: transformedBody,
|
|
85
|
-
});
|
|
86
|
-
return defaultParseResponse({ response, options, controller });
|
|
87
|
-
}
|
|
88
|
-
async request(options, remainingRetries = null) {
|
|
89
|
-
return this.makeRequest(options, remainingRetries);
|
|
90
|
-
}
|
|
91
|
-
async makeRequest(options, remainingRetries = null) {
|
|
92
|
-
if (remainingRetries === null) {
|
|
93
|
-
remainingRetries = options.maxRetries ?? this.maxRetries;
|
|
94
|
-
}
|
|
95
|
-
const { req, url, timeout } = this.buildRequest(options);
|
|
96
|
-
if (options.signal?.aborted) {
|
|
97
|
-
throw new UserAbortError();
|
|
98
|
-
}
|
|
99
|
-
const controller = new AbortController();
|
|
100
|
-
const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(toError);
|
|
101
|
-
if (response instanceof Error) {
|
|
102
|
-
debug('fetchWithTimeout error', response);
|
|
103
|
-
if (options.signal?.aborted) {
|
|
104
|
-
throw new UserAbortError();
|
|
105
|
-
}
|
|
106
|
-
if (remainingRetries) {
|
|
107
|
-
return this.retryRequest(options, remainingRetries);
|
|
108
|
-
}
|
|
109
|
-
if (response.name === 'AbortError') {
|
|
110
|
-
throw new ConnectionTimeout();
|
|
111
|
-
}
|
|
112
|
-
throw new ConnectionError({ cause: response });
|
|
113
|
-
}
|
|
114
|
-
const responseHeaders = createResponseHeaders(response.headers);
|
|
115
|
-
if (!response.ok) {
|
|
116
|
-
if (remainingRetries && this.shouldRetry(response)) {
|
|
117
|
-
debug('shouldRetry', `Retrying ${remainingRetries} more times`);
|
|
118
|
-
return this.retryRequest(options, remainingRetries, responseHeaders);
|
|
119
|
-
}
|
|
120
|
-
const errorText = await response.text().catch(e => toError(e).message);
|
|
121
|
-
const errorJSON = safeJSONParse(errorText);
|
|
122
|
-
const errorMessage = errorJSON ? undefined : errorText;
|
|
123
|
-
// const retryMessage = remainingRetries ? `(no more retries left)` : '(not retryable)'
|
|
124
|
-
const error = APIError.from(response.status, errorJSON, errorMessage);
|
|
125
|
-
debug('APIError', error);
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
return { response, options, controller };
|
|
129
|
-
}
|
|
130
|
-
shouldRetry(response) {
|
|
131
|
-
// Note this is not a standard header.
|
|
132
|
-
const shouldRetryHeader = response.headers.get('x-should-retry');
|
|
133
|
-
// If the server explicitly says whether or not to retry, obey.
|
|
134
|
-
if (shouldRetryHeader === 'true')
|
|
135
|
-
return true;
|
|
136
|
-
if (shouldRetryHeader === 'false')
|
|
137
|
-
return false;
|
|
138
|
-
if (response.status === 408)
|
|
139
|
-
return true;
|
|
140
|
-
if (response.status === 409)
|
|
141
|
-
return true;
|
|
142
|
-
if (response.status === 429)
|
|
143
|
-
return true;
|
|
144
|
-
if (response.status >= 500)
|
|
145
|
-
return true;
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
async retryRequest(options, retriesRemaining, responseHeaders) {
|
|
149
|
-
let timeoutMillis;
|
|
150
|
-
const retryAfterMillisHeader = responseHeaders?.get('retry-after-ms');
|
|
151
|
-
if (retryAfterMillisHeader) {
|
|
152
|
-
const timeoutMs = Number.parseFloat(retryAfterMillisHeader);
|
|
153
|
-
if (!Number.isNaN(timeoutMs)) {
|
|
154
|
-
timeoutMillis = timeoutMs;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
const retryAfterHeader = responseHeaders?.get('retry-after');
|
|
158
|
-
if (retryAfterHeader && !timeoutMillis) {
|
|
159
|
-
const timeoutSeconds = Number.parseFloat(retryAfterHeader);
|
|
160
|
-
if (!Number.isNaN(timeoutSeconds)) {
|
|
161
|
-
timeoutMillis = timeoutSeconds * 1000;
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
// If the API asks us to wait a certain amount of time (and it's a reasonable amount),
|
|
168
|
-
// just do what it says, but otherwise calculate a default
|
|
169
|
-
if (!(timeoutMillis && timeoutMillis >= 0 && timeoutMillis < 60 * 1000)) {
|
|
170
|
-
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
171
|
-
timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
|
|
172
|
-
}
|
|
173
|
-
await sleep(timeoutMillis);
|
|
174
|
-
return this.makeRequest(options, retriesRemaining - 1);
|
|
175
|
-
}
|
|
176
|
-
calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
|
|
177
|
-
const initialRetryDelay = 0.5;
|
|
178
|
-
const maxRetryDelay = 25.0;
|
|
179
|
-
const numRetries = maxRetries - retriesRemaining;
|
|
180
|
-
// Apply exponential backoff, but not more than the max.
|
|
181
|
-
const sleepSeconds = Math.min(initialRetryDelay * 2 ** numRetries, maxRetryDelay);
|
|
182
|
-
// Apply some jitter, take up to at most 25 percent of the retry time.
|
|
183
|
-
const jitter = 1 - Math.random() * 0.25;
|
|
184
|
-
return sleepSeconds * jitter * 1000;
|
|
185
|
-
}
|
|
186
|
-
async fetchWithTimeout(url, _options, timeout, controller) {
|
|
187
|
-
const { signal, ...options } = _options || {};
|
|
188
|
-
if (signal) {
|
|
189
|
-
signal.addEventListener('abort', () => {
|
|
190
|
-
controller.abort();
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
let timeoutId;
|
|
194
|
-
if (timeout > 0) {
|
|
195
|
-
timeoutId = setTimeout(() => {
|
|
196
|
-
debug('fetchWithTimeout timeout');
|
|
197
|
-
controller.abort();
|
|
198
|
-
}, timeout);
|
|
199
|
-
}
|
|
200
|
-
debug('fetchWithTimeout', { url, options });
|
|
201
|
-
return this.fetch
|
|
202
|
-
.call(undefined, url, {
|
|
203
|
-
signal: controller.signal,
|
|
204
|
-
...options,
|
|
205
|
-
...(options.body ? { body: JSON.stringify(options.body) } : {}),
|
|
206
|
-
})
|
|
207
|
-
.finally(() => {
|
|
208
|
-
if (timeoutId) {
|
|
209
|
-
clearTimeout(timeoutId);
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
buildRequest(options) {
|
|
214
|
-
const { method, path, query, headers = {}, body } = options;
|
|
215
|
-
if ('timeout' in options && options.timeout !== undefined) {
|
|
216
|
-
validateTimeout(options.timeout);
|
|
217
|
-
}
|
|
218
|
-
const url = this.buildURL(path, query);
|
|
219
|
-
const req = {
|
|
220
|
-
method,
|
|
221
|
-
...(body && { body: body }),
|
|
222
|
-
headers,
|
|
223
|
-
signal: options.signal ?? null,
|
|
224
|
-
};
|
|
225
|
-
return {
|
|
226
|
-
req,
|
|
227
|
-
url,
|
|
228
|
-
timeout: options.timeout ?? this.timeout,
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
buildURL(path, query) {
|
|
232
|
-
const startsWithSchemeRegexp = /^(?:[a-z]+:)?\/\//i;
|
|
233
|
-
const isAbsoluteURL = (url) => {
|
|
234
|
-
return startsWithSchemeRegexp.test(url);
|
|
235
|
-
};
|
|
236
|
-
const url = isAbsoluteURL(path)
|
|
237
|
-
? new URL(path)
|
|
238
|
-
: new URL(this.baseURL
|
|
239
|
-
+ (this.baseURL.endsWith('/') && path.startsWith('/')
|
|
240
|
-
? path.slice(1)
|
|
241
|
-
: path));
|
|
242
|
-
if (typeof query === 'object' && query && !Array.isArray(query)) {
|
|
243
|
-
url.search = this.stringifyQuery(query);
|
|
244
|
-
}
|
|
245
|
-
return url.toString();
|
|
246
|
-
}
|
|
247
|
-
stringifyQuery(query) {
|
|
248
|
-
return Object.entries(query)
|
|
249
|
-
.filter(([_, value]) => typeof value !== 'undefined')
|
|
250
|
-
.map(([key, value]) => {
|
|
251
|
-
if (typeof value === 'string'
|
|
252
|
-
|| typeof value === 'number'
|
|
253
|
-
|| typeof value === 'boolean') {
|
|
254
|
-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
255
|
-
}
|
|
256
|
-
if (value === null) {
|
|
257
|
-
return `${encodeURIComponent(key)}=`;
|
|
258
|
-
}
|
|
259
|
-
throw new TelaError(`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`);
|
|
260
|
-
})
|
|
261
|
-
.join('&');
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
export function createResponseHeaders(headers) {
|
|
265
|
-
const headersFromResponse = new Headers();
|
|
266
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
267
|
-
headersFromResponse.set(key, value);
|
|
268
|
-
}
|
|
269
|
-
return headersFromResponse;
|
|
270
|
-
}
|
|
271
|
-
function validateMaxRetries(maxRetries) {
|
|
272
|
-
if (maxRetries < 0) {
|
|
273
|
-
throw new TelaError('Max retries must be a non-negative integer');
|
|
274
|
-
}
|
|
275
|
-
return maxRetries;
|
|
276
|
-
}
|
|
277
|
-
function validateTimeout(timeout) {
|
|
278
|
-
if (timeout < 0) {
|
|
279
|
-
throw new TelaError('Timeout must be a non-negative integer');
|
|
280
|
-
}
|
|
281
|
-
return timeout;
|
|
282
|
-
}
|
|
283
|
-
function sleep(ms) {
|
|
284
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
285
|
-
}
|
|
286
|
-
function safeJSONParse(text) {
|
|
287
|
-
try {
|
|
288
|
-
return JSON.parse(text);
|
|
289
|
-
}
|
|
290
|
-
catch {
|
|
291
|
-
return text;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
export function debug(action, ...args) {
|
|
295
|
-
if (typeof process !== 'undefined' && process?.env?.DEBUG === 'true') {
|
|
296
|
-
// eslint-disable-next-line no-console
|
|
297
|
-
console.log(`TelaSDK:DEBUG:${action}`, ...args);
|
|
298
|
-
}
|
|
299
|
-
}
|
package/dist/errors.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export declare class TelaError extends Error {
|
|
2
|
-
}
|
|
3
|
-
export declare class ConflictApiKeyAndJWTError extends TelaError {
|
|
4
|
-
constructor();
|
|
5
|
-
}
|
|
6
|
-
export declare class MissingApiKeyOrJWTError extends TelaError {
|
|
7
|
-
constructor();
|
|
8
|
-
}
|
|
9
|
-
export declare class EmptyFileError extends TelaError {
|
|
10
|
-
constructor();
|
|
11
|
-
}
|
|
12
|
-
export declare class InvalidFileURL extends TelaError {
|
|
13
|
-
constructor(url: string);
|
|
14
|
-
}
|
|
15
|
-
export declare class FileUploadError extends TelaError {
|
|
16
|
-
constructor(message: string);
|
|
17
|
-
}
|
|
18
|
-
export declare class APIError extends TelaError {
|
|
19
|
-
readonly statusCode: number | undefined;
|
|
20
|
-
readonly error: any;
|
|
21
|
-
constructor(statusCode: number | undefined, error: any, _message: string | undefined);
|
|
22
|
-
static from(statusCode: number, responseError: object | undefined, message: string | undefined): APIError;
|
|
23
|
-
}
|
|
24
|
-
export declare class UserAbortError extends APIError {
|
|
25
|
-
readonly statusCode: undefined;
|
|
26
|
-
constructor({ message }?: {
|
|
27
|
-
message?: string;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
export declare class ConnectionError extends APIError {
|
|
31
|
-
readonly statusCode: undefined;
|
|
32
|
-
constructor({ message, cause, }: {
|
|
33
|
-
message?: string | undefined;
|
|
34
|
-
cause?: Error | undefined;
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
export declare class ConnectionTimeout extends APIError {
|
|
38
|
-
readonly statusCode: undefined;
|
|
39
|
-
constructor({ message, }?: {
|
|
40
|
-
message?: string | undefined;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
export declare class BadRequestError extends APIError {
|
|
44
|
-
readonly statusCode = 400;
|
|
45
|
-
}
|
|
46
|
-
export declare class AuthenticationError extends APIError {
|
|
47
|
-
readonly statusCode = 401;
|
|
48
|
-
}
|
|
49
|
-
export declare class AuthorizationError extends APIError {
|
|
50
|
-
readonly statusCode = 403;
|
|
51
|
-
}
|
|
52
|
-
export declare class NotFoundError extends APIError {
|
|
53
|
-
readonly statusCode = 404;
|
|
54
|
-
}
|
|
55
|
-
export declare class ConflictError extends APIError {
|
|
56
|
-
readonly statusCode = 409;
|
|
57
|
-
}
|
|
58
|
-
export declare class UnprocessableEntityError extends APIError {
|
|
59
|
-
readonly statusCode = 422;
|
|
60
|
-
}
|
|
61
|
-
export declare class RateLimitError extends APIError {
|
|
62
|
-
readonly statusCode = 429;
|
|
63
|
-
}
|
|
64
|
-
export declare class InternalServerError extends APIError {
|
|
65
|
-
readonly statusCode = 500;
|
|
66
|
-
}
|
|
67
|
-
export declare function toError(err: any): Error;
|
package/dist/errors.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
export class TelaError extends Error {
|
|
2
|
-
}
|
|
3
|
-
export class ConflictApiKeyAndJWTError extends TelaError {
|
|
4
|
-
constructor() {
|
|
5
|
-
super('You must provide either an API key or a JWT, but not both');
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export class MissingApiKeyOrJWTError extends TelaError {
|
|
9
|
-
constructor() {
|
|
10
|
-
super('You must provide an API key or a JWT');
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export class EmptyFileError extends TelaError {
|
|
14
|
-
constructor() {
|
|
15
|
-
super('File cannot be empty');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export class InvalidFileURL extends TelaError {
|
|
19
|
-
constructor(url) {
|
|
20
|
-
super(`Invalid file URL: ${url}`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
export class FileUploadError extends TelaError {
|
|
24
|
-
constructor(message) {
|
|
25
|
-
super(`Failed to upload file: ${message}`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
export class APIError extends TelaError {
|
|
29
|
-
statusCode;
|
|
30
|
-
error;
|
|
31
|
-
constructor(statusCode, error, _message) {
|
|
32
|
-
const message = error?.message
|
|
33
|
-
? typeof error.message === 'string'
|
|
34
|
-
? error.message
|
|
35
|
-
: JSON.stringify(error.message)
|
|
36
|
-
: error
|
|
37
|
-
? JSON.stringify(error)
|
|
38
|
-
: _message;
|
|
39
|
-
super(message);
|
|
40
|
-
this.statusCode = statusCode;
|
|
41
|
-
this.error = error;
|
|
42
|
-
}
|
|
43
|
-
static from(statusCode, responseError, message) {
|
|
44
|
-
if (!statusCode) {
|
|
45
|
-
return new ConnectionError({
|
|
46
|
-
message,
|
|
47
|
-
cause: toError(responseError),
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
const error = responseError;
|
|
51
|
-
if (error.status === 400) {
|
|
52
|
-
return new BadRequestError(statusCode, error, message);
|
|
53
|
-
}
|
|
54
|
-
if (error.status === 401) {
|
|
55
|
-
return new AuthenticationError(statusCode, error, message);
|
|
56
|
-
}
|
|
57
|
-
if (error.status === 403) {
|
|
58
|
-
return new AuthorizationError(statusCode, error, message);
|
|
59
|
-
}
|
|
60
|
-
if (error.status === 404) {
|
|
61
|
-
return new NotFoundError(statusCode, error, message);
|
|
62
|
-
}
|
|
63
|
-
if (error.status === 409) {
|
|
64
|
-
return new ConflictError(statusCode, error, message);
|
|
65
|
-
}
|
|
66
|
-
if (error.status === 422) {
|
|
67
|
-
return new UnprocessableEntityError(statusCode, error, message);
|
|
68
|
-
}
|
|
69
|
-
if (error.status === 429) {
|
|
70
|
-
return new RateLimitError(statusCode, error, message);
|
|
71
|
-
}
|
|
72
|
-
if (error.status >= 500) {
|
|
73
|
-
return new InternalServerError(statusCode, error, message);
|
|
74
|
-
}
|
|
75
|
-
return new APIError(statusCode, error, message);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
export class UserAbortError extends APIError {
|
|
79
|
-
statusCode = undefined;
|
|
80
|
-
constructor({ message } = {}) {
|
|
81
|
-
super(undefined, undefined, message || 'User aborted.');
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
export class ConnectionError extends APIError {
|
|
85
|
-
statusCode = undefined;
|
|
86
|
-
constructor({ message, cause, }) {
|
|
87
|
-
super(undefined, undefined, message || 'Connection error.');
|
|
88
|
-
if (cause)
|
|
89
|
-
this.cause = cause;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
export class ConnectionTimeout extends APIError {
|
|
93
|
-
statusCode = undefined;
|
|
94
|
-
constructor({ message, } = {}) {
|
|
95
|
-
super(undefined, undefined, message || 'Request timed out.');
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export class BadRequestError extends APIError {
|
|
99
|
-
statusCode = 400;
|
|
100
|
-
}
|
|
101
|
-
export class AuthenticationError extends APIError {
|
|
102
|
-
statusCode = 401;
|
|
103
|
-
}
|
|
104
|
-
export class AuthorizationError extends APIError {
|
|
105
|
-
statusCode = 403;
|
|
106
|
-
}
|
|
107
|
-
export class NotFoundError extends APIError {
|
|
108
|
-
statusCode = 404;
|
|
109
|
-
}
|
|
110
|
-
export class ConflictError extends APIError {
|
|
111
|
-
statusCode = 409;
|
|
112
|
-
}
|
|
113
|
-
export class UnprocessableEntityError extends APIError {
|
|
114
|
-
// todo: check if tela returns 400 or 422 for zod errors
|
|
115
|
-
statusCode = 422;
|
|
116
|
-
}
|
|
117
|
-
export class RateLimitError extends APIError {
|
|
118
|
-
statusCode = 429;
|
|
119
|
-
}
|
|
120
|
-
export class InternalServerError extends APIError {
|
|
121
|
-
statusCode = 500;
|
|
122
|
-
}
|
|
123
|
-
export function toError(err) {
|
|
124
|
-
if (err instanceof Error)
|
|
125
|
-
return err;
|
|
126
|
-
if (typeof err === 'object' && err !== null) {
|
|
127
|
-
try {
|
|
128
|
-
return new Error(JSON.stringify(err));
|
|
129
|
-
}
|
|
130
|
-
catch { }
|
|
131
|
-
}
|
|
132
|
-
return new Error(err);
|
|
133
|
-
}
|