Skip to content

Commit f98eef9

Browse files
committed
refactor(kitsu): merge params and headers parameters into a single config object
BREAKING CHANGE
1 parent 8d8eda3 commit f98eef9

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

packages/kitsu/src/get.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('kitsu', () => {
3030
})
3131
return [ 200, { data: [] } ]
3232
})
33-
api.get('anime', undefined, { extra: true }).catch(err => {
33+
api.get('anime', { headers: { extra: true } }).catch(err => {
3434
done.fail(err)
3535
})
3636
done()
@@ -80,7 +80,7 @@ describe('kitsu', () => {
8080
expect.assertions(1)
8181
const api = new Kitsu()
8282
mock.onGet(`anime/${getSingleWithIncludes.jsonapi.data.id}`, { include: 'author,comments' }).reply(200, getSingleWithIncludes.jsonapi)
83-
const request = await api.get('anime/1', { include: 'author,comments' })
83+
const request = await api.get('anime/1', { params: { include: 'author,comments' } })
8484
expect(request).toEqual(getSingleWithIncludes.kitsu)
8585
})
8686

@@ -97,7 +97,7 @@ describe('kitsu', () => {
9797
const api = new Kitsu()
9898
mock.onGet('articles', { include: 'author' }).reply(400, getError.jsonapi)
9999
try {
100-
await api.get('articles', { include: 'author' })
100+
await api.get('articles', { params: { include: 'author' } })
101101
} catch ({ errors }) {
102102
expect(errors).toHaveLength(1)
103103
expect(errors[0].title).toBe('Invalid field')

packages/kitsu/src/index.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,20 @@ export default class Kitsu {
119119
*
120120
* @memberof Kitsu
121121
* @param {string} model Model to fetch data from
122-
* @param {Object} [params] JSON-API request queries. Any JSON:API query parameter not mentioned below is supported out of the box.
123-
* @param {Object} [params.page] [JSON:API Pagination](http://jsonapi.org/format/#fetching-pagination). All pagination strategies are supported, even if they are not listed below.
124-
* @param {number} [params.page.limit] Number of resources to return in request (Offset-based) - **Note:** For Kitsu.io, max is `20` except on `libraryEntries` which has a max of `500`
125-
* @param {number} [params.page.offset] Number of resources to offset the dataset by (Offset-based)
126-
* @param {number} [params.page.number] Page of resources to return in request (Page-based) - **Note:** Not supported on Kitsu.io
127-
* @param {number} [params.page.size] Number of resources to return in request (Page-based and cursor-based) - **Note:** Not supported on Kitsu.io
128-
* @param {string} [params.page.before] Get the previous page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
129-
* @param {string} [params.page.after] Get the next page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
130-
* @param {Object} [params.fields] Return a sparse fieldset with only the included attributes/relationships - [JSON:API Sparse Fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets)
131-
* @param {Object} [params.filter] Filter dataset by attribute values - [JSON:API Filtering](http://jsonapi.org/format/#fetching-filtering)
132-
* @param {string} [params.sort] Sort dataset by one or more comma separated attributes (prepend `-` for descending order) - [JSON:API Sorting](http://jsonapi.org/format/#fetching-sorting)
133-
* @param {string} [params.include] Include relationship data - [JSON:API Includes](http://jsonapi.org/format/#fetching-includes)
134-
* @param {Object} [headers] Additional headers to send with the request
122+
* @param {Object} [config] Additional configuration
123+
* @param {Object} [config.headers] Additional headers to send with the request
124+
* @param {Object} [config.params] JSON:API request queries. JSON:API query parameters not listed are supported
125+
* @param {Object} [config.params.fields] Return a sparse fieldset with only the included attributes/relationships - [JSON:API Sparse Fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets)
126+
* @param {Object} [config.params.filter] Filter dataset by attribute values - [JSON:API Filtering](http://jsonapi.org/format/#fetching-filtering)
127+
* @param {string} [config.params.include] Include relationship data - [JSON:API Includes](http://jsonapi.org/format/#fetching-includes)
128+
* @param {string} [config.params.sort] Sort dataset by one or more comma separated attributes (prepend `-` for descending order) - [JSON:API Sorting](http://jsonapi.org/format/#fetching-sorting)
129+
* @param {Object} [config.params.page] [JSON:API Pagination](http://jsonapi.org/format/#fetching-pagination). All pagination strategies are supported, even if they are not listed below.
130+
* @param {number} [config.params.page.limit] Number of resources to return in request (Offset-based) - **Note:** For Kitsu.io, max is `20` except on `libraryEntries` which has a max of `500`
131+
* @param {number} [config.params.page.offset] Number of resources to offset the dataset by (Offset-based)
132+
* @param {number} [config.params.page.number] Page of resources to return in request (Page-based) - **Note:** Not supported on Kitsu.io
133+
* @param {number} [config.params.page.size] Number of resources to return in request (Page-based and cursor-based) - **Note:** Not supported on Kitsu.io
134+
* @param {string} [config.params.page.before] Get the previous page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
135+
* @param {string} [config.params.page.after] Get the next page of resources (Cursor-based) - **Note:** Not Supported on Kitsu.io
135136
* @returns {Object} JSON-parsed response
136137
* @example <caption>Getting a resource with JSON:API parameters</caption>
137138
* api.get('users', {
@@ -188,19 +189,20 @@ export default class Kitsu {
188189
* err.response
189190
* })
190191
*/
191-
async get (model, params = {}, headers = {}) {
192+
async get (model, config = {}) {
192193
try {
194+
const headers = merge(this.headers, config.headers)
195+
const params = merge({}, config.params)
193196
const [ res, id, relationship ] = model.split('/')
194197

195198
let url = this.plural(this.resCase(res))
196199
if (id) url += `/${id}`
197200
if (relationship) url += `/${this.resCase(relationship)}`
198201

199202
const { data } = await this.axios.get(url, {
203+
headers,
200204
params,
201-
/* istanbul ignore next */
202-
paramsSerializer: p => query(p),
203-
headers: Object.assign(this.headers, headers)
205+
paramsSerializer: /* istanbul ignore next */ p => query(p)
204206
})
205207

206208
return deserialise(data)

0 commit comments

Comments
 (0)