extractia-sdk 1.0.6 → 1.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/build.js CHANGED
@@ -1,35 +1,40 @@
1
1
  // build.js
2
- import fs from 'fs';
3
- import { build } from 'esbuild';
2
+ import fs from "fs";
3
+ import { build } from "esbuild";
4
4
 
5
- if (!fs.existsSync('./dist')) {
6
- fs.mkdirSync('./dist');
5
+ if (!fs.existsSync("./dist")) {
6
+ fs.mkdirSync("./dist");
7
7
  }
8
8
 
9
9
  const sharedConfig = {
10
- entryPoints: ['./src/index.js'],
10
+ entryPoints: ["./src/index.js"],
11
11
  bundle: true,
12
12
  minify: false,
13
13
  sourcemap: true,
14
- target: ['es2017'],
14
+ target: ["es2017"],
15
15
  };
16
16
 
17
17
  Promise.all([
18
18
  build({
19
19
  ...sharedConfig,
20
- outfile: './dist/extractia-sdk.esm.js',
21
- format: 'esm',
20
+ outfile: "./dist/extractia-sdk.esm.js",
21
+ format: "esm",
22
22
  }),
23
23
  build({
24
24
  ...sharedConfig,
25
- outfile: './dist/extractia-sdk.cjs.js',
26
- format: 'cjs',
25
+ outfile: "./dist/extractia-sdk.cjs.js",
26
+ format: "cjs",
27
27
  }),
28
28
  build({
29
29
  ...sharedConfig,
30
- entryPoints: ['./src/browser-entry.js'], // <-- usa el entry especial
31
- outfile: './dist/extractia-sdk.browser.js',
32
- format: 'iife',
33
- globalName: 'ExtractiaSDK',
30
+ entryPoints: ["./src/browser-entry.js"], // <-- usa el entry especial
31
+ outfile: "./dist/extractia-sdk.browser.js",
32
+ format: "iife",
33
+ globalName: "ExtractiaSDK",
34
34
  }),
35
- ]).catch(() => process.exit(1));
35
+ ])
36
+ .then(() => {
37
+ // Copy TypeScript declarations to dist so the `types` field in package.json resolves correctly.
38
+ fs.copyFileSync("./src/index.d.ts", "./dist/index.d.ts");
39
+ })
40
+ .catch(() => process.exit(1));
@@ -91,6 +91,7 @@ var ExtractiaSDK = (() => {
91
91
  // src/auth.js
92
92
  var auth_exports = {};
93
93
  __export(auth_exports, {
94
+ configure: () => configure,
94
95
  getMyProfile: () => getMyProfile,
95
96
  setToken: () => setToken,
96
97
  updateWebhook: () => updateWebhook
@@ -2608,22 +2609,93 @@ var ExtractiaSDK = (() => {
2608
2609
  mergeConfig: mergeConfig2
2609
2610
  } = axios_default;
2610
2611
 
2612
+ // src/errors.js
2613
+ var ExtractiaError = class extends Error {
2614
+ /** @param {string} message @param {number} status */
2615
+ constructor(message, status) {
2616
+ super(message);
2617
+ this.name = "ExtractiaError";
2618
+ this.status = status;
2619
+ }
2620
+ };
2621
+ var AuthError = class extends ExtractiaError {
2622
+ constructor(message = "Unauthorized. Check your API token.") {
2623
+ super(message, 401);
2624
+ this.name = "AuthError";
2625
+ }
2626
+ };
2627
+ var ForbiddenError = class extends ExtractiaError {
2628
+ constructor(message = "Forbidden. Insufficient permissions.") {
2629
+ super(message, 403);
2630
+ this.name = "ForbiddenError";
2631
+ }
2632
+ };
2633
+ var TierError = class extends ExtractiaError {
2634
+ constructor(message = "Tier limit reached. Upgrade your plan.", status = 402) {
2635
+ super(message, status);
2636
+ this.name = "TierError";
2637
+ }
2638
+ };
2639
+ var RateLimitError = class extends ExtractiaError {
2640
+ constructor(message = "Too many requests. Please slow down.") {
2641
+ super(message, 429);
2642
+ this.name = "RateLimitError";
2643
+ }
2644
+ };
2645
+ var NotFoundError = class extends ExtractiaError {
2646
+ constructor(message = "Resource not found.") {
2647
+ super(message, 404);
2648
+ this.name = "NotFoundError";
2649
+ }
2650
+ };
2651
+ function mapAxiosError(err) {
2652
+ var _a, _b;
2653
+ const status = (_a = err.response) == null ? void 0 : _a.status;
2654
+ const serverMessage = (_b = err.response) == null ? void 0 : _b.data;
2655
+ const detail = typeof serverMessage === "string" ? serverMessage : void 0;
2656
+ switch (status) {
2657
+ case 401:
2658
+ return new AuthError(detail);
2659
+ case 402:
2660
+ return new TierError(detail);
2661
+ case 403:
2662
+ return new ForbiddenError(detail);
2663
+ case 404:
2664
+ return new NotFoundError(detail);
2665
+ case 429:
2666
+ return new RateLimitError(detail);
2667
+ default:
2668
+ return new ExtractiaError(detail != null ? detail : err.message, status != null ? status : 0);
2669
+ }
2670
+ }
2671
+
2611
2672
  // src/apiClient.js
2612
2673
  var token = null;
2674
+ var DEFAULT_BASE_URL = "https://api.extractia.info/api/public";
2613
2675
  var api = axios_default.create({
2614
- baseURL: "https://www.extractia-api.cat/api/public",
2615
- timeout: 1e4
2676
+ baseURL: DEFAULT_BASE_URL,
2677
+ timeout: 6e4
2678
+ // 60s — AI processing can take 10–30s
2616
2679
  });
2617
2680
  api.interceptors.request.use((config) => {
2618
2681
  if (!token) {
2619
- throw new Error("API token is required");
2682
+ throw new Error(
2683
+ "API token is required. Call setToken(yourApiToken) before making requests."
2684
+ );
2620
2685
  }
2621
2686
  config.headers.Authorization = `Bearer ${token}`;
2622
2687
  return config;
2623
2688
  });
2689
+ api.interceptors.response.use(
2690
+ (response) => response,
2691
+ (err) => Promise.reject(mapAxiosError(err))
2692
+ );
2624
2693
  function setToken(newToken) {
2625
2694
  token = newToken;
2626
2695
  }
2696
+ function configure({ baseURL } = {}) {
2697
+ if (baseURL) api.defaults.baseURL = baseURL;
2698
+ }
2627
2699
  var apiClient_default = api;
2628
2700
 
2629
2701
  // src/auth.js
@@ -2647,6 +2719,7 @@ var ExtractiaSDK = (() => {
2647
2719
  getTemplateById: () => getTemplateById,
2648
2720
  getTemplateByName: () => getTemplateByName,
2649
2721
  getTemplates: () => getTemplates,
2722
+ suggestFields: () => suggestFields,
2650
2723
  updateTemplate: () => updateTemplate
2651
2724
  });
2652
2725
  async function getTemplates() {
@@ -2679,45 +2752,168 @@ var ExtractiaSDK = (() => {
2679
2752
  const res = await apiClient_default.delete(`/templates/${id}/documents`);
2680
2753
  return res.data;
2681
2754
  }
2755
+ async function suggestFields(templateName, extractionContext = "") {
2756
+ const res = await apiClient_default.post("/templates/suggest-fields", {
2757
+ templateName,
2758
+ extractionContext
2759
+ });
2760
+ return res.data;
2761
+ }
2682
2762
 
2683
2763
  // src/documents.js
2684
2764
  var documents_exports = {};
2685
2765
  __export(documents_exports, {
2766
+ bulkPreconform: () => bulkPreconform,
2686
2767
  deleteDocument: () => deleteDocument,
2768
+ exportDocumentsCsv: () => exportDocumentsCsv,
2769
+ exportDocumentsJson: () => exportDocumentsJson,
2770
+ generateDocumentSummary: () => generateDocumentSummary,
2771
+ getDocumentById: () => getDocumentById,
2687
2772
  getDocumentsByTemplateId: () => getDocumentsByTemplateId,
2773
+ getRecentDocuments: () => getRecentDocuments,
2688
2774
  processImage: () => processImage,
2689
- processImagesMultipage: () => processImagesMultipage
2775
+ processImagesMultipage: () => processImagesMultipage,
2776
+ updateDocumentData: () => updateDocumentData,
2777
+ updateDocumentNotes: () => updateDocumentNotes,
2778
+ updateDocumentStatus: () => updateDocumentStatus
2690
2779
  });
2691
2780
  async function getDocumentsByTemplateId(templateId, options = {}) {
2692
- var _a, _b, _c;
2693
- const params = {
2694
- preconformed: (_a = options.preconformed) != null ? _a : null,
2695
- index: (_b = options.index) != null ? _b : 0,
2696
- sort: (_c = options.sort) != null ? _c : -1,
2697
- includeImage: options.includeImage ? 1 : 0
2698
- };
2781
+ const params = {};
2782
+ if (options.preconformed != null) params.preconformed = options.preconformed;
2783
+ if (options.index != null) params.index = options.index;
2784
+ if (options.sort != null) params.sort = options.sort;
2785
+ params.includeImage = options.includeImage ? 1 : 0;
2699
2786
  const res = await apiClient_default.get(`/templates/${templateId}/documents`, { params });
2700
2787
  return res.data;
2701
2788
  }
2789
+ async function getDocumentById(templateId, docId, options = {}) {
2790
+ const params = { includeImage: options.includeImage ? 1 : 0 };
2791
+ const res = await apiClient_default.get(`/templates/${templateId}/documents/${docId}`, {
2792
+ params
2793
+ });
2794
+ return res.data;
2795
+ }
2796
+ async function getRecentDocuments(size = 10) {
2797
+ const res = await apiClient_default.get("/documents/recent", {
2798
+ params: { size: Math.min(size, 50) }
2799
+ });
2800
+ return res.data;
2801
+ }
2702
2802
  async function deleteDocument(documentId) {
2703
2803
  const res = await apiClient_default.delete(`/documents/${documentId}`);
2704
2804
  return res.data;
2705
2805
  }
2706
2806
  async function processImage(templateId, base64Image) {
2707
- const res = await apiClient_default.post(`/public/templates/${templateId}/process`, {
2807
+ const res = await apiClient_default.post(`/templates/${templateId}/process`, {
2708
2808
  image: base64Image
2709
2809
  });
2710
2810
  return res.data;
2711
2811
  }
2712
2812
  async function processImagesMultipage(templateId, base64ImagesArray) {
2713
- const res = await apiClient_default.post(`/public/templates/${templateId}/process-multipage`, {
2813
+ const res = await apiClient_default.post(`/templates/${templateId}/process-multipage`, {
2714
2814
  images: base64ImagesArray
2715
2815
  });
2716
2816
  return res.data;
2717
2817
  }
2818
+ async function generateDocumentSummary(docId) {
2819
+ const res = await apiClient_default.post(`/documents/${docId}/summary`);
2820
+ return res.data;
2821
+ }
2822
+ async function updateDocumentStatus(docId, status) {
2823
+ const res = await apiClient_default.put(`/documents/${docId}/status`, { status });
2824
+ return res.data;
2825
+ }
2826
+ async function updateDocumentNotes(docId, notes) {
2827
+ const res = await apiClient_default.put(`/documents/${docId}/notes`, { notes });
2828
+ return res.data;
2829
+ }
2830
+ async function updateDocumentData(docId, data, options = {}) {
2831
+ var _a;
2832
+ const res = await apiClient_default.put(`/documents/${docId}/data`, {
2833
+ data,
2834
+ preconformed: (_a = options.preconformed) != null ? _a : false
2835
+ });
2836
+ return res.data;
2837
+ }
2838
+ async function bulkPreconform(ids) {
2839
+ const res = await apiClient_default.post("/documents/bulk-preconform", { ids });
2840
+ return res.data;
2841
+ }
2842
+ async function exportDocumentsCsv(templateId, options = {}) {
2843
+ const params = {};
2844
+ if (options.fields && options.fields.length > 0)
2845
+ params.fields = options.fields.join(",");
2846
+ const res = await apiClient_default.get(`/templates/${templateId}/documents/export/csv`, {
2847
+ params,
2848
+ responseType: "text"
2849
+ });
2850
+ return res.data;
2851
+ }
2852
+ async function exportDocumentsJson(templateId) {
2853
+ const res = await apiClient_default.get(`/templates/${templateId}/documents/export/json`);
2854
+ return res.data;
2855
+ }
2856
+
2857
+ // src/analytics.js
2858
+ var analytics_exports = {};
2859
+ __export(analytics_exports, {
2860
+ getCreditsBalance: () => getCreditsBalance,
2861
+ getCreditsHistory: () => getCreditsHistory,
2862
+ getDocumentHistory: () => getDocumentHistory
2863
+ });
2864
+ async function getCreditsBalance() {
2865
+ const res = await apiClient_default.get("/me/credits");
2866
+ return res.data;
2867
+ }
2868
+ async function getCreditsHistory({ page = 0, size = 20 } = {}) {
2869
+ const res = await apiClient_default.get("/me/credits/history", {
2870
+ params: { page, size: Math.min(size, 100) }
2871
+ });
2872
+ return res.data;
2873
+ }
2874
+ async function getDocumentHistory({ page = 0, size = 20 } = {}) {
2875
+ const res = await apiClient_default.get("/me/documents/history", {
2876
+ params: { page, size: Math.min(size, 100) }
2877
+ });
2878
+ return res.data;
2879
+ }
2880
+
2881
+ // src/ocrTools.js
2882
+ var ocrTools_exports = {};
2883
+ __export(ocrTools_exports, {
2884
+ createOcrTool: () => createOcrTool,
2885
+ deleteOcrTool: () => deleteOcrTool,
2886
+ getOcrTools: () => getOcrTools,
2887
+ runOcrTool: () => runOcrTool,
2888
+ updateOcrTool: () => updateOcrTool
2889
+ });
2890
+ async function getOcrTools() {
2891
+ const res = await apiClient_default.get("/ocr-tools");
2892
+ return res.data;
2893
+ }
2894
+ async function createOcrTool(config) {
2895
+ const res = await apiClient_default.post("/ocr-tools", config);
2896
+ return res.data;
2897
+ }
2898
+ async function updateOcrTool(id, config) {
2899
+ const res = await apiClient_default.put(`/ocr-tools/${id}`, config);
2900
+ return res.data;
2901
+ }
2902
+ async function deleteOcrTool(id) {
2903
+ const res = await apiClient_default.delete(`/ocr-tools/${id}`);
2904
+ return res.data;
2905
+ }
2906
+ async function runOcrTool(id, base64Image, options = {}) {
2907
+ const body = { image: base64Image };
2908
+ if (options.params && Object.keys(options.params).length > 0) {
2909
+ body.params = options.params;
2910
+ }
2911
+ const res = await apiClient_default.post(`/ocr-tools/${id}/run`, body);
2912
+ return res.data;
2913
+ }
2718
2914
 
2719
2915
  // src/browser-entry.js
2720
- var extractia = __spreadValues(__spreadValues(__spreadValues({}, auth_exports), templates_exports), documents_exports);
2916
+ var extractia = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, auth_exports), templates_exports), documents_exports), analytics_exports), ocrTools_exports);
2721
2917
  var browser_entry_default = extractia;
2722
2918
  return __toCommonJS(browser_entry_exports);
2723
2919
  })();