File tree Expand file tree Collapse file tree 2 files changed +17
-8
lines changed Expand file tree Collapse file tree 2 files changed +17
-8
lines changed Original file line number Diff line number Diff line change 1
1
import { MissingPageInfo } from "./errors.js" ;
2
2
3
- const isObject = ( value : any ) =>
3
+ type unknownObject = Record < string | number | symbol , unknown > ;
4
+
5
+ const isObject = ( value : unknown ) : value is unknownObject =>
4
6
Object . prototype . toString . call ( value ) === "[object Object]" ;
5
7
6
8
function findPaginatedResourcePath ( responseData : any ) : string [ ] {
7
- const paginatedResourcePath : string [ ] | null = deepFindPathToProperty (
9
+ const paginatedResourcePath = deepFindPathToProperty (
8
10
responseData ,
9
11
"pageInfo" ,
10
12
) ;
11
- if ( paginatedResourcePath === null || paginatedResourcePath . length === 0 ) {
13
+ if ( paginatedResourcePath . length === 0 ) {
12
14
throw new MissingPageInfo ( responseData ) ;
13
15
}
14
16
return paginatedResourcePath ;
15
17
}
16
18
17
19
const deepFindPathToProperty = (
18
- object : any ,
20
+ object : unknownObject ,
19
21
searchProp : string ,
20
22
path : string [ ] = [ ] ,
21
23
) : string [ ] => {
22
24
for ( const key of Object . keys ( object ) ) {
23
25
const currentPath = [ ...path , key ] ;
24
26
const currentValue = object [ key ] ;
25
27
26
- if ( currentValue . hasOwnProperty ( searchProp ) ) {
27
- return currentPath ;
28
- }
29
-
30
28
if ( isObject ( currentValue ) ) {
29
+ if ( currentValue . hasOwnProperty ( searchProp ) ) {
30
+ return currentPath ;
31
+ }
32
+
31
33
const result = deepFindPathToProperty (
32
34
currentValue ,
33
35
searchProp ,
Original file line number Diff line number Diff line change
1
+ import { MissingPageInfo } from "../src/errors.js" ;
1
2
import { extractPageInfos } from "../src/extract-page-info.js" ;
2
3
import type { PageInfoContext } from "../src/page-info.js" ;
3
4
@@ -63,4 +64,10 @@ describe("extractPageInfos()", (): void => {
63
64
pathInQuery : [ "data" , "repository" , "issues" ] ,
64
65
} ) ;
65
66
} ) ;
67
+
68
+ it ( "throws a MissingPageInfo error if the response has unexpected structure" , async ( ) : Promise < void > => {
69
+ expect ( ( ) => extractPageInfos ( { unknown1 : null , unknown2 : 42 } ) ) . toThrow (
70
+ MissingPageInfo ,
71
+ ) ;
72
+ } ) ;
66
73
} ) ;
You can’t perform that action at this time.
0 commit comments