Skip to content

Commit 704c925

Browse files
committed
initial commit with client plugin
1 parent 4ae34c3 commit 704c925

File tree

9 files changed

+151
-1
lines changed

9 files changed

+151
-1
lines changed

package-lock.json

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

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
"lint": "vue-cli-service lint"
99
},
1010
"dependencies": {
11+
"axios": "^0.19.2",
1112
"core-js": "^3.6.5",
12-
"vue": "^2.6.11"
13+
"vue": "^2.6.11",
14+
"vuex": "^3.4.0"
1315
},
1416
"devDependencies": {
1517
"@vue/cli-plugin-babel": "~4.4.0",
1618
"@vue/cli-plugin-eslint": "~4.4.0",
19+
"@vue/cli-plugin-vuex": "^4.4.6",
1720
"@vue/cli-service": "~4.4.0",
1821
"@vue/eslint-config-airbnb": "^5.0.2",
1922
"babel-eslint": "^10.1.0",

src/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Vue from 'vue';
22
import App from './App.vue';
3+
import '@/plugins/httpClient';
4+
import store from './store';
35

46
Vue.config.productionTip = false;
57

68
new Vue({
9+
store,
710
render: (h) => h(App),
811
}).$mount('#app');

src/plugins/httpClient/HttpClient.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import axios from 'axios';
2+
import Vue from 'vue';
3+
import { capitalizeFirstLetter } from './helpers';
4+
5+
export default class HttpClient {
6+
constructor(baseConfig, methods) {
7+
const observableRequests = Vue.observable({ requests: [] });
8+
9+
this.client = axios.create(baseConfig);
10+
this.methodsList = methods;
11+
this.awakenRequests = observableRequests;
12+
13+
this.methodsList.forEach(({ name, config, setCustomLoader }) => {
14+
if (setCustomLoader) {
15+
Object.defineProperty(this, `isLoading${capitalizeFirstLetter(name)}`, {
16+
get: () => this.awakenRequests.requests.includes(name),
17+
});
18+
}
19+
this[name] = async () => {
20+
try {
21+
this.addToAwakenRequests(name);
22+
const result = await this.client.request(config);
23+
this.removeFromAwakenRequests(name);
24+
return result;
25+
} catch {
26+
return new Error();
27+
}
28+
};
29+
});
30+
}
31+
32+
addToAwakenRequests(name) {
33+
this.awakenRequests.requests.push(name);
34+
}
35+
36+
removeFromAwakenRequests(name) {
37+
this.awakenRequests.requests = this.awakenRequests.requests
38+
.filter((r) => r !== name);
39+
}
40+
41+
get isLoading() {
42+
return !!this.awakenRequests.requests.length;
43+
}
44+
}

src/plugins/httpClient/configs.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const baseConfig = {
2+
baseURL: 'https://jsonplaceholder.typicode.com',
3+
};
4+
5+
export const methodsList = [
6+
{
7+
name: 'getPosts',
8+
setCustomLoader: true,
9+
config: {
10+
url: '/posts',
11+
},
12+
},
13+
];

src/plugins/httpClient/helpers.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// eslint-disable-next-line import/prefer-default-export
2+
export function capitalizeFirstLetter(string) {
3+
return string[0].toUpperCase() + string.slice(1);
4+
}

src/plugins/httpClient/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Vue from 'vue';
2+
import { baseConfig, methodsList } from './configs';
3+
import httpClientPlugin from './plugin';
4+
5+
Vue.use(httpClientPlugin, { baseConfig, methodsList });

src/plugins/httpClient/plugin.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import HttpClient from './HttpClient';
2+
3+
const httpClientPlugin = {
4+
install(Vue, options) {
5+
if (!options || !options.baseConfig) {
6+
throw new Error('Please initialise plugin with baseConfig.');
7+
}
8+
if (!options.baseConfig.baseURL) {
9+
throw new Error('baseURL in baseConfig is required');
10+
}
11+
if (!options || !options.methodsList) {
12+
throw new Error('Please initialise plugin with methodsList');
13+
}
14+
if (options.methodsList.length < 1) {
15+
throw new Error('Please specify at least one method in list');
16+
}
17+
// eslint-disable-next-line no-param-reassign
18+
Vue.prototype.$httpClient = new HttpClient(options.baseConfig, options.methodsList);
19+
},
20+
};
21+
22+
export default httpClientPlugin;

src/store/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Vue from 'vue';
2+
import Vuex from 'vuex';
3+
4+
Vue.use(Vuex);
5+
6+
export default new Vuex.Store({
7+
state: {
8+
},
9+
mutations: {
10+
},
11+
actions: {
12+
async myAction() {
13+
// eslint-disable-next-line no-underscore-dangle
14+
const { data } = await this._vm.$httpClient.getPosts();
15+
console.log(data);
16+
},
17+
},
18+
modules: {
19+
},
20+
});

0 commit comments

Comments
 (0)