@zhiguang-gastrofy/capi
Version:
comany apis, including Northfork api and Gastrofy api
202 lines • 7.17 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var http_service_1 = require("./http.service");
var utils_1 = require("./utils");
var CancelToken = axios_1.default.CancelToken;
function parseRecipePrices(prices) {
for (var name_1 in prices) {
prices[name_1] = utils_1.parsePrice(prices[name_1]);
}
return prices;
}
var NorthforkService = /** @class */ (function () {
function NorthforkService(config) {
this._cartInited = false;
this._NFHttp = new http_service_1.HttpService(__assign({
host: 'https://api.northfork.se/api',
}, config || {}));
}
NorthforkService.prototype.getRecipes = function (params) {
var cancel;
var promise = this._NFHttp.get('discovery/recipes', {
params: params,
cancelToken: new CancelToken(function executor(c) {
cancel = c;
}),
}).then(function (res) {
res.data = res.data.map(function (item) {
item.image_url = utils_1.parseImage(item);
item.price = parseRecipePrices(item.price);
return item;
});
return res;
});
promise.cancel = cancel;
return promise;
};
NorthforkService.prototype.getRecipe = function (id, params) {
return this._NFHttp.get('discovery/recipes/' + id, {
params: params,
}).then(function (res) {
var data = res.data;
data.price = parseRecipePrices(data.price);
data.image_url = utils_1.parseImage(data);
return data;
});
};
NorthforkService.prototype.getStores = function (params) {
return this._NFHttp.get('int/stores', {
params: params,
});
};
NorthforkService.prototype.getProducts = function (params) {
var cancel;
var promise = this._NFHttp.get('int/products', {
params: params,
cancelToken: new CancelToken(function executor(c) {
cancel = c;
}),
}).then(function (res) {
res.data = res.data.map(function (item) {
var product = {
id: item.id,
external_id: item.external_id,
image_url: utils_1.parseImage(item),
name: item.meta.title,
brand: item.meta.brand_name,
price: utils_1.parsePrice(item.price.price),
is_active: item.is_active,
size: item.size.size,
size_unit_name: item.size.unit_name,
mid: item.mid,
};
return product;
});
return res;
});
promise.cancel = cancel;
return promise;
};
NorthforkService.prototype.getProduct = function (id, params) {
return this._NFHttp.get('int/products/' + id, {
params: params,
}).then(function (res) {
var data = res.data;
var product = {
id: data.id,
external_id: data.external_id,
image_url: utils_1.parseImage(data),
name: data.meta.title,
brand: data.meta.brand_name,
price: utils_1.parsePrice(data.price.price),
is_active: data.is_active,
size: data.size.size,
size_unit_name: data.size.unit_name,
mid: data.mid,
};
return product;
});
};
NorthforkService.prototype.getIngredients = function (params) {
return this._NFHttp.get('int/ingredients', {
params: params
});
};
NorthforkService.prototype.getCampaigns = function (params) {
return this._NFHttp.get('int/campaigns', {
params: params
});
};
NorthforkService.prototype.createCampaign = function (data) {
return this._NFHttp.post('int/campaigns', data, {});
};
NorthforkService.prototype.getSubstitutes = function (storeIdentifier, productIdentifier, params) {
var api = "smart-cart/stores/" + storeIdentifier + "/products/" + productIdentifier + "/substitutes";
return this._NFHttp.get(api, {
params: params,
}).then(function (res) {
res.data = res.data.map(function (product) {
product.image_url = utils_1.parseImage(product);
product.price = utils_1.parsePrice(product.price);
product.brand = product.brand !== 'na' ? product.brand : null;
product.is_active = product.in_stock === true;
delete product.in_stock;
return product;
});
return res;
});
};
NorthforkService.prototype.getProductCategories = function (params) {
return this._NFHttp.get('int/product-categories', {
params: params,
}).then(function (res) {
return res;
});
};
NorthforkService.prototype.getCart = function (userId) {
var _this = this;
if (!userId)
return;
var cancel;
var promise = this._NFHttp.get('int/carts?user_id=' + userId, {
cancelToken: new CancelToken(function executor(c) {
cancel = c;
}),
}).then(function (res) {
_this._cartInited = true;
var cart = res.data && res.data[0];
if (cart) {
_this._cartId = cart.id;
delete cart.id;
delete cart.user_id;
}
return cart || null;
});
promise.cancel = cancel;
return promise;
};
NorthforkService.prototype.saveCart = function (data) {
var _this = this;
if (!this._cartInited)
return;
var cancel;
var action;
var apiPath = 'int/carts';
if (!this._cartId) {
action = 'post';
}
else {
action = 'put';
apiPath += '/' + this._cartId;
}
var promise = this._NFHttp[action](apiPath, data, {
cancelToken: new CancelToken(function executor(c) {
cancel = c;
}),
}).then(function (res) {
var cart = res.data;
if (action === 'post')
_this._cartId = cart.id;
delete cart.id;
delete cart.user_id;
return cart;
});
promise.cancel = cancel;
return promise;
};
return NorthforkService;
}());
exports.NorthforkService = NorthforkService;
//# sourceMappingURL=northfork.service.js.map