@docmana/sdk 0.2.1
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 +17 -0
- package/LICENSE +21 -0
- package/README.md +307 -0
- package/dist/cli.mjs +772 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.cts +107 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.js +449 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +414 -0
- package/dist/index.mjs.map +1 -0
- package/dist/package.json +1 -0
- package/package.json +64 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
- 400 responses now throw `DocmanaRequestError` (was `DocmanaAuthError`); add `Incompleted` to `ExecutionStatus`.
|
|
5
|
+
|
|
6
|
+
## 0.2.0
|
|
7
|
+
- `RunFlowInput.once` selects the ad-hoc `run_once_flow` endpoint (current/draft flow) instead of the published `run_flow`.
|
|
8
|
+
- `DocmanaConfig.headers` are sent on every API request (e.g. `X-Selected-Organization`), without overriding the bearer token or per-request headers.
|
|
9
|
+
- `FileInput` now accepts a plain `Uint8Array` (e.g. pdf-lib output) in addition to path, `Buffer`, and `Stream`.
|
|
10
|
+
- Uploads send a `Content-Type` inferred from the filename extension instead of always `application/octet-stream`.
|
|
11
|
+
|
|
12
|
+
## 0.1.0
|
|
13
|
+
- Single-call `runFlow` orchestration: upload → run → poll → result.
|
|
14
|
+
- OAuth2 client-credentials auth with in-memory token cache and automatic refresh.
|
|
15
|
+
- Typed error hierarchy (`DocmanaError`, `DocmanaExecutionError`, …).
|
|
16
|
+
- Flexible file inputs: path, `Buffer`, `Stream`, and multi-file uploads.
|
|
17
|
+
- Dual ESM + CJS distribution with bundled type definitions.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Docmana
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
# @docmana/sdk
|
|
2
|
+
|
|
3
|
+
Official Docmana SDK — run document-analysis flows with a single call.
|
|
4
|
+
|
|
5
|
+
Docmana's HTTP API requires four steps to analyze a document: upload the file, start a flow run, poll for status, then fetch the result. This SDK collapses all four into one call. Authentication, token caching, and polling are handled for you.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const result = await docmana.runFlow("<flow-id>", { file: "./contract.pdf" });
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @docmana/sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires Node.js 20+.
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { Docmana } from "@docmana/sdk";
|
|
23
|
+
|
|
24
|
+
const docmana = new Docmana({
|
|
25
|
+
clientId: process.env.DOCMANA_CLIENT_ID!,
|
|
26
|
+
clientSecret: process.env.DOCMANA_CLIENT_SECRET!,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const result = await docmana.runFlow("<flow-id>", { file: "./contract.pdf" });
|
|
30
|
+
|
|
31
|
+
console.log(result.status, result.results);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The package ships both ESM and CommonJS builds. In a CommonJS project:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const { Docmana } = require("@docmana/sdk");
|
|
38
|
+
|
|
39
|
+
const docmana = new Docmana({
|
|
40
|
+
clientId: process.env.DOCMANA_CLIENT_ID,
|
|
41
|
+
clientSecret: process.env.DOCMANA_CLIENT_SECRET,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const result = await docmana.runFlow("<flow-id>", { file: "./contract.pdf" });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Authentication
|
|
48
|
+
|
|
49
|
+
The SDK authenticates with OAuth2 `client_credentials` against the Docmana CIAM tenant. Docmana provisions a `client_id` and `client_secret` for you during onboarding — pass them to the constructor. The SDK acquires the bearer token on the first request, then caches and refreshes it automatically in memory. You never handle tokens directly.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
The constructor takes a single `DocmanaConfig` object.
|
|
54
|
+
|
|
55
|
+
| Field | Type | Required | Default | Description |
|
|
56
|
+
| ---------------- | -------------- | -------- | ----------------------------------------- | ------------------------------------------------------------ |
|
|
57
|
+
| `clientId` | `string` | Yes | — | OAuth2 client ID provisioned by Docmana at onboarding. |
|
|
58
|
+
| `clientSecret` | `string` | Yes | — | OAuth2 client secret provisioned by Docmana at onboarding. |
|
|
59
|
+
| `apiBaseUrl` | `string` | No | `https://api.docmana.ai` | Base URL of the Docmana API. |
|
|
60
|
+
| `tokenEndpoint` | `string` | No | Docmana CIAM `oauth2/v2.0/token` endpoint | OAuth2 token endpoint used to acquire the bearer token. |
|
|
61
|
+
| `scope` | `string` | No | `api://d781e6ba-…/.default` | OAuth2 scope requested for the token. |
|
|
62
|
+
| `timeoutMs` | `number` | No | `300000` (5 min) | Maximum time `runFlow` will poll before throwing a timeout. |
|
|
63
|
+
| `pollIntervalMs` | `number` | No | `2000` (2 s) | Delay between status polls in `runFlow`. |
|
|
64
|
+
| `fetch` | `typeof fetch` | No | global `fetch` | Custom `fetch` implementation (e.g. for proxies or testing). |
|
|
65
|
+
|
|
66
|
+
Most integrations only set `clientId` and `clientSecret`. The other fields exist for non-production environments or advanced networking needs.
|
|
67
|
+
|
|
68
|
+
## Inputs
|
|
69
|
+
|
|
70
|
+
`runFlow(flowId, input)` (and the lower-level `runFlowAsync`) accept a `RunFlowInput`:
|
|
71
|
+
|
|
72
|
+
| Field | Type | Description |
|
|
73
|
+
| ---------------- | ------------------------------------------------------------------- | -------------------------------------------------------------------- |
|
|
74
|
+
| `file` | `string \| { path: string } \| Buffer \| NodeJS.ReadableStream` | A single document. A string or `{ path }` is treated as a file path. |
|
|
75
|
+
| `files` | `(string \| { path: string } \| Buffer \| NodeJS.ReadableStream)[]` | Multiple documents for a multi-document flow. |
|
|
76
|
+
| `fileName` | `string` | Required when passing a `Buffer` or stream with no derivable name. |
|
|
77
|
+
| `signal` | `AbortSignal` | Cancels the in-flight run. |
|
|
78
|
+
| `timeoutMs` | `number` | Overrides the configured polling timeout for this call. |
|
|
79
|
+
| `pollIntervalMs` | `number` | Overrides the configured poll interval for this call. |
|
|
80
|
+
|
|
81
|
+
Provide either `file` or `files`.
|
|
82
|
+
|
|
83
|
+
A file path:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
await docmana.runFlow("<flow-id>", { file: "./contract.pdf" });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
A `Buffer` (or stream) — supply `fileName` so Docmana can infer the document type:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { readFile } from "node:fs/promises";
|
|
93
|
+
|
|
94
|
+
const buffer = await readFile("./contract.pdf");
|
|
95
|
+
await docmana.runFlow("<flow-id>", { file: buffer, fileName: "contract.pdf" });
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Multiple documents:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
await docmana.runFlow("<flow-id>", {
|
|
102
|
+
files: ["./statement.pdf", "./id-card.png"],
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Path objects are also supported, which is useful when your caller already models uploads as
|
|
107
|
+
objects:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
await docmana.runFlow("<flow-id>", {
|
|
111
|
+
files: [{ path: "./contract.pdf" }],
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Lower-level methods
|
|
116
|
+
|
|
117
|
+
`runFlow` is the recommended entry point. If you need to manage polling yourself — for example to surface progress in your own job queue — the underlying steps are exposed:
|
|
118
|
+
|
|
119
|
+
- `runFlowAsync(flowId, input)` → `{ executionResultId, fileIds }` — uploads the file(s) and starts the flow without waiting; `fileIds` are the uploaded document UUIDs.
|
|
120
|
+
- `getStatus(executionResultId)` → `{ status }` — current execution status (`"Pending" | "Running" | "Completed" | "Failed"`).
|
|
121
|
+
- `getResult(executionResultId)` → `ExecutionResult` — the result payload once the flow has finished.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
const { executionResultId } = await docmana.runFlowAsync("<flow-id>", {
|
|
125
|
+
file: "./contract.pdf",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
let status = "Pending";
|
|
129
|
+
while (status !== "Completed" && status !== "Failed") {
|
|
130
|
+
await new Promise((r) => setTimeout(r, 2000));
|
|
131
|
+
({ status } = await docmana.getStatus(executionResultId));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const result = await docmana.getResult(executionResultId);
|
|
135
|
+
console.log(result.status, result.results);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Unlike `runFlow`, `getResult` does not throw on a failed execution — inspect `result.status` and `result.errors` yourself.
|
|
139
|
+
|
|
140
|
+
## CLI
|
|
141
|
+
|
|
142
|
+
The package also installs a `docmana` command for simple terminal usage:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
docmana flow run "<flow-id>" --files "./contract.pdf" --organisation "<organisation-id>"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
You can create a local config file interactively:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
docmana config init
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
This writes `docmana.config.json` in the current directory. The file can include Docmana URLs,
|
|
155
|
+
scope, organisation, client id, and client secret, so it is ignored by Git.
|
|
156
|
+
|
|
157
|
+
Use a custom config path when needed:
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
docmana config init --config ./docmana.dev.config.json
|
|
161
|
+
docmana flow run "<flow-id>" --files "./contract.pdf" --config ./docmana.dev.config.json
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`flow run` automatically loads `./docmana.config.json` when present. Credentials and connection
|
|
165
|
+
settings can also be read from the environment:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
DOCMANA_CLIENT_ID="<client-id>"
|
|
169
|
+
DOCMANA_CLIENT_SECRET="<client-secret>"
|
|
170
|
+
DOCMANA_ORGANISATION="<organisation-id>"
|
|
171
|
+
DOCMANA_API_URL="https://api.docmana.ai"
|
|
172
|
+
DOCMANA_TOKEN_URL="<token-endpoint>"
|
|
173
|
+
DOCMANA_SCOPE="<oauth-scope>"
|
|
174
|
+
|
|
175
|
+
docmana flow run "<flow-id>" --files "./contract.pdf,./annexe.pdf"
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
You can override config or environment values with flags:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
docmana flow run "<flow-id>" \
|
|
182
|
+
--files "./contract.pdf" \
|
|
183
|
+
--client-id "<client-id>" \
|
|
184
|
+
--client-secret "<client-secret>" \
|
|
185
|
+
--organisation "<organisation-id>" \
|
|
186
|
+
--api-url "https://api.docmana.ai" \
|
|
187
|
+
--token-url "<token-endpoint>" \
|
|
188
|
+
--scope "<oauth-scope>"
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
The CLI caches OAuth2 access tokens in `docmana.token.json` by default, so repeated commands do
|
|
192
|
+
not request a new token until the cached token expires. The cache file is ignored by Git. Use a
|
|
193
|
+
custom token cache path when needed:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
docmana login
|
|
197
|
+
docmana flow run "<flow-id>" --files "./contract.pdf" --token-cache ./docmana.dev.token.json
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
`docmana login` loads the same config, environment variables, and auth flags as `flow run`, then
|
|
201
|
+
ensures a valid token is cached. It does not print the token.
|
|
202
|
+
|
|
203
|
+
By default, the CLI prints a human-readable summary with a preview of the results. Use `--json`
|
|
204
|
+
to print the complete result payload to stdout:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
docmana flow run "<flow-id>" --files "./contract.pdf" --organisation "<organisation-id>" --json
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
`--files` is a simple comma-separated list. Paths containing commas are not supported.
|
|
211
|
+
|
|
212
|
+
To build the local checkout and expose the `docmana` command on your machine:
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
npm run cli:link
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Then run:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
docmana flow run "<flow-id>" --files "./contract.pdf" --organisation "<organisation-id>"
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Error handling
|
|
225
|
+
|
|
226
|
+
Every error thrown by the SDK extends `DocmanaError`, which carries `.status` (HTTP status, when applicable), `.code`, and an optional `.requestId`.
|
|
227
|
+
|
|
228
|
+
| Error | Thrown when |
|
|
229
|
+
| ------------------------ | ------------------------------------------------------------------------------- |
|
|
230
|
+
| `DocmanaRequestError` | `400` — bad request (e.g. calling for a result before the flow has finished). |
|
|
231
|
+
| `DocmanaAuthError` | `401` — bad credentials, wrong scope, or an expired/rejected token. |
|
|
232
|
+
| `DocmanaPermissionError` | `403` — the client is missing the `access_m2m_apps` role. |
|
|
233
|
+
| `DocmanaNotFoundError` | `404` — the flow or endpoint was not found. |
|
|
234
|
+
| `DocmanaExecutionError` | The flow finished with status `"Failed"`. Carries the flow errors in `.errors`. |
|
|
235
|
+
| `DocmanaTimeoutError` | Polling exceeded `timeoutMs` before the flow reached a terminal state. |
|
|
236
|
+
| `DocmanaAbortError` | The provided `AbortSignal` fired. |
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import {
|
|
240
|
+
Docmana,
|
|
241
|
+
DocmanaAuthError,
|
|
242
|
+
DocmanaExecutionError,
|
|
243
|
+
DocmanaTimeoutError,
|
|
244
|
+
} from "@docmana/sdk";
|
|
245
|
+
|
|
246
|
+
try {
|
|
247
|
+
const result = await docmana.runFlow("<flow-id>", { file: "./contract.pdf" });
|
|
248
|
+
console.log(result.results);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
if (err instanceof DocmanaAuthError) {
|
|
251
|
+
// Check client_id / client_secret / scope.
|
|
252
|
+
} else if (err instanceof DocmanaExecutionError) {
|
|
253
|
+
console.error("Flow failed:", err.errors);
|
|
254
|
+
} else if (err instanceof DocmanaTimeoutError) {
|
|
255
|
+
// Retry, or raise timeoutMs.
|
|
256
|
+
} else {
|
|
257
|
+
throw err;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Security
|
|
263
|
+
|
|
264
|
+
Treat `clientSecret` as a credential with full access to your Docmana account.
|
|
265
|
+
|
|
266
|
+
- Store `clientId` and `clientSecret` in a secrets manager (Azure Key Vault, AWS Secrets Manager, etc.).
|
|
267
|
+
- Never commit them to source control, and never expose them in a browser or frontend bundle — this SDK is server-side only.
|
|
268
|
+
- Never log them.
|
|
269
|
+
|
|
270
|
+
The SDK keeps the bearer token in memory only. It is never written to disk.
|
|
271
|
+
|
|
272
|
+
## Requirements
|
|
273
|
+
|
|
274
|
+
- Node.js 20 or later.
|
|
275
|
+
- ESM and CommonJS are both supported.
|
|
276
|
+
|
|
277
|
+
## Local development with yalc
|
|
278
|
+
|
|
279
|
+
To test changes to this SDK inside a consuming project without publishing to npm, use [yalc](https://github.com/wclr/yalc).
|
|
280
|
+
|
|
281
|
+
In **this library**:
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
npm run dev
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
This runs `tsup` in watch mode. After every rebuild it runs `yalc push`, propagating the freshly built package to every project that has linked it.
|
|
288
|
+
|
|
289
|
+
In the **consuming project** (once):
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
yalc add @docmana/sdk
|
|
293
|
+
npm install
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
From then on, each change you save here is rebuilt and pushed automatically — the consumer picks it up without reinstalling. To stop using the local copy and restore the registry version:
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
yalc remove @docmana/sdk
|
|
300
|
+
npm install
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
For a single one-shot publish to the local yalc store (no watch), use `npm run yalc:publish`.
|
|
304
|
+
|
|
305
|
+
## License
|
|
306
|
+
|
|
307
|
+
MIT
|