Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support geo+json and vnd.geo+json content types #1360

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 'bar',
})
})

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 validJsonContentTypes: 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 (validJsonContentTypes.includes(contentType)) {
return await response.json() // Will throw if invalid JSON!
}

Expand Down
Loading