@defarm/sdk 0.1.2 → 0.1.4

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.
Files changed (2) hide show
  1. package/dist/client.js +36 -8
  2. package/package.json +4 -2
package/dist/client.js CHANGED
@@ -28,19 +28,47 @@ export class DefarmHttpClient {
28
28
  if (this.apiKey) {
29
29
  headers["x-api-key"] = this.apiKey;
30
30
  }
31
- const response = await undiciRequest(url, {
32
- method,
33
- headers,
34
- body: body == null ? undefined : JSON.stringify(body),
35
- headersTimeout: this.config.timeoutMs,
36
- bodyTimeout: this.config.timeoutMs,
37
- });
31
+ let response;
32
+ try {
33
+ response = await undiciRequest(url, {
34
+ method,
35
+ headers,
36
+ body: body == null ? undefined : JSON.stringify(body),
37
+ headersTimeout: this.config.timeoutMs,
38
+ bodyTimeout: this.config.timeoutMs,
39
+ });
40
+ }
41
+ catch (err) {
42
+ const msg = err instanceof Error ? err.message : String(err);
43
+ if (msg.includes('Headers Timeout') ||
44
+ msg.includes('Body Timeout') ||
45
+ msg.includes('UND_ERR_HEADERS_TIMEOUT') ||
46
+ msg.includes('UND_ERR_BODY_TIMEOUT')) {
47
+ throw new Error(`Request to ${url} timed out after ${this.config.timeoutMs}ms. ` +
48
+ `Check if the gateway is reachable and the service is healthy.`);
49
+ }
50
+ // Enrich other transport errors with URL context
51
+ throw new Error(`Request to ${url} failed: ${msg}`);
52
+ }
38
53
  const text = await response.body.text();
39
54
  const parsed = text ? safeJsonParse(text) : null;
40
55
  if (response.statusCode < 200 || response.statusCode >= 300) {
41
- const message = parsed?.message ||
56
+ let message = parsed?.message ||
42
57
  parsed?.error ||
43
58
  `HTTP ${response.statusCode} on ${path}`;
59
+ const errorCode = parsed?.error;
60
+ const requiresBearer = response.statusCode === 401 &&
61
+ (errorCode === "missing_token" ||
62
+ String(parsed?.message || "")
63
+ .toLowerCase()
64
+ .includes("bearer token required"));
65
+ if (requiresBearer && this.apiKey && !this.accessToken) {
66
+ message = [
67
+ `Endpoint ${path} requires JWT Bearer authentication.`,
68
+ "Current session is API key mode.",
69
+ "Run: defarm auth login --email <email> --password '<password>'",
70
+ ].join(" ");
71
+ }
44
72
  throw new DefarmApiError(message, response.statusCode, parsed);
45
73
  }
46
74
  return parsed ?? {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defarm/sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "DeFarm SDK for CLI and partner integrations",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,7 +22,9 @@
22
22
  "type": "module",
23
23
  "main": "dist/index.js",
24
24
  "types": "dist/index.d.ts",
25
- "files": ["dist"],
25
+ "files": [
26
+ "dist"
27
+ ],
26
28
  "publishConfig": {
27
29
  "access": "public"
28
30
  },