@jaypie/express 1.2.20 → 1.2.22

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,13 @@ export type JaypieHandlerValidate = (req: Request, res: Response) => Promise<boo
5
5
  export type ExpressHandlerLocals = (req: Request, res: Response) => Promise<unknown> | unknown;
6
6
  export interface ExpressHandlerOptions {
7
7
  chaos?: string;
8
+ /**
9
+ * When true, the handler's return value is wrapped with
10
+ * `fabricApiResponse` before `res.json()`. Plain objects become
11
+ * `{ data: value }`; pre-wrapped `{ data }` or `{ errors }` payloads
12
+ * pass through unchanged. `null` / `undefined` become `{ data: null }`.
13
+ */
14
+ fabric?: boolean;
8
15
  locals?: Record<string, unknown | ExpressHandlerLocals>;
9
16
  name?: string;
10
17
  secrets?: string[];
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Wrap a service return value in the canonical Jaypie API envelope.
3
+ *
4
+ * Rules:
5
+ * - `null` / `undefined` → `{ data: null }`
6
+ * - `{ data }` with exactly one key → passthrough
7
+ * - `{ errors }` with exactly one key → passthrough
8
+ * - everything else (including `{ data, other }`) → `{ data: value }`
9
+ *
10
+ * The "only one key, and that key is `data` or `errors`" check prevents
11
+ * false positives where a domain object happens to contain a `data` field.
12
+ */
13
+ export declare function fabricApiResponse(result: unknown): unknown;
14
+ export default fabricApiResponse;
@@ -1357,6 +1357,48 @@ var cors_helper = (config) => {
1357
1357
  };
1358
1358
  };
1359
1359
 
1360
+ //
1361
+ //
1362
+ // Helpers
1363
+ //
1364
+ function isWrappedData(value) {
1365
+ return (typeof value === "object" &&
1366
+ value !== null &&
1367
+ !Array.isArray(value) &&
1368
+ Object.keys(value).length === 1 &&
1369
+ "data" in value);
1370
+ }
1371
+ function isWrappedErrors(value) {
1372
+ return (typeof value === "object" &&
1373
+ value !== null &&
1374
+ !Array.isArray(value) &&
1375
+ Object.keys(value).length === 1 &&
1376
+ "errors" in value);
1377
+ }
1378
+ //
1379
+ //
1380
+ // Main
1381
+ //
1382
+ /**
1383
+ * Wrap a service return value in the canonical Jaypie API envelope.
1384
+ *
1385
+ * Rules:
1386
+ * - `null` / `undefined` → `{ data: null }`
1387
+ * - `{ data }` with exactly one key → passthrough
1388
+ * - `{ errors }` with exactly one key → passthrough
1389
+ * - everything else (including `{ data, other }`) → `{ data: value }`
1390
+ *
1391
+ * The "only one key, and that key is `data` or `errors`" check prevents
1392
+ * false positives where a domain object happens to contain a `data` field.
1393
+ */
1394
+ function fabricApiResponse(result) {
1395
+ if (result === null || result === undefined)
1396
+ return { data: null };
1397
+ if (isWrappedData(result) || isWrappedErrors(result))
1398
+ return result;
1399
+ return { data: result };
1400
+ }
1401
+
1360
1402
  //
1361
1403
  //
1362
1404
  // Helper Functions
@@ -1719,7 +1761,7 @@ function expressHandler(handlerOrOptions, optionsOrHandler) {
1719
1761
  //
1720
1762
  // Validate
1721
1763
  //
1722
- let { chaos, locals, name, secrets, setup = [], teardown = [], unavailable, validate, } = options;
1764
+ let { chaos, fabric, locals, name, secrets, setup = [], teardown = [], unavailable, validate, } = options;
1723
1765
  if (typeof handler !== "function") {
1724
1766
  throw new errors.BadRequestError(`Argument "${handler}" doesn't match type "function"`);
1725
1767
  }
@@ -1873,6 +1915,9 @@ function expressHandler(handlerOrOptions, optionsOrHandler) {
1873
1915
  // Process
1874
1916
  //
1875
1917
  response = (await jaypieFunction(req, res, ...params));
1918
+ if (fabric) {
1919
+ response = fabricApiResponse(response);
1920
+ }
1876
1921
  //
1877
1922
  //
1878
1923
  // Error Handling
@@ -1989,7 +2034,11 @@ function expressHandler(handlerOrOptions, optionsOrHandler) {
1989
2034
  // Replace UUIDs with :id for better aggregation
1990
2035
  path = path.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, ":id");
1991
2036
  // Add request data to session report
1992
- logger$1.report({ path, status: String(res.statusCode) });
2037
+ logger$1.report({
2038
+ method: req.method,
2039
+ path,
2040
+ status: String(res.statusCode),
2041
+ });
1993
2042
  // Submit metric if Datadog is configured
1994
2043
  if (datadog.hasDatadogEnv()) {
1995
2044
  let metricPrefix = "project";
@@ -2270,7 +2319,11 @@ function expressStreamHandler(handlerOrOptions, optionsOrHandler) {
2270
2319
  }
2271
2320
  path = path.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, ":id");
2272
2321
  // Add request data to session report
2273
- logger.report({ path, status: String(res.statusCode) });
2322
+ logger.report({
2323
+ method: req.method,
2324
+ path,
2325
+ status: String(res.statusCode),
2326
+ });
2274
2327
  // Submit metric if Datadog is configured
2275
2328
  if (datadog.hasDatadogEnv()) {
2276
2329
  let metricPrefix = "project";
@@ -2362,6 +2415,7 @@ exports.echoRoute = echoRoute;
2362
2415
  exports.expressHandler = expressHandler;
2363
2416
  exports.expressHttpCodeHandler = httpHandler;
2364
2417
  exports.expressStreamHandler = expressStreamHandler;
2418
+ exports.fabricApiResponse = fabricApiResponse;
2365
2419
  exports.forbiddenRoute = forbiddenRoute;
2366
2420
  exports.getCurrentInvoke = getCurrentInvoke;
2367
2421
  exports.getCurrentInvokeUuid = getCurrentInvokeUuid;