@xemahq/space-registry-api-client 0.2.6 → 0.2.8

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.
@@ -47,6 +47,28 @@ export interface ClientConfig {
47
47
  getAuthToken?: () => Promise<string>;
48
48
  /** Optional callback returning headers to inject on every request. Per-call headers take precedence. */
49
49
  getHeaders?: () => Record<string, string> | Promise<Record<string, string>>;
50
+ /**
51
+ * Optional resolver for the CORRELATION ID of the request being made — the
52
+ * handle that ties one causal chain together across every service hop.
53
+ *
54
+ * WHY IT IS A CALLBACK AND NOT A VALUE. `ClientConfig` is process-global
55
+ * (`configureClient` is called once at wiring time), and a correlation id is
56
+ * per-request. This is invoked INSIDE the request, so a server can point it
57
+ * at whatever carries its ambient request context and get the CURRENT id
58
+ * rather than the one that happened to be live at boot.
59
+ *
60
+ * WHY THE TRANSPORT DOES NOT MINT ONE. Returning `undefined` sends no header,
61
+ * and the receiving service's `RequestContextMiddleware` mints its own — a
62
+ * new trace, which is honest. A transport that minted per call would produce
63
+ * a FRESH id on every hop while looking like propagation, which is strictly
64
+ * worse than none: every row would carry a correlation id and no two rows
65
+ * that belong together would share one. That is the exact defect this exists
66
+ * to fix, so the transport must not reproduce it one layer down.
67
+ *
68
+ * A caller-supplied `X-Correlation-Id` header always wins, and so does one
69
+ * from `getHeaders`.
70
+ */
71
+ getCorrelationId?: () => string | undefined | Promise<string | undefined>;
50
72
  /**
51
73
  * Optional callback invoked on a 401 before ONE re-attempt. Supplying it is
52
74
  * what opts this client into that re-attempt; without it a 401 comes back to
@@ -89,6 +89,16 @@ function getClientConfig() {
89
89
  */
90
90
  /** The only statuses that state the request was NOT processed. See above. */
91
91
  const RETRYABLE_STATUSES = [429, 503];
92
+ /**
93
+ * The platform's correlation header, spelled once.
94
+ *
95
+ * Value-identical to what `RequestContextMiddleware` reads in
96
+ * `@xemahq/platform-common`. It is a literal here rather than an import
97
+ * because this file has ZERO imports on purpose: it ships byte-identical into
98
+ * browser-target clients as well as server-target ones, and a dependency on a
99
+ * NestJS-peer package would follow it into every one of them.
100
+ */
101
+ const CORRELATION_ID_HEADER = 'X-Correlation-Id';
92
102
  /** Backoff floor, doubling per attempt up to {@link MAX_BACKOFF_MS}. */
93
103
  const BASE_BACKOFF_MS = 1000;
94
104
  /** Ceiling on a single backoff, however many attempts have elapsed. */
@@ -104,6 +114,23 @@ async function buildHeaders(config, callerHeaders) {
104
114
  }
105
115
  }
106
116
  }
117
+ // Correlation id (caller and global headers still take precedence).
118
+ //
119
+ // Without this, every server-to-server hop through a generated client started
120
+ // a NEW trace: the id is read-or-minted per hop by the receiving service's
121
+ // RequestContextMiddleware, and nothing carried it outbound — so an audit
122
+ // journal could record a whole causal chain and offer no way to join it back
123
+ // together.
124
+ //
125
+ // Absent resolver, or a resolver that answers `undefined`: NO header. The
126
+ // receiver mints and a new trace begins, which is the truthful outcome when
127
+ // there is nothing to continue.
128
+ if (config.getCorrelationId && !headers.has(CORRELATION_ID_HEADER)) {
129
+ const correlationId = await Promise.resolve(config.getCorrelationId());
130
+ if (correlationId) {
131
+ headers.set(CORRELATION_ID_HEADER, correlationId);
132
+ }
133
+ }
107
134
  // Auth token (caller or global headers take precedence)
108
135
  if (config.getAuthToken && !headers.has('Authorization')) {
109
136
  const token = await config.getAuthToken();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xemahq/space-registry-api-client",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [
@@ -19,7 +19,7 @@
19
19
  "service": "space-registry-api",
20
20
  "biome": "space-registry",
21
21
  "target": "server",
22
- "generator": "@xemahq/api-client-generator@0.13.0",
22
+ "generator": "@xemahq/api-client-generator@0.15.0",
23
23
  "source": "openapi.public.json"
24
24
  },
25
25
  "license": "LicenseRef-Xema-BSL-1.1",