From 78c12c641bc61c8c42adf98c5bea7c369c0423aa Mon Sep 17 00:00:00 2001 From: Slim Date: Wed, 8 Nov 2023 11:07:18 +0100 Subject: [PATCH 1/5] fixed: documentLoader() was not loading with method GET When parent function fetchJsonLd() was called with method POST is inherited the method which resulted in a status 415 response from the API which refused the unsafe method with unrecognized Content Type jsonld --- src/hydra/fetchHydra.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/hydra/fetchHydra.ts b/src/hydra/fetchHydra.ts index 99c8afa0..a6beab72 100644 --- a/src/hydra/fetchHydra.ts +++ b/src/hydra/fetchHydra.ts @@ -40,8 +40,12 @@ function fetchHydra( delete (body as NodeObject).trace; - const documentLoader = (input: string) => - fetchJsonLd(input, authOptions).then((response) => { + const documentLoader = (input: string) => { + let options = authOptions; + options.method = "GET"; + delete options.body; + + return fetchJsonLd(input, options).then((response) => { if (!('body' in response)) { throw new Error( 'An empty response was received when expanding JSON-LD error document.', @@ -49,6 +53,7 @@ function fetchHydra( } return response; }); + } return jsonld .expand(body, { From 19c73baa1a2ea4a75358ff3095ba87223ca550fd Mon Sep 17 00:00:00 2001 From: Slim Date: Wed, 8 Nov 2023 14:19:45 +0100 Subject: [PATCH 2/5] fixed: was not displaying a useful error message when creating an entity --- src/hydra/fetchHydra.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hydra/fetchHydra.ts b/src/hydra/fetchHydra.ts index a6beab72..e58140bb 100644 --- a/src/hydra/fetchHydra.ts +++ b/src/hydra/fetchHydra.ts @@ -55,11 +55,11 @@ function fetchHydra( }); } - return jsonld - .expand(body, { - base: getDocumentationUrlFromHeaders(headers), - documentLoader, - }) + return documentLoader(getDocumentationUrlFromHeaders(headers)) + .then((response) => jsonld.expand(body, { + expandContext: response.document as any + })) + .then((json) => Promise.reject( new HttpError( From 5758f0e2c32eb8b3508ae5df31adf7c658003174 Mon Sep 17 00:00:00 2001 From: Slim Date: Thu, 9 Nov 2023 08:29:46 +0100 Subject: [PATCH 3/5] fixed tests and linter complaints --- src/hydra/dataProvider.test.ts | 2 +- src/hydra/dataProvider.ts | 2 +- src/hydra/fetchHydra.ts | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/hydra/dataProvider.test.ts b/src/hydra/dataProvider.test.ts index 44674b40..1d07e17c 100644 --- a/src/hydra/dataProvider.test.ts +++ b/src/hydra/dataProvider.test.ts @@ -138,7 +138,7 @@ describe('Transform a React Admin request to an Hydra request', () => { perPage: 30, }, sort: { - order: '', + order: 'ASC', field: '', }, filter: { diff --git a/src/hydra/dataProvider.ts b/src/hydra/dataProvider.ts index 017f5457..bf1ff01b 100644 --- a/src/hydra/dataProvider.ts +++ b/src/hydra/dataProvider.ts @@ -722,7 +722,7 @@ function dataProvider( page: 1, }, filter: { id: params.ids }, - sort: { field: '', order: '' }, + sort: { field: '', order: 'ASC' }, }).then(({ data }) => ({ data })); } diff --git a/src/hydra/fetchHydra.ts b/src/hydra/fetchHydra.ts index e58140bb..dcc9f70a 100644 --- a/src/hydra/fetchHydra.ts +++ b/src/hydra/fetchHydra.ts @@ -4,7 +4,7 @@ import { getDocumentationUrlFromHeaders, } from '@api-platform/api-doc-parser'; import jsonld from 'jsonld'; -import type { NodeObject } from 'jsonld'; +import type { ContextDefinition, NodeObject } from 'jsonld'; import type { JsonLdObj } from 'jsonld/jsonld-spec'; import type { HttpClientOptions, HydraHttpClientResponse } from '../types.js'; @@ -41,11 +41,11 @@ function fetchHydra( delete (body as NodeObject).trace; const documentLoader = (input: string) => { - let options = authOptions; - options.method = "GET"; - delete options.body; + const loaderOptions = authOptions; + loaderOptions.method = 'GET'; + delete loaderOptions.body; - return fetchJsonLd(input, options).then((response) => { + return fetchJsonLd(input, loaderOptions).then((response) => { if (!('body' in response)) { throw new Error( 'An empty response was received when expanding JSON-LD error document.', @@ -53,12 +53,14 @@ function fetchHydra( } return response; }); - } + }; return documentLoader(getDocumentationUrlFromHeaders(headers)) - .then((response) => jsonld.expand(body, { - expandContext: response.document as any - })) + .then((response) => + jsonld.expand(body, { + expandContext: response.document as ContextDefinition, + }), + ) .then((json) => Promise.reject( From f1010fb3b1abbb5c4935c65ecee91c782c018a88 Mon Sep 17 00:00:00 2001 From: Slim Date: Mon, 20 Nov 2023 11:08:25 +0100 Subject: [PATCH 4/5] fixed tests were complaining that ReferenceError: TextEncoder is not defined --- jest.setup.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jest.setup.ts b/jest.setup.ts index 85e80307..663e582a 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -1,8 +1,9 @@ // eslint-disable-next-line import/no-extraneous-dependencies import '@testing-library/jest-dom'; -import { TextEncoder } from 'util'; +import { TextEncoder, TextDecoder } from 'util'; // eslint-disable-next-line import/no-extraneous-dependencies import { Request } from 'node-fetch'; global.TextEncoder = TextEncoder; +global.TextDecoder = TextDecoder as any; global.Request = Request as any; From 92838134fb3d130b7cc54c238c8b54400e5053a9 Mon Sep 17 00:00:00 2001 From: Slim Date: Fri, 24 Nov 2023 17:47:15 +0100 Subject: [PATCH 5/5] fixed linter complaints --- src/types.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/types.ts b/src/types.ts index f060ef14..30032f7d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -226,22 +226,22 @@ export type ApiPlatformAdminDataProviderTypeParams = T extends typeof GET_LIST ? ApiPlatformAdminGetListParams : T extends typeof GET_ONE - ? ApiPlatformAdminGetOneParams - : T extends typeof GET_MANY - ? ApiPlatformAdminGetManyParams - : T extends typeof GET_MANY_REFERENCE - ? ApiPlatformAdminGetManyReferenceParams - : T extends typeof UPDATE - ? ApiPlatformAdminUpdateParams - : T extends typeof UPDATE_MANY - ? ApiPlatformAdminUpdateManyParams - : T extends typeof CREATE - ? ApiPlatformAdminCreateParams - : T extends typeof DELETE - ? ApiPlatformAdminDeleteParams - : T extends typeof DELETE_MANY - ? ApiPlatformAdminDeleteManyParams - : never; + ? ApiPlatformAdminGetOneParams + : T extends typeof GET_MANY + ? ApiPlatformAdminGetManyParams + : T extends typeof GET_MANY_REFERENCE + ? ApiPlatformAdminGetManyReferenceParams + : T extends typeof UPDATE + ? ApiPlatformAdminUpdateParams + : T extends typeof UPDATE_MANY + ? ApiPlatformAdminUpdateManyParams + : T extends typeof CREATE + ? ApiPlatformAdminCreateParams + : T extends typeof DELETE + ? ApiPlatformAdminDeleteParams + : T extends typeof DELETE_MANY + ? ApiPlatformAdminDeleteManyParams + : never; export interface ApiPlatformAdminDataProviderFactoryParams { entrypoint: string;