Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace merge-options with spread operator #603

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/kitsu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"dependencies": {
"axios": "^0.23.0",
"kitsu-core": "^10.0.0-alpha.16",
"merge-options": "^3.0.0",
"pluralize": "^8.0.0"
},
"size-limit": [
Expand Down Expand Up @@ -101,4 +100,4 @@
"require": "./dist/index.js"
}
}
}
}
48 changes: 24 additions & 24 deletions packages/kitsu/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { camel, deserialise, error, kebab, query, serialise, snake, splitModel } from 'kitsu-core'
import axios from 'axios'
import merge from 'merge-options'
import pluralise from 'pluralize'

/**
Expand Down Expand Up @@ -65,14 +64,15 @@ export default class Kitsu {
* @example <caption>Add or update a header's value</caption>
* api.headers['Authorization'] = 'Bearer 1234567890'
*/
this.headers = merge({ Accept: 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json' }, options.headers)
this.headers = { ...{ Accept: 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json' }, ...options.headers }

this.axios = axios.create(
merge({
this.axios = axios.create({
...{
baseURL: options.baseURL || 'https://kitsu.io/api/edge',
timeout: options.timeout || 30000
}, options.axiosOptions)
)
},
...options.axiosOptions
})

this.fetch = this.get
this.update = this.patch
Expand Down Expand Up @@ -209,8 +209,8 @@ export default class Kitsu {
*/
async get (model, config = {}) {
try {
const headers = merge(this.headers, config.headers)
const params = merge({}, config.params)
const headers = { ...this.headers, ...config.headers }
const params = { ...{}, ...config.params }
const [ res, id, relationship, subRelationship ] = model.split('/')

// :resource
Expand All @@ -231,7 +231,7 @@ export default class Kitsu {
paramsSerializer: /* istanbul ignore next */ p => query(p)
})

return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -272,8 +272,8 @@ export default class Kitsu {
*/
async patch (model, body, config = {}) {
try {
const headers = merge(this.headers, config.headers)
const params = merge({}, config.params)
const headers = { ...this.headers, ...config.headers }
const params = { ...{}, ...config.params }
const [ resourceModel, url ] = splitModel(model, {
resourceCase: this.resCase,
pluralModel: this.plural
Expand All @@ -293,7 +293,7 @@ export default class Kitsu {
}
)

return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -329,8 +329,8 @@ export default class Kitsu {
*/
async post (model, body, config = {}) {
try {
const headers = merge(this.headers, config.headers)
const params = merge({}, config.params)
const headers = { ...this.headers, ...config.headers }
const params = { ...{}, ...config.params }
const [ resourceModel, url ] = splitModel(model, {
resourceCase: this.resCase,
pluralModel: this.plural
Expand All @@ -348,7 +348,7 @@ export default class Kitsu {
}
)

return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
} catch (E) {
throw error(E)
}
Expand All @@ -371,8 +371,8 @@ export default class Kitsu {
*/
async delete (model, id, config = {}) {
try {
const headers = merge(this.headers, config.headers)
const params = merge({}, config.params)
const headers = { ...this.headers, ...config.headers }
const params = { ...{}, ...config.params }
const [ resourceModel, url ] = splitModel(model, {
resourceCase: this.resCase,
pluralModel: this.plural
Expand All @@ -398,7 +398,7 @@ export default class Kitsu {
paramsSerializer: /* istanbul ignore next */ p => query(p)
})

return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -427,10 +427,10 @@ export default class Kitsu {
*/
async self (config = {}) {
try {
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.headers ? merge({ data: res.data[0] }, { headers: res.headers }) : { data: res.data[0] }
const headers = { ...this.headers, ...config.headers }
const params = { ...config.params, ...{ filter: { self: true } } }
const res = await this.get('users', { ...{ headers }, ...{ params } })
return res.headers ? { ...{ data: res.data[0] }, ...{ headers: res.headers } } : { data: res.data[0] }
} catch (E) {
throw error(E)
}
Expand Down Expand Up @@ -500,12 +500,12 @@ export default class Kitsu {
camelCaseTypes: this.camel,
pluralTypes: this.plural
}),
headers: merge(this.headers, headers),
headers: { ...this.headers, ...headers },
params,
paramsSerializer: /* istanbul ignore next */ p => query(p)
})

return responseHeaders ? merge(deserialise(data), { headers: responseHeaders }) : deserialise(data)
return responseHeaders ? { ...deserialise(data), ...{ headers: responseHeaders } } : deserialise(data)
} catch (E) {
throw error(E)
}
Expand Down
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6547,7 +6547,7 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=

is-plain-obj@^2.0.0, is-plain-obj@^2.1.0:
is-plain-obj@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
Expand Down Expand Up @@ -7871,13 +7871,6 @@ meow@^8.0.0:
type-fest "^0.18.0"
yargs-parser "^20.2.3"

merge-options@^3.0.0:
version "3.0.4"
resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
dependencies:
is-plain-obj "^2.1.0"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down