@emeryld/rrroutes-server 2.6.3 → 2.6.4

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.cjs CHANGED
@@ -235,6 +235,62 @@ var createRequestSanitizationMiddleware = (options = {}) => {
235
235
  };
236
236
  var requestSanitizationMiddleware = createRequestSanitizationMiddleware();
237
237
 
238
+ // src/routesV3.server.batch.ts
239
+ var isPlainObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
240
+ function batchLeaf(server, path, registry, options) {
241
+ const method = String(options?.method ?? "post").toLowerCase();
242
+ const allowedMethods = ["get", "post", "put", "patch", "delete"];
243
+ if (!allowedMethods.includes(method)) {
244
+ throw new Error(
245
+ `Invalid batch method "${String(options?.method)}". Expected one of: ${allowedMethods.join(", ")}.`
246
+ );
247
+ }
248
+ ;
249
+ server.router[method](
250
+ path,
251
+ async (req, res, next) => {
252
+ try {
253
+ const body = req.body;
254
+ if (!isPlainObject2(body)) {
255
+ throw new Error(
256
+ "Batch request body must be a plain object keyed by branch aliases."
257
+ );
258
+ }
259
+ const entries = Object.entries(body);
260
+ const outputEntries = await Promise.all(
261
+ entries.map(async ([alias, value]) => {
262
+ const payload = isPlainObject2(value) ? value : {};
263
+ if (typeof payload.encodedLeaf !== "string" || payload.encodedLeaf.length === 0) {
264
+ throw new Error(
265
+ `Batch entry "${alias}" must include a non-empty "encodedLeaf" string.`
266
+ );
267
+ }
268
+ const decodedKey = decodeURIComponent(payload.encodedLeaf);
269
+ const leaf = registry.byKey[decodedKey];
270
+ if (!leaf) {
271
+ throw new Error(`Unknown batch route key: ${decodedKey}`);
272
+ }
273
+ const result = await server.invoke(decodedKey, {
274
+ req,
275
+ res,
276
+ next,
277
+ params: payload.params,
278
+ query: payload.query,
279
+ body: payload.body,
280
+ bodyFiles: payload.bodyFiles
281
+ });
282
+ return [alias, result];
283
+ })
284
+ );
285
+ res.json(Object.fromEntries(outputEntries));
286
+ } catch (err) {
287
+ next(err);
288
+ }
289
+ }
290
+ );
291
+ return server.router;
292
+ }
293
+
238
294
  // src/routesV3.server.ts
239
295
  var serverDebugEventTypes = [
240
296
  "register",
@@ -269,12 +325,12 @@ function createServerDebugEmitter(option) {
269
325
  }
270
326
  return disabled;
271
327
  }
272
- var isPlainObject2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
328
+ var isPlainObject3 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
273
329
  var decodeJsonLikeQueryValue = (value) => {
274
330
  if (Array.isArray(value)) {
275
331
  return value.map((entry) => decodeJsonLikeQueryValue(entry));
276
332
  }
277
- if (isPlainObject2(value)) {
333
+ if (isPlainObject3(value)) {
278
334
  const next = {};
279
335
  for (const [key, child] of Object.entries(value)) {
280
336
  next[key] = decodeJsonLikeQueryValue(child);
@@ -298,7 +354,7 @@ var REQUEST_PAYLOAD_SYMBOL = /* @__PURE__ */ Symbol.for(
298
354
  "typedLeaves.requestPayload"
299
355
  );
300
356
  function isMulterFile(value) {
301
- if (!isPlainObject2(value)) return false;
357
+ if (!isPlainObject3(value)) return false;
302
358
  const candidate = value;
303
359
  return typeof candidate.fieldname === "string";
304
360
  }
@@ -314,7 +370,7 @@ function collectMulterFiles(req) {
314
370
  files.push(value);
315
371
  return;
316
372
  }
317
- if (isPlainObject2(value)) {
373
+ if (isPlainObject3(value)) {
318
374
  Object.values(value).forEach(pushValue);
319
375
  }
320
376
  };
@@ -899,59 +955,6 @@ function bindAll(router, registry, controllers, config) {
899
955
  server.registerControllers(registry, controllers);
900
956
  return router;
901
957
  }
902
- function batchLeaf(server, path, registry, options) {
903
- const method = String(options?.method ?? "post").toLowerCase();
904
- const allowedMethods = ["get", "post", "put", "patch", "delete"];
905
- if (!allowedMethods.includes(method)) {
906
- throw new Error(
907
- `Invalid batch method "${String(options?.method)}". Expected one of: ${allowedMethods.join(", ")}.`
908
- );
909
- }
910
- ;
911
- server.router[method](
912
- path,
913
- async (req, res, next) => {
914
- try {
915
- const body = req.body;
916
- if (!isPlainObject2(body)) {
917
- throw new Error(
918
- "Batch request body must be a plain object keyed by branch aliases."
919
- );
920
- }
921
- const entries = Object.entries(body);
922
- const outputEntries = await Promise.all(
923
- entries.map(async ([alias, value]) => {
924
- const payload = isPlainObject2(value) ? value : {};
925
- if (typeof payload.encodedLeaf !== "string" || payload.encodedLeaf.length === 0) {
926
- throw new Error(
927
- `Batch entry "${alias}" must include a non-empty "encodedLeaf" string.`
928
- );
929
- }
930
- const decodedKey = decodeURIComponent(payload.encodedLeaf);
931
- const leaf = registry.byKey[decodedKey];
932
- if (!leaf) {
933
- throw new Error(`Unknown batch route key: ${decodedKey}`);
934
- }
935
- const result = await server.invoke(decodedKey, {
936
- req,
937
- res,
938
- next,
939
- params: payload.params,
940
- query: payload.query,
941
- body: payload.body,
942
- bodyFiles: payload.bodyFiles
943
- });
944
- return [alias, result];
945
- })
946
- );
947
- res.json(Object.fromEntries(outputEntries));
948
- } catch (err) {
949
- next(err);
950
- }
951
- }
952
- );
953
- return server.router;
954
- }
955
958
  var defineControllers = () => (m) => m;
956
959
  function warnMissingControllers(router, registry, logger) {
957
960
  const registeredStore = router[REGISTERED_ROUTES_SYMBOL];