@oxyhq/core 3.7.1 → 3.8.1

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.
@@ -5,6 +5,11 @@ import { SignatureService } from '../crypto/signatureService.js';
5
5
  import { normalizeUserIdentity, normalizeUserIdentityOrNull } from '../utils/userIdentity.js';
6
6
  import { logger } from '../utils/loggerUtils.js';
7
7
  import { extractErrorStatus } from '../utils/errorUtils.js';
8
+ /**
9
+ * Maximum number of ids sent per `POST /users/by-ids` request. Matches the
10
+ * server-side batch cap; larger inputs are split into multiple chunked calls.
11
+ */
12
+ const USERS_BY_IDS_CHUNK_SIZE = 100;
8
13
  export function OxyServicesUserMixin(Base) {
9
14
  return class extends Base {
10
15
  constructor(...args) {
@@ -213,6 +218,64 @@ export function OxyServicesUserMixin(Base) {
213
218
  throw this.handleError(error);
214
219
  }
215
220
  }
221
+ /**
222
+ * Fetch many users by id in one round-trip per chunk.
223
+ *
224
+ * Built for feed/hydration call sites that would otherwise issue one
225
+ * `getUserById` request per unique author (the classic M+1). Ids are
226
+ * deduplicated and validated (empty/blank ids dropped) before being split
227
+ * into chunks of {@link USERS_BY_IDS_CHUNK_SIZE} and POSTed to
228
+ * `/users/by-ids` as `{ ids }`. The server returns the matched users as a
229
+ * flat `User[]` (order is not guaranteed and the caller is expected to map
230
+ * by `id`); each is run through `normalizeUserIdentity`, matching
231
+ * `getUserById`.
232
+ *
233
+ * **Service-token auth (required).** `/users/by-ids` is a server-to-server
234
+ * bulk fetch of PUBLIC user data and is called via `makeServiceRequest`,
235
+ * which attaches `Authorization: Bearer <serviceToken>`. oxy-api's CSRF
236
+ * middleware skips bearer-authenticated requests, so the calling client
237
+ * MUST be service-configured (`configureServiceAuth(apiKey, apiSecret)`)
238
+ * before invoking this method; otherwise `getServiceToken()` throws because
239
+ * no credentials are available. (A plain user-session request fails here:
240
+ * server-to-server there is no cookie jar, so the auto-attached
241
+ * `X-CSRF-Token` has no matching cookie and oxy-api rejects the POST with
242
+ * 403 "CSRF token missing".)
243
+ *
244
+ * Resilience: chunks are independent. A failed chunk is logged and skipped
245
+ * — the method returns every user that resolved successfully rather than
246
+ * discarding the whole call on one chunk's failure. An empty/whitespace-only
247
+ * input resolves immediately with `[]` and performs no network call.
248
+ *
249
+ * Not cached at the SDK layer: the response is keyed on a multi-id POST body
250
+ * (low hit rate) and the backend maintains its own per-id Redis cache.
251
+ */
252
+ async getUsersByIds(ids) {
253
+ const uniqueIds = Array.from(new Set(ids.filter((id) => typeof id === 'string' && id.trim().length > 0)));
254
+ if (uniqueIds.length === 0) {
255
+ return [];
256
+ }
257
+ const chunks = [];
258
+ for (let i = 0; i < uniqueIds.length; i += USERS_BY_IDS_CHUNK_SIZE) {
259
+ chunks.push(uniqueIds.slice(i, i + USERS_BY_IDS_CHUNK_SIZE));
260
+ }
261
+ // Run chunks concurrently; a single chunk failure must not sink the rest.
262
+ const settled = await Promise.all(chunks.map(async (chunk) => {
263
+ try {
264
+ const users = await this.makeServiceRequest('POST', '/users/by-ids', { ids: chunk });
265
+ return Array.isArray(users) ? users.map((user) => normalizeUserIdentity(user)) : [];
266
+ }
267
+ catch (error) {
268
+ logger.warn('getUsersByIds: chunk failed, continuing with remaining chunks', {
269
+ method: 'getUsersByIds',
270
+ chunkSize: chunk.length,
271
+ status: extractErrorStatus(error),
272
+ error: error instanceof Error ? error.message : String(error),
273
+ });
274
+ return [];
275
+ }
276
+ }));
277
+ return settled.flat();
278
+ }
216
279
  /**
217
280
  * Get current user
218
281
  */