Skip to content

Commit 7b17cc4

Browse files
authored
fix: TypeError with hasOwnProperty in deepFindPathToProperty (#234)
* chore: add failing test for response having null value * Revert "fix: add `null` check in `deepFindPathToProperty` function (#137)" This reverts commit cfda527. * fix: hasOwnProperty error in deepFindPathToProperty
1 parent 3ce6b78 commit 7b17cc4

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/object-helpers.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import { MissingPageInfo } from "./errors.js";
22

3-
const isObject = (value: any) =>
3+
type unknownObject = Record<string | number | symbol, unknown>;
4+
5+
const isObject = (value: unknown): value is unknownObject =>
46
Object.prototype.toString.call(value) === "[object Object]";
57

68
function findPaginatedResourcePath(responseData: any): string[] {
7-
const paginatedResourcePath: string[] | null = deepFindPathToProperty(
9+
const paginatedResourcePath = deepFindPathToProperty(
810
responseData,
911
"pageInfo",
1012
);
11-
if (paginatedResourcePath === null || paginatedResourcePath.length === 0) {
13+
if (paginatedResourcePath.length === 0) {
1214
throw new MissingPageInfo(responseData);
1315
}
1416
return paginatedResourcePath;
1517
}
1618

1719
const deepFindPathToProperty = (
18-
object: any,
20+
object: unknownObject,
1921
searchProp: string,
2022
path: string[] = [],
2123
): string[] => {
2224
for (const key of Object.keys(object)) {
2325
const currentPath = [...path, key];
2426
const currentValue = object[key];
2527

26-
if (currentValue.hasOwnProperty(searchProp)) {
27-
return currentPath;
28-
}
29-
3028
if (isObject(currentValue)) {
29+
if (currentValue.hasOwnProperty(searchProp)) {
30+
return currentPath;
31+
}
32+
3133
const result = deepFindPathToProperty(
3234
currentValue,
3335
searchProp,

test/extract-page-infos.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MissingPageInfo } from "../src/errors.js";
12
import { extractPageInfos } from "../src/extract-page-info.js";
23
import type { PageInfoContext } from "../src/page-info.js";
34

@@ -63,4 +64,10 @@ describe("extractPageInfos()", (): void => {
6364
pathInQuery: ["data", "repository", "issues"],
6465
});
6566
});
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+
});
6673
});

0 commit comments

Comments
 (0)