Skip to content

Commit 9244269

Browse files
bglimepointwopian
andcommitted
feat: add http status code to kitsu responses (#1041)
* Add status code to the result of `Kitsu.prototype.request`. This allows looking for status codes like 206 to know it's a partial response. * refactor: replace statusCode with status --------- Co-authored-by: James Harris <3440094+wopian@users.noreply.github.com>
1 parent 3e60ed0 commit 9244269

6 files changed

Lines changed: 53 additions & 47 deletions

File tree

packages/kitsu/src/delete.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('kitsu', () => {
3535
expect(await api.delete('anime', 1, { headers: { extra: true } })).toEqual({
3636
headers: {
3737
Accept: 'application/vnd.api+json'
38-
}
38+
},
39+
status: 200
3940
})
4041
})
4142

@@ -51,7 +52,7 @@ describe('kitsu', () => {
5152
})
5253
return [ 200 ]
5354
})
54-
await expect(await api.delete('post', 1)).toBeUndefined()
55+
await expect(await api.delete('post', 1)).toEqual({ status: 200 })
5556
})
5657

5758
it('handles nested routes', async () => {
@@ -66,7 +67,7 @@ describe('kitsu', () => {
6667
})
6768
return [ 200 ]
6869
})
69-
await expect(await api.delete('posts/1/comments', 1)).toBeUndefined()
70+
await expect(await api.delete('posts/1/comments', 1)).toEqual({ status: 200 })
7071
})
7172

7273
it('deletes multiple resources (bulk extension)', async () => {
@@ -81,7 +82,7 @@ describe('kitsu', () => {
8182
})
8283
return [ 200 ]
8384
})
84-
await expect(await api.delete('post', [ 1, 2 ])).toBeUndefined()
85+
await expect(await api.delete('post', [ 1, 2 ])).toEqual({ status: 200 })
8586
})
8687

8788
it('throws an error if ID is missing', async () => {

packages/kitsu/src/get.spec.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ describe('kitsu', () => {
5050
Accept: 'application/vnd.api+json',
5151
'Content-Type': 'application/vnd.api+json',
5252
extra: true
53-
}
53+
},
54+
status: 200
5455
})
5556
})
5657

@@ -59,63 +60,63 @@ describe('kitsu', () => {
5960
const api = new Kitsu()
6061
mock.onGet('/anime').reply(200, getCollection.jsonapi)
6162
const request = await api.get('anime')
62-
expect(request).toEqual(getCollection.kitsu)
63+
expect(request).toEqual({ ...getCollection.kitsu, status: 200 })
6364
})
6465

6566
it('fetches a single resource', async () => {
6667
expect.assertions(1)
6768
const api = new Kitsu()
6869
mock.onGet(`anime/${getSingle.jsonapi.data.id}`).reply(200, getSingle.jsonapi)
6970
const request = await api.get('anime/1')
70-
expect(request).toEqual(getSingle.kitsu)
71+
expect(request).toEqual({ ...getSingle.kitsu, status: 200 })
7172
})
7273

7374
it('fetches a relationship collection of resources', async () => {
7475
expect.assertions(1)
7576
const api = new Kitsu()
7677
mock.onGet('authors/1/anime').reply(200, getCollection.jsonapi)
7778
const request = await api.get('author/1/anime')
78-
expect(request).toEqual(getCollection.kitsu)
79+
expect(request).toEqual({ ...getCollection.kitsu, status: 200 })
7980
})
8081

8182
it('fetches a relationshop single resource', async () => {
8283
expect.assertions(1)
8384
const api = new Kitsu()
8485
mock.onGet('comments/1/anime').reply(200, getSingle.jsonapi)
8586
const request = await api.get('comment/1/anime')
86-
expect(request).toEqual(getSingle.kitsu)
87+
expect(request).toEqual({ ...getSingle.kitsu, status: 200 })
8788
})
8889

8990
it('fetches a collection of resources with includes', async () => {
9091
expect.assertions(1)
9192
const api = new Kitsu()
9293
mock.onGet('anime').reply(200, getCollectionWithIncludes.jsonapi)
9394
const request = await api.get('anime')
94-
expect(request).toEqual(getCollectionWithIncludes.kitsu)
95+
expect(request).toEqual({ ...getCollectionWithIncludes.kitsu, status: 200 })
9596
})
9697

9798
it('fetches a single resource with includes', async () => {
9899
expect.assertions(1)
99100
const api = new Kitsu()
100101
mock.onGet(`anime/${getSingleWithIncludes.jsonapi.data.id}`, { include: 'author,comments' }).reply(200, getSingleWithIncludes.jsonapi)
101102
const request = await api.get('anime/1', { params: { include: 'author,comments' } })
102-
expect(request).toEqual(getSingleWithIncludes.kitsu)
103+
expect(request).toEqual({ ...getSingleWithIncludes.kitsu, status: 200 })
103104
})
104105

105106
it('fetches a single resource with nested includes', async () => {
106107
expect.assertions(1)
107108
const api = new Kitsu()
108109
mock.onGet('anime/1').reply(200, getSingleWithNestedIncludes.jsonapi)
109110
const request = await api.get('anime/1')
110-
expect(request).toEqual(getSingleWithNestedIncludes.kitsu)
111+
expect(request).toEqual({ ...getSingleWithNestedIncludes.kitsu, status: 200 })
111112
})
112113

113114
it('fetches a single resource with a camelCase relationship include', async () => {
114115
expect.assertions(1)
115116
const api = new Kitsu()
116117
mock.onGet('anime/1', { params: { include: 'animeStaff' } }).reply(200, getSingleWithIncludes.jsonapi)
117118
const request = await api.get('anime/1', { params: { include: 'animeStaff' } })
118-
expect(request).toEqual(getSingleWithIncludes.kitsu)
119+
expect(request).toEqual({ ...getSingleWithIncludes.kitsu, status: 200 })
119120
})
120121

121122
it('fetches :resource/:id/relationships/:relationship', async () => {
@@ -133,7 +134,7 @@ describe('kitsu', () => {
133134
const api = new Kitsu()
134135
mock.onGet('media-relationships/1/relationships/destination').reply(200, response)
135136
const request = await api.get('mediaRelationships/1/relationships/destination')
136-
expect(request).toEqual(response)
137+
expect(request).toEqual({ ...response, status: 200 })
137138
})
138139

139140
it('fetches :resource/:relationship/:subRelationship', async () => {
@@ -147,7 +148,7 @@ describe('kitsu', () => {
147148
const api = new Kitsu({ pluralize: false })
148149
mock.onGet('profile/user-accounts/me').reply(200, response)
149150
const request = await api.get('profile/userAccounts/me')
150-
expect(request).toEqual(response)
151+
expect(request).toEqual({ ...response, status: 200 })
151152
})
152153

153154
it('returns a JSON:API error object for invalid queries', async () => {
@@ -271,7 +272,8 @@ describe('kitsu', () => {
271272
}
272273
}
273274
}
274-
]
275+
],
276+
status: 200
275277
})
276278
})
277279
})

packages/kitsu/src/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ export default class Kitsu {
233233
// :resource/:id/:relationship/:subRelationship
234234
if (subRelationship) url += `/${this.resCase(subRelationship)}`
235235

236-
const { data, headers: responseHeaders } = await this.axios.get(url, {
236+
const { data, headers: responseHeaders, status } = await this.axios.get(url, {
237237
headers,
238238
params,
239239
...config.axiosOptions
240240
})
241241

242-
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
242+
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
243243
} catch (E) {
244244
throw error(E)
245245
}
@@ -292,7 +292,7 @@ export default class Kitsu {
292292
pluralTypes: this.plural
293293
})
294294
const fullURL = body?.id ? `${url}/${body.id}` : url
295-
const { data, headers: responseHeaders } = await this.axios.patch(
295+
const { data, headers: responseHeaders, status } = await this.axios.patch(
296296
fullURL,
297297
serialData,
298298
{
@@ -302,7 +302,7 @@ export default class Kitsu {
302302
}
303303
)
304304

305-
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
305+
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
306306
} catch (E) {
307307
throw error(E)
308308
}
@@ -349,7 +349,7 @@ export default class Kitsu {
349349
resourceCase: this.resCase,
350350
pluralModel: this.plural
351351
})
352-
const { data, headers: responseHeaders } = await this.axios.post(
352+
const { data, headers: responseHeaders, status } = await this.axios.post(
353353
url,
354354
serialise(resourceModel, body, 'POST', {
355355
camelCaseTypes: this.camel,
@@ -362,7 +362,7 @@ export default class Kitsu {
362362
}
363363
)
364364

365-
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
365+
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
366366
} catch (E) {
367367
throw error(E)
368368
}
@@ -405,7 +405,7 @@ export default class Kitsu {
405405
payload = { id }
406406
}
407407

408-
const { data, headers: responseHeaders } = await this.axios.delete(path, {
408+
const { data, headers: responseHeaders, status } = await this.axios.delete(path, {
409409
data: serialise(resourceModel, payload, 'DELETE', {
410410
camelCaseTypes: this.camel,
411411
pluralTypes: this.plural
@@ -415,7 +415,7 @@ export default class Kitsu {
415415
...config.axiosOptions
416416
})
417417

418-
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
418+
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
419419
} catch (E) {
420420
throw error(E)
421421
}
@@ -510,7 +510,7 @@ export default class Kitsu {
510510
async request ({ body, method, params, type, url, headers, axiosOptions }) {
511511
try {
512512
method = method?.toUpperCase() || 'GET'
513-
const { data, headers: responseHeaders } = await this.axios.request({
513+
const { data, headers: responseHeaders, status } = await this.axios.request({
514514
method,
515515
url,
516516
data: [ 'GET', 'DELETE' ].includes(method)
@@ -524,7 +524,7 @@ export default class Kitsu {
524524
...axiosOptions
525525
})
526526

527-
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
527+
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
528528
} catch (E) {
529529
throw error(E)
530530
}

packages/kitsu/src/patch.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ describe('kitsu', () => {
3939
await expect(await api.patch('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
4040
headers: {
4141
Accept: 'application/vnd.api+json'
42-
}
42+
},
43+
status: 200
4344
})
4445
})
4546

@@ -58,7 +59,7 @@ describe('kitsu', () => {
5859
})
5960
return [ 200 ]
6061
})
61-
await expect(await api.patch('post', { id: '1', content: 'Hello World' })).toBeUndefined()
62+
await expect(await api.patch('post', { id: '1', content: 'Hello World' })).toEqual({ status: 200 })
6263
})
6364

6465
it('sends bulk data in request', async () => {
@@ -88,7 +89,7 @@ describe('kitsu', () => {
8889
await expect(await api.patch('post', [
8990
{ id: '1', content: 'Hello World' },
9091
{ id: '2', content: 'Hey World' }
91-
])).toBeUndefined()
92+
])).toEqual({ status: 200 })
9293
})
9394

9495
it('throws an error if missing a JSON object body', async () => {
@@ -116,15 +117,15 @@ describe('kitsu', () => {
116117
const api = new Kitsu({ headers: { Authorization: true } })
117118
mock.onPatch(`posts/${patchSingle.jsonapi.data.id}`).reply(200, patchSingle.jsonapi)
118119
const request = await api.patch('posts', patchSingle.kitsu)
119-
expect(request).toEqual({ data: patchSingle.kitsu })
120+
expect(request).toEqual({ data: patchSingle.kitsu, status: 200 })
120121
})
121122

122123
it('handes nested routes', async () => {
123124
expect.assertions(1)
124125
const api = new Kitsu({ headers: { Authorization: true } })
125126
mock.onPatch(`something/1/relationships/posts/${patchSingle.jsonapi.data.id}`).reply(200, patchSingle.jsonapi)
126127
const request = await api.patch('something/1/relationships/posts', patchSingle.kitsu)
127-
expect(request).toEqual({ data: patchSingle.kitsu })
128+
expect(request).toEqual({ data: patchSingle.kitsu, status: 200 })
128129
})
129130
})
130131
})

packages/kitsu/src/post.spec.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('kitsu', () => {
3535
await expect(await api.post('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
3636
headers: {
3737
Accept: 'application/vnd.api+json'
38-
}
38+
},
39+
status: 200
3940
})
4041
})
4142

@@ -53,7 +54,7 @@ describe('kitsu', () => {
5354
})
5455
return [ 200 ]
5556
})
56-
await expect(await api.post('anime', { type: 'anime', name: 'Name' })).toBeUndefined()
57+
await expect(await api.post('anime', { type: 'anime', name: 'Name' })).toEqual({ status: 200 })
5758
})
5859

5960
it('handles nested routes', async () => {
@@ -70,7 +71,7 @@ describe('kitsu', () => {
7071
})
7172
return [ 200 ]
7273
})
73-
await expect(await api.post('something/1/relationships/anime', { type: 'anime', name: 'Name' })).toBeUndefined()
74+
await expect(await api.post('something/1/relationships/anime', { type: 'anime', name: 'Name' })).toEqual({ status: 200 })
7475
})
7576

7677
it('sends data in request with client-generated ID', async () => {
@@ -88,7 +89,7 @@ describe('kitsu', () => {
8889
})
8990
return [ 200 ]
9091
})
91-
await expect(await api.post('anime', { id: 123456789, type: 'anime', name: 'Name' })).toBeUndefined()
92+
await expect(await api.post('anime', { id: 123456789, type: 'anime', name: 'Name' })).toEqual({ status: 200 })
9293
})
9394

9495
it('throws an error if missing a valid JSON object body', async () => {
@@ -116,8 +117,8 @@ describe('kitsu', () => {
116117
})
117118
return [ 200 ]
118119
})
119-
await expect(await api.post('anime')).toBeUndefined()
120-
await expect(await api.post('anime', {})).toBeUndefined()
120+
await expect(await api.post('anime')).toEqual({ status: 200 })
121+
await expect(await api.post('anime', {})).toEqual({ status: 200 })
121122
})
122123

123124
it('sends data in request if given empty JSON object in array body', async () => {
@@ -129,7 +130,7 @@ describe('kitsu', () => {
129130
})
130131
return [ 200 ]
131132
})
132-
await expect(await api.post('anime', [ {} ])).toBeUndefined()
133+
await expect(await api.post('anime', [ {} ])).toEqual({ status: 200 })
133134
})
134135
})
135136
})

0 commit comments

Comments
 (0)