Skip to content

Commit

Permalink
feat: added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LuXDAmore committed Dec 7, 2019
1 parent 5bee7dd commit 0cfd811
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 12 deletions.
4 changes: 0 additions & 4 deletions example/graphql/categories.json

This file was deleted.

4 changes: 2 additions & 2 deletions example/graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as CATEGORIES from './categories.json';
import * as USERS from './users.json';

export { CATEGORIES };
export { USERS };
16 changes: 16 additions & 0 deletions example/graphql/users.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query allUsers {
allUsers {
id
name
createdAt
email
comments {
id
text
}
posts {
id
title
}
}
}
5 changes: 5 additions & 0 deletions example/graphql/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"query": "query allUsers {\n allUsers {\n id\n name\n createdAt\n email\n comments {\n id\n text\n }\n posts {\n id\n title\n }\n }\n }",
"variables": {},
"operationName": "allUsers"
}
28 changes: 25 additions & 3 deletions example/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { resolve } from 'path';
import * as PACKAGE from '../package.json';

import { USERS } from './graphql';

const meta = [
{
once: true,
Expand All @@ -25,6 +27,29 @@ export default {
'../lib/module'
),
],
apisToFile: {
axios: {
baseURL: 'https://jsonplaceholder.typicode.com',
},
requests: [
{
endpoint: '/posts',
field: 'posts',
},
{
endpoint: '/comments',
field: 'comments',
},
// GraphQL
{
endpoint: 'https://api.graph.cool/simple/v1/ciyz901en4j590185wkmexyex',
method: 'post',
field: 'users',
pathToData: 'data.allUsers',
body: USERS,
},
],
},
srcDir: __dirname,
head: {
htmlAttrs: {
Expand All @@ -33,9 +58,6 @@ export default {
title: PACKAGE.name,
meta,
},
render: {
resourceHints: false,
},
/*
* Generate
*/
Expand Down
32 changes: 29 additions & 3 deletions example/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
<template>
<div>
Works!
</div>
<main>
<h1>NUXT Apis to file</h1>
<h2>Nuxt module to merge and transform API calls into a single file, during the build phase.. Like a payload extractor</h2>
<hr>
<section class="comments">
<h3>
<strong>Total number of comments preloaded:</strong>
</h3>
<p>
<em class="number" v-text="$store.state.comments.items.length" />
</p>
</section>
<section class="posts">
<h3>
<strong>Total number of posts preloaded:</strong>
</h3>
<p>
<em class="number" v-text="$store.state.posts.items.length" />
</p>
</section>
<section class="users">
<h3>
<strong>Total number of users preloaded:</strong>
</h3>
<p>
<em class="number" v-text="$store.state.users.items.length" />
</p>
</section>
</main>
</template>

<script>
Expand Down
68 changes: 68 additions & 0 deletions example/store/build-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const getFile = () => import(
'~/apis-to-file/data.json'
).then(
m => m.default || m
);

// Actions
export const actions = {
async getDataFromFile(
{ commit }
) {

let preloadedComments = []
, preloadedPosts = []
, preloadedUsers = []
;

try {

const {
comments,
posts,
users,
} = await getFile();

if( comments )
preloadedComments = comments;
if( posts )
preloadedPosts = posts;
if( users )
preloadedUsers = users;

} catch( e ) {

console.error(
{
e,
}
);

}

commit(
'comments/SET_ITEMS',
preloadedComments,
{
root: true,
}
);

commit(
'posts/SET_ITEMS',
preloadedPosts,
{
root: true,
}
);

commit(
'users/SET_ITEMS',
preloadedUsers,
{
root: true,
}
);

},
};
18 changes: 18 additions & 0 deletions example/store/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const state = () => (
{
items: [],
}
);

export const mutations = {
SET_ITEMS(
state,
items,
) {

state.items = Object.freeze(
items,
);

},
};
11 changes: 11 additions & 0 deletions example/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const actions = {
async nuxtServerInit(
{ dispatch },
) {

await dispatch(
'build-data/getDataFromFile',
);

},
};
18 changes: 18 additions & 0 deletions example/store/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const state = () => (
{
items: [],
}
);

export const mutations = {
SET_ITEMS(
state,
items,
) {

state.items = Object.freeze(
items,
);

},
};
18 changes: 18 additions & 0 deletions example/store/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const state = () => (
{
items: [],
}
);

export const mutations = {
SET_ITEMS(
state,
items,
) {

state.items = Object.freeze(
items,
);

},
};

0 comments on commit 0cfd811

Please sign in to comment.