Skip to content

Commit

Permalink
Created asyncData and fetch helpers for a universal data flow. Added …
Browse files Browse the repository at this point in the history
…CODE_OF_CONDUCT. Added license. Updated README.md
  • Loading branch information
ezypeeze committed May 12, 2018
1 parent 7afbee3 commit 6f69384
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
@@ -0,0 +1,3 @@
{
"extends": "standard"
}
46 changes: 46 additions & 0 deletions CODE_OF_CONDUCT.md
@@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at pooya@pi0.ir. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Pedro Pereira (ezypeeze)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 7 additions & 1 deletion README.md
@@ -1,2 +1,8 @@
# nuxt-neo
Universal way to take care of data flow, server or client side

> This module allows you to make a middleware API between the browser, your server and other private API's. Opinated, yet flexible, you can take care of your data flow in the same way, no matter if you are executing code on server or client side.
## Work in Progress

### License
MIT
24 changes: 7 additions & 17 deletions lib/module.js
Expand Up @@ -48,17 +48,13 @@ const DEFAULT_MODULE_OPTIONS = {
},
services: {
directory: '~/services'
}
},
helpers: true
};

module.exports = function NeoModule(moduleOptions) {
moduleOptions = _.merge(DEFAULT_MODULE_OPTIONS, moduleOptions);
const {api, services, configuration} = moduleOptions;

// Inject Configuration server middleware
if (configuration) {
this.addServerMiddleware(require('./server_middleware/configuration')(configuration));
}
const {api, services, helpers} = moduleOptions;

// Inject Services server middleware
if (services) {
Expand Down Expand Up @@ -87,15 +83,9 @@ module.exports = function NeoModule(moduleOptions) {
})
}

/**
* TODO: EXPRESS INTEGRATION (DONE)
* TODO: SERVICE CLASS (DONE)
* TODO: CONTROLLER CLASS (DONE)
* TODO: PROVIDER BOOTING (DONE)
* TODO: HTTP ERRORS EXCEPTIONS (DONE)
* TODO: MIDDLEWARE FOR ALL API, CONTROLLER AND ACTION. (DONE)
* TODO: PLUGIN TO INJECT SERVICES
* TODO: GLOBAL FUNCTIONS FOR ASYNC DATA AND FETCH - IF BROWSER: INJECT INTO WINDOW OBJECT, IF SERVER: global func()
*/
// Inject globally helpers
if (helpers) {
this.addPlugin(path.resolve(__dirname, 'plugins', 'helpers.js'))
}

};
70 changes: 70 additions & 0 deletions lib/plugins/helpers.js
@@ -0,0 +1,70 @@
/**
* Async Data helper to fetch data.
*
* @param flow
* @returns {Function}
*/
function asyncData(flow) {
return function (context) {
context.app.$api = process.server ? context.req.generateControllersTree() : context.app.$api;

switch (typeof flow) {
case 'function':
return Promise.resolve(flow(context));
case 'object':
const result = {};
return Promise.all(
Object.keys(flow).map(function (key) {
return Promise.resolve(flow[key] && flow[key](context)).then(function (value) {
result[key] = value;
});
})
).then(() => result);
default:
throw new Error('First parameters must be either a function or an object');
}
}
}

/**
* Fetch helper to fetch data.
*
* @param flow
* @returns {Function}
*/
function fetch(flow) {
return function (context) {
context.app.$api = context.req ? context.req.generateControllersTree() : context.app.$api;

switch (typeof flow) {
case 'function':
return Promise.resolve(flow(context))
.then(function (result) {
Object.keys(result || {}).forEach(function (key) {
context.store.commit(key, result[key]);
});
});
case 'object':
return Promise.all(
Object.keys(flow).map(function (key) {
Promise.resolve(flow[key] && flow[key](context)).then(function (result) {
context.store.commit(key, result);
});
})
);
default:
throw new Error('First parameters must be either a function or an object');
}
}
}


if (process.server) {
global.asyncData = asyncData;
global.fetch = fetch;
}

if (process.browser) {
window.asyncData = asyncData;
window.fetch = fetch;
}
14 changes: 3 additions & 11 deletions tests/fixtures/pages/index.vue
Expand Up @@ -11,17 +11,9 @@
<script>
export default {
name: 'index',
asyncData: async function (context) {
if (process.server) {
return {
data: await context.req.generateControllersTree().users.categories.types.allAction()
}
}
return {
data: await context.app.$api.users.categories.types.allAction()
}
},
asyncData: asyncData({
data: ({app}) => app.$api.users.categories.types.allAction()
}),
methods: {
async handleClick() {
this.data = await this.$api.users.allAction();
Expand Down

0 comments on commit 6f69384

Please sign in to comment.