This repository was archived by the owner on Sep 2, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -241,7 +241,10 @@ describe('apilyticsMiddleware()', () => {
241241 const agent = createAgent ( { apiKey } ) ;
242242 const numberSpy = jest
243243 . spyOn ( global , 'Number' )
244- . mockImplementation ( ( ) => NaN ) ;
244+ . mockImplementationOnce ( ( ) => {
245+ numberSpy . mockRestore ( ) ;
246+ return NaN ;
247+ } ) ;
245248 const response = await agent . get ( '/empty' ) ;
246249 numberSpy . mockRestore ( ) ;
247250 expect ( response . status ) . toEqual ( 200 ) ;
Original file line number Diff line number Diff line change @@ -42,15 +42,13 @@ export const apilyticsMiddleware = (
4242 'http://_' , // Cannot parse a relative URL, so make it absolute.
4343 ) ;
4444
45- const _requestSize = Number ( req . headers [ 'content-length' ] ) ;
46- const requestSize = isNaN ( _requestSize ) ? undefined : _requestSize ;
45+ const requestSize = numberOrUndefined ( req . headers [ 'content-length' ] ) ;
4746
48- const _responseSize = Number (
47+ const responseSize = numberOrUndefined (
4948 // @ts -ignore: `_contentLength` is not typed, but it does exist sometimes
5049 // when the header doesn't. Even if it doesn't this won't fail at runtime.
5150 res . getHeader ( 'content-length' ) ?? res . _contentLength ,
5251 ) ;
53- const responseSize = isNaN ( _responseSize ) ? undefined : _responseSize ;
5452
5553 sendApilyticsMetrics ( {
5654 apiKey,
@@ -71,3 +69,8 @@ export const apilyticsMiddleware = (
7169 next ( ) ;
7270 } ;
7371} ;
72+
73+ const numberOrUndefined = ( value : unknown ) : number | undefined => {
74+ const converted = Number ( value ) ;
75+ return Number . isNaN ( converted ) ? undefined : converted ;
76+ } ;
Original file line number Diff line number Diff line change @@ -280,7 +280,10 @@ describe('withApilytics()', () => {
280280 const agent = createAgent ( { apiKey } ) ;
281281 const numberSpy = jest
282282 . spyOn ( global , 'Number' )
283- . mockImplementation ( ( ) => NaN ) ;
283+ . mockImplementationOnce ( ( ) => {
284+ numberSpy . mockRestore ( ) ;
285+ return NaN ;
286+ } ) ;
284287 const response = await agent . get ( '/empty' ) ;
285288 numberSpy . mockRestore ( ) ;
286289 expect ( response . status ) . toEqual ( 200 ) ;
Original file line number Diff line number Diff line change @@ -40,7 +40,7 @@ export const withApilytics = <T>(
4040 res : NextApiResponse < T > ,
4141 ) : Promise < void > => {
4242 let statusCode : number | undefined ;
43- let responseSize : number | undefined ;
43+
4444 const timer = milliSecondTimer ( ) ;
4545
4646 try {
@@ -57,15 +57,13 @@ export const withApilytics = <T>(
5757 ) ) ;
5858 }
5959
60- const _requestSize = Number ( req . headers [ 'content-length' ] ) ;
61- const requestSize = isNaN ( _requestSize ) ? undefined : _requestSize ;
60+ const requestSize = numberOrUndefined ( req . headers [ 'content-length' ] ) ;
6261
63- const _responseSize = Number (
62+ const responseSize = numberOrUndefined (
6463 // @ts -ignore: `_contentLength` is not typed, but it does exist sometimes
6564 // when the header doesn't. Even if it doesn't this won't fail at runtime.
6665 res . getHeader ( 'content-length' ) ?? res . _contentLength ,
6766 ) ;
68- responseSize = isNaN ( _responseSize ) ? undefined : _responseSize ;
6967
7068 sendApilyticsMetrics ( {
7169 apiKey,
@@ -83,3 +81,8 @@ export const withApilytics = <T>(
8381 }
8482 } ;
8583} ;
84+
85+ const numberOrUndefined = ( value : unknown ) : number | undefined => {
86+ const converted = Number ( value ) ;
87+ return Number . isNaN ( converted ) ? undefined : converted ;
88+ } ;
You can’t perform that action at this time.
0 commit comments