Skip to content

Commit f13f5d9

Browse files
authored
fix: use json-with-bigint instead of built-in JSON methods in order to properly support int64's (#798)
* Refactor JSON response handling in fetch-wrapper * Add text variable for JSON response parsing Initialize text variable for JSON response handling. * Update src/fetch-wrapper.ts * Apply suggestions from code review * switch to json-with-bigint * Add test * properly serialises request bodies that contain int64 (BigInt) * remove test * bump endpoints
1 parent 9ba6ae0 commit f13f5d9

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

package-lock.json

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
"author": "Gregor Martynus (https://github.com/gr2m)",
2626
"license": "MIT",
2727
"dependencies": {
28-
"@octokit/endpoint": "^11.0.2",
28+
"@octokit/endpoint": "^11.0.3",
2929
"@octokit/request-error": "^7.0.2",
3030
"@octokit/types": "^16.0.0",
3131
"fast-content-type-parse": "^3.0.0",
32+
"json-with-bigint": "^3.5.3",
3233
"universal-user-agent": "^7.0.2"
3334
},
3435
"devDependencies": {
@@ -38,9 +39,9 @@
3839
"@vitest/coverage-v8": "^4.0.0",
3940
"esbuild": "^0.27.0",
4041
"fetch-mock": "^12.0.0",
41-
"tinyglobby": "^0.2.15",
4242
"prettier": "3.6.2",
4343
"semantic-release-plugin-update-version-in-files": "^2.0.0",
44+
"tinyglobby": "^0.2.15",
4445
"typescript": "^5.0.0",
4546
"undici": "^7.0.0",
4647
"vitest": "^4.0.0"

src/fetch-wrapper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { safeParse } from "fast-content-type-parse";
2+
import { JSONParse, JSONStringify } from "json-with-bigint";
23
import { isPlainObject } from "./is-plain-object.js";
34
import { RequestError } from "@octokit/request-error";
45
import type { EndpointInterface, OctokitResponse } from "@octokit/types";
@@ -26,7 +27,7 @@ export default async function fetchWrapper(
2627

2728
const body =
2829
isPlainObject(requestOptions.body) || Array.isArray(requestOptions.body)
29-
? JSON.stringify(requestOptions.body)
30+
? JSONStringify(requestOptions.body)
3031
: requestOptions.body;
3132

3233
// Header values must be `string`
@@ -167,7 +168,7 @@ async function getResponseData(response: Response): Promise<any> {
167168
let text = "";
168169
try {
169170
text = await response.text();
170-
return JSON.parse(text);
171+
return JSONParse(text);
171172
} catch (err) {
172173
return text;
173174
}

test/request.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
} from "@octokit/types";
1515

1616
import { request } from "../src/index.ts";
17+
import { JSONStringify } from "json-with-bigint";
1718

1819
const userAgent = `octokit-request.js/0.0.0-development ${getUserAgent()}`;
1920
const __filename = new URL(import.meta.url);
@@ -1328,4 +1329,41 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
13281329
],
13291330
});
13301331
});
1332+
1333+
it("parses JSON response bodies that contain int64 (BigInt)", async () => {
1334+
expect.assertions(2);
1335+
1336+
const mock = fetchMock.createInstance();
1337+
mock.get("https://api.github.com/app/hook/deliveries", {
1338+
status: 200,
1339+
headers: {
1340+
"Content-Type": "application/json; charset=utf-8",
1341+
},
1342+
body: [
1343+
{
1344+
id: Number.MAX_SAFE_INTEGER + 9,
1345+
guid: "0b989ba4-242f-11e5-81e1-c7b6966d2516",
1346+
delivered_at: "2019-06-03T00:57:16Z",
1347+
redelivery: false,
1348+
duration: 0.27,
1349+
status: "OK",
1350+
status_code: 200,
1351+
event: "issues",
1352+
action: "opened",
1353+
installation_id: 123,
1354+
repository_id: 456,
1355+
throttled_at: "2019-06-03T00:57:16Z",
1356+
},
1357+
],
1358+
});
1359+
1360+
const response = await request("GET /app/hook/deliveries", {
1361+
request: {
1362+
fetch: mock.fetchHandler,
1363+
},
1364+
});
1365+
1366+
expect(response.status).toEqual(200);
1367+
expect(response.data[0].id).toEqual(BigInt(Number.MAX_SAFE_INTEGER) + 9n);
1368+
});
13311369
});

0 commit comments

Comments
 (0)