Skip to content

Commit 70cc314

Browse files
authoredMar 15, 2019
Merge pull request #19 from bmartel/master
allow axios instance to be provided to plugin
2 parents 94ecbe9 + 1792259 commit 70cc314

File tree

10 files changed

+47
-6678
lines changed

10 files changed

+47
-6678
lines changed
 

‎.babelrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"@babel/preset-env"
44
],
55
"plugins": [
6-
"@babel/transform-runtime"
6+
"@babel/plugin-transform-runtime"
77
]
8-
}
8+
}

‎dist/index.js

+3-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vuex-orm/plugin-axios",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "Vuex-ORM Plugin to sync the data against a RESTful API.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -29,20 +29,18 @@
2929
}
3030
},
3131
"dependencies": {
32-
"@vuex-orm/core": "^0.30.0",
32+
"@vuex-orm/core": "^0.31.6",
3333
"axios": "^0.18.0",
3434
"lodash": "^4.17.11"
3535
},
3636
"devDependencies": {
37-
"@babel/core": "^7.1.6",
37+
"@babel/core": "^7.0.0",
38+
"@babel/plugin-transform-runtime": "^7.2.0",
39+
"@babel/preset-env": "^7.0.0",
40+
"@babel/runtime": "^7.3.1",
3841
"babel-core": "7.0.0-bridge.0",
39-
"@babel/runtime": "^7.1.5",
40-
"@babel/plugin-transform-runtime": "^7.1.0",
41-
"@babel/preset-env": "^7.1.6",
42-
"babel-jest": "^23.6.0",
43-
"babel-loader": "^8.0.4",
44-
"babel-plugin-transform-runtime": "^6.23.0",
45-
"babel-preset-env": "^1.7.0",
42+
"babel-jest": "^23.4.2",
43+
"babel-loader": "^8.0.0",
4644
"eslint": "^5.4.0",
4745
"eslint-config-airbnb": "^17.1.0",
4846
"eslint-plugin-import": "^2.14.0",

‎src/actions/Action.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import _ from 'lodash';
1+
import forEach from 'lodash/forEach';
2+
import has from 'lodash/has';
3+
import map from 'lodash/map';
4+
import merge from 'lodash/merge';
25
import Context from '../common/context';
36
import { ModuleConfig, ModelConfig } from '../support/interfaces';
47

@@ -8,7 +11,7 @@ export default class Action {
811
* @param {object} model
912
*/
1013
static transformModule(module) {
11-
return _.merge({}, ModuleConfig, module);
14+
return merge({}, ModuleConfig, module);
1215
}
1316

1417
/**
@@ -17,22 +20,22 @@ export default class Action {
1720
*/
1821
static transformModel(model) {
1922
const context = Context.getInstance();
20-
ModelConfig.http = _.merge({}, ModelConfig.http, context.options.http);
21-
model.methodConf = _.merge({}, ModelConfig, model.methodConf);
23+
ModelConfig.http = merge({}, ModelConfig.http, context.options.http);
24+
model.methodConf = merge({}, ModelConfig, model.methodConf);
2225
model.methodConf.http.url = (model.methodConf.http.url === '/') ? `/${model.entity}` : model.methodConf.http.url;
2326

2427
/**
2528
* Add Model Interface to each model
2629
*/
2730
model.getFields = () => {
2831
if (!model.cachedFields) {
29-
model.cachedFields = _.merge({}, {
32+
model.cachedFields = merge({}, {
3033
$id: model.attr(undefined),
3134
$isUpdating: model.boolean(false),
3235
$updateErrors: model.attr([]),
3336
$isDeleting: model.boolean(false),
3437
$deleteErrors: model.attr([]),
35-
}, model.fields())
38+
}, model.fields());
3639
}
3740

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

54-
_.forEach(params, (param) => {
55-
const paramValue = _.has(config.params, param.replace(':', '')) ? config.params[param.replace(':', '')] : ''
56-
endpoint = endpoint.replace(param, paramValue).replace('//', '/')
57-
})
57+
forEach(params, (param) => {
58+
const paramValue = has(config.params, param.replace(':', '')) ? config.params[param.replace(':', '')] : '';
59+
endpoint = endpoint.replace(param, paramValue).replace('//', '/');
60+
});
5861
if (config.query) endpoint += `?${Object.keys(config.query).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(config.query[k])}`).join('&')}`;
5962
return endpoint;
6063
}

‎src/actions/Update.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from 'lodash';
1+
import merge from 'lodash/merge';
22
import Axios from '../orm/axios';
33
import Action from './Action'
44
import Context from '../common/context'
@@ -53,7 +53,7 @@ export default class Update extends Action {
5353
static onSuccess(model, params, data) {
5454
model.update({
5555
where: params.params.id || data.id,
56-
data: _.merge({}, data, {
56+
data: merge({}, data, {
5757
$isUpdating: false,
5858
$updateErrors: []
5959
})

‎src/common/context.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import _ from 'lodash';
1+
import merge from 'lodash/merge'
2+
import find from 'lodash/find'
23
import { VuexOrmPluginConfig } from '../support/interfaces';
34

45
export default class Context {
@@ -11,7 +12,7 @@ export default class Context {
1112
*/
1213
constructor(components, options) {
1314
this.components = components;
14-
this.options = _.merge({}, VuexOrmPluginConfig, options);
15+
this.options = merge({}, VuexOrmPluginConfig, options);
1516
this.database = options.database;
1617

1718
if (!options.database) {
@@ -43,7 +44,7 @@ export default class Context {
4344
* @param {object} state
4445
*/
4546
getModelFromState(state) {
46-
return _.find(this.database.entities, {
47+
return find(this.database.entities, {
4748
name: state.$name
4849
}).model;
4950
}

‎src/orm/axios.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import axios from 'axios';
22

33
export default class Axios {
44
constructor(http) {
5-
this.instance = axios.create(http);
5+
this.instance = http.axios || axios.create(http);
66
this.setAuthentication(http.access_token);
77

88
this.instance.interceptors.response.use(
9-
response => http.onResponse(response),
10-
error => http.onError(error),
9+
response => http.onResponse(response, this.instance),
10+
error => http.onError(error, this.instance),
1111
);
1212

1313
return this.instance;
1414
}
1515

1616
setAuthentication(token) {
1717
if (!token) return;
18-
const isFunction = typeof token === "function";
18+
const isFunction = typeof token
Has a conversation. Original line has a conversation.
19+
"function";
1920
const tokenStr = isFunction ? token() : token;
2021

2122
this.instance.defaults.headers.common['Authorization'] = `Bearer ${tokenStr}`;

‎src/support/interfaces.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Database } from '@vuex-orm/core';
22

33
export const AxiosRequestConfig = {
4+
/**
5+
* Default create new axios instance, provide
6+
* option to pass an existing instance through.
7+
*/
8+
axios: undefined,
9+
410
/**
511
* Default Base URL
612
*/

‎src/vuex-orm-axios.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import map from 'lodash/map';
12
import Context from './common/context';
23
import Action from './actions/Action'
34
import Fetch from './actions/Fetch'
@@ -41,7 +42,7 @@ export default class VuexOrmAxios {
4142
/**
4243
* Transform Model and Modules
4344
*/
44-
_.map(context.database.entities, entity => {
45+
map(context.database.entities, entity => {
4546
entity.module = Action.transformModule(entity.module);
4647
entity.model = Action.transformModel(entity.model);
4748
return entity;

‎yarn.lock

-6,634
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.