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/src/index.js CHANGED
@@ -1,16 +1,32 @@
1
- export * from './auth.js';
2
- export * from './templates.js';
3
- export * from './documents.js';
4
-
5
- import * as auth from './auth.js';
6
- import * as templates from './templates.js';
7
- import * as documents from './documents.js';
1
+ export * from "./auth.js";
2
+ export * from "./templates.js";
3
+ export * from "./documents.js";
4
+ export * from "./analytics.js";
5
+ export * from "./ocrTools.js";
6
+ export * from "./subusers.js";
7
+ export {
8
+ ExtractiaError,
9
+ AuthError,
10
+ ForbiddenError,
11
+ TierError,
12
+ RateLimitError,
13
+ NotFoundError,
14
+ } from "./errors.js";
8
15
 
16
+ import * as auth from "./auth.js";
17
+ import * as templates from "./templates.js";
18
+ import * as documents from "./documents.js";
19
+ import * as analytics from "./analytics.js";
20
+ import * as ocrTools from "./ocrTools.js";
21
+ import * as subusers from "./subusers.js";
9
22
 
10
23
  const extractia = {
11
24
  ...auth,
12
25
  ...templates,
13
26
  ...documents,
27
+ ...analytics,
28
+ ...ocrTools,
29
+ ...subusers,
14
30
  };
15
31
 
16
- export default extractia;
32
+ export default extractia;
@@ -0,0 +1,84 @@
1
+ // src/ocrTools.js
2
+ import api from "./apiClient.js";
3
+
4
+ /**
5
+ * Returns all OCR tool configurations belonging to the authenticated user.
6
+ *
7
+ * @returns {Promise<Object[]>} Array of OcrToolConfig objects.
8
+ */
9
+ export async function getOcrTools() {
10
+ const res = await api.get("/ocr-tools");
11
+ return res.data;
12
+ }
13
+
14
+ /**
15
+ * Creates a new OCR tool configuration.
16
+ *
17
+ * @param {Object} config - Tool definition.
18
+ * @param {string} config.name - Human-friendly display name.
19
+ * @param {string} config.prompt - The AI instruction/question (max 3 000 chars). May include
20
+ * {{?1}}, {{?2}}, … placeholders for dynamic parameters.
21
+ * @param {'YES_NO'|'LABEL'|'TEXT'} config.outputType - Expected output shape.
22
+ * @param {string[]} [config.labels] - Required when outputType is 'LABEL'.
23
+ * @param {Array<{key:number,label:string,description?:string,maxChars?:number}>} [config.parameterDefinitions]
24
+ * Dynamic parameter definitions for {{?N}} placeholders. Each entry needs at minimum `key` (1-based) and
25
+ * `label`. Optional `maxChars` (1–500, default 200) limits per-parameter input length.
26
+ * @returns {Promise<Object>} The created OcrToolConfig.
27
+ */
28
+ export async function createOcrTool(config) {
29
+ const res = await api.post("/ocr-tools", config);
30
+ return res.data;
31
+ }
32
+
33
+ /**
34
+ * Updates an existing OCR tool configuration.
35
+ *
36
+ * @param {string} id - The OCR tool config ID.
37
+ * @param {Object} config - Updated fields (name, prompt, outputType, labels).
38
+ * @returns {Promise<Object>} The updated OcrToolConfig.
39
+ */
40
+ export async function updateOcrTool(id, config) {
41
+ const res = await api.put(`/ocr-tools/${id}`, config);
42
+ return res.data;
43
+ }
44
+
45
+ /**
46
+ * Deletes an OCR tool configuration.
47
+ *
48
+ * @param {string} id - The OCR tool config ID.
49
+ * @returns {Promise<void>}
50
+ */
51
+ export async function deleteOcrTool(id) {
52
+ const res = await api.delete(`/ocr-tools/${id}`);
53
+ return res.data;
54
+ }
55
+
56
+ /**
57
+ * Runs an OCR tool against a base64-encoded image.
58
+ *
59
+ * The image may include a data-URL prefix (e.g. `data:image/jpeg;base64,...`)
60
+ * or be a raw base64 string.
61
+ *
62
+ * Each run deducts **1 document credit** from the user's quota plus AI credits
63
+ * based on token usage: `ceil(totalTokens / 1000)` credits.
64
+ *
65
+ * The AI response language matches the language used in the prompt / parameter values.
66
+ *
67
+ * @param {string} id - The OCR tool config ID.
68
+ * @param {string} base64Image - Base64-encoded image (with or without data-URL prefix). Max 5 MB.
69
+ * @param {{ params?: Record<string, string> }} [options]
70
+ * Optional. `params` is a map from placeholder key (as string, e.g. "1") to value, used to
71
+ * substitute {{?N}} placeholders in the tool's prompt. Values must not exceed the per-parameter
72
+ * `maxChars` limit; the substituted prompt must not exceed 3 000 characters total.
73
+ * @returns {Promise<{ answer: string, explanation: string }>} The AI result.
74
+ * @throws {ExtractiaError} 400 if a required parameter is missing/too long or the prompt limit is exceeded.
75
+ * @throws {TierError} 402 if the document quota or AI credits are exhausted.
76
+ */
77
+ export async function runOcrTool(id, base64Image, options = {}) {
78
+ const body = { image: base64Image };
79
+ if (options.params && Object.keys(options.params).length > 0) {
80
+ body.params = options.params;
81
+ }
82
+ const res = await api.post(`/ocr-tools/${id}/run`, body);
83
+ return res.data;
84
+ }
@@ -0,0 +1,85 @@
1
+ // src/subusers.js
2
+ import api from "./apiClient.js";
3
+
4
+ /**
5
+ * Returns all sub-users for the authenticated account.
6
+ *
7
+ * Sub-users can log in to the web app and perform actions within the
8
+ * permissions granted by the owner. This endpoint is only available on
9
+ * plans that support sub-users (Pro and above).
10
+ *
11
+ * @returns {Promise<Array<{
12
+ * username: string,
13
+ * permissions: string[],
14
+ * suspended: boolean,
15
+ * lastKnownLocation?: string,
16
+ * lastLocationAt?: string
17
+ * }>>} Array of sub-user objects. Password hashes are never returned.
18
+ */
19
+ export async function getSubUsers() {
20
+ const res = await api.get("/me/subusers");
21
+ return res.data;
22
+ }
23
+
24
+ /**
25
+ * Creates a new sub-user for the authenticated account.
26
+ *
27
+ * Available permissions: `"upload"`, `"view"`, `"template"`, `"settings"`,
28
+ * `"export"`, `"api"`.
29
+ *
30
+ * @param {Object} subUser - Sub-user definition.
31
+ * @param {string} subUser.username - Unique username (must not be an existing account email).
32
+ * @param {string} subUser.password - Initial password (must differ from the owner's password).
33
+ * @param {string[]} subUser.permissions - List of permission strings to grant.
34
+ * @returns {Promise<{ username: string, permissions: string[] }>} Confirmation object.
35
+ * @throws {ForbiddenError} 403 if the plan does not allow sub-users or the limit is reached.
36
+ * @throws {ExtractiaError} 409 if the username is already taken.
37
+ */
38
+ export async function createSubUser({ username, password, permissions }) {
39
+ const res = await api.post("/me/subusers", { username, password, permissions });
40
+ return res.data;
41
+ }
42
+
43
+ /**
44
+ * Deletes a sub-user by username.
45
+ *
46
+ * @param {string} username - The sub-user's username.
47
+ * @returns {Promise<{ deleted: string }>} Confirmation with the deleted username.
48
+ * @throws {NotFoundError} 404 if the sub-user does not exist.
49
+ */
50
+ export async function deleteSubUser(username) {
51
+ const res = await api.delete(`/me/subusers/${encodeURIComponent(username)}`);
52
+ return res.data;
53
+ }
54
+
55
+ /**
56
+ * Updates the permissions and/or password of a sub-user.
57
+ *
58
+ * Pass only the fields you want to change. Omitting `password` leaves it unchanged.
59
+ *
60
+ * @param {string} username - The sub-user's username.
61
+ * @param {Object} updates - Fields to update.
62
+ * @param {string[]} [updates.permissions] - New permission set (replaces existing).
63
+ * @param {string} [updates.password] - New password (must differ from owner's password).
64
+ * @returns {Promise<{ username: string, updated: boolean }>}
65
+ * @throws {NotFoundError} 404 if the sub-user does not exist.
66
+ */
67
+ export async function updateSubUser(username, updates = {}) {
68
+ const res = await api.put(`/me/subusers/${encodeURIComponent(username)}`, updates);
69
+ return res.data;
70
+ }
71
+
72
+ /**
73
+ * Toggles the suspended state of a sub-user.
74
+ *
75
+ * A suspended sub-user cannot log in until unsuspended. Their permissions
76
+ * are retained and restored on unsuspension.
77
+ *
78
+ * @param {string} username - The sub-user's username.
79
+ * @returns {Promise<{ username: string, suspended: boolean }>} New suspension state.
80
+ * @throws {NotFoundError} 404 if the sub-user does not exist.
81
+ */
82
+ export async function toggleSuspendSubUser(username) {
83
+ const res = await api.put(`/me/subusers/${encodeURIComponent(username)}/suspend`);
84
+ return res.data;
85
+ }
package/src/templates.js CHANGED
@@ -1,39 +1,94 @@
1
1
  // src/templates.js
2
- import api from './apiClient.js';
2
+ import api from "./apiClient.js";
3
3
 
4
+ /**
5
+ * Returns all templates belonging to the authenticated user.
6
+ * @returns {Promise<Object[]>} Array of template objects.
7
+ */
4
8
  export async function getTemplates() {
5
- const res = await api.get('/templates');
9
+ const res = await api.get("/templates");
6
10
  return res.data;
7
11
  }
8
12
 
13
+ /**
14
+ * Returns a single template by its ID.
15
+ * @param {string} id - The template ID.
16
+ * @returns {Promise<Object>} Template object.
17
+ */
9
18
  export async function getTemplateById(id) {
10
19
  const res = await api.get(`/templates/${id}`);
11
20
  return res.data;
12
21
  }
13
22
 
23
+ /**
24
+ * Returns a template by its label name.
25
+ * @param {string} name - The template label.
26
+ * @returns {Promise<Object>} Template object.
27
+ */
14
28
  export async function getTemplateByName(name) {
15
29
  const res = await api.get(`/templates/name`, {
16
- params: { name }
30
+ params: { name },
17
31
  });
18
32
  return res.data;
19
33
  }
20
34
 
35
+ /**
36
+ * Creates a new template.
37
+ * @param {Object} template - Template definition object.
38
+ * @returns {Promise<Object>} The created template.
39
+ */
21
40
  export async function createTemplate(template) {
22
- const res = await api.post('/templates', template);
41
+ const res = await api.post("/templates", template);
23
42
  return res.data;
24
43
  }
25
44
 
45
+ /**
46
+ * Updates an existing template.
47
+ * @param {string} id - The template ID to update.
48
+ * @param {Object} template - Partial or full template definition.
49
+ * @returns {Promise<Object>} The updated template.
50
+ */
26
51
  export async function updateTemplate(id, template) {
27
52
  const res = await api.put(`/templates/${id}`, template);
28
53
  return res.data;
29
54
  }
30
55
 
56
+ /**
57
+ * Deletes a template by its ID.
58
+ * @param {string} id - The template ID to delete.
59
+ * @returns {Promise<void>}
60
+ */
31
61
  export async function deleteTemplate(id) {
32
62
  const res = await api.delete(`/templates/${id}`);
33
63
  return res.data;
34
64
  }
35
65
 
66
+ /**
67
+ * Deletes all documents associated with a template.
68
+ * @param {string} id - The template ID.
69
+ * @returns {Promise<void>}
70
+ */
36
71
  export async function deleteAllTemplateDocuments(id) {
37
72
  const res = await api.delete(`/templates/${id}/documents`);
38
73
  return res.data;
39
74
  }
75
+
76
+ /**
77
+ * Uses AI to suggest extraction field definitions for a given document type.
78
+ *
79
+ * Returns an array of field definitions that you can use directly when creating
80
+ * or updating a FormTemplate.
81
+ *
82
+ * @param {string} templateName - Document type name (e.g. "Invoice", "Receipt").
83
+ * @param {string} [extractionContext] - Optional natural-language description of what
84
+ * to extract (e.g. "I need all line items with
85
+ * product name and quantity").
86
+ * @returns {Promise<Array<{ label: string, type: string, required: boolean, listLabel?: string }>>}
87
+ */
88
+ export async function suggestFields(templateName, extractionContext = "") {
89
+ const res = await api.post("/templates/suggest-fields", {
90
+ templateName,
91
+ extractionContext,
92
+ });
93
+ return res.data;
94
+ }