@@ -71,9 +71,10 @@ import type {
7171 * });
7272 *
7373 * export const handler = async (event: { requestId: string }) => {
74- * metrics.addMetadata('request_id', event.requestId);
75- * metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
76- * metrics.publishStoredMetrics();
74+ * metrics
75+ * .addMetadata('request_id', event.requestId)
76+ * .addMetric('successfulBooking', MetricUnit.Count, 1)
77+ * .publishStoredMetrics();
7778 * };
7879 * ```
7980 *
@@ -110,7 +111,7 @@ import type {
110111 * ```
111112 *
112113 * Note that decorators are a Stage 3 proposal for JavaScript and are not yet part of the ECMAScript standard.
113- * The current implmementation in this library is based on the legacy TypeScript decorator syntax enabled by the [`experimentalDecorators` flag](https://www.typescriptlang.org/tsconfig/#experimentalDecorators)
114+ * The current implementation in this library is based on the legacy TypeScript decorator syntax enabled by the [`experimentalDecorators` flag](https://www.typescriptlang.org/tsconfig/#experimentalDecorators)
114115 * set to `true` in the `tsconfig.json` file.
115116 *
116117 * **Middy.js middleware**
@@ -239,12 +240,12 @@ class Metrics extends Utility implements MetricsInterface {
239240 * @param name - The name of the dimension
240241 * @param value - The value of the dimension
241242 */
242- public addDimension ( name : string , value : string ) : void {
243+ public addDimension ( name : string , value : string ) : this {
243244 if ( isStringUndefinedNullEmpty ( name ) || isStringUndefinedNullEmpty ( value ) ) {
244245 this . #logger. warn (
245246 `The dimension ${ name } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
246247 ) ;
247- return ;
248+ return this ;
248249 }
249250 if ( MAX_DIMENSION_COUNT <= this . #dimensionsStore. getDimensionCount ( ) ) {
250251 throw new RangeError (
@@ -262,6 +263,7 @@ class Metrics extends Utility implements MetricsInterface {
262263 ) ;
263264 }
264265 this . #dimensionsStore. addDimension ( name , value ) ;
266+ return this ;
265267 }
266268
267269 /**
@@ -276,7 +278,7 @@ class Metrics extends Utility implements MetricsInterface {
276278 *
277279 * @param dimensions - An object with key-value pairs of dimensions
278280 */
279- public addDimensions ( dimensions : Dimensions ) : void {
281+ public addDimensions ( dimensions : Dimensions ) : this {
280282 const newDimensions = this . #sanitizeDimensions( dimensions ) ;
281283 const currentCount = this . #dimensionsStore. getDimensionCount ( ) ;
282284 const newSetCount = Object . keys ( newDimensions ) . length ;
@@ -287,6 +289,7 @@ class Metrics extends Utility implements MetricsInterface {
287289 }
288290
289291 this . #dimensionsStore. addDimensionSet ( newDimensions ) ;
292+ return this ;
290293 }
291294
292295 /**
@@ -316,8 +319,9 @@ class Metrics extends Utility implements MetricsInterface {
316319 * @param key - The key of the metadata
317320 * @param value - The value of the metadata
318321 */
319- public addMetadata ( key : string , value : string ) : void {
322+ public addMetadata ( key : string , value : string ) : this {
320323 this . #metadataStore. set ( key , value ) ;
324+ return this ;
321325 }
322326
323327 /**
@@ -359,9 +363,10 @@ class Metrics extends Utility implements MetricsInterface {
359363 unit : MetricUnit ,
360364 value : number ,
361365 resolution : MetricResolution = MetricResolutions . Standard
362- ) : void {
366+ ) : this {
363367 this . storeMetric ( name , unit , value , resolution ) ;
364368 if ( this . isSingleMetric ) this . publishStoredMetrics ( ) ;
369+ return this ;
365370 }
366371
367372 /**
@@ -450,7 +455,7 @@ class Metrics extends Utility implements MetricsInterface {
450455 *
451456 * // ...
452457 *
453- * metrics.clearDimensions(); // olnly the region dimension is removed
458+ * metrics.clearDimensions(); // only the region dimension is removed
454459 * };
455460 * ```
456461 *
@@ -595,7 +600,7 @@ class Metrics extends Utility implements MetricsInterface {
595600 * };
596601 * ```
597602 */
598- public publishStoredMetrics ( ) : void {
603+ public publishStoredMetrics ( ) : this {
599604 const hasMetrics = this . hasStoredMetrics ( ) ;
600605 if ( ! this . shouldThrowOnEmptyMetrics && ! hasMetrics ) {
601606 this . #logger. warn (
@@ -613,6 +618,7 @@ class Metrics extends Utility implements MetricsInterface {
613618 this . clearMetrics ( ) ;
614619 this . clearDimensions ( ) ;
615620 this . clearMetadata ( ) ;
621+ return this ;
616622 }
617623
618624 /**
@@ -645,14 +651,15 @@ class Metrics extends Utility implements MetricsInterface {
645651 * ```
646652 * @param timestamp - The timestamp to set, which can be a number or a Date object.
647653 */
648- public setTimestamp ( timestamp : number | Date ) : void {
654+ public setTimestamp ( timestamp : number | Date ) : this {
649655 if ( ! this . #validateEmfTimestamp( timestamp ) ) {
650656 this . #logger. warn (
651657 "This metric doesn't meet the requirements and will be skipped by Amazon CloudWatch. " +
652658 'Ensure the timestamp is within 14 days in the past or up to 2 hours in the future and is also a valid number or Date object.'
653659 ) ;
654660 }
655661 this . #metricsStore. setTimestamp ( timestamp ) ;
662+ return this ;
656663 }
657664
658665 /**
@@ -786,7 +793,7 @@ class Metrics extends Utility implements MetricsInterface {
786793 *
787794 * @param dimensions - The dimensions to be added to the default dimensions object
788795 */
789- public setDefaultDimensions ( dimensions : Dimensions ) : void {
796+ public setDefaultDimensions ( dimensions : Dimensions ) : this {
790797 const newDimensions = this . #sanitizeDimensions( dimensions ) ;
791798 const currentDefaultDimensions =
792799 this . #dimensionsStore. getDefaultDimensions ( ) ;
@@ -802,6 +809,7 @@ class Metrics extends Utility implements MetricsInterface {
802809 ...currentDefaultDimensions ,
803810 ...newDimensions ,
804811 } ) ;
812+ return this ;
805813 }
806814
807815 /**
@@ -823,8 +831,9 @@ class Metrics extends Utility implements MetricsInterface {
823831 *
824832 * @param enabled - Whether to throw an error if no metrics are emitted
825833 */
826- public setThrowOnEmptyMetrics ( enabled : boolean ) : void {
834+ public setThrowOnEmptyMetrics ( enabled : boolean ) : this {
827835 this . shouldThrowOnEmptyMetrics = enabled ;
836+ return this ;
828837 }
829838
830839 /**
@@ -1042,11 +1051,15 @@ class Metrics extends Utility implements MetricsInterface {
10421051 throw new RangeError ( `${ value } is not a valid number` ) ;
10431052 if ( ! Object . values ( MetricUnits ) . includes ( unit ) )
10441053 throw new RangeError (
1045- `Invalid metric unit '${ unit } ', expected either option: ${ Object . values ( MetricUnits ) . join ( ',' ) } `
1054+ `Invalid metric unit '${ unit } ', expected either option: ${ Object . values (
1055+ MetricUnits
1056+ ) . join ( ',' ) } `
10461057 ) ;
10471058 if ( ! Object . values ( MetricResolutions ) . includes ( resolution ) )
10481059 throw new RangeError (
1049- `Invalid metric resolution '${ resolution } ', expected either option: ${ Object . values ( MetricResolutions ) . join ( ',' ) } `
1060+ `Invalid metric resolution '${ resolution } ', expected either option: ${ Object . values (
1061+ MetricResolutions
1062+ ) . join ( ',' ) } `
10501063 ) ;
10511064
10521065 if ( this . #metricsStore. getMetricsCount ( ) >= MAX_METRICS_SIZE ) {
0 commit comments