Skip to content

Commit ba48434

Browse files
committed
[Release] 0.9.0
1 parent efe2e8b commit ba48434

5 files changed

+230
-98
lines changed

dist/vuex-orm-axios.common.js

+76-32
Original file line numberDiff line numberDiff line change
@@ -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

dist/vuex-orm-axios.esm.js

+76-32
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,73 @@ var Response = /** @class */ (function () {
6565
/**
6666
* Create a new response instance.
6767
*/
68-
function Response(model, config, response, entities) {
68+
function Response(model, config, response) {
69+
/**
70+
* Entities created by Vuex ORM.
71+
*/
72+
this.entities = null;
73+
/**
74+
* Whether if response data is saved to the store or not.
75+
*/
76+
this.isSaved = false;
6977
this.model = model;
7078
this.config = config;
7179
this.response = response;
72-
this.entities = entities;
7380
}
81+
/**
82+
* Save response data to the store.
83+
*/
84+
Response.prototype.save = function () {
85+
return __awaiter(this, void 0, void 0, function () {
86+
var _a;
87+
return __generator(this, function (_b) {
88+
switch (_b.label) {
89+
case 0:
90+
_a = this;
91+
return [4 /*yield*/, this.model.insertOrUpdate({
92+
data: this.getDataFromResponse()
93+
})];
94+
case 1:
95+
_a.entities = _b.sent();
96+
this.isSaved = true;
97+
return [2 /*return*/];
98+
}
99+
});
100+
});
101+
};
102+
/**
103+
* Delete store record depending on `delete` option.
104+
*/
105+
Response.prototype.delete = function () {
106+
return __awaiter(this, void 0, void 0, function () {
107+
return __generator(this, function (_a) {
108+
switch (_a.label) {
109+
case 0:
110+
if (this.config.delete === undefined) {
111+
throw new Error('[Vuex ORM Axios] Could not delete records because the `delete` option is not set.');
112+
}
113+
return [4 /*yield*/, this.model.delete(this.config.delete)];
114+
case 1:
115+
_a.sent();
116+
return [2 /*return*/];
117+
}
118+
});
119+
});
120+
};
121+
/**
122+
* Get data from the given response object. If the `dataTransformer` config is
123+
* provided, it tries to execute the method with the response as param. If the
124+
* `dataKey` config is provided, it tries to fetch the data at that key.
125+
*/
126+
Response.prototype.getDataFromResponse = function () {
127+
if (this.config.dataTransformer) {
128+
return this.config.dataTransformer(this.response);
129+
}
130+
if (this.config.dataKey) {
131+
return this.response.data[this.config.dataKey];
132+
}
133+
return this.response.data;
134+
};
74135
return Response;
75136
}());
76137

@@ -174,18 +235,15 @@ var Request = /** @class */ (function () {
174235
*/
175236
Request.prototype.request = function (config) {
176237
return __awaiter(this, void 0, void 0, function () {
177-
var requestConfig, response, entities;
238+
var requestConfig, axiosResponse;
178239
return __generator(this, function (_a) {
179240
switch (_a.label) {
180241
case 0:
181242
requestConfig = this.createConfig(config);
182243
return [4 /*yield*/, this.axios.request(requestConfig)];
183244
case 1:
184-
response = _a.sent();
185-
return [4 /*yield*/, this.persistResponseData(response, requestConfig)];
186-
case 2:
187-
entities = _a.sent();
188-
return [2 /*return*/, new Response(this.model, requestConfig, response, entities)];
245+
axiosResponse = _a.sent();
246+
return [2 /*return*/, this.createResponse(axiosResponse, requestConfig)];
189247
}
190248
});
191249
});
@@ -198,42 +256,28 @@ var Request = /** @class */ (function () {
198256
return __assign(__assign(__assign(__assign({}, this.config), this.model.globalApiConfig), this.model.apiConfig), config);
199257
};
200258
/**
201-
* Persist the response data to the vuex store.
259+
* Create a new response instance by applying a few initialization processes.
260+
* For example, it saves response data if `save` option id set to `true`.
202261
*/
203-
Request.prototype.persistResponseData = function (response, config) {
262+
Request.prototype.createResponse = function (axiosResponse, config) {
204263
return __awaiter(this, void 0, void 0, function () {
264+
var response;
205265
return __generator(this, function (_a) {
206266
switch (_a.label) {
207267
case 0:
208-
if (!config.save) {
209-
return [2 /*return*/, null];
210-
}
268+
response = new Response(this.model, config, axiosResponse);
211269
if (!(config.delete !== undefined)) return [3 /*break*/, 2];
212-
return [4 /*yield*/, this.model.delete(config.delete)];
270+
return [4 /*yield*/, response.delete()];
213271
case 1:
214272
_a.sent();
215-
return [2 /*return*/, null];
216-
case 2: return [2 /*return*/, this.model.insertOrUpdate({
217-
data: this.getDataFromResponse(response, config)
218-
})];
273+
return [2 /*return*/, response];
274+
case 2:
275+
config.save && response.save();
276+
return [2 /*return*/, response];
219277
}
220278
});
221279
});
222280
};
223-
/**
224-
* Get data from the given response object. If the `dataTransformer` config is
225-
* provided, it tries to execute the method with the response as param. If the
226-
* `dataKey` config is provided, it tries to fetch the data at that key.
227-
*/
228-
Request.prototype.getDataFromResponse = function (response, config) {
229-
if (config.dataTransformer) {
230-
return config.dataTransformer(response);
231-
}
232-
if (config.dataKey) {
233-
return response.data[config.dataKey];
234-
}
235-
return response.data;
236-
};
237281
return Request;
238282
}());
239283

0 commit comments

Comments
 (0)