66[ ![ npm] ( https://img.shields.io/npm/dt/vue-api-queries.svg?style=flat-square )] ( https://npmjs.com/package/vue-api-queries )
77[ ![ npm] ( https://img.shields.io/npm/dm/vue-api-queries.svg?style=flat-square )] ( https://npmjs.com/package/vue-api-queries )
88
9- 🔥 If you use Laravel, this package matches perfectly with [ andersao/l5-repository] ( https://github.com/andersao/l5-repository ) .
9+ 🔥 If you use Laravel, this package matches perfectly
10+ with [ andersao/l5-repository] ( https://github.com/andersao/l5-repository ) .
1011
11- This package helps you quickly to build requests for REST API. Move your logic and backend requests to dedicated classes. Keep your code clean and elegant.
12+ This package helps you quickly to build requests for REST API. Move your logic and backend requests to dedicated
13+ classes. Keep your code clean and elegant.
1214
1315Wouldn't it be great if you could just use your back end to validate forms on the front end? This package provides a
14- ` BaseProxy ` class that does exactly that. It can post itself to a configured endpoint and manage errors. The class
15- is meant to be used with a Laravel back end, and it doesn't limit that you need only to work with laravel, Ruby on
16- Rail, NodeJs, ExpressJs, or any other languages.
16+ ` BaseProxy ` class that does exactly that. It can post itself to a configured endpoint and manage errors. The class is
17+ meant to be used with a Laravel back end, and it doesn't limit that you need only to work with laravel, Ruby on Rail,
18+ NodeJs, ExpressJs, or any other languages.
1719
1820Take a look at the [ usage section] ( #usage ) to view a detailed example on how to use it.
1921
@@ -24,6 +26,7 @@ You can install the package via yarn (or npm):
2426``` npm
2527npm install vue-api-queries
2628```
29+
2730``` yarn
2831yarn add vue-api-queries
2932```
@@ -43,32 +46,40 @@ Put it on top of axios module
4346
4447``` js
4548export default {
46- modules: [
47- ' vue-api-queries/nuxt' ,
48- ' @nuxtjs/axios' ,
49- ]
49+ modules: [
50+ // simple usage
51+ ' vue-api-queries/nuxt' ,
52+ // With options
53+ [' vue-api-queries/nuxt' , { errorsKeyName: ' errors' }],
54+ ' @nuxtjs/axios' ,
55+ ],
56+ apiQueries: { errorsKeyName: ' errors' },
5057}
5158```
5259
5360### Note:
61+
5462` baseURL ` is required.
5563
5664You can define ` baseURL ` at .env just one of them
65+
5766``` bash
5867API_URL=http://localhost::3000/api
5968API_HOST=http://localhost::3000/api
6069```
6170
6271if your axios already defined in ` nuxt.config.js `
72+
6373``` js
6474export default {
65- axios: {
66- baseURL: process .env .API_URL
67- }
75+ axios: {
76+ baseURL: process .env .API_URL
77+ }
6878}
6979```
7080
7181### Advance usage
82+
7283-------------- Todo --------------
7384
7485### Vue plugins
@@ -81,14 +92,16 @@ Vue.use(VueApiQueries)
8192```
8293
8394### Note
84- Error response must look like:
95+
96+ Error response must look like:
97+
8598``` json
8699{
87- "errors" : {
88- "field" : [
89- " The field is required."
90- ]
91- }
100+ "errors" : {
101+ "field" : [
102+ " The field is required."
103+ ]
104+ }
92105}
93106```
94107
@@ -131,6 +144,7 @@ export default NewsProxy
1311442.Store
132145
133146- Create news store
147+
1341481 . actions.js
1351492 . getters.js
1361503 . mutation-types.js
@@ -139,6 +153,7 @@ export default NewsProxy
139153
140154---
141155actions.js
156+
142157``` js
143158import { ALL } from ' ./mutation-types'
144159import { NewsProxy } from ' ~/proxies'
@@ -170,8 +185,10 @@ export default {
170185 all
171186}
172187```
188+
173189---
174190getters.js
191+
175192``` js
176193export default {
177194 all : (state ) => state .all
@@ -180,13 +197,16 @@ export default {
180197
181198---
182199mutation-types.js
200+
183201``` js
184202export const ALL = ' ALL'
185203
186204export default { ALL }
187205```
206+
188207---
189208mutations.js
209+
190210``` js
191211import { ALL } from ' ./mutation-types'
192212
@@ -198,54 +218,60 @@ export default {
198218 }
199219}
200220```
221+
201222---
202223state.js
224+
203225``` js
204226export default () => ({
205227 all: [],
206228 pagination: {}
207229})
208230```
231+
209232## How to call in components or pages
233+
210234- news.vue pages
211235
212236It can be called in ` mounted() ` or ` asyncData() `
213237
214238- ` asyncData() `
239+
215240``` js
216241export default {
217- async asyncData ({ app, store }) {
218- const { id = null } = app .$auth .user
219- await store .dispatch (' news/all' , {
220- fn : (proxy ) => {
221- proxy
222- .setParameters ({
223- userId: id,
224- include: [' categories' ]
225- })
226- .removeParameters ([' page' , ' limit' ])
227- }
228- })
229- }
242+ async asyncData ({ app, store }) {
243+ const { id = null } = app .$auth .user
244+ await store .dispatch (' news/all' , {
245+ fn : (proxy ) => {
246+ proxy
247+ .setParameters ({
248+ userId: id,
249+ include: [' categories' ]
250+ })
251+ .removeParameters ([' page' , ' limit' ])
252+ }
253+ })
254+ }
230255}
231256```
232257
233258- ` mounted() `
259+
234260``` js
235261export default {
236- mounted () {
237- const { id = null } = this .$auth .user
238- this .$store .dispatch (' news/all' , {
239- fn : (proxy ) => {
240- proxy
241- .setParameters ({
242- userId: id,
243- include: [' categories' ]
244- })
245- .removeParameters ([' page' , ' limit' ])
246- }
247- })
248- }
262+ mounted () {
263+ const { id = null } = this .$auth .user
264+ this .$store .dispatch (' news/all' , {
265+ fn : (proxy ) => {
266+ proxy
267+ .setParameters ({
268+ userId: id,
269+ include: [' categories' ]
270+ })
271+ .removeParameters ([' page' , ' limit' ])
272+ }
273+ })
274+ }
249275}
250276```
251277
@@ -289,6 +315,7 @@ const parameters = {
289315const { data } = proxy .setParameters (parameters).all ()
290316this .data = data
291317```
318+
292319** Note** : Query object above will transform into query string like:
293320
294321``` text
@@ -300,24 +327,32 @@ if setParameter that value is empty or null it will remove that param for query
300327#### setParameter()
301328
302329#### Example 1
330+
303331``` js
304332const proxy = new ExampleProxy ()
305333const { data } = await proxy .setParameter (' page' , 1 ).all ()
306334this .data = data
307335```
336+
308337Expected will be:
338+
309339``` json
310- { "page" : 1 }
340+ {
341+ "page" : 1
342+ }
311343```
312344
313345#### Example 2
346+
314347``` js
315348const proxy = new ExampleProxy ()
316349const queryString = ' limit=10&page=1&search[name]=hello'
317350const { data } = await proxy .setParameter (queryString).all ()
318351this .data = data
319352```
353+
320354Expected will be:
355+
321356``` json
322357{
323358 "limit" : 10 ,
@@ -340,25 +375,25 @@ import { NewsProxy } from '~/proxies'
340375const proxy = new NewsProxy ()
341376
342377export default {
343- methods: {
344- async fetchNews (id ) {
345- try {
346- const { data } = await proxy .find (id)
347- this .detail = data
348- } catch (e) {
349- console .log (e)
350- }
351- }
352- },
353- mounted () {
354- this .fetchNews (this .$route .params .id )
378+ methods: {
379+ async fetchNews (id ) {
380+ try {
381+ const { data } = await proxy .find (id)
382+ this .detail = data
383+ } catch (e) {
384+ console .log (e)
385+ }
355386 }
387+ },
388+ mounted () {
389+ this .fetchNews (this .$route .params .id )
390+ }
356391}
357392```
358393
359394## Validations
360395
361- Can use ` vue-vlidator ` for client-side validator that inspired by Laravel.
396+ Can use ` vue-vlidator ` for client-side validator that inspired by Laravel.
362397[ Chantouch/vue-vlidator] ( https://github.com/Chantouch/vue-vlidator )
363398
364399### Errors methods available
@@ -372,44 +407,44 @@ Method | Description
372407** has(attributes)** | To check multiple attributes given have any errors
373408** first(attribute)** | To get errors message by an attribute
374409
375-
376410## How to use in vue component
377411
378412``` vue
413+
379414<template>
380- <v-form v-model=" valid" lazy-validation @keydown.native=" $errors.onKeydown" @submit.prevent='submit'>
415+ <v-form v-model=' valid' lazy-validation @keydown.native=' $errors.onKeydown' @submit.prevent='submit'>
381416 <v-container>
382417 <v-row>
383- <v-col cols="12" md="4" >
418+ <v-col cols='12' md='4' >
384419 <v-text-field
385- v-model=" firstname"
420+ v-model=' firstname'
386421 :error-messages="$errors.first(['firstname'])"
387- :counter="10"
388- label=" First name"
422+ :counter='10'
423+ label=' First name'
389424 required
390- name=" firstname"
425+ name=' firstname'
391426 />
392427 </v-col>
393- <v-col cols="12" md="4" >
428+ <v-col cols='12' md='4' >
394429 <v-text-field
395- v-model=" lastname"
396- :counter="10"
397- label=" Last name"
430+ v-model=' lastname'
431+ :counter='10'
432+ label=' Last name'
398433 required
399434 :error-messages="$errors.first(['lastname'])"
400435 />
401436 </v-col>
402- <v-col cols="12" md="4" >
437+ <v-col cols='12' md='4' >
403438 <v-text-field
404- v-model=" email"
405- :counter="10"
406- label=" Email"
439+ v-model=' email'
440+ :counter='10'
441+ label=' Email'
407442 required
408443 :error-messages="$errors.first('email')"
409444 />
410445 </v-col>
411- <v-col cols="12" md="4" >
412- <v-text-field v-model=" email" label=" E-mail" required />
446+ <v-col cols='12' md='4' >
447+ <v-text-field v-model=' email' label=' E-mail' required />
413448 </v-col>
414449 </v-row>
415450 </v-container>
0 commit comments