@fonderie/client 0.18.0 → 0.19.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,26 @@ 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 newRequestId() {
94
+ const c = globalThis.crypto;
95
+ if (c?.randomUUID) return c.randomUUID();
96
+ return `r-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
97
+ }
91
98
  var HttpClient = class {
92
99
  constructor(baseUrl, deps = {}) {
93
100
  this.baseUrl = baseUrl;
@@ -135,7 +142,11 @@ var HttpClient = class {
135
142
  return data;
136
143
  }
137
144
  async exec(opts, retried = false) {
138
- const headers = { "Content-Type": "application/json" };
145
+ const requestId = newRequestId();
146
+ const headers = {
147
+ "Content-Type": "application/json",
148
+ "X-Request-ID": requestId
149
+ };
139
150
  if (opts.token) headers["Authorization"] = `Bearer ${opts.token}`;
140
151
  if (opts.cookie) headers["Cookie"] = opts.cookie;
141
152
  if (opts.workspaceId) headers["X-Workspace-ID"] = opts.workspaceId;
@@ -143,18 +154,19 @@ var HttpClient = class {
143
154
  const fetchInit = { method: opts.method, headers, credentials: "include" };
144
155
  if (opts.body !== void 0) fetchInit.body = JSON.stringify(opts.body);
145
156
  const res = await fetch(`${this.baseUrl}${opts.path}`, fetchInit);
157
+ const rid = res.headers.get("x-request-id") ?? requestId;
146
158
  if (res.status === 401 && this.refresh && !retried && !opts.path.startsWith("/auth/")) {
147
159
  const newToken = await this.refresh();
148
160
  if (newToken) return this.exec({ ...opts, token: newToken }, true);
149
161
  }
150
162
  if (res.status === 204) {
151
- if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status);
163
+ if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status, void 0, rid);
152
164
  return void 0;
153
165
  }
154
166
  const data = await res.json();
155
167
  if (!res.ok || res.status === 202) {
156
168
  const err = data;
157
- throw new FonderieApiError(err.reason, err.explanation, res.status, err.details);
169
+ throw new FonderieApiError(err.reason, err.explanation, res.status, err.details, rid);
158
170
  }
159
171
  return data;
160
172
  }