Skip to content

Commit

Permalink
fix: support geo+json and vnd.geo+json content types
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Nov 7, 2023
1 parent 2aaee0e commit 7bc8b5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
22 changes: 22 additions & 0 deletions services/data/src/links/RestAPILink/fetchData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
}))
Expand All @@ -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')
Expand Down
9 changes: 7 additions & 2 deletions services/data/src/links/RestAPILink/fetchData.ts
Original file line number Diff line number Diff line change
@@ -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() : ''

Expand Down Expand Up @@ -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!
}

Expand Down

0 comments on commit 7bc8b5b

Please sign in to comment.