xmemory 3.8.1 → 3.8.3

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.
@@ -62,6 +62,16 @@ export declare class XmemoryClient {
62
62
  checkHealth(): Promise<void>;
63
63
  private _fetch;
64
64
  private _headers;
65
+ /**
66
+ * The request URL for `path`, honouring whatever the configured base carries.
67
+ *
68
+ * Composed through `URL` rather than concatenated. Concatenation put the API path
69
+ * inside the query of a base that had one (`https://host?tenant=x` requested `/`),
70
+ * and appended a second `?` for request parameters. A plain `new URL(path, base)`
71
+ * is not the fix either: it drops a base path prefix, so a gateway base like
72
+ * `https://gw.example/xmemory` would lose `/xmemory`.
73
+ */
74
+ private _requestUrl;
65
75
  /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
66
76
  private _request;
67
77
  private _requestOne;
@@ -8,6 +8,24 @@ exports.xmemoryInstance = xmemoryInstance;
8
8
  const instance_js_1 = require("./instance.js");
9
9
  const types_js_1 = require("./types.js");
10
10
  const DEFAULT_BASE_URL = "https://api.xmemory.ai";
11
+ /**
12
+ * An *own* property of a decoded response, or `undefined`.
13
+ *
14
+ * A decoded body is a plain object, so a field the server omitted is answered by
15
+ * `Object.prototype`. Reading the envelope that way let a polluted prototype supply
16
+ * an `items` array — and a 200 with an empty body then came back as a genuine
17
+ * result. Every read of a server-controlled envelope goes through here.
18
+ */
19
+ function own(source, key) {
20
+ return typeof source === "object" && source !== null && Object.hasOwn(source, key)
21
+ ? source[key]
22
+ : undefined;
23
+ }
24
+ /** An own array field, or an empty one. Never the prototype's. */
25
+ function ownArray(source, key) {
26
+ const value = own(source, key);
27
+ return Array.isArray(value) ? value : [];
28
+ }
11
29
  const DEFAULT_TIMEOUT_MS = 60_000;
12
30
  const ORANGE = "\x1b[38;5;208m";
13
31
  const RESET = "\x1b[0m";
@@ -31,7 +49,7 @@ function extractStructuredError(payload) {
31
49
  details: (p.details ?? null),
32
50
  };
33
51
  }
34
- const errors = p.errors;
52
+ const errors = own(p, "errors");
35
53
  if (Array.isArray(errors) && errors.length > 0 && typeof errors[0] === "object" && errors[0] !== null) {
36
54
  const first = errors[0];
37
55
  return {
@@ -140,24 +158,32 @@ class XmemoryClient {
140
158
  h["Authorization"] = `Bearer ${this._apiKey}`;
141
159
  return h;
142
160
  }
143
- /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
144
- async _request(method, path, options) {
145
- let url = `${this._baseUrl}${path}`;
146
- if (options?.params) {
147
- const sp = new URLSearchParams();
148
- for (const [key, val] of Object.entries(options.params)) {
149
- if (Array.isArray(val)) {
150
- for (const v of val)
151
- sp.append(key, v);
152
- }
153
- else {
154
- sp.append(key, val);
155
- }
161
+ /**
162
+ * The request URL for `path`, honouring whatever the configured base carries.
163
+ *
164
+ * Composed through `URL` rather than concatenated. Concatenation put the API path
165
+ * inside the query of a base that had one (`https://host?tenant=x` requested `/`),
166
+ * and appended a second `?` for request parameters. A plain `new URL(path, base)`
167
+ * is not the fix either: it drops a base path prefix, so a gateway base like
168
+ * `https://gw.example/xmemory` would lose `/xmemory`.
169
+ */
170
+ _requestUrl(path, params) {
171
+ const url = new URL(this._baseUrl);
172
+ url.pathname = `${url.pathname.replace(/\/+$/, "")}${path}`;
173
+ for (const [key, val] of Object.entries(params ?? {})) {
174
+ if (Array.isArray(val)) {
175
+ for (const v of val)
176
+ url.searchParams.append(key, v);
177
+ }
178
+ else {
179
+ url.searchParams.append(key, val);
156
180
  }
157
- const qs = sp.toString();
158
- if (qs)
159
- url += `?${qs}`;
160
181
  }
182
+ return url.toString();
183
+ }
184
+ /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
185
+ async _request(method, path, options) {
186
+ const url = this._requestUrl(path, options?.params);
161
187
  const timeoutMs = options?.timeoutMs ?? this._timeoutMs;
162
188
  const hasBody = !!(options?.body && method !== "GET");
163
189
  const init = { method, headers: this._headers(hasBody) };
@@ -182,15 +208,16 @@ class XmemoryClient {
182
208
  throw new types_js_1.XmemoryAPIError(`HTTP ${res.status}: ${msg}`, res.status, structured.code, structured.details, parseRetryAfter(res));
183
209
  }
184
210
  const response = payload;
185
- if (response.errors?.length) {
186
- const first = response.errors[0];
211
+ const responseErrors = ownArray(response, "errors");
212
+ if (responseErrors.length > 0) {
213
+ const first = responseErrors[0];
187
214
  throw new types_js_1.XmemoryAPIError(`API error: ${first.message} (${first.code})`, res.status, first.code);
188
215
  }
189
216
  return response;
190
217
  }
191
218
  async _requestOne(method, path, options) {
192
219
  const response = await this._request(method, path, options);
193
- const items = response.items ?? [];
220
+ const items = ownArray(response, "items");
194
221
  if (items.length === 0) {
195
222
  throw new types_js_1.XmemoryAPIError(`Expected one item from ${method} ${path}, got none`);
196
223
  }
@@ -201,11 +228,11 @@ class XmemoryClient {
201
228
  }
202
229
  async _requestList(method, path, options) {
203
230
  const response = await this._request(method, path, options);
204
- return (response.items ?? []);
231
+ return ownArray(response, "items");
205
232
  }
206
233
  async _requestIds(method, path, options) {
207
234
  const response = await this._request(method, path, options);
208
- return response.ids ?? [];
235
+ return ownArray(response, "ids");
209
236
  }
210
237
  // -----------------------------------------------------------------------
211
238
  // Private: build admin namespace
@@ -62,6 +62,16 @@ export declare class XmemoryClient {
62
62
  checkHealth(): Promise<void>;
63
63
  private _fetch;
64
64
  private _headers;
65
+ /**
66
+ * The request URL for `path`, honouring whatever the configured base carries.
67
+ *
68
+ * Composed through `URL` rather than concatenated. Concatenation put the API path
69
+ * inside the query of a base that had one (`https://host?tenant=x` requested `/`),
70
+ * and appended a second `?` for request parameters. A plain `new URL(path, base)`
71
+ * is not the fix either: it drops a base path prefix, so a gateway base like
72
+ * `https://gw.example/xmemory` would lose `/xmemory`.
73
+ */
74
+ private _requestUrl;
65
75
  /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
66
76
  private _request;
67
77
  private _requestOne;
@@ -4,6 +4,24 @@
4
4
  import { InstanceHandle } from "./instance.js";
5
5
  import { SetupFormat, XmemoryAPIError, XmemoryHealthCheckError, buildInstanceSchema, } from "./types.js";
6
6
  const DEFAULT_BASE_URL = "https://api.xmemory.ai";
7
+ /**
8
+ * An *own* property of a decoded response, or `undefined`.
9
+ *
10
+ * A decoded body is a plain object, so a field the server omitted is answered by
11
+ * `Object.prototype`. Reading the envelope that way let a polluted prototype supply
12
+ * an `items` array — and a 200 with an empty body then came back as a genuine
13
+ * result. Every read of a server-controlled envelope goes through here.
14
+ */
15
+ function own(source, key) {
16
+ return typeof source === "object" && source !== null && Object.hasOwn(source, key)
17
+ ? source[key]
18
+ : undefined;
19
+ }
20
+ /** An own array field, or an empty one. Never the prototype's. */
21
+ function ownArray(source, key) {
22
+ const value = own(source, key);
23
+ return Array.isArray(value) ? value : [];
24
+ }
7
25
  const DEFAULT_TIMEOUT_MS = 60_000;
8
26
  const ORANGE = "\x1b[38;5;208m";
9
27
  const RESET = "\x1b[0m";
@@ -27,7 +45,7 @@ function extractStructuredError(payload) {
27
45
  details: (p.details ?? null),
28
46
  };
29
47
  }
30
- const errors = p.errors;
48
+ const errors = own(p, "errors");
31
49
  if (Array.isArray(errors) && errors.length > 0 && typeof errors[0] === "object" && errors[0] !== null) {
32
50
  const first = errors[0];
33
51
  return {
@@ -136,24 +154,32 @@ export class XmemoryClient {
136
154
  h["Authorization"] = `Bearer ${this._apiKey}`;
137
155
  return h;
138
156
  }
139
- /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
140
- async _request(method, path, options) {
141
- let url = `${this._baseUrl}${path}`;
142
- if (options?.params) {
143
- const sp = new URLSearchParams();
144
- for (const [key, val] of Object.entries(options.params)) {
145
- if (Array.isArray(val)) {
146
- for (const v of val)
147
- sp.append(key, v);
148
- }
149
- else {
150
- sp.append(key, val);
151
- }
157
+ /**
158
+ * The request URL for `path`, honouring whatever the configured base carries.
159
+ *
160
+ * Composed through `URL` rather than concatenated. Concatenation put the API path
161
+ * inside the query of a base that had one (`https://host?tenant=x` requested `/`),
162
+ * and appended a second `?` for request parameters. A plain `new URL(path, base)`
163
+ * is not the fix either: it drops a base path prefix, so a gateway base like
164
+ * `https://gw.example/xmemory` would lose `/xmemory`.
165
+ */
166
+ _requestUrl(path, params) {
167
+ const url = new URL(this._baseUrl);
168
+ url.pathname = `${url.pathname.replace(/\/+$/, "")}${path}`;
169
+ for (const [key, val] of Object.entries(params ?? {})) {
170
+ if (Array.isArray(val)) {
171
+ for (const v of val)
172
+ url.searchParams.append(key, v);
173
+ }
174
+ else {
175
+ url.searchParams.append(key, val);
152
176
  }
153
- const qs = sp.toString();
154
- if (qs)
155
- url += `?${qs}`;
156
177
  }
178
+ return url.toString();
179
+ }
180
+ /** Core request — returns the raw API wrapper `{ids, items, errors}`. */
181
+ async _request(method, path, options) {
182
+ const url = this._requestUrl(path, options?.params);
157
183
  const timeoutMs = options?.timeoutMs ?? this._timeoutMs;
158
184
  const hasBody = !!(options?.body && method !== "GET");
159
185
  const init = { method, headers: this._headers(hasBody) };
@@ -178,15 +204,16 @@ export class XmemoryClient {
178
204
  throw new XmemoryAPIError(`HTTP ${res.status}: ${msg}`, res.status, structured.code, structured.details, parseRetryAfter(res));
179
205
  }
180
206
  const response = payload;
181
- if (response.errors?.length) {
182
- const first = response.errors[0];
207
+ const responseErrors = ownArray(response, "errors");
208
+ if (responseErrors.length > 0) {
209
+ const first = responseErrors[0];
183
210
  throw new XmemoryAPIError(`API error: ${first.message} (${first.code})`, res.status, first.code);
184
211
  }
185
212
  return response;
186
213
  }
187
214
  async _requestOne(method, path, options) {
188
215
  const response = await this._request(method, path, options);
189
- const items = response.items ?? [];
216
+ const items = ownArray(response, "items");
190
217
  if (items.length === 0) {
191
218
  throw new XmemoryAPIError(`Expected one item from ${method} ${path}, got none`);
192
219
  }
@@ -197,11 +224,11 @@ export class XmemoryClient {
197
224
  }
198
225
  async _requestList(method, path, options) {
199
226
  const response = await this._request(method, path, options);
200
- return (response.items ?? []);
227
+ return ownArray(response, "items");
201
228
  }
202
229
  async _requestIds(method, path, options) {
203
230
  const response = await this._request(method, path, options);
204
- return response.ids ?? [];
231
+ return ownArray(response, "ids");
205
232
  }
206
233
  // -----------------------------------------------------------------------
207
234
  // Private: build admin namespace
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmemory",
3
- "version": "3.8.1",
3
+ "version": "3.8.3",
4
4
  "description": "xmemory",
5
5
  "keywords": [
6
6
  "xmemory"