Skip to content

Commit 00bf316

Browse files
authored
feat: correctly parse response bodies as JSON where the Content-Type is application/scim+json (#731)
GitHub has APIs that return `application/scim+json` response bodies. Currently, these responses are not parsed as JSON, and instead, an `ArrayBuffer` is returned in `response.data`. This adds handling for `application/scim+json` so they are parsed as JSON as normal.
1 parent 324ffef commit 00bf316

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/fetch-wrapper.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { isPlainObject } from "./is-plain-object.js";
33
import { RequestError } from "@octokit/request-error";
44
import type { EndpointInterface, OctokitResponse } from "@octokit/types";
55

6+
type ContentType = ReturnType<typeof safeParse>;
7+
68
export default async function fetchWrapper(
79
requestOptions: ReturnType<EndpointInterface>,
810
): Promise<OctokitResponse<any>> {
@@ -155,7 +157,7 @@ async function getResponseData(response: Response): Promise<any> {
155157

156158
const mimetype = safeParse(contentType);
157159

158-
if (mimetype.type === "application/json") {
160+
if (isJSONResponse(mimetype)) {
159161
let text = "";
160162
try {
161163
text = await response.text();
@@ -173,6 +175,13 @@ async function getResponseData(response: Response): Promise<any> {
173175
}
174176
}
175177

178+
function isJSONResponse(mimetype: ContentType): boolean {
179+
return (
180+
mimetype.type === "application/json" ||
181+
mimetype.type === "application/scim+json"
182+
);
183+
}
184+
176185
function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {
177186
if (typeof data === "string") {
178187
return data;

test/request.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1263,4 +1263,41 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
12631263
}),
12641264
).rejects.toHaveProperty("message", "Unknown error: {}");
12651265
});
1266+
1267+
it("parses response bodies as JSON when Content-Type is application/scim+json", async () => {
1268+
expect.assertions(1);
1269+
1270+
const mock = fetchMock.createInstance();
1271+
mock.get("https://api.github.com/scim/v2/Users", {
1272+
status: 200,
1273+
body: {
1274+
totalResults: 1,
1275+
Resources: [
1276+
{
1277+
id: "123",
1278+
userName: "octocat",
1279+
},
1280+
],
1281+
},
1282+
headers: {
1283+
"Content-Type": "application/scim+json",
1284+
},
1285+
});
1286+
1287+
const response = await request("GET /scim/v2/Users", {
1288+
request: {
1289+
fetch: mock.fetchHandler,
1290+
},
1291+
});
1292+
1293+
expect(response.data).toEqual({
1294+
totalResults: 1,
1295+
Resources: [
1296+
{
1297+
id: "123",
1298+
userName: "octocat",
1299+
},
1300+
],
1301+
});
1302+
});
12661303
});

0 commit comments

Comments
 (0)