Skip to content

Commit bd3aad1

Browse files
authored
Merge pull request #41 from MatthewKennedy/feature/widen-scope-of-json
Allow for 'application/vnd.api+json' response.
2 parents 4c5455a + db4f691 commit bd3aad1

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

__tests__/fetch_request.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('header handling', () => {
9191
test('json', async () => {
9292
const jsonRequest = new FetchRequest("get", "localhost", { responseKind: 'json' })
9393
expect(jsonRequest.fetchOptions.headers)
94-
.toStrictEqual({...defaultHeaders, 'Accept' : 'application/json'})
94+
.toStrictEqual({...defaultHeaders, 'Accept' : 'application/json, application/vnd.api+json'})
9595
})
9696
test('turbo-stream', async () => {
9797
const turboRequest = new FetchRequest("get", "localhost", { responseKind: 'turbo-stream' })
@@ -235,4 +235,4 @@ describe('query params are parsed', () => {
235235
const emptyQueryRequest = new FetchRequest("get", "localhost/test")
236236
expect(emptyQueryRequest.url).toBe("localhost/test")
237237
})
238-
})
238+
})

__tests__/fetch_response.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,27 @@ describe('body accessors', () => {
5252
expect({ json: 'body' }).toStrictEqual(await testResponse.json)
5353
})
5454
test('rejects on invalid content-type', async () => {
55-
const mockResponse = new Response("<h1>hi</h1>", { status: 200, headers: new Headers({'Content-Type': 'text/plain'}) })
55+
const mockResponse = new Response("<h1>hi</h1>", { status: 200, headers: new Headers({'Content-Type': 'text/json'}) })
5656
const testResponse = new FetchResponse(mockResponse)
5757

5858
expect(testResponse.json).rejects.toBeInstanceOf(Error)
5959
})
6060
})
61+
describe('application/vnd.api+json', () => {
62+
test('works multiple times', async () => {
63+
const mockResponse = new Response(JSON.stringify({ json: 'body' }), { status: 200, headers: new Headers({'Content-Type': 'application/vnd.api+json'}) })
64+
const testResponse = new FetchResponse(mockResponse)
65+
66+
expect({ json: 'body' }).toStrictEqual(await testResponse.json)
67+
expect({ json: 'body' }).toStrictEqual(await testResponse.json)
68+
})
69+
test('rejects on invalid content-type', async () => {
70+
const mockResponse = new Response("<h1>hi</h1>", { status: 200, headers: new Headers({'Content-Type': 'application/plain'}) })
71+
const testResponse = new FetchResponse(mockResponse)
72+
73+
expect(testResponse.json).rejects.toBeInstanceOf(Error)
74+
})
75+
})
6176
describe('turbostream', () => {
6277
const mockTurboStreamMessage = `
6378
<turbo-stream action="append" target="mock_collection"><template>

src/fetch_request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class FetchRequest {
8484
case 'turbo-stream':
8585
return 'text/vnd.turbo-stream.html, text/html, application/xhtml+xml'
8686
case 'json':
87-
return 'application/json'
87+
return 'application/json, application/vnd.api+json'
8888
default:
8989
return '*/*'
9090
}

src/fetch_response.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class FetchResponse {
4646
}
4747

4848
get json () {
49-
if (this.contentType.match(/^application\/json/)) {
49+
if (this.contentType.match(/^application\/.*json$/)) {
5050
return this.responseJson || (this.responseJson = this.response.json())
5151
}
5252

0 commit comments

Comments
 (0)