@benjy2976/pmsg 0.1.2 → 0.1.4

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.2",
3
+ "version": "0.1.4",
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,15 +50,17 @@ 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 = {}
54
58
  }
55
59
 
56
- get(url = '', params = {}) {
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)
63
+ return this.instance.get(url, params, config)
60
64
  }
61
65
 
62
66
  post(url = '', params = {}) {
@@ -66,9 +70,9 @@ export default class Model {
66
70
  }
67
71
 
68
72
  // Función para obtener el listado de Objetos de la base de datos
69
- getAll(params = {}) {
73
+ getAll(params = {}, config = {}) {
70
74
  params = Object.assign(params, this.params)
71
- return this.instance.get(this.route, params)
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
 
@@ -216,5 +222,4 @@ export default class Model {
216
222
  getNameAttribute() {
217
223
  return this.name
218
224
  }
219
- }
220
-
225
+ }
@@ -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]}
@@ -22,9 +24,8 @@ export default function createModelStore (model, state = {}, getters = {}, actio
22
24
  return areObjEquals(d,data)
23
25
  }
24
26
  }
25
- const resolveR = (item) => item
26
27
  return {
27
- state : {
28
+ state : () => ({
28
29
  itemSelected : { loading: false, ...model.getDefault() },
29
30
  items : [],
30
31
  keysAsinc : [],
@@ -34,13 +35,14 @@ export default function createModelStore (model, state = {}, getters = {}, actio
34
35
  maxRelationsResolve : config.maxRelationsResolve,
35
36
  relations : config.relations,
36
37
  syncStatus : config.sync,
37
- paginate : config.paginate,
38
+ paginate : config.paginate,
38
39
  pagination : config.pagination,
39
40
  selectedStatus : false,
40
41
  timeOutAsinc : null,
41
42
  check : check,
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 },
@@ -52,21 +54,21 @@ export default function createModelStore (model, state = {}, getters = {}, actio
52
54
  return item ? item[model.getNameAttribute()] : null
53
55
  },
54
56
  // Getter para obtener el objeto seleccionado
55
- find : ({ items, key }) => (id, level = 1) => {
56
- const item = items.find(d => d[key] === id)
57
- return item ? resolveR(item, level):model.getDefault()
57
+ find : ({ items, key, resolve }) => (id, level = 1) => {
58
+ const item = items.find(d => d[key] === Number(id))
59
+ return item ? resolve(item, level):model.getDefault()
58
60
  },
59
61
  // Getter para obtener la lista de objetos
60
- list : ({items}) => {
61
- return items.map(resolveR)
62
+ list : ({items, resolve}) => {
63
+ return items.map(resolve)
62
64
  },
63
65
  // Getter para obtener la lista de objetos filtrados
64
- filter : ({items}) => (filter, level = 1) => {
65
- return items.filter(filter).map(item => resolveR(item, level))
66
+ filter : ({items,resolve}) => (filter, level = 1) => {
67
+ return items.filter(filter).map(item => resolve(item, level))
66
68
  },
67
69
  // Getter para obtener el objeto seleccionado o falso si no hay seleccion
68
- selected : ({ itemSelected, selectedStatus }) => {
69
- return selectedStatus ? resolveR(itemSelected) : selectedStatus
70
+ selected : ({ itemSelected, selectedStatus, resolve }) => {
71
+ return selectedStatus ? resolve(itemSelected) : selectedStatus
70
72
  },
71
73
  ...getters
72
74
  },
@@ -124,17 +126,17 @@ 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
  })
131
133
  },
132
134
  // Action para obtener la lista de objetos de el servidor
133
135
  async get (params = {}, pagination={}) {
134
- //var commit = store.commit
136
+ //var commit = store.commit
135
137
  const action = this.syncStatus ? 'sync' : 'setItems';
136
138
 
137
- if (model.paginate) {
139
+ if (this.paginate) {
138
140
  params = {...params, ...Object.assign({
139
141
  page : this.pagination.current_page, per_page : this.pagination.per_page
140
142
  }, pagination) };
@@ -142,14 +144,16 @@ 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);
148
- // eslint-disable-next-line no-unused-vars
149
- const { data: _, ...pagination } = data.meta;
150
- this.pagination = Object.assign(this.pagination, pagination);
147
+ const data = config.responseAdapter(response);
148
+ model.save(data);
149
+ this[action](data);
150
+ if (this.paginate) {
151
+ const pagination = config.paginationAdapter(response);
152
+ if (pagination) {
153
+ this.pagination = Object.assign(this.pagination, pagination);
154
+ }
155
+ }
151
156
  this.afterGet();
152
- console.log('get', this)
153
157
  resolve(response);
154
158
  }).catch(reject);
155
159
  })
@@ -176,7 +180,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
176
180
  //var commit = store.commit
177
181
  return new Promise((resolve, reject) => {
178
182
  model.getAll(params).then(response => {
179
- this.sync(response.data);
183
+ this.sync(config.responseAdapter(response));
180
184
  this.afterGet();
181
185
  resolve(response);
182
186
  }).catch(reject);
@@ -189,7 +193,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
189
193
  // divide y vencceras
190
194
  this.setItems([]);
191
195
  model.getAll(params).then(response => {
192
- this.setItems(response.data);
196
+ this.setItems(config.responseAdapter(response));
193
197
  this.afterGet();
194
198
  resolve(response);
195
199
  }).catch(reject);
@@ -204,7 +208,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
204
208
  create( data){
205
209
  return new Promise((resolve, reject) => {
206
210
  model.create(data).then(response => {
207
- this.syncItem(response.data.data);
211
+ this.syncItem(config.responseAdapter(response));
208
212
  resolve(response)
209
213
  }).catch(error => {
210
214
  reject(error)
@@ -216,7 +220,7 @@ export default function createModelStore (model, state = {}, getters = {}, actio
216
220
  update( data){
217
221
  return new Promise((resolve, reject) => {
218
222
  model.update(data).then(response => {
219
- this.syncItem(response.data.data);
223
+ this.syncItem(config.responseAdapter(response));
220
224
  resolve(response)
221
225
  }).catch(error => {
222
226
  reject(error)
@@ -348,4 +352,4 @@ export default function createModelStore (model, state = {}, getters = {}, actio
348
352
  ...actions
349
353
  }
350
354
  };
351
- }
355
+ }