11
2- import  {  Query  }  from  '../types' ; 
2+ import  type   {  Query  }  from  '../types' ; 
33
44/** 
55 * Get query parameters from the specified request url. 
@@ -87,6 +87,25 @@ export function tryToParseObject(body: unknown) {
8787  } 
8888} 
8989
90+ export  function  tryToParsePostBody ( body : unknown ) : unknown  { 
91+   if  ( ! body )  { 
92+     return  body ; 
93+   } 
94+ 
95+   if  ( typeof  body  ===  'string' )  { 
96+     const  info  =  tryToParseObject ( body ) ; 
97+     if  ( info  &&  typeof  info  ===  'object' )  { 
98+       return  info ; 
99+     } 
100+   } 
101+ 
102+   if  ( typeof  body  ===  'string'  &&  body . includes ( '&' )  &&  body . includes ( '=' ) )  { 
103+     return  getQuery ( body ) ; 
104+   } 
105+ 
106+   return  body ; 
107+ } 
108+ 
90109/** 
91110 * Try to parse a JSON string 
92111 * @param  {unknown } body 
@@ -224,9 +243,11 @@ export function isImported(obj: unknown) {
224243 * Get caller file from error stack 
225244 */ 
226245export  function  getCallerFile ( )  { 
246+   type  SimplifiedStackInfo  =  {  getFileName : ( )  =>  string  } ; 
247+ 
227248  const  oldPrepareStackTrace  =  Error . prepareStackTrace ; 
228249  Error . prepareStackTrace  =  ( _ ,  stack )   =>  stack ; 
229-   const  stack  =  new  Error ( ) . stack  as  unknown  as  Record < string ,  {   getFileName :  ( )   =>   string   } > ; 
250+   const  stack  =  new  Error ( ) . stack  as  unknown  as  Record < string ,  SimplifiedStackInfo > ; 
230251  Error . prepareStackTrace  =  oldPrepareStackTrace ; 
231252
232253
@@ -240,3 +261,22 @@ export function getCallerFile() {
240261    } 
241262  } 
242263} 
264+ 
265+ export  function  get < T > ( obj : object ,  path : string  |  Array < string  |  number > ,  defaultValue ?: unknown ) : T  { 
266+   if  ( typeof  path  ===  'string' )  { 
267+     path  =  path . replace ( / \[ ( \w + ) \] / g,  '.$1' ) ; 
268+     path  =  path . split ( '.' ) . filter ( Boolean ) ; 
269+   } 
270+ 
271+   let  result : unknown  =  obj ; 
272+   for  ( const  key  of  path  as  Array < string  |  number > )  { 
273+     if  ( result  &&  result [ key  as  keyof  typeof  result ]  !==  undefined )  { 
274+       result  =  result [ key  as  keyof  typeof  result ] ; 
275+     }  else  { 
276+       result  =  undefined ; 
277+       break ; 
278+     } 
279+   } 
280+ 
281+   return  ( result  ===  undefined  ? defaultValue  : result )  as  T ; 
282+ } 
0 commit comments