@@ -12,6 +12,10 @@ import { error } from '../error'
1212function 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 */
134138function 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 ) {
0 commit comments