@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.
@@ -59,11 +59,12 @@ new FonderieClient(opts: IFonderieClientOptions): FonderieClient
59
59
  .patch<T = unknown>(path: string, body?: unknown, config?: IRequestConfig | undefined): Promise<IApiResponse<T>>
60
60
  .delete<T = unknown>(path: string, config?: IRequestConfig | undefined): Promise<IApiResponse<T>>
61
61
 
62
- new FonderieApiError(reason: string, explanation: string, status: number, details?: unknown): FonderieApiError
62
+ new FonderieApiError(reason: string, explanation: string, status: number, details?: unknown, requestId?: string | undefined): FonderieApiError
63
63
  .reason: string
64
64
  .explanation: string
65
65
  .status: number
66
66
  .details: unknown
67
+ .requestId: string | undefined
67
68
  .name: string
68
69
  .message: string
69
70
  .stack: string
package/dist/index.cjs CHANGED
@@ -75,19 +75,30 @@ function createMemoryCache(opts = {}) {
75
75
 
76
76
  // src/http.ts
77
77
  var FonderieApiError = class extends Error {
78
- constructor(reason, explanation, status, details) {
78
+ constructor(reason, explanation, status, details, requestId) {
79
79
  super(explanation);
80
80
  this.reason = reason;
81
81
  this.explanation = explanation;
82
82
  this.status = status;
83
83
  this.details = details;
84
+ this.requestId = requestId;
84
85
  this.name = "FonderieApiError";
85
86
  }
86
87
  reason;
87
88
  explanation;
88
89
  status;
89
90
  details;
91
+ requestId;
90
92
  };
93
+ function randHex(bytes) {
94
+ const c = globalThis.crypto;
95
+ const arr = new Uint8Array(bytes);
96
+ if (c?.getRandomValues) c.getRandomValues(arr);
97
+ else for (let i = 0; i < bytes; i++) arr[i] = Math.floor(Math.random() * 256);
98
+ let out = "";
99
+ for (const b of arr) out += b.toString(16).padStart(2, "0");
100
+ return out;
101
+ }
91
102
  var HttpClient = class {
92
103
  constructor(baseUrl, deps = {}) {
93
104
  this.baseUrl = baseUrl;
@@ -135,7 +146,13 @@ var HttpClient = class {
135
146
  return data;
136
147
  }
137
148
  async exec(opts, retried = false) {
138
- const headers = { "Content-Type": "application/json" };
149
+ const traceId = randHex(16);
150
+ const requestId = traceId;
151
+ const headers = {
152
+ "Content-Type": "application/json",
153
+ traceparent: `00-${traceId}-${randHex(8)}-01`,
154
+ "X-Request-ID": requestId
155
+ };
139
156
  if (opts.token) headers["Authorization"] = `Bearer ${opts.token}`;
140
157
  if (opts.cookie) headers["Cookie"] = opts.cookie;
141
158
  if (opts.workspaceId) headers["X-Workspace-ID"] = opts.workspaceId;
@@ -143,18 +160,19 @@ var HttpClient = class {
143
160
  const fetchInit = { method: opts.method, headers, credentials: "include" };
144
161
  if (opts.body !== void 0) fetchInit.body = JSON.stringify(opts.body);
145
162
  const res = await fetch(`${this.baseUrl}${opts.path}`, fetchInit);
163
+ const rid = res.headers.get("x-request-id") ?? requestId;
146
164
  if (res.status === 401 && this.refresh && !retried && !opts.path.startsWith("/auth/")) {
147
165
  const newToken = await this.refresh();
148
166
  if (newToken) return this.exec({ ...opts, token: newToken }, true);
149
167
  }
150
168
  if (res.status === 204) {
151
- if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status);
169
+ if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status, void 0, rid);
152
170
  return void 0;
153
171
  }
154
172
  const data = await res.json();
155
173
  if (!res.ok || res.status === 202) {
156
174
  const err = data;
157
- throw new FonderieApiError(err.reason, err.explanation, res.status, err.details);
175
+ throw new FonderieApiError(err.reason, err.explanation, res.status, err.details, rid);
158
176
  }
159
177
  return data;
160
178
  }