Skip to content

Commit

Permalink
Merge pull request #19 from bmartel/master
Browse files Browse the repository at this point in the history
allow axios instance to be provided to plugin
  • Loading branch information
KaniRobinson authored Mar 15, 2019
2 parents 94ecbe9 + 1792259 commit 70cc314
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 6,678 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"@babel/preset-env"
],
"plugins": [
"@babel/transform-runtime"
"@babel/plugin-transform-runtime"
]
}
}
13 changes: 3 additions & 10 deletions dist/index.js

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vuex-orm/plugin-axios",
"version": "0.4.0",
"version": "0.5.0",
"description": "Vuex-ORM Plugin to sync the data against a RESTful API.",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -29,20 +29,18 @@
}
},
"dependencies": {
"@vuex-orm/core": "^0.30.0",
"@vuex-orm/core": "^0.31.6",
"axios": "^0.18.0",
"lodash": "^4.17.11"
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/core": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.3.1",
"babel-core": "7.0.0-bridge.0",
"@babel/runtime": "^7.1.5",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"babel-jest": "^23.6.0",
"babel-loader": "^8.0.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.0",
"eslint": "^5.4.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
Expand Down
27 changes: 15 additions & 12 deletions src/actions/Action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import _ from 'lodash';
import forEach from 'lodash/forEach';
import has from 'lodash/has';
import map from 'lodash/map';
import merge from 'lodash/merge';
import Context from '../common/context';
import { ModuleConfig, ModelConfig } from '../support/interfaces';

Expand All @@ -8,7 +11,7 @@ export default class Action {
* @param {object} model
*/
static transformModule(module) {
return _.merge({}, ModuleConfig, module);
return merge({}, ModuleConfig, module);
}

/**
Expand All @@ -17,22 +20,22 @@ export default class Action {
*/
static transformModel(model) {
const context = Context.getInstance();
ModelConfig.http = _.merge({}, ModelConfig.http, context.options.http);
model.methodConf = _.merge({}, ModelConfig, model.methodConf);
ModelConfig.http = merge({}, ModelConfig.http, context.options.http);
model.methodConf = merge({}, ModelConfig, model.methodConf);
model.methodConf.http.url = (model.methodConf.http.url === '/') ? `/${model.entity}` : model.methodConf.http.url;

/**
* Add Model Interface to each model
*/
model.getFields = () => {
if (!model.cachedFields) {
model.cachedFields = _.merge({}, {
model.cachedFields = merge({}, {
$id: model.attr(undefined),
$isUpdating: model.boolean(false),
$updateErrors: model.attr([]),
$isDeleting: model.boolean(false),
$deleteErrors: model.attr([]),
}, model.fields())
}, model.fields());
}

return model.cachedFields;
Expand All @@ -47,14 +50,14 @@ export default class Action {
* @param {object} model
* @param {object} config
*/
static transformParams (type, model, config = {}) {
static transformParams(type, model, config = {}) {
let endpoint = `${model.methodConf.http.url}${model.methodConf.methods[type].http.url}`;
let params = _.map(endpoint.match(/(\/?)(\:)([A-z]*)/gm), (param) => { return param.replace('/', '') })
const params = map(endpoint.match(/(\/?)(\:)([A-z]*)/gm), param => param.replace('/', ''));

_.forEach(params, (param) => {
const paramValue = _.has(config.params, param.replace(':', '')) ? config.params[param.replace(':', '')] : ''
endpoint = endpoint.replace(param, paramValue).replace('//', '/')
})
forEach(params, (param) => {
const paramValue = has(config.params, param.replace(':', '')) ? config.params[param.replace(':', '')] : '';
endpoint = endpoint.replace(param, paramValue).replace('//', '/');
});
if (config.query) endpoint += `?${Object.keys(config.query).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(config.query[k])}`).join('&')}`;
return endpoint;
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/Update.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import merge from 'lodash/merge';
import Axios from '../orm/axios';
import Action from './Action'
import Context from '../common/context'
Expand Down Expand Up @@ -53,7 +53,7 @@ export default class Update extends Action {
static onSuccess(model, params, data) {
model.update({
where: params.params.id || data.id,
data: _.merge({}, data, {
data: merge({}, data, {
$isUpdating: false,
$updateErrors: []
})
Expand Down
7 changes: 4 additions & 3 deletions src/common/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import merge from 'lodash/merge'
import find from 'lodash/find'
import { VuexOrmPluginConfig } from '../support/interfaces';

export default class Context {
Expand All @@ -11,7 +12,7 @@ export default class Context {
*/
constructor(components, options) {
this.components = components;
this.options = _.merge({}, VuexOrmPluginConfig, options);
this.options = merge({}, VuexOrmPluginConfig, options);
this.database = options.database;

if (!options.database) {
Expand Down Expand Up @@ -43,7 +44,7 @@ export default class Context {
* @param {object} state
*/
getModelFromState(state) {
return _.find(this.database.entities, {
return find(this.database.entities, {
name: state.$name
}).model;
}
Expand Down
9 changes: 5 additions & 4 deletions src/orm/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import axios from 'axios';

export default class Axios {
constructor(http) {
this.instance = axios.create(http);
this.instance = http.axios || axios.create(http);
this.setAuthentication(http.access_token);

this.instance.interceptors.response.use(
response => http.onResponse(response),
error => http.onError(error),
response => http.onResponse(response, this.instance),
error => http.onError(error, this.instance),
);

return this.instance;
}

setAuthentication(token) {
if (!token) return;
const isFunction = typeof token === "function";
const isFunction = typeof token

This comment has been minimized.

Copy link
@russsiq

russsiq Mar 30, 2019

syntax error

"function";
const tokenStr = isFunction ? token() : token;

this.instance.defaults.headers.common['Authorization'] = `Bearer ${tokenStr}`;
Expand Down
6 changes: 6 additions & 0 deletions src/support/interfaces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Database } from '@vuex-orm/core';

export const AxiosRequestConfig = {
/**
* Default create new axios instance, provide
* option to pass an existing instance through.
*/
axios: undefined,

/**
* Default Base URL
*/
Expand Down
3 changes: 2 additions & 1 deletion src/vuex-orm-axios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import map from 'lodash/map';
import Context from './common/context';
import Action from './actions/Action'
import Fetch from './actions/Fetch'
Expand Down Expand Up @@ -41,7 +42,7 @@ export default class VuexOrmAxios {
/**
* Transform Model and Modules
*/
_.map(context.database.entities, entity => {
map(context.database.entities, entity => {
entity.module = Action.transformModule(entity.module);
entity.model = Action.transformModel(entity.model);
return entity;
Expand Down
Loading

0 comments on commit 70cc314

Please sign in to comment.