@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.
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,26 @@ 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 newRequestId() {
56
+ const c = globalThis.crypto;
57
+ if (c?.randomUUID) return c.randomUUID();
58
+ return `r-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
59
+ }
53
60
  var HttpClient = class {
54
61
  constructor(baseUrl, deps = {}) {
55
62
  this.baseUrl = baseUrl;
@@ -97,7 +104,11 @@ var HttpClient = class {
97
104
  return data;
98
105
  }
99
106
  async exec(opts, retried = false) {
100
- const headers = { "Content-Type": "application/json" };
107
+ const requestId = newRequestId();
108
+ const headers = {
109
+ "Content-Type": "application/json",
110
+ "X-Request-ID": requestId
111
+ };
101
112
  if (opts.token) headers["Authorization"] = `Bearer ${opts.token}`;
102
113
  if (opts.cookie) headers["Cookie"] = opts.cookie;
103
114
  if (opts.workspaceId) headers["X-Workspace-ID"] = opts.workspaceId;
@@ -105,18 +116,19 @@ var HttpClient = class {
105
116
  const fetchInit = { method: opts.method, headers, credentials: "include" };
106
117
  if (opts.body !== void 0) fetchInit.body = JSON.stringify(opts.body);
107
118
  const res = await fetch(`${this.baseUrl}${opts.path}`, fetchInit);
119
+ const rid = res.headers.get("x-request-id") ?? requestId;
108
120
  if (res.status === 401 && this.refresh && !retried && !opts.path.startsWith("/auth/")) {
109
121
  const newToken = await this.refresh();
110
122
  if (newToken) return this.exec({ ...opts, token: newToken }, true);
111
123
  }
112
124
  if (res.status === 204) {
113
- if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status);
125
+ if (!res.ok) throw new FonderieApiError("unknown", res.statusText, res.status, void 0, rid);
114
126
  return void 0;
115
127
  }
116
128
  const data = await res.json();
117
129
  if (!res.ok || res.status === 202) {
118
130
  const err = data;
119
- throw new FonderieApiError(err.reason, err.explanation, res.status, err.details);
131
+ throw new FonderieApiError(err.reason, err.explanation, res.status, err.details, rid);
120
132
  }
121
133
  return data;
122
134
  }