Skip to content

Commit 0a05a3a

Browse files
committed
Added Plugin and Axios Config and Built it into the Context
1 parent ccce977 commit 0a05a3a

2 files changed

Lines changed: 205 additions & 0 deletions

File tree

src/common/context.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import _ from 'lodash';
2+
import { VuexOrmPluginConfig } from '../support/interfaces';
3+
4+
export default class Context {
5+
/**
6+
* Private constructor, called by the setup method
7+
*
8+
* @constructor
9+
* @param {Components} components The Vuex-ORM Components collection
10+
* @param {Options} options The options passed to VuexORM.install
11+
*/
12+
constructor(components, options) {
13+
this.components = components;
14+
this.options = _.merge({}, VuexOrmPluginConfig, options);
15+
this.database = options.database;
16+
17+
if (!options.database) {
18+
throw new Error('database option is required to initialise!');
19+
}
20+
}
21+
22+
/**
23+
* This is called only once and creates a new instance of the Context.
24+
* @param {Components} components The Vuex-ORM Components collection
25+
* @param {Options} options The options passed to VuexORM.install
26+
* @returns {Context}
27+
*/
28+
static setup(components, options) {
29+
this.instance = new Context(components, options);
30+
return this.instance;
31+
}
32+
33+
/**
34+
* Get the singleton instance of the context.
35+
* @returns {Context}
36+
*/
37+
static getInstance() {
38+
return this.instance;
39+
}
40+
}

src/support/interfaces.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { Database } from '@vuex-orm/core';
2+
3+
export const AxiosRequestConfig = {
4+
/**
5+
* Default Base URL
6+
*/
7+
baseURL: 'http://localhost:3000',
8+
9+
/**
10+
* Default URL
11+
*/
12+
url: '/',
13+
14+
/**
15+
* Default Method
16+
*/
17+
method: 'get',
18+
19+
/**
20+
* Access Token Variable
21+
*/
22+
access_token: '',
23+
24+
/**
25+
* Default Headers
26+
*/
27+
headers: {
28+
'Content-Type': 'application/json',
29+
Accept: 'application/json',
30+
},
31+
32+
/**
33+
* Default Data
34+
*/
35+
data: {},
36+
37+
/**
38+
* Default Timout
39+
*/
40+
timeout: 0,
41+
42+
/**
43+
* Default With Credentials Flag
44+
*/
45+
withCredentials: false,
46+
47+
/**
48+
* Default Response Type
49+
*/
50+
responseType: 'json',
51+
52+
/**
53+
* Default Response Encoding
54+
*/
55+
responseEncoding: 'utf8',
56+
57+
/**
58+
* Default Validate Status Method
59+
* @param {number} status
60+
*/
61+
validateStatus(status) {
62+
return status >= 200 && status < 300; // default
63+
},
64+
65+
/**
66+
* Default Max Redirects
67+
*/
68+
maxRedirects: 5,
69+
70+
/**
71+
* Default Socket Path
72+
*/
73+
socketPath: null,
74+
75+
/**
76+
* Default Proxy
77+
*/
78+
proxy: {},
79+
80+
/**
81+
* Default on Response
82+
* @param {object} response
83+
*/
84+
onResponse(response) {
85+
return response.data;
86+
},
87+
88+
/**
89+
* On 401 Unauthorised
90+
* @param {object} error
91+
*/
92+
onUnauthorised(error) {
93+
//
94+
},
95+
96+
/**
97+
* On 404 Not Found
98+
* @param {object} error
99+
*/
100+
onNotFound(error) {
101+
//
102+
},
103+
104+
/**
105+
* On 500 Server Error
106+
* @param {object} error
107+
*/
108+
onServerError(error) {
109+
//
110+
},
111+
112+
/**
113+
* On Generic Error
114+
* @param {object} error
115+
*/
116+
onGenericError(error) {
117+
//
118+
},
119+
120+
/**
121+
* On Laravel Validation Error (Or 422 Error).
122+
* @param {object} error
123+
*/
124+
onValidationError(error) {
125+
//
126+
},
127+
128+
/**
129+
* Default on Error
130+
* @param {object} error
131+
*/
132+
onError(error) {
133+
switch (error.response.status) {
134+
case 401:
135+
this.onUnauthorised(error);
136+
break;
137+
case 404:
138+
this.onNotFound(error);
139+
break;
140+
case 422:
141+
this.onValidationError(error);
142+
break;
143+
case 500:
144+
this.onServerError(error);
145+
break;
146+
default:
147+
this.onGenericError(error);
148+
break;
149+
}
150+
151+
return Promise.reject(error);
152+
},
153+
};
154+
155+
export const VuexOrmPluginConfig = {
156+
/**
157+
* Default VuexORM Database
158+
*/
159+
database: new Database(),
160+
161+
/**
162+
* Default Axios Config
163+
*/
164+
http: AxiosRequestConfig
165+
};

0 commit comments

Comments
 (0)