Skip to content

Commit

Permalink
feat: add http status code to kitsu responses (#1041)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bglimepoint and wopian committed Jan 3, 2025
1 parent 3e60ed0 commit 9244269
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
9 changes: 5 additions & 4 deletions packages/kitsu/src/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('kitsu', () => {
expect(await api.delete('anime', 1, { headers: { extra: true } })).toEqual({
headers: {
Accept: 'application/vnd.api+json'
}
},
status: 200
})
})

Expand All @@ -51,7 +52,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.delete('post', 1)).toBeUndefined()
await expect(await api.delete('post', 1)).toEqual({ status: 200 })
})

it('handles nested routes', async () => {
Expand All @@ -66,7 +67,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.delete('posts/1/comments', 1)).toBeUndefined()
await expect(await api.delete('posts/1/comments', 1)).toEqual({ status: 200 })
})

it('deletes multiple resources (bulk extension)', async () => {
Expand All @@ -81,7 +82,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.delete('post', [ 1, 2 ])).toBeUndefined()
await expect(await api.delete('post', [ 1, 2 ])).toEqual({ status: 200 })
})

it('throws an error if ID is missing', async () => {
Expand Down
26 changes: 14 additions & 12 deletions packages/kitsu/src/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ describe('kitsu', () => {
Accept: 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json',
extra: true
}
},
status: 200
})
})

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

it('fetches a single resource', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet(`anime/${getSingle.jsonapi.data.id}`).reply(200, getSingle.jsonapi)
const request = await api.get('anime/1')
expect(request).toEqual(getSingle.kitsu)
expect(request).toEqual({ ...getSingle.kitsu, status: 200 })
})

it('fetches a relationship collection of resources', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet('authors/1/anime').reply(200, getCollection.jsonapi)
const request = await api.get('author/1/anime')
expect(request).toEqual(getCollection.kitsu)
expect(request).toEqual({ ...getCollection.kitsu, status: 200 })
})

it('fetches a relationshop single resource', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet('comments/1/anime').reply(200, getSingle.jsonapi)
const request = await api.get('comment/1/anime')
expect(request).toEqual(getSingle.kitsu)
expect(request).toEqual({ ...getSingle.kitsu, status: 200 })
})

it('fetches a collection of resources with includes', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet('anime').reply(200, getCollectionWithIncludes.jsonapi)
const request = await api.get('anime')
expect(request).toEqual(getCollectionWithIncludes.kitsu)
expect(request).toEqual({ ...getCollectionWithIncludes.kitsu, status: 200 })
})

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

it('fetches a single resource with nested includes', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet('anime/1').reply(200, getSingleWithNestedIncludes.jsonapi)
const request = await api.get('anime/1')
expect(request).toEqual(getSingleWithNestedIncludes.kitsu)
expect(request).toEqual({ ...getSingleWithNestedIncludes.kitsu, status: 200 })
})

it('fetches a single resource with a camelCase relationship include', async () => {
expect.assertions(1)
const api = new Kitsu()
mock.onGet('anime/1', { params: { include: 'animeStaff' } }).reply(200, getSingleWithIncludes.jsonapi)
const request = await api.get('anime/1', { params: { include: 'animeStaff' } })
expect(request).toEqual(getSingleWithIncludes.kitsu)
expect(request).toEqual({ ...getSingleWithIncludes.kitsu, status: 200 })
})

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

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

it('returns a JSON:API error object for invalid queries', async () => {
Expand Down Expand Up @@ -271,7 +272,8 @@ describe('kitsu', () => {
}
}
}
]
],
status: 200
})
})
})
Expand Down
20 changes: 10 additions & 10 deletions packages/kitsu/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ export default class Kitsu {
// :resource/:id/:relationship/:subRelationship
if (subRelationship) url += `/${this.resCase(subRelationship)}`

const { data, headers: responseHeaders } = await this.axios.get(url, {
const { data, headers: responseHeaders, status } = await this.axios.get(url, {
headers,
params,
...config.axiosOptions
})

return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -292,7 +292,7 @@ export default class Kitsu {
pluralTypes: this.plural
})
const fullURL = body?.id ? `${url}/${body.id}` : url
const { data, headers: responseHeaders } = await this.axios.patch(
const { data, headers: responseHeaders, status } = await this.axios.patch(
fullURL,
serialData,
{
Expand All @@ -302,7 +302,7 @@ export default class Kitsu {
}
)

return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ export default class Kitsu {
resourceCase: this.resCase,
pluralModel: this.plural
})
const { data, headers: responseHeaders } = await this.axios.post(
const { data, headers: responseHeaders, status } = await this.axios.post(
url,
serialise(resourceModel, body, 'POST', {
camelCaseTypes: this.camel,
Expand All @@ -362,7 +362,7 @@ export default class Kitsu {
}
)

return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -405,7 +405,7 @@ export default class Kitsu {
payload = { id }
}

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

return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -510,7 +510,7 @@ export default class Kitsu {
async request ({ body, method, params, type, url, headers, axiosOptions }) {
try {
method = method?.toUpperCase() || 'GET'
const { data, headers: responseHeaders } = await this.axios.request({
const { data, headers: responseHeaders, status } = await this.axios.request({
method,
url,
data: [ 'GET', 'DELETE' ].includes(method)
Expand All @@ -524,7 +524,7 @@ export default class Kitsu {
...axiosOptions
})

return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
return { ...deserialise(data), status, ...(responseHeaders ? { headers: responseHeaders } : {}) }
} catch (E) {
throw error(E)
}
Expand Down
11 changes: 6 additions & 5 deletions packages/kitsu/src/patch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe('kitsu', () => {
await expect(await api.patch('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
headers: {
Accept: 'application/vnd.api+json'
}
},
status: 200
})
})

Expand All @@ -58,7 +59,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.patch('post', { id: '1', content: 'Hello World' })).toBeUndefined()
await expect(await api.patch('post', { id: '1', content: 'Hello World' })).toEqual({ status: 200 })
})

it('sends bulk data in request', async () => {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('kitsu', () => {
await expect(await api.patch('post', [
{ id: '1', content: 'Hello World' },
{ id: '2', content: 'Hey World' }
])).toBeUndefined()
])).toEqual({ status: 200 })
})

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

it('handes nested routes', async () => {
expect.assertions(1)
const api = new Kitsu({ headers: { Authorization: true } })
mock.onPatch(`something/1/relationships/posts/${patchSingle.jsonapi.data.id}`).reply(200, patchSingle.jsonapi)
const request = await api.patch('something/1/relationships/posts', patchSingle.kitsu)
expect(request).toEqual({ data: patchSingle.kitsu })
expect(request).toEqual({ data: patchSingle.kitsu, status: 200 })
})
})
})
15 changes: 8 additions & 7 deletions packages/kitsu/src/post.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('kitsu', () => {
await expect(await api.post('anime', { id: '1', type: 'anime' }, { headers: { extra: true } })).toEqual({
headers: {
Accept: 'application/vnd.api+json'
}
},
status: 200
})
})

Expand All @@ -53,7 +54,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.post('anime', { type: 'anime', name: 'Name' })).toBeUndefined()
await expect(await api.post('anime', { type: 'anime', name: 'Name' })).toEqual({ status: 200 })
})

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

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

it('throws an error if missing a valid JSON object body', async () => {
Expand Down Expand Up @@ -116,8 +117,8 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.post('anime')).toBeUndefined()
await expect(await api.post('anime', {})).toBeUndefined()
await expect(await api.post('anime')).toEqual({ status: 200 })
await expect(await api.post('anime', {})).toEqual({ status: 200 })
})

it('sends data in request if given empty JSON object in array body', async () => {
Expand All @@ -129,7 +130,7 @@ describe('kitsu', () => {
})
return [ 200 ]
})
await expect(await api.post('anime', [ {} ])).toBeUndefined()
await expect(await api.post('anime', [ {} ])).toEqual({ status: 200 })
})
})
})
Loading

0 comments on commit 9244269

Please sign in to comment.