Skip to content

Commit

Permalink
feat: handle endpoints.propertyName
Browse files Browse the repository at this point in the history
closes #46.
  • Loading branch information
Pooya Parsa committed Feb 1, 2018
1 parent 35962c8 commit 9660902
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cookie from 'cookie'
import Cookies from 'js-cookie'
import getProp from 'dotprop'
import Vue from 'vue'

export default class Auth {
Expand Down Expand Up @@ -185,16 +186,15 @@ export default class Auth {
}

async _request (name, endpoint) {
const defaults = this.options.endpoints[name]
if (!defaults) {
if (!this.options.endpoints[name]) {
return
}

const opts = Object.assign({}, this.options.endpoints[name], endpoint)

try {
const { data } = await this.$axios.request(
Object.assign({}, defaults, endpoint)
)
return data
const { data } = await this.$axios.request(opts)
return opts.propertyName ? getProp(data, opts.propertyName) : data
} catch (err) {
this._onError({ name, err, endpoint })
}
Expand All @@ -207,10 +207,10 @@ export default class Auth {
}

// Extract and set token
this.setToken(data.token)
this.setToken(data)

// Fetch User
if (this.options.fetchUserOnLogin) {
if (this.options.fetchUserOnLogin && this.options.endpoints.user) {
return this.fetchUser()
}

Expand All @@ -228,7 +228,7 @@ export default class Auth {
return
}

this.setState('user', data.user)
this.setState('user', data)
this.setState('loggedIn', true)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@nuxtjs/axios": "^5.0.0-rc.2",
"cookie": "^0.3.1",
"dotprop": "^1.0.2",
"js-cookie": "^2.2.0",
"lodash": "^4.17.4"
},
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,10 @@ dotgitignore@^1.0.3:
find-up "^2.1.0"
minimatch "^3.0.4"

dotprop@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/dotprop/-/dotprop-1.0.2.tgz#0559e898156f3dddb6be62794e5d6ef30eceb705"

duplexer@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1"
Expand Down

0 comments on commit 9660902

Please sign in to comment.