Skip to content

Commit

Permalink
[Release] 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 5, 2019
1 parent efe2e8b commit ba48434
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 98 deletions.
108 changes: 76 additions & 32 deletions dist/vuex-orm-axios.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,73 @@ var Response = /** @class */ (function () {
/**
* Create a new response instance.
*/
function Response(model, config, response, entities) {
function Response(model, config, response) {
/**
* Entities created by Vuex ORM.
*/
this.entities = null;
/**
* Whether if response data is saved to the store or not.
*/
this.isSaved = false;
this.model = model;
this.config = config;
this.response = response;
this.entities = entities;
}
/**
* Save response data to the store.
*/
Response.prototype.save = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this;
return [4 /*yield*/, this.model.insertOrUpdate({
data: this.getDataFromResponse()
})];
case 1:
_a.entities = _b.sent();
this.isSaved = true;
return [2 /*return*/];
}
});
});
};
/**
* Delete store record depending on `delete` option.
*/
Response.prototype.delete = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.config.delete === undefined) {
throw new Error('[Vuex ORM Axios] Could not delete records because the `delete` option is not set.');
}
return [4 /*yield*/, this.model.delete(this.config.delete)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Get data from the given response object. If the `dataTransformer` config is
* provided, it tries to execute the method with the response as param. If the
* `dataKey` config is provided, it tries to fetch the data at that key.
*/
Response.prototype.getDataFromResponse = function () {
if (this.config.dataTransformer) {
return this.config.dataTransformer(this.response);
}
if (this.config.dataKey) {
return this.response.data[this.config.dataKey];
}
return this.response.data;
};
return Response;
}());

Expand Down Expand Up @@ -176,18 +237,15 @@ var Request = /** @class */ (function () {
*/
Request.prototype.request = function (config) {
return __awaiter(this, void 0, void 0, function () {
var requestConfig, response, entities;
var requestConfig, axiosResponse;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
requestConfig = this.createConfig(config);
return [4 /*yield*/, this.axios.request(requestConfig)];
case 1:
response = _a.sent();
return [4 /*yield*/, this.persistResponseData(response, requestConfig)];
case 2:
entities = _a.sent();
return [2 /*return*/, new Response(this.model, requestConfig, response, entities)];
axiosResponse = _a.sent();
return [2 /*return*/, this.createResponse(axiosResponse, requestConfig)];
}
});
});
Expand All @@ -200,42 +258,28 @@ var Request = /** @class */ (function () {
return __assign(__assign(__assign(__assign({}, this.config), this.model.globalApiConfig), this.model.apiConfig), config);
};
/**
* Persist the response data to the vuex store.
* Create a new response instance by applying a few initialization processes.
* For example, it saves response data if `save` option id set to `true`.
*/
Request.prototype.persistResponseData = function (response, config) {
Request.prototype.createResponse = function (axiosResponse, config) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!config.save) {
return [2 /*return*/, null];
}
response = new Response(this.model, config, axiosResponse);
if (!(config.delete !== undefined)) return [3 /*break*/, 2];
return [4 /*yield*/, this.model.delete(config.delete)];
return [4 /*yield*/, response.delete()];
case 1:
_a.sent();
return [2 /*return*/, null];
case 2: return [2 /*return*/, this.model.insertOrUpdate({
data: this.getDataFromResponse(response, config)
})];
return [2 /*return*/, response];
case 2:
config.save && response.save();
return [2 /*return*/, response];
}
});
});
};
/**
* Get data from the given response object. If the `dataTransformer` config is
* provided, it tries to execute the method with the response as param. If the
* `dataKey` config is provided, it tries to fetch the data at that key.
*/
Request.prototype.getDataFromResponse = function (response, config) {
if (config.dataTransformer) {
return config.dataTransformer(response);
}
if (config.dataKey) {
return response.data[config.dataKey];
}
return response.data;
};
return Request;
}());

Expand Down
108 changes: 76 additions & 32 deletions dist/vuex-orm-axios.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,73 @@ var Response = /** @class */ (function () {
/**
* Create a new response instance.
*/
function Response(model, config, response, entities) {
function Response(model, config, response) {
/**
* Entities created by Vuex ORM.
*/
this.entities = null;
/**
* Whether if response data is saved to the store or not.
*/
this.isSaved = false;
this.model = model;
this.config = config;
this.response = response;
this.entities = entities;
}
/**
* Save response data to the store.
*/
Response.prototype.save = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this;
return [4 /*yield*/, this.model.insertOrUpdate({
data: this.getDataFromResponse()
})];
case 1:
_a.entities = _b.sent();
this.isSaved = true;
return [2 /*return*/];
}
});
});
};
/**
* Delete store record depending on `delete` option.
*/
Response.prototype.delete = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.config.delete === undefined) {
throw new Error('[Vuex ORM Axios] Could not delete records because the `delete` option is not set.');
}
return [4 /*yield*/, this.model.delete(this.config.delete)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* Get data from the given response object. If the `dataTransformer` config is
* provided, it tries to execute the method with the response as param. If the
* `dataKey` config is provided, it tries to fetch the data at that key.
*/
Response.prototype.getDataFromResponse = function () {
if (this.config.dataTransformer) {
return this.config.dataTransformer(this.response);
}
if (this.config.dataKey) {
return this.response.data[this.config.dataKey];
}
return this.response.data;
};
return Response;
}());

Expand Down Expand Up @@ -174,18 +235,15 @@ var Request = /** @class */ (function () {
*/
Request.prototype.request = function (config) {
return __awaiter(this, void 0, void 0, function () {
var requestConfig, response, entities;
var requestConfig, axiosResponse;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
requestConfig = this.createConfig(config);
return [4 /*yield*/, this.axios.request(requestConfig)];
case 1:
response = _a.sent();
return [4 /*yield*/, this.persistResponseData(response, requestConfig)];
case 2:
entities = _a.sent();
return [2 /*return*/, new Response(this.model, requestConfig, response, entities)];
axiosResponse = _a.sent();
return [2 /*return*/, this.createResponse(axiosResponse, requestConfig)];
}
});
});
Expand All @@ -198,42 +256,28 @@ var Request = /** @class */ (function () {
return __assign(__assign(__assign(__assign({}, this.config), this.model.globalApiConfig), this.model.apiConfig), config);
};
/**
* Persist the response data to the vuex store.
* Create a new response instance by applying a few initialization processes.
* For example, it saves response data if `save` option id set to `true`.
*/
Request.prototype.persistResponseData = function (response, config) {
Request.prototype.createResponse = function (axiosResponse, config) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!config.save) {
return [2 /*return*/, null];
}
response = new Response(this.model, config, axiosResponse);
if (!(config.delete !== undefined)) return [3 /*break*/, 2];
return [4 /*yield*/, this.model.delete(config.delete)];
return [4 /*yield*/, response.delete()];
case 1:
_a.sent();
return [2 /*return*/, null];
case 2: return [2 /*return*/, this.model.insertOrUpdate({
data: this.getDataFromResponse(response, config)
})];
return [2 /*return*/, response];
case 2:
config.save && response.save();
return [2 /*return*/, response];
}
});
});
};
/**
* Get data from the given response object. If the `dataTransformer` config is
* provided, it tries to execute the method with the response as param. If the
* `dataKey` config is provided, it tries to fetch the data at that key.
*/
Request.prototype.getDataFromResponse = function (response, config) {
if (config.dataTransformer) {
return config.dataTransformer(response);
}
if (config.dataKey) {
return response.data[config.dataKey];
}
return response.data;
};
return Request;
}());

Expand Down
Loading

0 comments on commit ba48434

Please sign in to comment.