@@ -117,15 +117,23 @@ export function replacer(key: any, value: any): any {
117117 return value ;
118118}
119119
120+ function dateReviver ( key , value ) {
121+ // check if value match ISO 8601 date format
122+ if ( typeof value === "string" && / ^ ( \d { 4 } ) - ( \d { 2 } ) - ( \d { 2 } ) T ( \d { 2 } ) : ( \d { 2 } ) : ( \d { 2 } ) ( .\d + ) ? ( Z ) ? $ / . test ( value ) ) {
123+ return new Date ( value ) ;
124+ }
125+ return value ;
126+ }
127+
120128export function fetchReq < I , O > ( path : string , init ?: InitReq ) : Promise < O > {
121129 const { pathPrefix, ...req } = init || { }
122130
123131 const url = pathPrefix ? `${ pathPrefix } ${ path } ` : path
124132
125- return fetch ( url , req ) . then ( r => r . json ( ) . then ( ( body : O ) => {
126- if ( ! r . ok ) { throw body ; }
127- return body ;
128- } ) ) as Promise < O >
133+ return fetch ( url , req ) . then ( async r => {
134+ if ( ! r . ok ) { throw await r . json ( ) }
135+ return JSON . parse ( await r . text ( ) , dateReviver ) ;
136+ } ) as Promise < O >
129137}
130138
131139// NotifyStreamEntityArrival is a callback that will be called on streaming entity arrival
@@ -301,6 +309,8 @@ function flattenRequestPayload<T extends RequestPayload>(
301309 objectToMerge = flattenRequestPayload ( value as RequestPayload , newPath ) ;
302310 } else if ( isNonZeroValuePrimitive || isNonEmptyPrimitiveArray ) {
303311 objectToMerge = { [ newPath ] : value } ;
312+ } else if ( value instanceof Date ) {
313+ objectToMerge = { [ newPath ] : value . toISOString ( ) } ;
304314 }
305315
306316 return { ...acc , ...objectToMerge } ;
0 commit comments