Skip to content

Commit b3e2624

Browse files
author
wopian
committed
feat: return header object when present
closes #575
1 parent b543989 commit b3e2624

8 files changed

Lines changed: 76 additions & 36 deletions

File tree

packages/kitsu/MIGRATING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ The `headers` parameter has been merged with the existing `config` parameter
4545

4646
`Kitsu.request(config?, headers?)` is now `Kitsu.request(config? { headers? })`
4747

48+
### Response Changes
49+
50+
1. The header object is now returned in requests when headers are returned by the API. You can now do the following for all requests:
51+
52+
`const { data, headers } = await api.get('products')`
53+
54+
2. `api.self` now returns data within a `data` object to support the header object without conflicting with attribute names:
55+
56+
`await api.self()` now returns `{ data: { name: 'wopian' }, headers?: {} }` instead of `{ name: 'wopian' }`
57+
4858
### Serialisation Changes
4959

5060
This change does not affect `Kitsu.get`.

packages/kitsu/src/delete.spec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ afterEach(() => {
1010

1111
describe('kitsu', () => {
1212
describe('delete', () => {
13-
it('sends headers', done => {
14-
expect.assertions(1)
13+
it('sends and recieves headers', async () => {
14+
expect.assertions(2)
1515
const api = new Kitsu({ headers: { Authorization: true } })
1616
mock.onDelete('/anime/1').reply(config => {
1717
expect(config.headers).toEqual({
@@ -20,12 +20,15 @@ describe('kitsu', () => {
2020
Authorization: true,
2121
extra: true
2222
})
23-
return [ 200 ]
23+
return [ 200, undefined, {
24+
Accept: 'application/vnd.api+json'
25+
} ]
2426
})
25-
api.delete('anime', 1, { headers: { extra: true } }).catch(err => {
26-
done.fail(err)
27+
expect(await api.delete('anime', 1, { headers: { extra: true } })).toEqual({
28+
headers: {
29+
Accept: 'application/vnd.api+json'
30+
}
2731
})
28-
done()
2932
})
3033

3134
it('sends data in request', async () => {

packages/kitsu/src/get.spec.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ afterEach(() => {
1818

1919
describe('kitsu', () => {
2020
describe('get', () => {
21-
it('sends headers', async () => {
21+
it('sends and recieves headers', async () => {
2222
expect.assertions(2)
2323
const api = new Kitsu({ headers: { init: true } })
2424
mock.onGet('/anime').reply(config => {
@@ -28,10 +28,21 @@ describe('kitsu', () => {
2828
init: true,
2929
extra: true
3030
})
31-
return [ 200, { data: [] } ]
31+
return [ 200, { data: [] }, {
32+
Accept: 'application/vnd.api+json',
33+
'Content-Type': 'application/vnd.api+json',
34+
extra: true
35+
} ]
3236
})
33-
await expect(await api.get('anime', { headers: { extra: true } })).toEqual({
34-
data: []
37+
const response = await api.get('anime', { headers: { extra: true } })
38+
console.log(response)
39+
await expect(await response).toEqual({
40+
data: [],
41+
headers: {
42+
Accept: 'application/vnd.api+json',
43+
'Content-Type': 'application/vnd.api+json',
44+
extra: true
45+
}
3546
})
3647
})
3748

packages/kitsu/src/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ export default class Kitsu {
225225
// :resource/:id/:relationship/:subRelationship
226226
if (subRelationship) url += `/${this.resCase(subRelationship)}`
227227

228-
const { data } = await this.axios.get(url, {
228+
const { data, headers: responseHeaders } = await this.axios.get(url, {
229229
headers,
230230
params,
231231
paramsSerializer: /* istanbul ignore next */ p => query(p)
232232
})
233233

234-
return deserialise(data)
234+
return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
235235
} catch (E) {
236236
throw error(E)
237237
}
@@ -283,7 +283,7 @@ export default class Kitsu {
283283
pluralTypes: this.plural
284284
})
285285
const fullURL = body?.id ? `${url}/${body.id}` : url
286-
const { data } = await this.axios.patch(
286+
const { data, headers: responseHeaders } = await this.axios.patch(
287287
fullURL,
288288
serialData,
289289
{
@@ -293,7 +293,7 @@ export default class Kitsu {
293293
}
294294
)
295295

296-
return deserialise(data)
296+
return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
297297
} catch (E) {
298298
throw error(E)
299299
}
@@ -335,7 +335,7 @@ export default class Kitsu {
335335
resourceCase: this.resCase,
336336
pluralModel: this.plural
337337
})
338-
const { data } = await this.axios.post(
338+
const { data, headers: responseHeaders } = await this.axios.post(
339339
url,
340340
serialise(resourceModel, body, 'POST', {
341341
camelCaseTypes: this.camel,
@@ -348,7 +348,7 @@ export default class Kitsu {
348348
}
349349
)
350350

351-
return deserialise(data)
351+
return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
352352
} catch (E) {
353353
throw error(E)
354354
}
@@ -388,7 +388,7 @@ export default class Kitsu {
388388
payload = { id }
389389
}
390390

391-
const { data } = await this.axios.delete(path, {
391+
const { data, headers: responseHeaders } = await this.axios.delete(path, {
392392
data: serialise(resourceModel, payload, 'DELETE', {
393393
camelCaseTypes: this.camel,
394394
pluralTypes: this.plural
@@ -398,7 +398,7 @@ export default class Kitsu {
398398
paramsSerializer: /* istanbul ignore next */ p => query(p)
399399
})
400400

401-
return data
401+
return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
402402
} catch (E) {
403403
throw error(E)
404404
}
@@ -430,7 +430,7 @@ export default class Kitsu {
430430
const headers = merge(this.headers, config.headers)
431431
const params = merge(config.params, { filter: { self: true } })
432432
const res = await this.get('users', merge({ headers }, { params }))
433-
return res.data[0]
433+
return res.headers ? merge({ data: res.data[0] }, { headers: res.headers }) : { data: res.data[0] }
434434
} catch (E) {
435435
throw error(E)
436436
}
@@ -491,7 +491,7 @@ export default class Kitsu {
491491
async request ({ body, method, params, type, url, headers }) {
492492
try {
493493
method = method?.toUpperCase() || 'GET'
494-
const { data } = await this.axios.request({
494+
const { data, headers: responseHeaders } = await this.axios.request({
495495
method,
496496
url,
497497
data: [ 'GET', 'DELETE' ].includes(method)
@@ -505,7 +505,7 @@ export default class Kitsu {
505505
paramsSerializer: /* istanbul ignore next */ p => query(p)
506506
})
507507

508-
return deserialise(data)
508+
return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
509509
} catch (E) {
510510
throw error(E)
511511
}

packages/kitsu/src/patch.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ afterEach(() => {
1414

1515
describe('kitsu', () => {
1616
describe('patch', () => {
17-
it('sends headers', async () => {
17+
it('sends and receieves headers', async () => {
1818
expect.assertions(2)
1919
const api = new Kitsu({ headers: { Authorization: true } })
2020
mock.onPatch('/anime/1').reply(config => {
@@ -24,9 +24,15 @@ describe('kitsu', () => {
2424
Authorization: true,
2525
extra: true
2626
})
27-
return [ 200 ]
27+
return [ 200, undefined, {
28+
Accept: 'application/vnd.api+json'
29+
} ]
30+
})
31+
await expect(await api.patch('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
32+
headers: {
33+
Accept: 'application/vnd.api+json'
34+
}
2835
})
29-
await expect(await api.patch('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toBeUndefined()
3036
})
3137

3238
it('sends data in request', async () => {

packages/kitsu/src/post.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ afterEach(() => {
1010

1111
describe('kitsu', () => {
1212
describe('post', () => {
13-
it('sends headers', async () => {
13+
it('sends and recieves headers', async () => {
1414
expect.assertions(2)
1515
const api = new Kitsu({ headers: { Authorization: true } })
1616
mock.onPost('/anime').reply(config => {
@@ -20,9 +20,15 @@ describe('kitsu', () => {
2020
Authorization: true,
2121
extra: true
2222
})
23-
return [ 200 ]
23+
return [ 200, undefined, {
24+
Accept: 'application/vnd.api+json'
25+
} ]
26+
})
27+
await expect(await api.post('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
28+
headers: {
29+
Accept: 'application/vnd.api+json'
30+
}
2431
})
25-
await expect(await api.post('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toBeUndefined()
2632
})
2733

2834
it('sends data in request', async () => {

packages/kitsu/src/request.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ afterEach(() => {
2929

3030
describe('kitsu', () => {
3131
describe('request', () => {
32-
it('sends headers', async () => {
32+
it('sends and receives headers', async () => {
3333
expect.assertions(2)
3434
const api = new Kitsu({ headers: { Authorization: true } })
3535
mock.onGet('/users').reply(config => {
@@ -39,14 +39,14 @@ describe('kitsu', () => {
3939
Authorization: true,
4040
extra: true
4141
})
42-
return [ 200, { data: [] } ]
42+
return [ 200, { data: [] }, { Accept: 'application/vnd.api+json' } ]
4343
})
4444
await expect(await api.request({
4545
method: 'GET',
4646
url: 'users',
4747
model: 'users',
4848
headers: { extra: true }
49-
})).toEqual({ data: [] })
49+
})).toEqual({ data: [], headers: { Accept: 'application/vnd.api+json' } })
5050
})
5151

5252
it('sends parameters', async () => {

packages/kitsu/src/self.spec.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ afterEach(() => {
1313

1414
describe('kitsu', () => {
1515
describe('self', () => {
16-
it('sends headers', async () => {
16+
it('sends and recieves headers', async () => {
1717
expect.assertions(2)
1818
const api = new Kitsu({ headers: { Authorization: true } })
1919
mock.onGet('/users', { filter: { self: true } }).reply(config => {
@@ -23,9 +23,11 @@ describe('kitsu', () => {
2323
Authorization: true,
2424
extra: true
2525
})
26-
return [ 200, { data: [] } ]
26+
return [ 200, { data: [] }, { Accept: 'application/vnd.api+json' } ]
27+
})
28+
await expect(await api.self({ headers: { extra: true } })).toEqual({
29+
headers: { Accept: 'application/vnd.api+json' }
2730
})
28-
await expect(await api.self({ headers: { extra: true } })).toBeUndefined()
2931
})
3032

3133
it('fetches the authenticated user', async () => {
@@ -41,9 +43,11 @@ describe('kitsu', () => {
4143
]
4244
})
4345
expect(await api.self()).toEqual({
44-
id: '1',
45-
type: 'users',
46-
name: 'John'
46+
data: {
47+
id: '1',
48+
type: 'users',
49+
name: 'John'
50+
}
4751
})
4852
})
4953

0 commit comments

Comments
 (0)