@zhiguang-gastrofy/capi
Version:
comany apis, including Northfork api and Gastrofy api
63 lines • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var northfork_service_1 = require("./northfork.service");
var gastrofy_service_1 = require("./gastrofy.service");
function attachMeta(recipe, metas) {
if (!metas || !metas.length)
return;
for (var _i = 0, metas_1 = metas; _i < metas_1.length; _i++) {
var meta = metas_1[_i];
if (recipe.gastrofy_id == meta.id) {
recipe.rating = meta.rating;
recipe.nutrition = meta.nutrition;
recipe.chef = meta.chefs && meta.chefs[0] ? meta.chefs[0] : {};
break;
}
}
}
// GastrofyService with NorthforkService
var GNService = /** @class */ (function () {
function GNService(config) {
this._NFHttp = new northfork_service_1.NorthforkService({
customerToken: config.customerToken
});
this._GFHttp = new gastrofy_service_1.GastrofyService();
}
GNService.prototype.getRecipe = function (id, params) {
var _this = this;
return this._NFHttp.getRecipe(id, params).then(function (recipe) {
return _this._GFHttp.getRecipeMeta(recipe.gastrofy_id).then(function (metas) {
attachMeta(recipe, metas);
return recipe;
});
});
};
GNService.prototype.getRecipes = function (params) {
var _this = this;
var promise = this._NFHttp.getRecipes(params);
promise.then(function (recipesResponse) {
if (!recipesResponse.data || !recipesResponse.data.length)
return recipesResponse;
var gastrofyIds = recipesResponse.data.map(function (recipe) {
return recipe.gastrofy_id;
}).filter(function (gastrofyId) {
return !!gastrofyId;
});
if (!gastrofyIds.length)
return recipesResponse;
return _this._GFHttp.getRecipeMeta(gastrofyIds).then(function (metaResponse) {
recipesResponse.data = recipesResponse.data.map(function (recipe) {
attachMeta(recipe, metaResponse);
return recipe;
});
return recipesResponse;
});
}, function (error) {
return error;
});
return promise;
};
return GNService;
}());
exports.GNService = GNService;
//# sourceMappingURL=gn.service.js.map