This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from aarondfrancis/2.0
2.0
- Loading branch information
Showing
12 changed files
with
723 additions
and
17,739 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
var _ = require('lodash'); | ||
|
||
module.exports = { | ||
// Names of any attributes that you want to be reactive | ||
attributes: [], | ||
|
||
http: { | ||
baseRoute: null, | ||
|
||
// The _minimum_ amount of time that a successful ajax | ||
// request should take. If the request fails it will | ||
// return immediately, but if it's successful it won't | ||
// return until `takeAtLeast` milliseconds have passed | ||
takeAtLeast: 0, | ||
|
||
// Return an axios request configuration object | ||
// https://github.com/axios/axios#request-config | ||
axios(configuration, action, definition, runtimeArgs) { | ||
return configuration; | ||
}, | ||
|
||
// Turn an axios response into the model data | ||
getDataFromResponse(response) { | ||
return response.data.data; | ||
}, | ||
|
||
// Where you want your errors to live. You can use a | ||
// dot delimited path or put them at the top level | ||
errorKey: 'http.errors', | ||
|
||
// Determine whether an error response is a model | ||
// validation error. 422 is the correct status code, | ||
// so if you use Laravel, no need to update this. | ||
isValidationError (error) { | ||
return _.get(error, 'response.status') === 422; | ||
}, | ||
|
||
// The error object should have the field names | ||
// as the keys and an array of errors as the | ||
// values. This default is set for Laravel 5.5. | ||
getErrorsFromResponse(response) { | ||
return response.data.errors; | ||
}, | ||
|
||
// Base defaults for every action | ||
actionDefaults: { | ||
// Apply the response data to the model | ||
apply: false, | ||
|
||
// The server validates this request and could | ||
// return a validation error in response | ||
validation: true, | ||
|
||
// Modify what data we should send to the server | ||
data: { | ||
// only: [], | ||
// with: [], | ||
// without: [], | ||
// custom: function(data, definition) { | ||
// return data; | ||
// } | ||
} | ||
}, | ||
|
||
// Default HTTP Actions that every model gets. You can | ||
// set an action to `false` to disable it in a model | ||
actions: { | ||
index: { | ||
method: 'GET', | ||
route: '', | ||
data: { | ||
only: [] | ||
} | ||
}, | ||
store: { | ||
method: 'POST', | ||
route: '', | ||
}, | ||
fetch: { | ||
method: 'GET', | ||
route: '{id}', | ||
apply: true, | ||
data: { | ||
only: [] | ||
} | ||
}, | ||
update: { | ||
method: 'PUT', | ||
route: '{id}', | ||
apply: true, | ||
}, | ||
destroy: { | ||
method: 'DELETE', | ||
route: '{id}', | ||
data: { | ||
only: [] | ||
} | ||
} | ||
}, | ||
}, | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
var _ = require('lodash'); | ||
var Vue = require('vue'); | ||
|
||
module.exports = class Errors { | ||
|
||
constructor(errors) { | ||
this.set(errors); | ||
} | ||
|
||
set (errors) { | ||
if (_.isEmpty(errors)) { | ||
errors = {}; | ||
} | ||
|
||
if (!_.isPlainObject(errors)) { | ||
throw new Error('ModelErrors data must be a plain object'); | ||
} | ||
|
||
for (const [field, messages] of Object.entries(errors)) { | ||
errors[field] = _.castArray(messages); | ||
} | ||
|
||
Vue.set(this, 'errors', errors); | ||
} | ||
|
||
all() { | ||
return this.errors; | ||
} | ||
|
||
flat() { | ||
var flat = []; | ||
|
||
for (const [field, messages] of Object.entries(this.errors)) { | ||
for (var i = 0; i < messages.length; i++) { | ||
flat.push({ | ||
field: field, | ||
message: messages[i] | ||
}); | ||
} | ||
} | ||
|
||
return flat; | ||
} | ||
|
||
any() { | ||
return this.flat().length > 0; | ||
} | ||
|
||
has(field) { | ||
return !!_.get(this.errors, field, []).length; | ||
} | ||
|
||
get (field) { | ||
return _.get(this.errors, field, []); | ||
} | ||
|
||
first(field) { | ||
return _.head(this.get(field)); | ||
} | ||
|
||
clear(field) { | ||
if (_.isUndefined(field)) { | ||
this.errors = {}; | ||
return; | ||
} | ||
|
||
this.errors[field] = []; | ||
} | ||
|
||
add(field, message) { | ||
return this.push(field, message); | ||
} | ||
|
||
push(field, message) { | ||
Vue.set(this.errors, field, this.get(field).concat(_.castArray(message))); | ||
} | ||
|
||
} |
Oops, something went wrong.