@@ -67,12 +67,73 @@ var Response = /** @class */ (function () {
67
67
/**
68
68
* Create a new response instance.
69
69
*/
70
- function Response ( model , config , response , entities ) {
70
+ function Response ( model , config , response ) {
71
+ /**
72
+ * Entities created by Vuex ORM.
73
+ */
74
+ this . entities = null ;
75
+ /**
76
+ * Whether if response data is saved to the store or not.
77
+ */
78
+ this . isSaved = false ;
71
79
this . model = model ;
72
80
this . config = config ;
73
81
this . response = response ;
74
- this . entities = entities ;
75
82
}
83
+ /**
84
+ * Save response data to the store.
85
+ */
86
+ Response . prototype . save = function ( ) {
87
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
88
+ var _a ;
89
+ return __generator ( this , function ( _b ) {
90
+ switch ( _b . label ) {
91
+ case 0 :
92
+ _a = this ;
93
+ return [ 4 /*yield*/ , this . model . insertOrUpdate ( {
94
+ data : this . getDataFromResponse ( )
95
+ } ) ] ;
96
+ case 1 :
97
+ _a . entities = _b . sent ( ) ;
98
+ this . isSaved = true ;
99
+ return [ 2 /*return*/ ] ;
100
+ }
101
+ } ) ;
102
+ } ) ;
103
+ } ;
104
+ /**
105
+ * Delete store record depending on `delete` option.
106
+ */
107
+ Response . prototype . delete = function ( ) {
108
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
109
+ return __generator ( this , function ( _a ) {
110
+ switch ( _a . label ) {
111
+ case 0 :
112
+ if ( this . config . delete === undefined ) {
113
+ throw new Error ( '[Vuex ORM Axios] Could not delete records because the `delete` option is not set.' ) ;
114
+ }
115
+ return [ 4 /*yield*/ , this . model . delete ( this . config . delete ) ] ;
116
+ case 1 :
117
+ _a . sent ( ) ;
118
+ return [ 2 /*return*/ ] ;
119
+ }
120
+ } ) ;
121
+ } ) ;
122
+ } ;
123
+ /**
124
+ * Get data from the given response object. If the `dataTransformer` config is
125
+ * provided, it tries to execute the method with the response as param. If the
126
+ * `dataKey` config is provided, it tries to fetch the data at that key.
127
+ */
128
+ Response . prototype . getDataFromResponse = function ( ) {
129
+ if ( this . config . dataTransformer ) {
130
+ return this . config . dataTransformer ( this . response ) ;
131
+ }
132
+ if ( this . config . dataKey ) {
133
+ return this . response . data [ this . config . dataKey ] ;
134
+ }
135
+ return this . response . data ;
136
+ } ;
76
137
return Response ;
77
138
} ( ) ) ;
78
139
@@ -176,18 +237,15 @@ var Request = /** @class */ (function () {
176
237
*/
177
238
Request . prototype . request = function ( config ) {
178
239
return __awaiter ( this , void 0 , void 0 , function ( ) {
179
- var requestConfig , response , entities ;
240
+ var requestConfig , axiosResponse ;
180
241
return __generator ( this , function ( _a ) {
181
242
switch ( _a . label ) {
182
243
case 0 :
183
244
requestConfig = this . createConfig ( config ) ;
184
245
return [ 4 /*yield*/ , this . axios . request ( requestConfig ) ] ;
185
246
case 1 :
186
- response = _a . sent ( ) ;
187
- return [ 4 /*yield*/ , this . persistResponseData ( response , requestConfig ) ] ;
188
- case 2 :
189
- entities = _a . sent ( ) ;
190
- return [ 2 /*return*/ , new Response ( this . model , requestConfig , response , entities ) ] ;
247
+ axiosResponse = _a . sent ( ) ;
248
+ return [ 2 /*return*/ , this . createResponse ( axiosResponse , requestConfig ) ] ;
191
249
}
192
250
} ) ;
193
251
} ) ;
@@ -200,42 +258,28 @@ var Request = /** @class */ (function () {
200
258
return __assign ( __assign ( __assign ( __assign ( { } , this . config ) , this . model . globalApiConfig ) , this . model . apiConfig ) , config ) ;
201
259
} ;
202
260
/**
203
- * Persist the response data to the vuex store.
261
+ * Create a new response instance by applying a few initialization processes.
262
+ * For example, it saves response data if `save` option id set to `true`.
204
263
*/
205
- Request . prototype . persistResponseData = function ( response , config ) {
264
+ Request . prototype . createResponse = function ( axiosResponse , config ) {
206
265
return __awaiter ( this , void 0 , void 0 , function ( ) {
266
+ var response ;
207
267
return __generator ( this , function ( _a ) {
208
268
switch ( _a . label ) {
209
269
case 0 :
210
- if ( ! config . save ) {
211
- return [ 2 /*return*/ , null ] ;
212
- }
270
+ response = new Response ( this . model , config , axiosResponse ) ;
213
271
if ( ! ( config . delete !== undefined ) ) return [ 3 /*break*/ , 2 ] ;
214
- return [ 4 /*yield*/ , this . model . delete ( config . delete ) ] ;
272
+ return [ 4 /*yield*/ , response . delete ( ) ] ;
215
273
case 1 :
216
274
_a . sent ( ) ;
217
- return [ 2 /*return*/ , null ] ;
218
- case 2 : return [ 2 /*return*/ , this . model . insertOrUpdate ( {
219
- data : this . getDataFromResponse ( response , config )
220
- } ) ] ;
275
+ return [ 2 /*return*/ , response ] ;
276
+ case 2 :
277
+ config . save && response . save ( ) ;
278
+ return [ 2 /*return*/ , response ] ;
221
279
}
222
280
} ) ;
223
281
} ) ;
224
282
} ;
225
- /**
226
- * Get data from the given response object. If the `dataTransformer` config is
227
- * provided, it tries to execute the method with the response as param. If the
228
- * `dataKey` config is provided, it tries to fetch the data at that key.
229
- */
230
- Request . prototype . getDataFromResponse = function ( response , config ) {
231
- if ( config . dataTransformer ) {
232
- return config . dataTransformer ( response ) ;
233
- }
234
- if ( config . dataKey ) {
235
- return response . data [ config . dataKey ] ;
236
- }
237
- return response . data ;
238
- } ;
239
283
return Request ;
240
284
} ( ) ) ;
241
285
0 commit comments