extractia-sdk 1.3.0 → 1.4.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/dist/extractia-sdk.browser.js +516 -82
- package/dist/extractia-sdk.cjs.js +499 -87
- package/dist/extractia-sdk.esm.js +499 -87
- package/dist/index.d.ts +197 -15
- package/package.json +22 -4
- package/src/apiClient.js +189 -23
- package/src/browser-entry.js +35 -0
- package/src/errors.js +278 -40
- package/src/index.d.ts +197 -15
- package/src/index.js +21 -0
- package/src/utils.js +219 -0
- package/dist/extractia-sdk.browser.js.map +0 -7
- package/dist/extractia-sdk.cjs.js.map +0 -7
- package/dist/extractia-sdk.esm.js.map +0 -7
- package/dist/extractia-sdk.js.map +0 -7
|
@@ -374,15 +374,15 @@ var _setImmediate = ((setImmediateSupported, postMessageSupported) => {
|
|
|
374
374
|
if (setImmediateSupported) {
|
|
375
375
|
return setImmediate;
|
|
376
376
|
}
|
|
377
|
-
return postMessageSupported ? ((
|
|
377
|
+
return postMessageSupported ? ((token, callbacks) => {
|
|
378
378
|
_global.addEventListener("message", ({ source, data }) => {
|
|
379
|
-
if (source === _global && data ===
|
|
379
|
+
if (source === _global && data === token) {
|
|
380
380
|
callbacks.length && callbacks.shift()();
|
|
381
381
|
}
|
|
382
382
|
}, false);
|
|
383
383
|
return (cb) => {
|
|
384
384
|
callbacks.push(cb);
|
|
385
|
-
_global.postMessage(
|
|
385
|
+
_global.postMessage(token, "*");
|
|
386
386
|
};
|
|
387
387
|
})(`axios@${Math.random()}`, []) : (cb) => setTimeout(cb);
|
|
388
388
|
})(
|
|
@@ -538,9 +538,9 @@ function removeBrackets(key) {
|
|
|
538
538
|
}
|
|
539
539
|
function renderKey(path, key, dots) {
|
|
540
540
|
if (!path) return key;
|
|
541
|
-
return path.concat(key).map(function each(
|
|
542
|
-
|
|
543
|
-
return !dots && i ? "[" +
|
|
541
|
+
return path.concat(key).map(function each(token, i) {
|
|
542
|
+
token = removeBrackets(token);
|
|
543
|
+
return !dots && i ? "[" + token + "]" : token;
|
|
544
544
|
}).join(dots ? "." : "");
|
|
545
545
|
}
|
|
546
546
|
function isFlatArray(arr) {
|
|
@@ -1583,7 +1583,7 @@ var resolveConfig_default = (config) => {
|
|
|
1583
1583
|
if (platform_default.hasStandardBrowserEnv || platform_default.hasStandardBrowserWebWorkerEnv) {
|
|
1584
1584
|
headers.setContentType(void 0);
|
|
1585
1585
|
} else if ((contentType = headers.getContentType()) !== false) {
|
|
1586
|
-
const [type, ...tokens] = contentType ? contentType.split(";").map((
|
|
1586
|
+
const [type, ...tokens] = contentType ? contentType.split(";").map((token) => token.trim()).filter(Boolean) : [];
|
|
1587
1587
|
headers.setContentType([type || "multipart/form-data", ...tokens].join("; "));
|
|
1588
1588
|
}
|
|
1589
1589
|
}
|
|
@@ -1603,22 +1603,22 @@ var resolveConfig_default = (config) => {
|
|
|
1603
1603
|
var isXHRAdapterSupported = typeof XMLHttpRequest !== "undefined";
|
|
1604
1604
|
var xhr_default = isXHRAdapterSupported && function(config) {
|
|
1605
1605
|
return new Promise(function dispatchXhrRequest(resolve, reject) {
|
|
1606
|
-
const
|
|
1607
|
-
let requestData =
|
|
1608
|
-
const requestHeaders = AxiosHeaders_default.from(
|
|
1609
|
-
let { responseType, onUploadProgress, onDownloadProgress } =
|
|
1606
|
+
const _config2 = resolveConfig_default(config);
|
|
1607
|
+
let requestData = _config2.data;
|
|
1608
|
+
const requestHeaders = AxiosHeaders_default.from(_config2.headers).normalize();
|
|
1609
|
+
let { responseType, onUploadProgress, onDownloadProgress } = _config2;
|
|
1610
1610
|
let onCanceled;
|
|
1611
1611
|
let uploadThrottled, downloadThrottled;
|
|
1612
1612
|
let flushUpload, flushDownload;
|
|
1613
1613
|
function done() {
|
|
1614
1614
|
flushUpload && flushUpload();
|
|
1615
1615
|
flushDownload && flushDownload();
|
|
1616
|
-
|
|
1617
|
-
|
|
1616
|
+
_config2.cancelToken && _config2.cancelToken.unsubscribe(onCanceled);
|
|
1617
|
+
_config2.signal && _config2.signal.removeEventListener("abort", onCanceled);
|
|
1618
1618
|
}
|
|
1619
1619
|
let request = new XMLHttpRequest();
|
|
1620
|
-
request.open(
|
|
1621
|
-
request.timeout =
|
|
1620
|
+
request.open(_config2.method.toUpperCase(), _config2.url, true);
|
|
1621
|
+
request.timeout = _config2.timeout;
|
|
1622
1622
|
function onloadend() {
|
|
1623
1623
|
if (!request) {
|
|
1624
1624
|
return;
|
|
@@ -1669,10 +1669,10 @@ var xhr_default = isXHRAdapterSupported && function(config) {
|
|
|
1669
1669
|
request = null;
|
|
1670
1670
|
};
|
|
1671
1671
|
request.ontimeout = function handleTimeout() {
|
|
1672
|
-
let timeoutErrorMessage =
|
|
1673
|
-
const transitional2 =
|
|
1674
|
-
if (
|
|
1675
|
-
timeoutErrorMessage =
|
|
1672
|
+
let timeoutErrorMessage = _config2.timeout ? "timeout of " + _config2.timeout + "ms exceeded" : "timeout exceeded";
|
|
1673
|
+
const transitional2 = _config2.transitional || transitional_default;
|
|
1674
|
+
if (_config2.timeoutErrorMessage) {
|
|
1675
|
+
timeoutErrorMessage = _config2.timeoutErrorMessage;
|
|
1676
1676
|
}
|
|
1677
1677
|
reject(new AxiosError_default(
|
|
1678
1678
|
timeoutErrorMessage,
|
|
@@ -1688,11 +1688,11 @@ var xhr_default = isXHRAdapterSupported && function(config) {
|
|
|
1688
1688
|
request.setRequestHeader(key, val);
|
|
1689
1689
|
});
|
|
1690
1690
|
}
|
|
1691
|
-
if (!utils_default.isUndefined(
|
|
1692
|
-
request.withCredentials = !!
|
|
1691
|
+
if (!utils_default.isUndefined(_config2.withCredentials)) {
|
|
1692
|
+
request.withCredentials = !!_config2.withCredentials;
|
|
1693
1693
|
}
|
|
1694
1694
|
if (responseType && responseType !== "json") {
|
|
1695
|
-
request.responseType =
|
|
1695
|
+
request.responseType = _config2.responseType;
|
|
1696
1696
|
}
|
|
1697
1697
|
if (onDownloadProgress) {
|
|
1698
1698
|
[downloadThrottled, flushDownload] = progressEventReducer(onDownloadProgress, true);
|
|
@@ -1703,7 +1703,7 @@ var xhr_default = isXHRAdapterSupported && function(config) {
|
|
|
1703
1703
|
request.upload.addEventListener("progress", uploadThrottled);
|
|
1704
1704
|
request.upload.addEventListener("loadend", flushUpload);
|
|
1705
1705
|
}
|
|
1706
|
-
if (
|
|
1706
|
+
if (_config2.cancelToken || _config2.signal) {
|
|
1707
1707
|
onCanceled = (cancel) => {
|
|
1708
1708
|
if (!request) {
|
|
1709
1709
|
return;
|
|
@@ -1712,12 +1712,12 @@ var xhr_default = isXHRAdapterSupported && function(config) {
|
|
|
1712
1712
|
request.abort();
|
|
1713
1713
|
request = null;
|
|
1714
1714
|
};
|
|
1715
|
-
|
|
1716
|
-
if (
|
|
1717
|
-
|
|
1715
|
+
_config2.cancelToken && _config2.cancelToken.subscribe(onCanceled);
|
|
1716
|
+
if (_config2.signal) {
|
|
1717
|
+
_config2.signal.aborted ? onCanceled() : _config2.signal.addEventListener("abort", onCanceled);
|
|
1718
1718
|
}
|
|
1719
1719
|
}
|
|
1720
|
-
const protocol = parseProtocol(
|
|
1720
|
+
const protocol = parseProtocol(_config2.url);
|
|
1721
1721
|
if (protocol && platform_default.protocols.indexOf(protocol) === -1) {
|
|
1722
1722
|
reject(new AxiosError_default("Unsupported protocol " + protocol + ":", AxiosError_default.ERR_BAD_REQUEST, config));
|
|
1723
1723
|
return;
|
|
@@ -2367,32 +2367,32 @@ var CancelToken = class _CancelToken {
|
|
|
2367
2367
|
this.promise = new Promise(function promiseExecutor(resolve) {
|
|
2368
2368
|
resolvePromise = resolve;
|
|
2369
2369
|
});
|
|
2370
|
-
const
|
|
2370
|
+
const token = this;
|
|
2371
2371
|
this.promise.then((cancel) => {
|
|
2372
|
-
if (!
|
|
2373
|
-
let i =
|
|
2372
|
+
if (!token._listeners) return;
|
|
2373
|
+
let i = token._listeners.length;
|
|
2374
2374
|
while (i-- > 0) {
|
|
2375
|
-
|
|
2375
|
+
token._listeners[i](cancel);
|
|
2376
2376
|
}
|
|
2377
|
-
|
|
2377
|
+
token._listeners = null;
|
|
2378
2378
|
});
|
|
2379
2379
|
this.promise.then = (onfulfilled) => {
|
|
2380
2380
|
let _resolve;
|
|
2381
2381
|
const promise = new Promise((resolve) => {
|
|
2382
|
-
|
|
2382
|
+
token.subscribe(resolve);
|
|
2383
2383
|
_resolve = resolve;
|
|
2384
2384
|
}).then(onfulfilled);
|
|
2385
2385
|
promise.cancel = function reject() {
|
|
2386
|
-
|
|
2386
|
+
token.unsubscribe(_resolve);
|
|
2387
2387
|
};
|
|
2388
2388
|
return promise;
|
|
2389
2389
|
};
|
|
2390
2390
|
executor(function cancel(message, config, request) {
|
|
2391
|
-
if (
|
|
2391
|
+
if (token.reason) {
|
|
2392
2392
|
return;
|
|
2393
2393
|
}
|
|
2394
|
-
|
|
2395
|
-
resolvePromise(
|
|
2394
|
+
token.reason = new CanceledError_default(message, config, request);
|
|
2395
|
+
resolvePromise(token.reason);
|
|
2396
2396
|
});
|
|
2397
2397
|
}
|
|
2398
2398
|
/**
|
|
@@ -2444,11 +2444,11 @@ var CancelToken = class _CancelToken {
|
|
|
2444
2444
|
*/
|
|
2445
2445
|
static source() {
|
|
2446
2446
|
let cancel;
|
|
2447
|
-
const
|
|
2447
|
+
const token = new _CancelToken(function executor(c) {
|
|
2448
2448
|
cancel = c;
|
|
2449
2449
|
});
|
|
2450
2450
|
return {
|
|
2451
|
-
token
|
|
2451
|
+
token,
|
|
2452
2452
|
cancel
|
|
2453
2453
|
};
|
|
2454
2454
|
}
|
|
@@ -2592,93 +2592,347 @@ var {
|
|
|
2592
2592
|
} = axios_default;
|
|
2593
2593
|
|
|
2594
2594
|
// src/errors.js
|
|
2595
|
+
var STATUS_MESSAGES = {
|
|
2596
|
+
400: "The request contains invalid data. Please check your input.",
|
|
2597
|
+
401: "Your API token is invalid or has expired. Please check your credentials.",
|
|
2598
|
+
402: "Your current plan does not support this feature or your document quota is exhausted.",
|
|
2599
|
+
403: "You do not have permission to perform this action.",
|
|
2600
|
+
404: "The requested resource could not be found.",
|
|
2601
|
+
409: "A resource with this identifier already exists.",
|
|
2602
|
+
429: "Too many requests. Please wait a moment and try again.",
|
|
2603
|
+
500: "The server encountered an unexpected error. Please try again in a moment.",
|
|
2604
|
+
502: "The server received an unexpected response. Please try again.",
|
|
2605
|
+
503: "The service is temporarily unavailable. Please try again in a few minutes.",
|
|
2606
|
+
504: "The server timed out while processing the request. Please try again."
|
|
2607
|
+
};
|
|
2595
2608
|
var ExtractiaError = class extends Error {
|
|
2596
|
-
/**
|
|
2597
|
-
|
|
2609
|
+
/**
|
|
2610
|
+
* @param {string} message — Technical detail string.
|
|
2611
|
+
* @param {number} [status] — HTTP status code (0 = no response).
|
|
2612
|
+
* @param {string} [userMessage] — Human-friendly sentence shown to end users.
|
|
2613
|
+
* @param {string} [code] — Machine-readable error code.
|
|
2614
|
+
*/
|
|
2615
|
+
constructor(message, status = 0, userMessage = null, code = "SDK_ERROR") {
|
|
2598
2616
|
super(message);
|
|
2599
2617
|
this.name = "ExtractiaError";
|
|
2600
2618
|
this.status = status;
|
|
2619
|
+
this.userMessage = userMessage != null ? userMessage : message;
|
|
2620
|
+
this.code = code;
|
|
2621
|
+
this.requestId = null;
|
|
2622
|
+
}
|
|
2623
|
+
/** Returns true if automatically retrying the same request may succeed. */
|
|
2624
|
+
isRetryable() {
|
|
2625
|
+
return this.status === 429 || this.status >= 500;
|
|
2626
|
+
}
|
|
2627
|
+
toJSON() {
|
|
2628
|
+
return {
|
|
2629
|
+
name: this.name,
|
|
2630
|
+
code: this.code,
|
|
2631
|
+
status: this.status,
|
|
2632
|
+
message: this.message,
|
|
2633
|
+
userMessage: this.userMessage,
|
|
2634
|
+
requestId: this.requestId
|
|
2635
|
+
};
|
|
2601
2636
|
}
|
|
2602
2637
|
};
|
|
2603
2638
|
var AuthError = class extends ExtractiaError {
|
|
2604
|
-
constructor(message =
|
|
2605
|
-
super(message, 401);
|
|
2639
|
+
constructor(message = STATUS_MESSAGES[401]) {
|
|
2640
|
+
super(message, 401, STATUS_MESSAGES[401], "AUTH_ERROR");
|
|
2606
2641
|
this.name = "AuthError";
|
|
2607
2642
|
}
|
|
2608
2643
|
};
|
|
2609
2644
|
var ForbiddenError = class extends ExtractiaError {
|
|
2610
|
-
constructor(message =
|
|
2611
|
-
super(message, 403);
|
|
2645
|
+
constructor(message = STATUS_MESSAGES[403]) {
|
|
2646
|
+
super(message, 403, STATUS_MESSAGES[403], "FORBIDDEN");
|
|
2612
2647
|
this.name = "ForbiddenError";
|
|
2613
2648
|
}
|
|
2614
2649
|
};
|
|
2615
2650
|
var TierError = class extends ExtractiaError {
|
|
2616
|
-
constructor(message =
|
|
2617
|
-
super(message, status);
|
|
2651
|
+
constructor(message = STATUS_MESSAGES[402], status = 402) {
|
|
2652
|
+
super(message, status, STATUS_MESSAGES[402], "TIER_LIMIT");
|
|
2618
2653
|
this.name = "TierError";
|
|
2619
2654
|
}
|
|
2620
2655
|
};
|
|
2656
|
+
var QuotaError = class extends ExtractiaError {
|
|
2657
|
+
constructor(message = "You have reached your document processing quota for this billing period. Please upgrade or wait for the next cycle.") {
|
|
2658
|
+
super(message, 402, message, "QUOTA_EXCEEDED");
|
|
2659
|
+
this.name = "QuotaError";
|
|
2660
|
+
}
|
|
2661
|
+
};
|
|
2621
2662
|
var RateLimitError = class extends ExtractiaError {
|
|
2622
|
-
|
|
2623
|
-
|
|
2663
|
+
/**
|
|
2664
|
+
* @param {string} [message]
|
|
2665
|
+
* @param {number|null} [retryAfter] — Seconds to wait before retrying (from Retry-After header).
|
|
2666
|
+
*/
|
|
2667
|
+
constructor(message = STATUS_MESSAGES[429], retryAfter = null) {
|
|
2668
|
+
super(message, 429, STATUS_MESSAGES[429], "RATE_LIMITED");
|
|
2624
2669
|
this.name = "RateLimitError";
|
|
2670
|
+
this.retryAfter = retryAfter;
|
|
2671
|
+
}
|
|
2672
|
+
isRetryable() {
|
|
2673
|
+
return true;
|
|
2625
2674
|
}
|
|
2626
2675
|
};
|
|
2627
2676
|
var NotFoundError = class extends ExtractiaError {
|
|
2628
|
-
constructor(message =
|
|
2629
|
-
super(message, 404);
|
|
2677
|
+
constructor(message = STATUS_MESSAGES[404]) {
|
|
2678
|
+
super(message, 404, STATUS_MESSAGES[404], "NOT_FOUND");
|
|
2630
2679
|
this.name = "NotFoundError";
|
|
2631
2680
|
}
|
|
2632
2681
|
};
|
|
2682
|
+
var ValidationError = class extends ExtractiaError {
|
|
2683
|
+
/**
|
|
2684
|
+
* @param {string} [message]
|
|
2685
|
+
* @param {Record<string,string>|null} [fields] — Field-level errors, if the server provides them.
|
|
2686
|
+
*/
|
|
2687
|
+
constructor(message = STATUS_MESSAGES[400], fields = null) {
|
|
2688
|
+
super(message, 400, STATUS_MESSAGES[400], "VALIDATION_ERROR");
|
|
2689
|
+
this.name = "ValidationError";
|
|
2690
|
+
this.fields = fields;
|
|
2691
|
+
}
|
|
2692
|
+
};
|
|
2693
|
+
var ConflictError = class extends ExtractiaError {
|
|
2694
|
+
constructor(message = STATUS_MESSAGES[409]) {
|
|
2695
|
+
super(message, 409, STATUS_MESSAGES[409], "CONFLICT");
|
|
2696
|
+
this.name = "ConflictError";
|
|
2697
|
+
}
|
|
2698
|
+
};
|
|
2699
|
+
var ServerError = class extends ExtractiaError {
|
|
2700
|
+
constructor(message = STATUS_MESSAGES[500], status = 500) {
|
|
2701
|
+
var _a;
|
|
2702
|
+
super(
|
|
2703
|
+
message,
|
|
2704
|
+
status,
|
|
2705
|
+
(_a = STATUS_MESSAGES[status]) != null ? _a : STATUS_MESSAGES[500],
|
|
2706
|
+
"SERVER_ERROR"
|
|
2707
|
+
);
|
|
2708
|
+
this.name = "ServerError";
|
|
2709
|
+
}
|
|
2710
|
+
isRetryable() {
|
|
2711
|
+
return true;
|
|
2712
|
+
}
|
|
2713
|
+
};
|
|
2714
|
+
var NetworkError = class extends ExtractiaError {
|
|
2715
|
+
constructor(message = "Unable to reach the Extractia API. Please check your network connection and try again.") {
|
|
2716
|
+
super(message, 0, message, "NETWORK_ERROR");
|
|
2717
|
+
this.name = "NetworkError";
|
|
2718
|
+
}
|
|
2719
|
+
isRetryable() {
|
|
2720
|
+
return true;
|
|
2721
|
+
}
|
|
2722
|
+
};
|
|
2723
|
+
var TimeoutError = class extends ExtractiaError {
|
|
2724
|
+
constructor(message = "The request timed out. The server may be under heavy load \u2014 please try again in a moment.") {
|
|
2725
|
+
super(message, 0, message, "TIMEOUT");
|
|
2726
|
+
this.name = "TimeoutError";
|
|
2727
|
+
}
|
|
2728
|
+
isRetryable() {
|
|
2729
|
+
return true;
|
|
2730
|
+
}
|
|
2731
|
+
};
|
|
2732
|
+
function extractServerDetail(data) {
|
|
2733
|
+
var _a, _b, _c, _d, _e;
|
|
2734
|
+
if (!data) return null;
|
|
2735
|
+
if (typeof data === "string" && data.trim()) return data.trim();
|
|
2736
|
+
if (typeof data === "object") {
|
|
2737
|
+
const msg = (_c = (_b = (_a = data.message) != null ? _a : data.error) != null ? _b : data.detail) != null ? _c : data.title;
|
|
2738
|
+
if (msg && typeof msg === "string") return msg.trim();
|
|
2739
|
+
if (Array.isArray(data.errors) && data.errors.length > 0) {
|
|
2740
|
+
const first = data.errors[0];
|
|
2741
|
+
return typeof first === "string" ? first : (_e = (_d = first.message) != null ? _d : first.msg) != null ? _e : JSON.stringify(first);
|
|
2742
|
+
}
|
|
2743
|
+
if (Array.isArray(data.fieldErrors) && data.fieldErrors.length > 0) {
|
|
2744
|
+
return data.fieldErrors.map((e) => {
|
|
2745
|
+
var _a2, _b2;
|
|
2746
|
+
return `${(_a2 = e.field) != null ? _a2 : "field"}: ${(_b2 = e.message) != null ? _b2 : e.defaultMessage}`;
|
|
2747
|
+
}).join("; ");
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
return null;
|
|
2751
|
+
}
|
|
2633
2752
|
function mapAxiosError(err) {
|
|
2634
|
-
var _a, _b;
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2753
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2754
|
+
if (!err.response) {
|
|
2755
|
+
if (err.code === "ECONNABORTED" || err.code === "ETIMEDOUT" || err.message && err.message.toLowerCase().includes("timeout")) {
|
|
2756
|
+
return new TimeoutError();
|
|
2757
|
+
}
|
|
2758
|
+
return new NetworkError(err.message || void 0);
|
|
2759
|
+
}
|
|
2760
|
+
const status = err.response.status;
|
|
2761
|
+
const detail = extractServerDetail(err.response.data);
|
|
2762
|
+
const userMessage = (_a = STATUS_MESSAGES[status]) != null ? _a : status >= 500 ? STATUS_MESSAGES[500] : "Something went wrong. Please try again.";
|
|
2763
|
+
let error;
|
|
2638
2764
|
switch (status) {
|
|
2765
|
+
case 400: {
|
|
2766
|
+
const body = err.response.data;
|
|
2767
|
+
const fields = (_b = body == null ? void 0 : body.fields) != null ? _b : Array.isArray(body == null ? void 0 : body.fieldErrors) ? Object.fromEntries(
|
|
2768
|
+
body.fieldErrors.map((f) => {
|
|
2769
|
+
var _a2;
|
|
2770
|
+
return [f.field, (_a2 = f.message) != null ? _a2 : f.defaultMessage];
|
|
2771
|
+
})
|
|
2772
|
+
) : null;
|
|
2773
|
+
error = new ValidationError(detail != null ? detail : STATUS_MESSAGES[400], fields);
|
|
2774
|
+
break;
|
|
2775
|
+
}
|
|
2639
2776
|
case 401:
|
|
2640
|
-
|
|
2777
|
+
error = new AuthError(detail != null ? detail : void 0);
|
|
2778
|
+
break;
|
|
2641
2779
|
case 402:
|
|
2642
|
-
|
|
2780
|
+
if (detail && (detail.toLowerCase().includes("quota") || detail.toLowerCase().includes("document") || detail.toLowerCase().includes("limit reached"))) {
|
|
2781
|
+
error = new QuotaError(detail);
|
|
2782
|
+
} else {
|
|
2783
|
+
error = new TierError(detail != null ? detail : void 0);
|
|
2784
|
+
}
|
|
2785
|
+
break;
|
|
2643
2786
|
case 403:
|
|
2644
|
-
|
|
2787
|
+
error = new ForbiddenError(detail != null ? detail : void 0);
|
|
2788
|
+
break;
|
|
2645
2789
|
case 404:
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2790
|
+
error = new NotFoundError(detail != null ? detail : void 0);
|
|
2791
|
+
break;
|
|
2792
|
+
case 409:
|
|
2793
|
+
error = new ConflictError(detail != null ? detail : void 0);
|
|
2794
|
+
break;
|
|
2795
|
+
case 429: {
|
|
2796
|
+
const retryAfterHeader = (_c = err.response.headers) == null ? void 0 : _c["retry-after"];
|
|
2797
|
+
const retryAfter = retryAfterHeader != null ? parseInt(retryAfterHeader, 10) : null;
|
|
2798
|
+
error = new RateLimitError(detail != null ? detail : void 0, isNaN(retryAfter) ? null : retryAfter);
|
|
2799
|
+
break;
|
|
2800
|
+
}
|
|
2649
2801
|
default:
|
|
2650
|
-
|
|
2802
|
+
if (status >= 500) {
|
|
2803
|
+
error = new ServerError((_d = detail != null ? detail : STATUS_MESSAGES[status]) != null ? _d : STATUS_MESSAGES[500], status);
|
|
2804
|
+
} else {
|
|
2805
|
+
error = new ExtractiaError(
|
|
2806
|
+
detail != null ? detail : err.message,
|
|
2807
|
+
status,
|
|
2808
|
+
userMessage,
|
|
2809
|
+
`HTTP_${status}`
|
|
2810
|
+
);
|
|
2811
|
+
}
|
|
2651
2812
|
}
|
|
2813
|
+
error.userMessage = userMessage;
|
|
2814
|
+
const reqId = (_h = (_g = (_e = err.response.headers) == null ? void 0 : _e["x-request-id"]) != null ? _g : (_f = err.response.headers) == null ? void 0 : _f["x-correlation-id"]) != null ? _h : null;
|
|
2815
|
+
if (reqId) error.requestId = reqId;
|
|
2816
|
+
return error;
|
|
2652
2817
|
}
|
|
2653
2818
|
|
|
2654
2819
|
// src/apiClient.js
|
|
2655
|
-
var
|
|
2656
|
-
var
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2820
|
+
var _token = null;
|
|
2821
|
+
var _config = {
|
|
2822
|
+
baseURL: "https://api.extractia.info/api/public",
|
|
2823
|
+
timeout: 6e4,
|
|
2824
|
+
retries: 1,
|
|
2825
|
+
retryDelay: 1e3,
|
|
2826
|
+
debug: false,
|
|
2827
|
+
defaultHeaders: {},
|
|
2828
|
+
onBeforeRequest: null,
|
|
2829
|
+
onAfterResponse: null,
|
|
2830
|
+
onError: null
|
|
2831
|
+
};
|
|
2832
|
+
var _api = axios_default.create({
|
|
2833
|
+
baseURL: _config.baseURL,
|
|
2834
|
+
timeout: _config.timeout
|
|
2661
2835
|
});
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2836
|
+
_api.interceptors.request.use((config) => {
|
|
2837
|
+
var _a, _b, _c, _d, _e;
|
|
2838
|
+
if (!_token) {
|
|
2839
|
+
const err = new ExtractiaError(
|
|
2840
|
+
"API token not set. Call setToken(token) before making requests.",
|
|
2841
|
+
0,
|
|
2842
|
+
"API token not set. Call setToken(token) before making requests.",
|
|
2843
|
+
"TOKEN_MISSING"
|
|
2844
|
+
);
|
|
2845
|
+
return Promise.reject(err);
|
|
2846
|
+
}
|
|
2847
|
+
config.headers = (_a = config.headers) != null ? _a : {};
|
|
2848
|
+
config.headers["Authorization"] = `Bearer ${_token}`;
|
|
2849
|
+
Object.assign(config.headers, _config.defaultHeaders);
|
|
2850
|
+
if (_config.debug) {
|
|
2851
|
+
console.debug(
|
|
2852
|
+
`[ExtractIA SDK] \u2192 ${((_b = config.method) != null ? _b : "GET").toUpperCase()} ${(_c = config.baseURL) != null ? _c : ""}${(_d = config.url) != null ? _d : ""}`,
|
|
2853
|
+
(_e = config.params) != null ? _e : ""
|
|
2666
2854
|
);
|
|
2667
2855
|
}
|
|
2668
|
-
|
|
2856
|
+
if (typeof _config.onBeforeRequest === "function") {
|
|
2857
|
+
const modified = _config.onBeforeRequest(config);
|
|
2858
|
+
return modified != null ? modified : config;
|
|
2859
|
+
}
|
|
2669
2860
|
return config;
|
|
2670
2861
|
});
|
|
2671
|
-
|
|
2672
|
-
(response) =>
|
|
2673
|
-
|
|
2862
|
+
_api.interceptors.response.use(
|
|
2863
|
+
(response) => {
|
|
2864
|
+
var _a, _b;
|
|
2865
|
+
if (_config.debug) {
|
|
2866
|
+
console.debug(
|
|
2867
|
+
`[ExtractIA SDK] \u2190 ${response.status} ${(_b = (_a = response.config) == null ? void 0 : _a.url) != null ? _b : ""}`
|
|
2868
|
+
);
|
|
2869
|
+
}
|
|
2870
|
+
if (typeof _config.onAfterResponse === "function") {
|
|
2871
|
+
_config.onAfterResponse(response);
|
|
2872
|
+
}
|
|
2873
|
+
return response;
|
|
2874
|
+
},
|
|
2875
|
+
async (err) => {
|
|
2876
|
+
var _a, _b;
|
|
2877
|
+
if (err instanceof ExtractiaError) {
|
|
2878
|
+
if (typeof _config.onError === "function") _config.onError(err);
|
|
2879
|
+
return Promise.reject(err);
|
|
2880
|
+
}
|
|
2881
|
+
const mapped = mapAxiosError(err);
|
|
2882
|
+
const cfg = err.config;
|
|
2883
|
+
if (cfg && mapped.isRetryable() && ((_a = cfg._retryCount) != null ? _a : 0) < _config.retries) {
|
|
2884
|
+
cfg._retryCount = ((_b = cfg._retryCount) != null ? _b : 0) + 1;
|
|
2885
|
+
const delayMs = mapped.retryAfter != null ? mapped.retryAfter * 1e3 : _config.retryDelay * cfg._retryCount;
|
|
2886
|
+
if (_config.debug) {
|
|
2887
|
+
console.debug(
|
|
2888
|
+
`[ExtractIA SDK] retrying (${cfg._retryCount}/${_config.retries}) in ${delayMs}ms\u2026`
|
|
2889
|
+
);
|
|
2890
|
+
}
|
|
2891
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
2892
|
+
return _api(cfg);
|
|
2893
|
+
}
|
|
2894
|
+
if (typeof _config.onError === "function") _config.onError(mapped);
|
|
2895
|
+
return Promise.reject(mapped);
|
|
2896
|
+
}
|
|
2674
2897
|
);
|
|
2675
|
-
function setToken(
|
|
2676
|
-
token
|
|
2898
|
+
function setToken(token) {
|
|
2899
|
+
if (!token || typeof token !== "string" || !token.trim()) {
|
|
2900
|
+
throw new Error("setToken: token must be a non-empty string.");
|
|
2901
|
+
}
|
|
2902
|
+
_token = token.trim();
|
|
2903
|
+
}
|
|
2904
|
+
function getToken() {
|
|
2905
|
+
return _token;
|
|
2906
|
+
}
|
|
2907
|
+
function hasToken() {
|
|
2908
|
+
return Boolean(_token);
|
|
2909
|
+
}
|
|
2910
|
+
function clearToken() {
|
|
2911
|
+
_token = null;
|
|
2912
|
+
}
|
|
2913
|
+
function configure(opts = {}) {
|
|
2914
|
+
if (opts.baseURL) {
|
|
2915
|
+
_config.baseURL = opts.baseURL;
|
|
2916
|
+
_api.defaults.baseURL = opts.baseURL;
|
|
2917
|
+
}
|
|
2918
|
+
if (opts.timeout != null) {
|
|
2919
|
+
_config.timeout = opts.timeout;
|
|
2920
|
+
_api.defaults.timeout = opts.timeout;
|
|
2921
|
+
}
|
|
2922
|
+
if (opts.retries != null) _config.retries = opts.retries;
|
|
2923
|
+
if (opts.retryDelay != null) _config.retryDelay = opts.retryDelay;
|
|
2924
|
+
if (opts.debug != null) _config.debug = Boolean(opts.debug);
|
|
2925
|
+
if (opts.defaultHeaders) {
|
|
2926
|
+
_config.defaultHeaders = __spreadValues(__spreadValues({}, _config.defaultHeaders), opts.defaultHeaders);
|
|
2927
|
+
}
|
|
2928
|
+
if (opts.onBeforeRequest) _config.onBeforeRequest = opts.onBeforeRequest;
|
|
2929
|
+
if (opts.onAfterResponse) _config.onAfterResponse = opts.onAfterResponse;
|
|
2930
|
+
if (opts.onError) _config.onError = opts.onError;
|
|
2677
2931
|
}
|
|
2678
|
-
function
|
|
2679
|
-
|
|
2932
|
+
function getConfig() {
|
|
2933
|
+
return __spreadValues({}, _config);
|
|
2680
2934
|
}
|
|
2681
|
-
var apiClient_default =
|
|
2935
|
+
var apiClient_default = _api;
|
|
2682
2936
|
|
|
2683
2937
|
// src/auth.js
|
|
2684
2938
|
async function getMyProfile() {
|
|
@@ -2907,8 +3161,17 @@ async function getSubUsers() {
|
|
|
2907
3161
|
const res = await apiClient_default.get("/me/subusers");
|
|
2908
3162
|
return res.data;
|
|
2909
3163
|
}
|
|
2910
|
-
async function createSubUser({
|
|
2911
|
-
|
|
3164
|
+
async function createSubUser({
|
|
3165
|
+
username,
|
|
3166
|
+
password,
|
|
3167
|
+
permissions,
|
|
3168
|
+
allowedFormIds
|
|
3169
|
+
}) {
|
|
3170
|
+
const res = await apiClient_default.post("/me/subusers", __spreadValues({
|
|
3171
|
+
username,
|
|
3172
|
+
password,
|
|
3173
|
+
permissions
|
|
3174
|
+
}, allowedFormIds !== void 0 ? { allowedFormIds } : {}));
|
|
2912
3175
|
return res.data;
|
|
2913
3176
|
}
|
|
2914
3177
|
async function deleteSubUser(username) {
|
|
@@ -2916,43 +3179,184 @@ async function deleteSubUser(username) {
|
|
|
2916
3179
|
return res.data;
|
|
2917
3180
|
}
|
|
2918
3181
|
async function updateSubUser(username, updates = {}) {
|
|
2919
|
-
const res = await apiClient_default.put(
|
|
3182
|
+
const res = await apiClient_default.put(
|
|
3183
|
+
`/me/subusers/${encodeURIComponent(username)}`,
|
|
3184
|
+
updates
|
|
3185
|
+
);
|
|
2920
3186
|
return res.data;
|
|
2921
3187
|
}
|
|
2922
3188
|
async function toggleSuspendSubUser(username) {
|
|
2923
|
-
const res = await apiClient_default.put(
|
|
3189
|
+
const res = await apiClient_default.put(
|
|
3190
|
+
`/me/subusers/${encodeURIComponent(username)}/suspend`
|
|
3191
|
+
);
|
|
2924
3192
|
return res.data;
|
|
2925
3193
|
}
|
|
2926
3194
|
|
|
3195
|
+
// src/utils.js
|
|
3196
|
+
var utils_exports2 = {};
|
|
3197
|
+
__export(utils_exports2, {
|
|
3198
|
+
delay: () => delay,
|
|
3199
|
+
ensureBase64: () => ensureBase64,
|
|
3200
|
+
fileToBase64: () => fileToBase64,
|
|
3201
|
+
getMimeType: () => getMimeType,
|
|
3202
|
+
isBase64: () => isBase64,
|
|
3203
|
+
paginate: () => paginate,
|
|
3204
|
+
paginateAll: () => paginateAll,
|
|
3205
|
+
stripDataUrlPrefix: () => stripDataUrlPrefix,
|
|
3206
|
+
withRetry: () => withRetry
|
|
3207
|
+
});
|
|
3208
|
+
function fileToBase64(file) {
|
|
3209
|
+
if (typeof FileReader === "undefined") {
|
|
3210
|
+
return Promise.reject(
|
|
3211
|
+
new Error(
|
|
3212
|
+
"fileToBase64: FileReader is not available in this environment. In Node.js, read the file manually and encode it with Buffer.from(data).toString('base64')."
|
|
3213
|
+
)
|
|
3214
|
+
);
|
|
3215
|
+
}
|
|
3216
|
+
return new Promise((resolve, reject) => {
|
|
3217
|
+
const reader = new FileReader();
|
|
3218
|
+
reader.onload = () => resolve(
|
|
3219
|
+
/** @type {string} */
|
|
3220
|
+
reader.result
|
|
3221
|
+
);
|
|
3222
|
+
reader.onerror = () => {
|
|
3223
|
+
var _a;
|
|
3224
|
+
return reject(new Error(`fileToBase64: failed to read file "${(_a = file.name) != null ? _a : "unknown"}".`));
|
|
3225
|
+
};
|
|
3226
|
+
reader.readAsDataURL(file);
|
|
3227
|
+
});
|
|
3228
|
+
}
|
|
3229
|
+
function stripDataUrlPrefix(base64OrDataUrl) {
|
|
3230
|
+
if (typeof base64OrDataUrl !== "string") {
|
|
3231
|
+
throw new TypeError(
|
|
3232
|
+
"stripDataUrlPrefix: argument must be a string."
|
|
3233
|
+
);
|
|
3234
|
+
}
|
|
3235
|
+
const idx = base64OrDataUrl.indexOf(";base64,");
|
|
3236
|
+
return idx !== -1 ? base64OrDataUrl.slice(idx + 8) : base64OrDataUrl;
|
|
3237
|
+
}
|
|
3238
|
+
function getMimeType(dataUrl) {
|
|
3239
|
+
if (typeof dataUrl !== "string") return null;
|
|
3240
|
+
const match = dataUrl.match(/^data:([^;]+);base64,/);
|
|
3241
|
+
return match ? match[1] : null;
|
|
3242
|
+
}
|
|
3243
|
+
function isBase64(str) {
|
|
3244
|
+
if (typeof str !== "string" || !str.trim()) return false;
|
|
3245
|
+
const raw = stripDataUrlPrefix(str);
|
|
3246
|
+
return raw.length > 0 && raw.length % 4 === 0 && /^[A-Za-z0-9+/]*={0,2}$/.test(raw);
|
|
3247
|
+
}
|
|
3248
|
+
async function ensureBase64(fileOrBase64) {
|
|
3249
|
+
if (typeof fileOrBase64 === "string") return fileOrBase64;
|
|
3250
|
+
if (typeof File !== "undefined" && fileOrBase64 instanceof File || typeof Blob !== "undefined" && fileOrBase64 instanceof Blob) {
|
|
3251
|
+
return fileToBase64(fileOrBase64);
|
|
3252
|
+
}
|
|
3253
|
+
throw new TypeError(
|
|
3254
|
+
"ensureBase64: argument must be a File, Blob, or a base64 string."
|
|
3255
|
+
);
|
|
3256
|
+
}
|
|
3257
|
+
function paginate(_0) {
|
|
3258
|
+
return __asyncGenerator(this, arguments, function* (fn, { size = 50, startPage = 0, maxPages = 1e3 } = {}) {
|
|
3259
|
+
var _a;
|
|
3260
|
+
let page = startPage;
|
|
3261
|
+
let pagesRead = 0;
|
|
3262
|
+
while (pagesRead < maxPages) {
|
|
3263
|
+
const result = yield new __await(fn({ page, size }));
|
|
3264
|
+
const items = Array.isArray(result == null ? void 0 : result.content) ? result.content : [];
|
|
3265
|
+
for (const item of items) yield item;
|
|
3266
|
+
const totalPages = (_a = result == null ? void 0 : result.totalPages) != null ? _a : 1;
|
|
3267
|
+
if (items.length === 0 || page + 1 >= totalPages) break;
|
|
3268
|
+
page++;
|
|
3269
|
+
pagesRead++;
|
|
3270
|
+
}
|
|
3271
|
+
});
|
|
3272
|
+
}
|
|
3273
|
+
async function paginateAll(fn, opts) {
|
|
3274
|
+
const items = [];
|
|
3275
|
+
try {
|
|
3276
|
+
for (var iter = __forAwait(paginate(fn, opts)), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
|
|
3277
|
+
const item = temp.value;
|
|
3278
|
+
items.push(item);
|
|
3279
|
+
}
|
|
3280
|
+
} catch (temp) {
|
|
3281
|
+
error = [temp];
|
|
3282
|
+
} finally {
|
|
3283
|
+
try {
|
|
3284
|
+
more && (temp = iter.return) && await temp.call(iter);
|
|
3285
|
+
} finally {
|
|
3286
|
+
if (error)
|
|
3287
|
+
throw error[0];
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
return items;
|
|
3291
|
+
}
|
|
3292
|
+
function delay(ms) {
|
|
3293
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3294
|
+
}
|
|
3295
|
+
async function withRetry(fn, { retries = 3, initialDelay = 500, shouldRetry } = {}) {
|
|
3296
|
+
const isRetryable = shouldRetry != null ? shouldRetry : (err) => typeof err.isRetryable === "function" && err.isRetryable();
|
|
3297
|
+
let lastErr;
|
|
3298
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
3299
|
+
try {
|
|
3300
|
+
return await fn();
|
|
3301
|
+
} catch (err) {
|
|
3302
|
+
lastErr = err;
|
|
3303
|
+
if (attempt < retries && isRetryable(err)) {
|
|
3304
|
+
const wait = err.retryAfter != null ? err.retryAfter * 1e3 : initialDelay * 2 ** attempt;
|
|
3305
|
+
await delay(wait);
|
|
3306
|
+
continue;
|
|
3307
|
+
}
|
|
3308
|
+
throw err;
|
|
3309
|
+
}
|
|
3310
|
+
}
|
|
3311
|
+
throw lastErr;
|
|
3312
|
+
}
|
|
3313
|
+
|
|
2927
3314
|
// src/index.js
|
|
2928
|
-
var extractia = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, auth_exports), templates_exports), documents_exports), analytics_exports), ocrTools_exports), subusers_exports)
|
|
3315
|
+
var extractia = __spreadProps(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, auth_exports), templates_exports), documents_exports), analytics_exports), ocrTools_exports), subusers_exports), utils_exports2), {
|
|
3316
|
+
getToken,
|
|
3317
|
+
hasToken,
|
|
3318
|
+
clearToken,
|
|
3319
|
+
getConfig
|
|
3320
|
+
});
|
|
2929
3321
|
var index_default = extractia;
|
|
2930
3322
|
export {
|
|
2931
3323
|
AuthError,
|
|
3324
|
+
ConflictError,
|
|
2932
3325
|
ExtractiaError,
|
|
2933
3326
|
ForbiddenError,
|
|
3327
|
+
NetworkError,
|
|
2934
3328
|
NotFoundError,
|
|
3329
|
+
QuotaError,
|
|
2935
3330
|
RateLimitError,
|
|
3331
|
+
ServerError,
|
|
2936
3332
|
TierError,
|
|
3333
|
+
TimeoutError,
|
|
3334
|
+
ValidationError,
|
|
2937
3335
|
bulkPreconform,
|
|
3336
|
+
clearToken,
|
|
2938
3337
|
configure,
|
|
2939
3338
|
createOcrTool,
|
|
2940
3339
|
createSubUser,
|
|
2941
3340
|
createTemplate,
|
|
2942
3341
|
index_default as default,
|
|
3342
|
+
delay,
|
|
2943
3343
|
deleteAllTemplateDocuments,
|
|
2944
3344
|
deleteDocument,
|
|
2945
3345
|
deleteOcrTool,
|
|
2946
3346
|
deleteSubUser,
|
|
2947
3347
|
deleteTemplate,
|
|
3348
|
+
ensureBase64,
|
|
2948
3349
|
exportDocumentsCsv,
|
|
2949
3350
|
exportDocumentsJson,
|
|
3351
|
+
fileToBase64,
|
|
2950
3352
|
generateDocumentSummary,
|
|
3353
|
+
getConfig,
|
|
2951
3354
|
getCreditsBalance,
|
|
2952
3355
|
getCreditsHistory,
|
|
2953
3356
|
getDocumentById,
|
|
2954
3357
|
getDocumentHistory,
|
|
2955
3358
|
getDocumentsByTemplateId,
|
|
3359
|
+
getMimeType,
|
|
2956
3360
|
getMyProfile,
|
|
2957
3361
|
getOcrTools,
|
|
2958
3362
|
getRecentDocuments,
|
|
@@ -2960,10 +3364,17 @@ export {
|
|
|
2960
3364
|
getTemplateById,
|
|
2961
3365
|
getTemplateByName,
|
|
2962
3366
|
getTemplates,
|
|
3367
|
+
getToken,
|
|
3368
|
+
hasToken,
|
|
3369
|
+
isBase64,
|
|
3370
|
+
mapAxiosError,
|
|
3371
|
+
paginate,
|
|
3372
|
+
paginateAll,
|
|
2963
3373
|
processImage,
|
|
2964
3374
|
processImagesMultipage,
|
|
2965
3375
|
runOcrTool,
|
|
2966
3376
|
setToken,
|
|
3377
|
+
stripDataUrlPrefix,
|
|
2967
3378
|
suggestFields,
|
|
2968
3379
|
toggleSuspendSubUser,
|
|
2969
3380
|
updateDocumentData,
|
|
@@ -2972,6 +3383,7 @@ export {
|
|
|
2972
3383
|
updateOcrTool,
|
|
2973
3384
|
updateSubUser,
|
|
2974
3385
|
updateTemplate,
|
|
2975
|
-
updateWebhook
|
|
3386
|
+
updateWebhook,
|
|
3387
|
+
withRetry
|
|
2976
3388
|
};
|
|
2977
3389
|
//# sourceMappingURL=extractia-sdk.esm.js.map
|