From 710561bfc9c6a7537e7c52993cb6d1a12578f8b0 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 2 Feb 2018 03:11:53 +0330 Subject: [PATCH] feat: handle endpoints.propertyName closes #46. --- lib/auth.js | 18 +++++++++--------- package.json | 1 + yarn.lock | 4 ++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/auth.js b/lib/auth.js index 1d0119fb8..e4c8c9b0d 100644 --- a/lib/auth.js +++ b/lib/auth.js @@ -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 { @@ -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 }) } @@ -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() } @@ -228,7 +228,7 @@ export default class Auth { return } - this.setState('user', data.user) + this.setState('user', data) this.setState('loggedIn', true) } diff --git a/package.json b/package.json index c92573a21..53c970046 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/yarn.lock b/yarn.lock index eb7844ba8..1bf2e2f2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"