@jaypie/express 1.1.11 → 1.1.13

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/module.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var errors = require('@jaypie/errors');
4
4
  var core = require('@jaypie/core');
5
5
  var expressCors = require('cors');
6
+ var datadog = require('@jaypie/datadog');
6
7
  var serverlessExpress = require('@codegenie/serverless-express');
7
8
 
8
9
  //
@@ -290,6 +291,7 @@ const expressHandler = (handler, options = {}) => {
290
291
  // Validate
291
292
  //
292
293
  let {
294
+ chaos,
293
295
  locals,
294
296
  name,
295
297
  setup = [],
@@ -312,6 +314,13 @@ const expressHandler = (handler, options = {}) => {
312
314
  return async (req, res, ...params) => {
313
315
  // * This is the first line of code that runs when a request is received
314
316
 
317
+ // Set default chaos value
318
+ if (chaos === undefined) {
319
+ chaos =
320
+ process.env.PROJECT_CHAOS ||
321
+ core.getHeaderFrom(core.HTTP.HEADER.PROJECT.CHAOS, req);
322
+ }
323
+
315
324
  // Re-init the logger
316
325
  core.log.init();
317
326
  // Very low-level, internal sub-trace details
@@ -368,7 +377,7 @@ const expressHandler = (handler, options = {}) => {
368
377
  status: res.status,
369
378
  statusSent: false,
370
379
  };
371
- // eslint-disable-next-line no-shadow
380
+
372
381
  res.end = (...params) => {
373
382
  originalRes.attemptedCall = originalRes.end;
374
383
  originalRes.attemptedParams = params;
@@ -376,7 +385,7 @@ const expressHandler = (handler, options = {}) => {
376
385
  "[jaypie] Illegal call to res.end(); prefer Jaypie response conventions",
377
386
  );
378
387
  };
379
- // eslint-disable-next-line no-shadow
388
+
380
389
  res.json = (...params) => {
381
390
  originalRes.attemptedCall = originalRes.json;
382
391
  originalRes.attemptedParams = params;
@@ -384,7 +393,7 @@ const expressHandler = (handler, options = {}) => {
384
393
  "[jaypie] Illegal call to res.json(); prefer Jaypie response conventions",
385
394
  );
386
395
  };
387
- // eslint-disable-next-line no-shadow
396
+
388
397
  res.send = (...params) => {
389
398
  originalRes.attemptedCall = originalRes.send;
390
399
  originalRes.attemptedParams = params;
@@ -392,9 +401,9 @@ const expressHandler = (handler, options = {}) => {
392
401
  "[jaypie] Illegal call to res.send(); prefer Jaypie response conventions",
393
402
  );
394
403
  };
395
- // eslint-disable-next-line no-shadow
404
+
396
405
  res.status = (...params) => {
397
- originalRes.statusSent = params;
406
+ originalRes.statusSent = params[0];
398
407
  return originalRes.status(...params);
399
408
  };
400
409
 
@@ -430,6 +439,7 @@ const expressHandler = (handler, options = {}) => {
430
439
 
431
440
  // Initialize after logging is set up
432
441
  jaypieFunction = core.jaypieHandler(handler, {
442
+ chaos,
433
443
  name,
434
444
  setup,
435
445
  teardown,
@@ -535,6 +545,45 @@ const expressHandler = (handler, options = {}) => {
535
545
  res: summarizeResponse(res, extras),
536
546
  });
537
547
 
548
+ // Submit metric if Datadog is configured
549
+ if (datadog.hasDatadogEnv()) {
550
+ // Construct path from baseUrl and url
551
+ let path = (req.baseUrl || "") + (req.url || "");
552
+ // Ensure path starts with /
553
+ if (!path.startsWith("/")) {
554
+ path = "/" + path;
555
+ }
556
+ // Remove trailing slash unless it's the root path
557
+ if (path.length > 1 && path.endsWith("/")) {
558
+ path = path.slice(0, -1);
559
+ }
560
+
561
+ // Replace UUIDs with :id for better aggregation
562
+ // Matches standard UUID v4 format (8-4-4-4-12 hex characters)
563
+ path = path.replace(
564
+ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
565
+ ":id",
566
+ );
567
+
568
+ // Determine metric name based on environment variables
569
+ let metricPrefix = "project";
570
+ if (process.env.PROJECT_SPONSOR) {
571
+ metricPrefix = process.env.PROJECT_SPONSOR;
572
+ } else if (process.env.PROJECT_KEY) {
573
+ metricPrefix = `project.${process.env.PROJECT_KEY}`;
574
+ }
575
+
576
+ await datadog.submitMetric({
577
+ name: `${metricPrefix}.api.response`,
578
+ type: datadog.DATADOG.METRIC.TYPE.COUNT,
579
+ value: 1,
580
+ tags: {
581
+ code: res.statusCode,
582
+ path,
583
+ },
584
+ });
585
+ }
586
+
538
587
  // Clean up the public logger
539
588
  core.log.untag("handler");
540
589
 
@@ -1,6 +1,7 @@
1
1
  import { CorsError } from '@jaypie/errors';
2
- import { force, envBoolean, log, JAYPIE, HTTP, validate, jaypieHandler, UnhandledError, GatewayTimeoutError, UnavailableError, BadGatewayError, InternalError, TeapotError, GoneError, MethodNotAllowedError, NotFoundError, ForbiddenError, UnauthorizedError, BadRequestError, NotImplementedError } from '@jaypie/core';
2
+ import { force, envBoolean, log, JAYPIE, HTTP, validate, getHeaderFrom, jaypieHandler, UnhandledError, GatewayTimeoutError, UnavailableError, BadGatewayError, InternalError, TeapotError, GoneError, MethodNotAllowedError, NotFoundError, ForbiddenError, UnauthorizedError, BadRequestError, NotImplementedError } from '@jaypie/core';
3
3
  import expressCors from 'cors';
4
+ import { hasDatadogEnv, submitMetric, DATADOG } from '@jaypie/datadog';
4
5
  import { getCurrentInvoke } from '@codegenie/serverless-express';
5
6
 
6
7
  //
@@ -288,6 +289,7 @@ const expressHandler = (handler, options = {}) => {
288
289
  // Validate
289
290
  //
290
291
  let {
292
+ chaos,
291
293
  locals,
292
294
  name,
293
295
  setup = [],
@@ -310,6 +312,13 @@ const expressHandler = (handler, options = {}) => {
310
312
  return async (req, res, ...params) => {
311
313
  // * This is the first line of code that runs when a request is received
312
314
 
315
+ // Set default chaos value
316
+ if (chaos === undefined) {
317
+ chaos =
318
+ process.env.PROJECT_CHAOS ||
319
+ getHeaderFrom(HTTP.HEADER.PROJECT.CHAOS, req);
320
+ }
321
+
313
322
  // Re-init the logger
314
323
  log.init();
315
324
  // Very low-level, internal sub-trace details
@@ -366,7 +375,7 @@ const expressHandler = (handler, options = {}) => {
366
375
  status: res.status,
367
376
  statusSent: false,
368
377
  };
369
- // eslint-disable-next-line no-shadow
378
+
370
379
  res.end = (...params) => {
371
380
  originalRes.attemptedCall = originalRes.end;
372
381
  originalRes.attemptedParams = params;
@@ -374,7 +383,7 @@ const expressHandler = (handler, options = {}) => {
374
383
  "[jaypie] Illegal call to res.end(); prefer Jaypie response conventions",
375
384
  );
376
385
  };
377
- // eslint-disable-next-line no-shadow
386
+
378
387
  res.json = (...params) => {
379
388
  originalRes.attemptedCall = originalRes.json;
380
389
  originalRes.attemptedParams = params;
@@ -382,7 +391,7 @@ const expressHandler = (handler, options = {}) => {
382
391
  "[jaypie] Illegal call to res.json(); prefer Jaypie response conventions",
383
392
  );
384
393
  };
385
- // eslint-disable-next-line no-shadow
394
+
386
395
  res.send = (...params) => {
387
396
  originalRes.attemptedCall = originalRes.send;
388
397
  originalRes.attemptedParams = params;
@@ -390,9 +399,9 @@ const expressHandler = (handler, options = {}) => {
390
399
  "[jaypie] Illegal call to res.send(); prefer Jaypie response conventions",
391
400
  );
392
401
  };
393
- // eslint-disable-next-line no-shadow
402
+
394
403
  res.status = (...params) => {
395
- originalRes.statusSent = params;
404
+ originalRes.statusSent = params[0];
396
405
  return originalRes.status(...params);
397
406
  };
398
407
 
@@ -428,6 +437,7 @@ const expressHandler = (handler, options = {}) => {
428
437
 
429
438
  // Initialize after logging is set up
430
439
  jaypieFunction = jaypieHandler(handler, {
440
+ chaos,
431
441
  name,
432
442
  setup,
433
443
  teardown,
@@ -533,6 +543,45 @@ const expressHandler = (handler, options = {}) => {
533
543
  res: summarizeResponse(res, extras),
534
544
  });
535
545
 
546
+ // Submit metric if Datadog is configured
547
+ if (hasDatadogEnv()) {
548
+ // Construct path from baseUrl and url
549
+ let path = (req.baseUrl || "") + (req.url || "");
550
+ // Ensure path starts with /
551
+ if (!path.startsWith("/")) {
552
+ path = "/" + path;
553
+ }
554
+ // Remove trailing slash unless it's the root path
555
+ if (path.length > 1 && path.endsWith("/")) {
556
+ path = path.slice(0, -1);
557
+ }
558
+
559
+ // Replace UUIDs with :id for better aggregation
560
+ // Matches standard UUID v4 format (8-4-4-4-12 hex characters)
561
+ path = path.replace(
562
+ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
563
+ ":id",
564
+ );
565
+
566
+ // Determine metric name based on environment variables
567
+ let metricPrefix = "project";
568
+ if (process.env.PROJECT_SPONSOR) {
569
+ metricPrefix = process.env.PROJECT_SPONSOR;
570
+ } else if (process.env.PROJECT_KEY) {
571
+ metricPrefix = `project.${process.env.PROJECT_KEY}`;
572
+ }
573
+
574
+ await submitMetric({
575
+ name: `${metricPrefix}.api.response`,
576
+ type: DATADOG.METRIC.TYPE.COUNT,
577
+ value: 1,
578
+ tags: {
579
+ code: res.statusCode,
580
+ path,
581
+ },
582
+ });
583
+ }
584
+
536
585
  // Clean up the public logger
537
586
  log.untag("handler");
538
587
 
package/index.d.ts CHANGED
@@ -10,7 +10,7 @@ export const EXPRESS: {
10
10
  };
11
11
 
12
12
  export interface CorsConfig {
13
- origins?: string | string[];
13
+ origin?: string | string[];
14
14
  overrides?: Record<string, unknown>;
15
15
  }
16
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/express",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "license": "MIT",
5
5
  "author": "Finlayson Studio",
6
6
  "type": "module",
@@ -39,11 +39,15 @@
39
39
  "dependencies": {
40
40
  "@codegenie/serverless-express": "^4.15.0",
41
41
  "@jaypie/core": "^1.1.0",
42
+ "@jaypie/datadog": "^1.1.8",
42
43
  "@jaypie/errors": "^1.1.2",
43
44
  "cors": "^2.8.5"
44
45
  },
46
+ "devDependencies": {
47
+ "express": "^4.21.2"
48
+ },
45
49
  "publishConfig": {
46
50
  "access": "public"
47
51
  },
48
- "gitHead": "07f0f0f27e55014e63ccccb433ad515e39552db6"
52
+ "gitHead": "8d2d1b78cc7181d8bc1ec097bb8e425959e9cd79"
49
53
  }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  force,
3
+ getHeaderFrom,
3
4
  HTTP,
4
5
  JAYPIE,
5
6
  jaypieHandler,
@@ -7,6 +8,7 @@ import {
7
8
  UnhandledError,
8
9
  validate as validateIs,
9
10
  } from "@jaypie/core";
11
+ import { DATADOG, hasDatadogEnv, submitMetric } from "@jaypie/datadog";
10
12
 
11
13
  import getCurrentInvokeUuid from "./getCurrentInvokeUuid.adapter.js";
12
14
  import decorateResponse from "./decorateResponse.helper.js";
@@ -31,6 +33,7 @@ const expressHandler = (handler, options = {}) => {
31
33
  // Validate
32
34
  //
33
35
  let {
36
+ chaos,
34
37
  locals,
35
38
  name,
36
39
  setup = [],
@@ -53,6 +56,13 @@ const expressHandler = (handler, options = {}) => {
53
56
  return async (req, res, ...params) => {
54
57
  // * This is the first line of code that runs when a request is received
55
58
 
59
+ // Set default chaos value
60
+ if (chaos === undefined) {
61
+ chaos =
62
+ process.env.PROJECT_CHAOS ||
63
+ getHeaderFrom(HTTP.HEADER.PROJECT.CHAOS, req);
64
+ }
65
+
56
66
  // Re-init the logger
57
67
  publicLogger.init();
58
68
  // Very low-level, internal sub-trace details
@@ -109,7 +119,7 @@ const expressHandler = (handler, options = {}) => {
109
119
  status: res.status,
110
120
  statusSent: false,
111
121
  };
112
- // eslint-disable-next-line no-shadow
122
+
113
123
  res.end = (...params) => {
114
124
  originalRes.attemptedCall = originalRes.end;
115
125
  originalRes.attemptedParams = params;
@@ -117,7 +127,7 @@ const expressHandler = (handler, options = {}) => {
117
127
  "[jaypie] Illegal call to res.end(); prefer Jaypie response conventions",
118
128
  );
119
129
  };
120
- // eslint-disable-next-line no-shadow
130
+
121
131
  res.json = (...params) => {
122
132
  originalRes.attemptedCall = originalRes.json;
123
133
  originalRes.attemptedParams = params;
@@ -125,7 +135,7 @@ const expressHandler = (handler, options = {}) => {
125
135
  "[jaypie] Illegal call to res.json(); prefer Jaypie response conventions",
126
136
  );
127
137
  };
128
- // eslint-disable-next-line no-shadow
138
+
129
139
  res.send = (...params) => {
130
140
  originalRes.attemptedCall = originalRes.send;
131
141
  originalRes.attemptedParams = params;
@@ -133,9 +143,9 @@ const expressHandler = (handler, options = {}) => {
133
143
  "[jaypie] Illegal call to res.send(); prefer Jaypie response conventions",
134
144
  );
135
145
  };
136
- // eslint-disable-next-line no-shadow
146
+
137
147
  res.status = (...params) => {
138
- originalRes.statusSent = params;
148
+ originalRes.statusSent = params[0];
139
149
  return originalRes.status(...params);
140
150
  };
141
151
 
@@ -171,6 +181,7 @@ const expressHandler = (handler, options = {}) => {
171
181
 
172
182
  // Initialize after logging is set up
173
183
  jaypieFunction = jaypieHandler(handler, {
184
+ chaos,
174
185
  name,
175
186
  setup,
176
187
  teardown,
@@ -276,6 +287,45 @@ const expressHandler = (handler, options = {}) => {
276
287
  res: summarizeResponse(res, extras),
277
288
  });
278
289
 
290
+ // Submit metric if Datadog is configured
291
+ if (hasDatadogEnv()) {
292
+ // Construct path from baseUrl and url
293
+ let path = (req.baseUrl || "") + (req.url || "");
294
+ // Ensure path starts with /
295
+ if (!path.startsWith("/")) {
296
+ path = "/" + path;
297
+ }
298
+ // Remove trailing slash unless it's the root path
299
+ if (path.length > 1 && path.endsWith("/")) {
300
+ path = path.slice(0, -1);
301
+ }
302
+
303
+ // Replace UUIDs with :id for better aggregation
304
+ // Matches standard UUID v4 format (8-4-4-4-12 hex characters)
305
+ path = path.replace(
306
+ /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,
307
+ ":id",
308
+ );
309
+
310
+ // Determine metric name based on environment variables
311
+ let metricPrefix = "project";
312
+ if (process.env.PROJECT_SPONSOR) {
313
+ metricPrefix = process.env.PROJECT_SPONSOR;
314
+ } else if (process.env.PROJECT_KEY) {
315
+ metricPrefix = `project.${process.env.PROJECT_KEY}`;
316
+ }
317
+
318
+ await submitMetric({
319
+ name: `${metricPrefix}.api.response`,
320
+ type: DATADOG.METRIC.TYPE.COUNT,
321
+ value: 1,
322
+ tags: {
323
+ code: res.statusCode,
324
+ path,
325
+ },
326
+ });
327
+ }
328
+
279
329
  // Clean up the public logger
280
330
  publicLogger.untag("handler");
281
331