Skip to content

Commit 11aaafd

Browse files
authored
fix: two bug fixes (#132)
* fix: was not handling Content-Type: application/problem+json This content type is defined in RFC7807 and is used for errors in Symfony and API Platform. * fix: getDocumentationFromHeaders() was not extracting URL It was not working when there were multiple urls in the Link: header. There was a defect in the regex.
1 parent f5dca27 commit 11aaafd

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/hydra/fetchJsonLd.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test("fetch a non JSON-LD document", () => {
4343
);
4444
});
4545

46-
test("fetch an error", () => {
46+
test("fetch an error with Content-Type application/ld+json", () => {
4747
fetchMock.mockResponseOnce(
4848
`{
4949
"@context": "http://json-ld.org/contexts/person.jsonld",
@@ -69,6 +69,32 @@ test("fetch an error", () => {
6969
);
7070
});
7171

72+
test("fetch an error with Content-Type application/error+json", () => {
73+
fetchMock.mockResponseOnce(
74+
`{
75+
"@context": "http://json-ld.org/contexts/person.jsonld",
76+
"@id": "http://dbpedia.org/resource/John_Lennon",
77+
"name": "John Lennon",
78+
"born": "1940-10-09",
79+
"spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
80+
}`,
81+
{
82+
status: 400,
83+
statusText: "Bad Request",
84+
headers: { "Content-Type": "application/error+json" },
85+
}
86+
);
87+
88+
return fetchJsonLd("/foo.jsonld").catch(
89+
({ response }: { response: Response }) => {
90+
void response.json().then((body: { born: string }) => {
91+
expect(response.ok).toBe(false);
92+
expect(body.born).toBe("1940-10-09");
93+
});
94+
}
95+
);
96+
});
97+
7298
test("fetch an empty document", () => {
7399
fetchMock.mockResponseOnce("", {
74100
status: 204,

src/hydra/fetchJsonLd.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Document, JsonLd, RemoteDocument } from "jsonld/jsonld-spec";
22
import type { RequestInitExtended } from "./types.js";
33

44
const jsonLdMimeType = "application/ld+json";
5+
const jsonProblemMimeType = "application/problem+json";
56

67
export type RejectedResponseDocument = {
78
response: Response;
@@ -31,7 +32,12 @@ export default async function fetchJsonLd(
3132
return Promise.resolve({ response });
3233
}
3334

34-
if (500 <= status || !contentType || !contentType.includes(jsonLdMimeType)) {
35+
if (
36+
500 <= status ||
37+
!contentType ||
38+
(!contentType.includes(jsonLdMimeType) &&
39+
!contentType.includes(jsonProblemMimeType))
40+
) {
3541
const reason: RejectedResponseDocument = { response };
3642
return Promise.reject(reason);
3743
}

src/hydra/parseHydraDocumentation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ const init: MockParams = {
12951295
status: 200,
12961296
statusText: "OK",
12971297
headers: {
1298-
Link: '<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"',
1298+
Link: '<http://example.com/docs>; rel="http://example.com",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"',
12991299
"Content-Type": "application/ld+json",
13001300
},
13011301
};

src/hydra/parseHydraDocumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function getDocumentationUrlFromHeaders(headers: Headers): string {
5959
}
6060

6161
const matches =
62-
/<(.+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec(
62+
/<([^<]+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec(
6363
linkHeader
6464
);
6565
if (matches === null) {

0 commit comments

Comments
 (0)