Skip to content

Commit 570ef11

Browse files
committed
fix(kitsu-core): throw error if type is missing during serialisation
1 parent d90350d commit 570ef11

7 files changed

Lines changed: 26 additions & 11 deletions

File tree

packages/kitsu-core/src/serialise/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { error } from '../error'
1212
function isValid (isArray, type, payload, method) {
1313
const requireID = new Error(`${method} requires an ID for the ${type} type`)
1414

15+
if (type === undefined) {
16+
throw new Error(`${method} requires a resource type`)
17+
}
18+
1519
if (isArray) {
1620
// A POST request is the only request to not require an ID in spec
1721
if (method !== 'POST' && payload.length > 0) {
@@ -21,7 +25,7 @@ function isValid (isArray, type, payload, method) {
2125
}
2226
} else {
2327
if (payload.constructor !== Object || Object.keys(payload).length === 0) {
24-
throw new Error(`${method} requires a JSON object body`)
28+
throw new Error(`${method} requires an object or array body`)
2529
}
2630
// A POST request is the only request to not require an ID in spec
2731
if (method !== 'POST' && !payload.id) {
@@ -133,6 +137,7 @@ function serialiseRootArray (type, payload, method, options) {
133137
*/
134138
function serialiseRootObject (type, payload, method, options) {
135139
isValid(false, type, payload, method)
140+
type = options.pluralTypes(options.camelCaseTypes(type))
136141
let data = { type }
137142

138143
if (payload?.id) data.id = String(payload.id)
@@ -191,9 +196,6 @@ export function serialise (type, data = {}, method = 'POST', options = {}) {
191196
if (!options.pluralTypes) options.pluralTypes = s => s
192197
// Delete relationship to-one (data: null) or to-many (data: [])
193198
if (data === null || (Array.isArray(data) && data.length === 0)) return { data }
194-
195-
type = options.pluralTypes(options.camelCaseTypes(type))
196-
197199
if (Array.isArray(data) && data?.length > 0) return serialiseRootArray(type, data, method, options)
198200
else return serialiseRootObject(type, data, method, options)
199201
} catch (E) {

packages/kitsu-core/src/serialise/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ describe('kitsu-core', () => {
236236
it('throws an error if obj is missing', () => {
237237
expect.assertions(1)
238238
expect(() => serialise('post'))
239-
.toThrowError('POST requires a JSON object body')
239+
.toThrowError('POST requires an object or array body')
240240
})
241241

242242
it('throws an error if obj is not an Object', () => {
243243
expect.assertions(1)
244244
expect(() => serialise('post', 'id: 1', 'DELETE'))
245-
.toThrowError('DELETE requires a JSON object body')
245+
.toThrowError('DELETE requires an object or array body')
246246
})
247247

248248
it('throws an error when missing ID', () => {

packages/kitsu/MIGRATING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ Unchanged from `3.x`, if there is an error thrown by `kitsu` itself, then you on
241241
```js
242242
catch (err) {
243243
err.name // 'Error'
244-
err.message // 'POST requires a JSON object body'
244+
err.message // 'POST requires an object or array body'
245245
}
246246
247247
## Migrating to `3.0.0`

packages/kitsu/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ export default class Kitsu {
189189
if (id) url += `/${id}`
190190
if (relationship) url += `/${this.resCase(relationship)}`
191191

192-
// eslint-disable-next-line no-console
193192
const { data } = await this.axios.get(url, {
194193
params,
194+
/* istanbul ignore next */
195195
paramsSerializer: p => query(p),
196196
headers: Object.assign(this.headers, headers)
197197
})
@@ -425,7 +425,6 @@ export default class Kitsu {
425425
async request ({ body, method, params, type, url }, headers = {}) {
426426
try {
427427
method = method?.toUpperCase() || 'GET'
428-
// eslint-disable-next-line no-console
429428
const { data } = await this.axios.request({
430429
method,
431430
url,
@@ -434,6 +433,7 @@ export default class Kitsu {
434433
pluralTypes: this.plural
435434
}),
436435
params,
436+
/* istanbul ignore next */
437437
paramsSerializer: p => query(p),
438438
headers: Object.assign(this.headers, headers)
439439
})

packages/kitsu/src/patch.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('kitsu', () => {
5959
try {
6060
await api.patch('posts')
6161
} catch (err) {
62-
expect(err.message).toEqual('PATCH requires a JSON object body')
62+
expect(err.message).toEqual('PATCH requires an object or array body')
6363
}
6464
})
6565

packages/kitsu/src/post.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('kitsu', () => {
9595
try {
9696
await api.post('posts')
9797
} catch (err) {
98-
expect(err.message).toEqual('POST requires a JSON object body')
98+
expect(err.message).toEqual('POST requires an object or array body')
9999
}
100100
})
101101
})

packages/kitsu/src/request.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,22 @@ describe('kitsu', () => {
142142
mock.onDelete('anime/1').reply(200, genericResponse)
143143
const request = await api.request({
144144
method: 'delete',
145+
type: 'anime',
145146
url: 'anime/1'
146147
})
147148
expect(request).toEqual(genericRequest)
148149
})
150+
151+
it('throws an error if body is missing', async () => {
152+
expect.assertions(1)
153+
const api = new Kitsu()
154+
try {
155+
await api.request({
156+
method: 'patch'
157+
})
158+
} catch (err) {
159+
expect(err.message).toEqual('PATCH requires a resource type')
160+
}
161+
})
149162
})
150163
})

0 commit comments

Comments
 (0)