@pdfvector/instance-client 0.0.40 → 0.0.41

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.
@@ -18,8 +18,9 @@ export interface ClientContext {
18
18
  /**
19
19
  * Custom error class for PDF Vector API errors.
20
20
  *
21
- * All API errors thrown by the client are instances of this class.
22
- * Provides structured access to error code, HTTP status, message, and additional data.
21
+ * All errors thrown by the client are instances of this class, including
22
+ * transport-layer failures (DNS, connection refused, timeouts) which surface
23
+ * with `code: "NETWORK_ERROR"` and `status: 0`.
23
24
  *
24
25
  * @example
25
26
  * ```typescript
@@ -27,10 +28,11 @@ export interface ClientContext {
27
28
  * await client.document.parse({ url: "..." });
28
29
  * } catch (error) {
29
30
  * if (error instanceof PDFVectorError) {
30
- * console.error(error.code); // "UNAUTHORIZED" | "BAD_REQUEST" | ...
31
- * console.error(error.status); // 401 | 400 | 422 | 500
32
- * console.error(error.message); // "Invalid API key"
33
- * console.error(error.data); // { requestId: 1 }
31
+ * console.error(error.code); // "UNAUTHORIZED" | "BAD_REQUEST" | "NETWORK_ERROR" | ...
32
+ * console.error(error.status); // 401 | 400 | 422 | 500 | 0 (network)
33
+ * console.error(error.message); // "Invalid API key"
34
+ * console.error(error.userError); // true if the error is caused by user input (illegible upload, etc.)
35
+ * console.error(error.data); // { requestId: 1, userError: true }
34
36
  * }
35
37
  * }
36
38
  * ```
@@ -38,10 +40,16 @@ export interface ClientContext {
38
40
  export declare class PDFVectorError extends Error {
39
41
  /** Error code identifying the type of error */
40
42
  readonly code: string;
41
- /** HTTP status code */
43
+ /** HTTP status code. `0` for transport-layer failures (see `NETWORK_ERROR`). */
42
44
  readonly status: number;
43
- /** Additional error data from the server (e.g., requestId) */
45
+ /** Additional error data from the server (e.g., `requestId`, `userError`) */
44
46
  readonly data: unknown;
47
+ /**
48
+ * True when the server flagged this error as caused by user input
49
+ * (e.g. illegible uploads, empty documents) rather than a server-side
50
+ * failure. Mirrors `data.userError === true`.
51
+ */
52
+ readonly userError: boolean;
45
53
  constructor(options: {
46
54
  code: string;
47
55
  message: string;
package/.tsc/lib/index.js CHANGED
@@ -4,8 +4,9 @@ const DEFAULT_DOMAIN = "global.pdfvector.com";
4
4
  /**
5
5
  * Custom error class for PDF Vector API errors.
6
6
  *
7
- * All API errors thrown by the client are instances of this class.
8
- * Provides structured access to error code, HTTP status, message, and additional data.
7
+ * All errors thrown by the client are instances of this class, including
8
+ * transport-layer failures (DNS, connection refused, timeouts) which surface
9
+ * with `code: "NETWORK_ERROR"` and `status: 0`.
9
10
  *
10
11
  * @example
11
12
  * ```typescript
@@ -13,10 +14,11 @@ const DEFAULT_DOMAIN = "global.pdfvector.com";
13
14
  * await client.document.parse({ url: "..." });
14
15
  * } catch (error) {
15
16
  * if (error instanceof PDFVectorError) {
16
- * console.error(error.code); // "UNAUTHORIZED" | "BAD_REQUEST" | ...
17
- * console.error(error.status); // 401 | 400 | 422 | 500
18
- * console.error(error.message); // "Invalid API key"
19
- * console.error(error.data); // { requestId: 1 }
17
+ * console.error(error.code); // "UNAUTHORIZED" | "BAD_REQUEST" | "NETWORK_ERROR" | ...
18
+ * console.error(error.status); // 401 | 400 | 422 | 500 | 0 (network)
19
+ * console.error(error.message); // "Invalid API key"
20
+ * console.error(error.userError); // true if the error is caused by user input (illegible upload, etc.)
21
+ * console.error(error.data); // { requestId: 1, userError: true }
20
22
  * }
21
23
  * }
22
24
  * ```
@@ -24,16 +26,26 @@ const DEFAULT_DOMAIN = "global.pdfvector.com";
24
26
  export class PDFVectorError extends Error {
25
27
  /** Error code identifying the type of error */
26
28
  code;
27
- /** HTTP status code */
29
+ /** HTTP status code. `0` for transport-layer failures (see `NETWORK_ERROR`). */
28
30
  status;
29
- /** Additional error data from the server (e.g., requestId) */
31
+ /** Additional error data from the server (e.g., `requestId`, `userError`) */
30
32
  data;
33
+ /**
34
+ * True when the server flagged this error as caused by user input
35
+ * (e.g. illegible uploads, empty documents) rather than a server-side
36
+ * failure. Mirrors `data.userError === true`.
37
+ */
38
+ userError;
31
39
  constructor(options) {
32
40
  super(options.message);
33
41
  this.name = "PDFVectorError";
34
42
  this.code = options.code;
35
43
  this.status = options.status;
36
44
  this.data = options.data;
45
+ this.userError =
46
+ options.data != null &&
47
+ typeof options.data === "object" &&
48
+ options.data.userError === true;
37
49
  }
38
50
  /**
39
51
  * Type guard to check if an unknown error is a PDFVectorError.
@@ -48,6 +60,23 @@ export function _buildClient(options) {
48
60
  const isLocal = domain.startsWith("localhost") || domain.startsWith("127.0.0.1");
49
61
  const baseUrl = `${isLocal ? "http" : "https"}://${domain}`;
50
62
  const token = options?.apiKey ?? options?.secret;
63
+ const wrappedFetch = async (input, init) => {
64
+ try {
65
+ return await (token
66
+ ? fetch(input, init)
67
+ : fetch(input, { ...init, credentials: "include" }));
68
+ }
69
+ catch (err) {
70
+ throw new PDFVectorError({
71
+ code: "NETWORK_ERROR",
72
+ message: err instanceof Error
73
+ ? err.message
74
+ : "Network request failed before reaching the server",
75
+ status: 0,
76
+ data: { cause: err },
77
+ });
78
+ }
79
+ };
51
80
  const link = new RPCLink({
52
81
  url: `${baseUrl}/rpc`,
53
82
  headers: ({ context }) => {
@@ -62,11 +91,12 @@ export function _buildClient(options) {
62
91
  }
63
92
  return headers;
64
93
  },
65
- fetch: token
66
- ? undefined
67
- : (input, init) => fetch(input, { ...init, credentials: "include" }),
94
+ fetch: wrappedFetch,
68
95
  interceptors: [
69
96
  onError((error) => {
97
+ if (error instanceof PDFVectorError) {
98
+ throw error;
99
+ }
70
100
  if (error instanceof ORPCError) {
71
101
  throw new PDFVectorError({
72
102
  code: error.code,
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @pdfvector/instance-client
2
2
 
3
+ ## 0.0.41
4
+ ### Patch Changes
5
+
6
+
7
+
8
+ - [#205](https://github.com/phuctm97/pdfvector/pull/205) [`93b9624`](https://github.com/phuctm97/pdfvector/commit/93b9624333f643b143062e08c6af216eed5a859a) Thanks [@khanhduyvt0101](https://github.com/khanhduyvt0101)! - Improve SDK error handling and tighten academic provider types
9
+
10
+ - Updated dependencies [[`93b9624`](https://github.com/phuctm97/pdfvector/commit/93b9624333f643b143062e08c6af216eed5a859a)]:
11
+ - @pdfvector/instance-contract@0.0.40
12
+
3
13
  ## 0.0.40
4
14
  ### Patch Changes
5
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdfvector/instance-client",
3
- "version": "0.0.40",
3
+ "version": "0.0.41",
4
4
  "type": "module",
5
5
  "description": "Official TypeScript/JavaScript SDK for PDF Vector API - Parse PDF/Word/Image/Excel documents to clean, structured markdown format and search academic publications across multiple databases",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "dependencies": {
36
36
  "@orpc/client": "^1.13.14",
37
37
  "@orpc/contract": "^1.13.14",
38
- "@pdfvector/instance-contract": "^0.0.39"
38
+ "@pdfvector/instance-contract": "^0.0.40"
39
39
  },
40
40
  "files": [
41
41
  ".tsc",