@benjy2976/pmsg 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -114,10 +114,10 @@ Producto.show(1)
114
114
  if you are using Pinia you can create the storeDefault
115
115
  crating the next structure
116
116
  ```js
117
- /store
118
- /store/modules
117
+ /stores
118
+ /stores/modules
119
119
  ```
120
- into /store/modules you can create a products.js file
120
+ into /stores/modules you can create a products.js file
121
121
  ```js
122
122
  //import {Product} from '../../models' //import if necessary
123
123
 
@@ -216,7 +216,7 @@ export default {
216
216
 
217
217
 
218
218
  ```
219
- you need to create a file index.js into a /store folder you have two ways to declare the state
219
+ you need to create a file index.js into a /stores folder you have two ways to declare the state
220
220
 
221
221
  ````js
222
222
  import { defineStore } from "pinia";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benjy2976/pmsg",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "generator of models and store default for pinia",
5
5
  "main": "src/Model.js",
6
6
  "scripts": {
package/src/Model.js CHANGED
@@ -24,6 +24,8 @@ export default class Model {
24
24
  modelGetters : {},// Aquí se configuran parámetros adicionales a enviar en los request excepto DELETE
25
25
  paginate : false,// Condicional para definir si el modelo tiene paginación
26
26
  pagination : {current_page: 1, per_page: 10},// Configuración de paginación
27
+ responseAdapter : (response) => response?.data?.data ?? response?.data ?? response,// Extrae la data del response
28
+ paginationAdapter : (response) => response?.data?.meta ?? null,// Extrae datos de paginación si existen
27
29
  }
28
30
  //config.hasKey=config.hasKey!=undefined?config.hasKey:config.key!=undefined
29
31
  defaultValues = Object.assign(defaultValues, config)
@@ -48,6 +50,8 @@ export default class Model {
48
50
  this.modelGetters = defaultValues.modelGetters
49
51
  this.paginate = defaultValues.paginate
50
52
  this.pagination = defaultValues.pagination
53
+ this.responseAdapter = defaultValues.responseAdapter
54
+ this.paginationAdapter = defaultValues.paginationAdapter
51
55
  this.state = {}
52
56
  this.getters = {}
53
57
  this.actions = {}
@@ -56,7 +60,7 @@ export default class Model {
56
60
  get(url = '', params = {}, config = {}) {
57
61
  params = Object.assign(params, this.params)
58
62
  url = this.route + '/' + url
59
- return this.instance.get(url, params, config)
63
+ return this.instance.get(url, { params }, config)
60
64
  }
61
65
 
62
66
  post(url = '', params = {}) {
@@ -68,7 +72,7 @@ export default class Model {
68
72
  // Función para obtener el listado de Objetos de la base de datos
69
73
  getAll(params = {}, config = {}) {
70
74
  params = Object.assign(params, this.params)
71
- return this.instance.get(this.route, params, config)
75
+ return this.instance.get(this.route, { params }, config)
72
76
  }
73
77
 
74
78
  // Función para crear un objeto en la base de datos
@@ -190,9 +194,11 @@ export default class Model {
190
194
 
191
195
  }
192
196
  }),
193
- sync : this.sync,
194
- paginate : this.paginate,
195
- pagination : this.pagination,
197
+ sync : this.sync,
198
+ paginate : this.paginate,
199
+ pagination : this.pagination,
200
+ responseAdapter : this.responseAdapter,
201
+ paginationAdapter : this.paginationAdapter,
196
202
  }
197
203
  }
198
204
 
@@ -5,7 +5,9 @@ import { areObjEquals } from "./helpers";
5
5
 
6
6
  export default function createModelStore (model, state = {}, getters = {}, actions = {}) {
7
7
  const defData = {
8
- key : 'id',
8
+ key : 'id',
9
+ responseAdapter : (response) => response?.data?.data ?? response?.data ?? response,
10
+ paginationAdapter : (response) => response?.data?.meta ?? null,
9
11
  }
10
12
  const config = Object.assign(defData, model.getStoreConfig())
11
13
  let check =(d,data)=>{return d[config.key] === data[config.key]}
@@ -23,7 +25,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
23
25
  }
24
26
  }
25
27
  return {
26
- state : {
28
+ state : () => ({
27
29
  itemSelected : { loading: false, ...model.getDefault() },
28
30
  items : [],
29
31
  keysAsinc : [],
@@ -33,14 +35,14 @@ export default function createModelStore (model, state = {}, getters = {}, actio
33
35
  maxRelationsResolve : config.maxRelationsResolve,
34
36
  relations : config.relations,
35
37
  syncStatus : config.sync,
36
- paginate : config.paginate,
38
+ paginate : config.paginate,
37
39
  pagination : config.pagination,
38
40
  selectedStatus : false,
39
41
  timeOutAsinc : null,
40
42
  check : check,
41
43
  resolve : (item) => item,
42
44
  ...state
43
- },
45
+ }),
44
46
  getters : {
45
47
  // Getter para obtener el indice de la tabla
46
48
  //key : ({key}) => { return key },
@@ -124,7 +126,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
124
126
  //var commit = store.commit
125
127
  return new Promise((resolve, reject) => {
126
128
  model.show(id).then(response => {
127
- this.syncItem(response.data)
129
+ this.syncItem(config.responseAdapter(response))
128
130
  resolve(response);
129
131
  }).catch(reject);
130
132
  })
@@ -142,13 +144,14 @@ export default function createModelStore (model, state = {}, getters = {}, actio
142
144
  if (!(await model.saved(params))) {
143
145
  return new Promise((resolve, reject) => {
144
146
  model.getAll(params).then(response => {
145
- const data = response.data;
146
- model.save(data.data);
147
- this[action](data.data);
147
+ const data = config.responseAdapter(response);
148
+ model.save(data);
149
+ this[action](data);
148
150
  if (this.paginate) {
149
- // eslint-disable-next-line no-unused-vars
150
- const { data: _, ...pagination } = data.meta;
151
- this.pagination = Object.assign(this.pagination, pagination);
151
+ const pagination = config.paginationAdapter(response);
152
+ if (pagination) {
153
+ this.pagination = Object.assign(this.pagination, pagination);
154
+ }
152
155
  }
153
156
  this.afterGet();
154
157
  resolve(response);
@@ -177,7 +180,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
177
180
  //var commit = store.commit
178
181
  return new Promise((resolve, reject) => {
179
182
  model.getAll(params).then(response => {
180
- this.sync(response.data.data);
183
+ this.sync(config.responseAdapter(response));
181
184
  this.afterGet();
182
185
  resolve(response);
183
186
  }).catch(reject);
@@ -190,7 +193,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
190
193
  // divide y vencceras
191
194
  this.setItems([]);
192
195
  model.getAll(params).then(response => {
193
- this.setItems(response.data.data);
196
+ this.setItems(config.responseAdapter(response));
194
197
  this.afterGet();
195
198
  resolve(response);
196
199
  }).catch(reject);
@@ -205,7 +208,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
205
208
  create( data){
206
209
  return new Promise((resolve, reject) => {
207
210
  model.create(data).then(response => {
208
- this.syncItem(response.data.data);
211
+ this.syncItem(config.responseAdapter(response));
209
212
  resolve(response)
210
213
  }).catch(error => {
211
214
  reject(error)
@@ -217,7 +220,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
217
220
  update( data){
218
221
  return new Promise((resolve, reject) => {
219
222
  model.update(data).then(response => {
220
- this.syncItem(response.data.data);
223
+ this.syncItem(config.responseAdapter(response));
221
224
  resolve(response)
222
225
  }).catch(error => {
223
226
  reject(error)