Skip to content

Commit bd5efb6

Browse files
authored
Merge pull request #2 from vuex-orm/release/1.0.0
Release/1.0.0
2 parents 6b62507 + db27002 commit bd5efb6

File tree

12 files changed

+1146
-2
lines changed

12 files changed

+1146
-2
lines changed

README.md

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,311 @@
22

33
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
44
[![License](https://img.shields.io/npm/l/@vuex-orm/plugin-axios.svg)](https://github.com/vuex-orm/plugin-axios/blob/master/LICENSE.md)
5+
6+
# Installation
7+
``` js
8+
import VuexORM from '@vuex-orm/core'
9+
import VuexORMAxios from '@vuex-orm/plugin-axios'
10+
import database from './database'
11+
..
12+
13+
VuexORM.use(VuexORMAxios, {
14+
database,
15+
http: {
16+
baseURL: 'https://jsonplaceholder.typicode.com',
17+
url: '/',
18+
headers: {
19+
'Accept': 'application/json',
20+
'Content-Type': 'application/json'
21+
}
22+
}
23+
})
24+
..
25+
26+
export default () => new Vuex.Store({
27+
namespaced: true,
28+
plugins: [VuexORM.install(database)]
29+
})
30+
31+
```
32+
33+
# Axios Request Config
34+
35+
``` js
36+
export const AxiosRequestConfig = {
37+
/**
38+
* Default Base URL
39+
*/
40+
baseURL: 'http://localhost:3000',
41+
42+
/**
43+
* Default URL
44+
*/
45+
url: '/',
46+
47+
/**
48+
* Default Method
49+
*/
50+
method: 'get',
51+
52+
/**
53+
* Access Token Variable
54+
*/
55+
access_token: '',
56+
57+
/**
58+
* Default Headers
59+
*/
60+
headers: {
61+
'Content-Type': 'application/json',
62+
Accept: 'application/json',
63+
},
64+
65+
/**
66+
* Default Data
67+
*/
68+
data: {},
69+
70+
/**
71+
* Default Timout
72+
*/
73+
timeout: 0,
74+
75+
/**
76+
* Default With Credentials Flag
77+
*/
78+
withCredentials: false,
79+
80+
/**
81+
* Default Response Type
82+
*/
83+
responseType: 'json',
84+
85+
/**
86+
* Default Response Encoding
87+
*/
88+
responseEncoding: 'utf8',
89+
90+
/**
91+
* Default Validate Status Method
92+
* @param {number} status
93+
*/
94+
validateStatus(status) {
95+
return status >= 200 && status < 300; // default
96+
},
97+
98+
/**
99+
* Default Max Redirects
100+
*/
101+
maxRedirects: 5,
102+
103+
/**
104+
* Default Socket Path
105+
*/
106+
socketPath: null,
107+
108+
/**
109+
* Default Proxy
110+
*/
111+
proxy: {},
112+
113+
/**
114+
* Default on Response
115+
* @param {object} response
116+
*/
117+
onResponse(response) {
118+
return response.data;
119+
},
120+
121+
/**
122+
* On 401 Unauthorised
123+
* @param {object} error
124+
*/
125+
onUnauthorised(error) {
126+
//
127+
},
128+
129+
/**
130+
* On 404 Not Found
131+
* @param {object} error
132+
*/
133+
onNotFound(error) {
134+
//
135+
},
136+
137+
/**
138+
* On 500 Server Error
139+
* @param {object} error
140+
*/
141+
onServerError(error) {
142+
//
143+
},
144+
145+
/**
146+
* On Generic Error
147+
* @param {object} error
148+
*/
149+
onGenericError(error) {
150+
//
151+
},
152+
153+
/**
154+
* On Laravel Validation Error (Or 422 Error).
155+
* @param {object} error
156+
*/
157+
onValidationError(error) {
158+
//
159+
},
160+
161+
/**
162+
* Default on Error
163+
* @param {object} error
164+
*/
165+
onError(error) {
166+
switch (error.response.status) {
167+
case 401:
168+
this.onUnauthorised(error);
169+
break;
170+
case 404:
171+
this.onNotFound(error);
172+
break;
173+
case 422:
174+
this.onValidationError(error);
175+
break;
176+
case 500:
177+
this.onServerError(error);
178+
break;
179+
default:
180+
this.onGenericError(error);
181+
break;
182+
}
183+
184+
return Promise.reject(error);
185+
},
186+
};
187+
```
188+
189+
190+
# Model Methods
191+
``` js
192+
import User from '../models/User';
193+
194+
/**
195+
* @uri `/users`
196+
*/
197+
User.$fetch();
198+
199+
/**
200+
* @uri `/users/:id`
201+
*/
202+
User.$get({
203+
params: {
204+
id: 1
205+
}
206+
});
207+
208+
/**
209+
* @uri `/users`
210+
*/
211+
User.$create({
212+
data: {}
213+
});
214+
215+
/**
216+
* @uri `/users/:id`
217+
*/
218+
User.$update({
219+
params: {
220+
id: 1
221+
},
222+
data: {}
223+
});
224+
225+
/**
226+
* @uri `/users/:id`
227+
*/
228+
User.$delete({
229+
params: {
230+
id: 1
231+
}
232+
});
233+
```
234+
235+
# Model Config
236+
237+
> Default Model
238+
239+
``` js
240+
import { Model } from '@vuex-orm/core'
241+
242+
export default class Post extends Model {
243+
static entity = 'posts'
244+
245+
static fields () {
246+
return {
247+
id: this.increment(),
248+
title: this.string('')
249+
}
250+
}
251+
}
252+
```
253+
254+
> Edited Model
255+
256+
``` js
257+
import { Model } from '@vuex-orm/core'
258+
259+
export default class Post extends Model {
260+
static entity = 'posts'
261+
262+
static fields () {
263+
return {
264+
id: this.increment(),
265+
title: this.string('')
266+
}
267+
}
268+
269+
static methodConf = {
270+
http: {
271+
url: '/post'
272+
},
273+
methods: {
274+
$fetch: {
275+
name: 'fetch',
276+
http: {
277+
url: '',
278+
method: 'get',
279+
},
280+
},
281+
$get: {
282+
name: 'get',
283+
http: {
284+
url: '/:id',
285+
method: 'get',
286+
},
287+
},
288+
$create: {
289+
name: 'create',
290+
http: {
291+
url: '',
292+
method: 'post',
293+
},
294+
},
295+
$update: {
296+
name: 'update',
297+
http: {
298+
url: '/:id',
299+
method: 'put',
300+
},
301+
},
302+
$delete: {
303+
name: 'delete',
304+
http: {
305+
url: '/:id',
306+
method: 'delete',
307+
},
308+
},
309+
},
310+
}
311+
}
312+
```

dist/index.js

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/Action.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import _ from 'lodash';
2+
import Context from '../common/context';
3+
import { ModuleConfig, ModelConfig } from '../support/interfaces';
4+
5+
export default class Action {
6+
/**
7+
* Transform Module to include ModuleConfig
8+
* @param {object} model
9+
*/
10+
static transformModule(module) {
11+
return _.merge({}, ModuleConfig, module);
12+
}
13+
14+
/**
15+
* Transform Model to include ModelConfig
16+
* @param {object} model
17+
*/
18+
static transformModel(model) {
19+
const context = Context.getInstance();
20+
ModelConfig.http = _.merge({}, ModelConfig.http, context.options.http);
21+
model.methodConf = _.merge({}, ModelConfig, model.methodConf);
22+
model.methodConf.http.url = (model.methodConf.http.url === '/') ? `/${model.entity}` : model.methodConf.http.url;
23+
24+
/**
25+
* Add Model Interface to each model
26+
*/
27+
model.getFields = () => {
28+
if (!model.cachedFields) {
29+
model.cachedFields = _.merge({}, {
30+
$id: model.attr(undefined),
31+
$isUpdating: model.boolean(false),
32+
$updateErrors: model.attr([]),
33+
$isDeleting: model.boolean(false),
34+
$deleteErrors: model.attr([]),
35+
}, model.fields())
36+
}
37+
38+
return model.cachedFields;
39+
};
40+
41+
return model;
42+
}
43+
44+
/**
45+
* Transform Params and Return Endpoint
46+
* @param {string} type
47+
* @param {object} model
48+
* @param {object} config
49+
*/
50+
static transformParams (type, model, config = {}) {
51+
let endpoint = `${model.methodConf.http.url}${model.methodConf.methods[type].http.url}`;
52+
if (config.params) _.forOwn(config.params, (value, param) => { endpoint = endpoint.replace(`:${param}`, value); });
53+
if (config.query) endpoint += `?${Object.keys(config.query).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(config.query[k])}`).join('&')}`;
54+
return endpoint;
55+
}
56+
}

0 commit comments

Comments
 (0)