From 7bc8b5b422c4a2a5b5a3fd7d72cea6a3adaa55d5 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Tue, 7 Nov 2023 14:00:18 +0100 Subject: [PATCH 1/3] fix: support geo+json and vnd.geo+json content types --- .../src/links/RestAPILink/fetchData.test.ts | 22 +++++++++++++++++++ .../data/src/links/RestAPILink/fetchData.ts | 9 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/services/data/src/links/RestAPILink/fetchData.test.ts b/services/data/src/links/RestAPILink/fetchData.test.ts index 5b47b5dd5..92e373289 100644 --- a/services/data/src/links/RestAPILink/fetchData.test.ts +++ b/services/data/src/links/RestAPILink/fetchData.test.ts @@ -9,6 +9,12 @@ describe('networkFetch', () => { expect(parseContentType('application/vnd.api+json')).toBe( 'application/vnd.api+json' ) + expect(parseContentType('application/vnd.geo+json')).toBe( + 'application/vnd.geo+json' + ) + expect(parseContentType('application/geo+json')).toBe( + 'application/geo+json' + ) }) it('should strip parameters', () => { @@ -121,6 +127,8 @@ describe('networkFetch', () => { get: (name: string) => headers[name] && headers[name](url), }, json: async () => ({ foo: 'bar' }), + 'geo+json': async () => ({ foo: 'geobar' }), + 'vnd.geo+json': async () => ({ foo: 'vndgeobar' }), text: async () => 'foobar', blob: async () => 'blob of foobar', })) @@ -135,6 +143,20 @@ describe('networkFetch', () => { }) }) + it('Should correctly parse a successful GEOJSON response', () => { + ;(global as any).fetch = mockFetch + expect(fetchData('geo+json', {})).resolves.toMatchObject({ + foo: 'geobar', + }) + }) + + it('Should correctly parse a successful VND.GEOJSON response', () => { + ;(global as any).fetch = mockFetch + expect(fetchData('vnd.geo+json', {})).resolves.toMatchObject({ + foo: 'geobar', + }) + }) + it('Should correctly parse a successful TEXT response', () => { ;(global as any).fetch = mockFetch expect(fetchData('text')).resolves.toBe('foobar') diff --git a/services/data/src/links/RestAPILink/fetchData.ts b/services/data/src/links/RestAPILink/fetchData.ts index 6ac7bdbbc..e5262f43f 100644 --- a/services/data/src/links/RestAPILink/fetchData.ts +++ b/services/data/src/links/RestAPILink/fetchData.ts @@ -1,5 +1,11 @@ import { FetchError, FetchErrorDetails, JsonValue } from '../../engine' +const validJsonJsonContentTypes: string[] = [ + 'application/json', + 'application/geo+json', + 'application/vnd.geo+json', +] + export const parseContentType = (contentType: string | null) => contentType ? contentType.split(';')[0].trim().toLowerCase() : '' @@ -78,8 +84,7 @@ export function fetchData( response.headers.get('Content-Type') ) - // 'application/json' - if (contentType === 'application/json') { + if (validJsonJsonContentTypes.includes(contentType)) { return await response.json() // Will throw if invalid JSON! } From 7764adb5988a9259950224bf64e681499c78dd28 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Tue, 7 Nov 2023 14:25:48 +0100 Subject: [PATCH 2/3] chore: verify test fails --- services/data/src/links/RestAPILink/fetchData.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/data/src/links/RestAPILink/fetchData.test.ts b/services/data/src/links/RestAPILink/fetchData.test.ts index 92e373289..33762b285 100644 --- a/services/data/src/links/RestAPILink/fetchData.test.ts +++ b/services/data/src/links/RestAPILink/fetchData.test.ts @@ -153,7 +153,7 @@ describe('networkFetch', () => { it('Should correctly parse a successful VND.GEOJSON response', () => { ;(global as any).fetch = mockFetch expect(fetchData('vnd.geo+json', {})).resolves.toMatchObject({ - foo: 'geobar', + foo: 'bar', }) }) From 6d3eaf06b3ec4d69a1ad9d17d047bd061714242e Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Fri, 10 Nov 2023 07:15:50 -0800 Subject: [PATCH 3/3] chore: function name --- services/data/src/links/RestAPILink/fetchData.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/data/src/links/RestAPILink/fetchData.ts b/services/data/src/links/RestAPILink/fetchData.ts index e5262f43f..712a336bd 100644 --- a/services/data/src/links/RestAPILink/fetchData.ts +++ b/services/data/src/links/RestAPILink/fetchData.ts @@ -1,6 +1,6 @@ import { FetchError, FetchErrorDetails, JsonValue } from '../../engine' -const validJsonJsonContentTypes: string[] = [ +const validJsonContentTypes: string[] = [ 'application/json', 'application/geo+json', 'application/vnd.geo+json', @@ -84,7 +84,7 @@ export function fetchData( response.headers.get('Content-Type') ) - if (validJsonJsonContentTypes.includes(contentType)) { + if (validJsonContentTypes.includes(contentType)) { return await response.json() // Will throw if invalid JSON! }