@@ -67,12 +67,73 @@ var Response = /** @class */ (function () {
6767 /**
6868 * Create a new response instance.
6969 */
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 ;
7179 this . model = model ;
7280 this . config = config ;
7381 this . response = response ;
74- this . entities = entities ;
7582 }
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+ } ;
76137 return Response ;
77138} ( ) ) ;
78139
@@ -176,18 +237,15 @@ var Request = /** @class */ (function () {
176237 */
177238 Request . prototype . request = function ( config ) {
178239 return __awaiter ( this , void 0 , void 0 , function ( ) {
179- var requestConfig , response , entities ;
240+ var requestConfig , axiosResponse ;
180241 return __generator ( this , function ( _a ) {
181242 switch ( _a . label ) {
182243 case 0 :
183244 requestConfig = this . createConfig ( config ) ;
184245 return [ 4 /*yield*/ , this . axios . request ( requestConfig ) ] ;
185246 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 ) ] ;
191249 }
192250 } ) ;
193251 } ) ;
@@ -200,42 +258,28 @@ var Request = /** @class */ (function () {
200258 return __assign ( __assign ( __assign ( __assign ( { } , this . config ) , this . model . globalApiConfig ) , this . model . apiConfig ) , config ) ;
201259 } ;
202260 /**
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`.
204263 */
205- Request . prototype . persistResponseData = function ( response , config ) {
264+ Request . prototype . createResponse = function ( axiosResponse , config ) {
206265 return __awaiter ( this , void 0 , void 0 , function ( ) {
266+ var response ;
207267 return __generator ( this , function ( _a ) {
208268 switch ( _a . label ) {
209269 case 0 :
210- if ( ! config . save ) {
211- return [ 2 /*return*/ , null ] ;
212- }
270+ response = new Response ( this . model , config , axiosResponse ) ;
213271 if ( ! ( config . delete !== undefined ) ) return [ 3 /*break*/ , 2 ] ;
214- return [ 4 /*yield*/ , this . model . delete ( config . delete ) ] ;
272+ return [ 4 /*yield*/ , response . delete ( ) ] ;
215273 case 1 :
216274 _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 ] ;
221279 }
222280 } ) ;
223281 } ) ;
224282 } ;
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- } ;
239283 return Request ;
240284} ( ) ) ;
241285
0 commit comments