Skip to content

Commit

Permalink
refactor(kitsu): self - move params and headers into a config object
Browse files Browse the repository at this point in the history
  • Loading branch information
wopian committed Jun 14, 2020
1 parent 8b49cc1 commit c55949a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions packages/kitsu/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,9 @@ export default class Kitsu {
* **Note** Requires the JSON:API server to support `filter[self]=true`
*
* @memberof Kitsu
* @param {Object} [params] JSON-API request queries
* @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)
* @param {string} [params.include] Include relationship data - [JSON:API Includes](http://jsonapi.org/format/#fetching-includes)
* @param {Object} [headers] Additional headers to send with the request
* @param {Object} [config] Additional configuration
* @param {Object} [config.params] JSON:API request queries. See [#get](#get) for documentation
* @param {Object} [config.headers] Additional headers to send with the request
* @returns {Object} JSON-parsed response
* @example <caption>Get the authenticated user's resource</caption>
* api.self()
Expand All @@ -374,9 +373,11 @@ export default class Kitsu {
* }
* })
*/
async self (params = {}, headers = {}) {
async self (config = {}) {
try {
const res = await this.get('users', Object.assign({ filter: { self: true } }, params), headers)
const headers = merge(this.headers, config.headers)
const params = merge(config.params, { filter: { self: true } })
const res = await this.get('users', merge({ headers }, { params }))
return res.data[0]
} catch (E) {
throw error(E)
Expand Down
4 changes: 2 additions & 2 deletions packages/kitsu/src/self.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('kitsu', () => {
})
return [ 200, { data: [] } ]
})
api.self(undefined, { extra: true }).catch(err => {
api.self({ headers: { extra: true } }).catch(err => {
done.fail(err)
})
done()
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('kitsu', () => {
const api = new Kitsu()
mock.onGet('/users', { filter: { self: true }, include: 'author' }).reply(400, getError.jsonapi)
try {
await api.self({ include: 'author' })
await api.self({ params: { include: 'author' } })
} catch ({ errors }) {
expect(errors).toHaveLength(1)
expect(errors[0].title).toBe('Invalid field')
Expand Down

0 comments on commit c55949a

Please sign in to comment.