@fonderie/client 0.18.0 → 0.20.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/dist/index.d.cts CHANGED
@@ -17,7 +17,8 @@ declare class FonderieApiError extends Error {
17
17
  readonly explanation: string;
18
18
  readonly status: number;
19
19
  readonly details?: unknown | undefined;
20
- constructor(reason: string, explanation: string, status: number, details?: unknown | undefined);
20
+ readonly requestId?: string | undefined;
21
+ constructor(reason: string, explanation: string, status: number, details?: unknown | undefined, requestId?: string | undefined);
21
22
  }
22
23
  interface IRequestOptions {
23
24
  method: string;
package/dist/index.d.ts CHANGED
@@ -17,7 +17,8 @@ declare class FonderieApiError extends Error {
17
17
  readonly explanation: string;
18
18
  readonly status: number;
19
19
  readonly details?: unknown | undefined;
20
- constructor(reason: string, explanation: string, status: number, details?: unknown | undefined);
20
+ readonly requestId?: string | undefined;
21
+ constructor(reason: string, explanation: string, status: number, details?: unknown | undefined, requestId?: string | undefined);
21
22
  }
22
23
  interface IRequestOptions {
23
24
  method: string;
package/dist/index.js CHANGED
@@ -37,19 +37,30 @@ function createMemoryCache(opts = {}) {
37
37
 
38
38
  // src/http.ts
39
39
  var FonderieApiError = class extends Error {
40
- constructor(reason, explanation, status, details) {
40
+ constructor(reason, explanation, status, details, requestId) {
41
41
  super(explanation);
42
42
  this.reason = reason;
43
43
  this.explanation = explanation;
44
44
  this.status = status;
45
45
  this.details = details;
46
+ this.requestId = requestId;
46
47
  this.name = "FonderieApiError";
47
48
  }
48
49
  reason;
49
50
  explanation;
50
51
  status;
51
52
  details;
53
+ requestId;
52
54
  };
55
+ function randHex(bytes) {
56
+ const c = globalThis.crypto;
57
+ const arr = new Uint8Array(bytes);
58
+ if (c?.getRandomValues) c.getRandomValues(arr);
59
+ else for (let i = 0; i < bytes; i++) arr[i] = Math.floor(Math.random() * 256);
60
+ let out = "";
61
+ for (const b of arr) out += b.toString(16).padStart(2, "0");
62
+ return out;
63
+ }
53
64
  var HttpClient = class {
54
65
  constructor(baseUrl, deps = {}) {
55
66
  this.baseUrl = baseUrl;
@@ -97,7 +108,13 @@ var HttpClient = class {
97
108
  return data;
98
109
  }
99
110
  async exec(opts, retried = false) {
100
- const headers = { "Content-Type": "application/json" };
111
+ const traceId = randHex(16);
112
+ const requestId = traceId;
113
+ const headers = {
114
+ "Content-Type": "application/json",
115
+ traceparent: `00-${traceId}-${randHex(8)}-01`,
116
+ "X-Request-ID": requestId
117
+ };
101
118
  if (opts.token) headers["Authorization"] = `Bearer ${opts.token}`;
102
119
  if (opts.cookie) headers["Cookie"] = opts.cookie;
103
120
  if (opts.workspaceId) headers["X-Workspace-ID"] = opts.workspaceId;
@@ -105,18 +122,19 @@ var HttpClient = class {
105
122
  const fetchInit = { method: opts.method, headers, credentials: "include" };
106
123
  if (opts.body !== void 0) fetchInit.body = JSON.stringify(opts.body);
107
124
  const res = await fetch(`${this.baseUrl}${opts.path}`, fetchInit);
125
+ const rid = res.headers.get("x-request-id") ?? requestId;
108
126
  if (res.status === 401 && this.refresh && !retried && !opts.path.startsWith("/auth/")) {
109
127
  const newToken = await this.refresh();
110
128
  if (newToken) return this.exec({ ...opts, token: newToken }, true);
111
129
  }
112
130
  if (res.status === 204) {
113
- if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status);
131
+ if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status, void 0, rid);
114
132
  return void 0;
115
133
  }
116
134
  const data = await res.json();
117
135
  if (!res.ok || res.status === 202) {
118
136
  const err = data;
119
- throw new FonderieApiError(err.reason, err.explanation, res.status, err.details);
137
+ throw new FonderieApiError(err.reason, err.explanation, res.status, err.details, rid);
120
138
  }
121
139
  return data;
122
140
  }