From 30b67b06098c86eb2f30f2167745796232a636da Mon Sep 17 00:00:00 2001 From: Slim Date: Thu, 2 Nov 2023 10:08:38 +0100 Subject: [PATCH 1/4] fixed: 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 --- src/hydra/fetchJsonLd.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hydra/fetchJsonLd.ts b/src/hydra/fetchJsonLd.ts index 447879c..22b50b5 100644 --- a/src/hydra/fetchJsonLd.ts +++ b/src/hydra/fetchJsonLd.ts @@ -2,6 +2,7 @@ import type { Document, JsonLd, RemoteDocument } from "jsonld/jsonld-spec"; import type { RequestInitExtended } from "./types.js"; const jsonLdMimeType = "application/ld+json"; +const jsonProblemMimeType = "application/problem+json"; export type RejectedResponseDocument = { response: Response; @@ -31,7 +32,7 @@ export default async function fetchJsonLd( return Promise.resolve({ response }); } - if (500 <= status || !contentType || !contentType.includes(jsonLdMimeType)) { + if (500 <= status || !contentType || (!contentType.includes(jsonLdMimeType) && !contentType.includes(jsonProblemMimeType))) { const reason: RejectedResponseDocument = { response }; return Promise.reject(reason); } From f5343cb3dd67d92600e54b7ea7a05808b0c789f7 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 6 Nov 2023 13:17:57 +0100 Subject: [PATCH 2/4] fixed: 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 --- src/hydra/parseHydraDocumentation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hydra/parseHydraDocumentation.ts b/src/hydra/parseHydraDocumentation.ts index bccc2cf..e54fca4 100644 --- a/src/hydra/parseHydraDocumentation.ts +++ b/src/hydra/parseHydraDocumentation.ts @@ -59,7 +59,7 @@ export function getDocumentationUrlFromHeaders(headers: Headers): string { } const matches = - /<(.+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec( + /<([^<]+)>; rel="http:\/\/www.w3.org\/ns\/hydra\/core#apiDocumentation"/.exec( linkHeader ); if (matches === null) { From a5fd94439325a9bec082b5b266c79a9831e2ce90 Mon Sep 17 00:00:00 2001 From: Slim Date: Thu, 9 Nov 2023 07:52:03 +0100 Subject: [PATCH 3/4] fixed linter complaints --- src/hydra/fetchJsonLd.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/hydra/fetchJsonLd.ts b/src/hydra/fetchJsonLd.ts index 22b50b5..ddcf720 100644 --- a/src/hydra/fetchJsonLd.ts +++ b/src/hydra/fetchJsonLd.ts @@ -32,7 +32,12 @@ export default async function fetchJsonLd( return Promise.resolve({ response }); } - if (500 <= status || !contentType || (!contentType.includes(jsonLdMimeType) && !contentType.includes(jsonProblemMimeType))) { + if ( + 500 <= status || + !contentType || + (!contentType.includes(jsonLdMimeType) && + !contentType.includes(jsonProblemMimeType)) + ) { const reason: RejectedResponseDocument = { response }; return Promise.reject(reason); } From 203bfdeef391b1f998faf0445fc2ac9b80c950bd Mon Sep 17 00:00:00 2001 From: Slim Date: Fri, 10 Nov 2023 16:30:26 +0100 Subject: [PATCH 4/4] added tests --- src/hydra/fetchJsonLd.test.ts | 28 ++++++++++++++++++++++- src/hydra/parseHydraDocumentation.test.ts | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/hydra/fetchJsonLd.test.ts b/src/hydra/fetchJsonLd.test.ts index ece1e3d..d226681 100644 --- a/src/hydra/fetchJsonLd.test.ts +++ b/src/hydra/fetchJsonLd.test.ts @@ -43,7 +43,7 @@ test("fetch a non JSON-LD document", () => { ); }); -test("fetch an error", () => { +test("fetch an error with Content-Type application/ld+json", () => { fetchMock.mockResponseOnce( `{ "@context": "http://json-ld.org/contexts/person.jsonld", @@ -69,6 +69,32 @@ test("fetch an error", () => { ); }); +test("fetch an error with Content-Type application/error+json", () => { + fetchMock.mockResponseOnce( + `{ + "@context": "http://json-ld.org/contexts/person.jsonld", + "@id": "http://dbpedia.org/resource/John_Lennon", + "name": "John Lennon", + "born": "1940-10-09", + "spouse": "http://dbpedia.org/resource/Cynthia_Lennon" +}`, + { + status: 400, + statusText: "Bad Request", + headers: { "Content-Type": "application/error+json" }, + } + ); + + return fetchJsonLd("/foo.jsonld").catch( + ({ response }: { response: Response }) => { + void response.json().then((body: { born: string }) => { + expect(response.ok).toBe(false); + expect(body.born).toBe("1940-10-09"); + }); + } + ); +}); + test("fetch an empty document", () => { fetchMock.mockResponseOnce("", { status: 204, diff --git a/src/hydra/parseHydraDocumentation.test.ts b/src/hydra/parseHydraDocumentation.test.ts index deb8fac..cf8d07e 100644 --- a/src/hydra/parseHydraDocumentation.test.ts +++ b/src/hydra/parseHydraDocumentation.test.ts @@ -1295,7 +1295,7 @@ const init: MockParams = { status: 200, statusText: "OK", headers: { - Link: '; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"', + Link: '; rel="http://example.com",; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"', "Content-Type": "application/ld+json", }, };