const express = require("@runkit/runkit/express-endpoint/1.0.0");
const request = require('request');
var bodyParser = require('body-parser')
const app = express(exports);
app.use(bodyParser.json())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
class Item {
constructor({ title, description, price }) {
this.id = Item.curId++;
this.title = title || '';
this.description = description || '';
this.price = price || 0;
}
}
Item.curId = 0;
const items = [
new Item({ title: "Mango LaCroix", description: "The slightest hint of mango", price: 1.00}),
new Item({ title: "Lemon LaCroix", description: "Like a really far away lemon", price: 2.00}),
new Item({ title: "Berry LaCroix", description: "This kinda tastes like feet", price: 0.50})
];
app.get('/items', (req, res) => res.send(items));
app.post('/cart', (req, res) => {
const cart = {
items: (req.body.items || []).map(spec => new Item(spec))
};
request.post('https://api.keyvalue.xyz/new/cart', (err, _, body) => {
cart.id = body.trim().match(/https:\/\/api\.keyvalue\.xyz\/([^\~\/]*)\/cart/)[1];
request.post(`https://api.keyvalue.xyz/${cart.id}/cart/${JSON.stringify(cart)}`, (err, _, body) => {
res.send(cart);
});
});
});
app.get('/cart/:id', (req, res) => {
request(`https://api.keyvalue.xyz/${req.params.id}/cart`, (err, _, body) => {
res.json(JSON.parse(body));
});
});
app.post('/cart/:id', (req, res) => {
request.post(`https://api.keyvalue.xyz/${req.params.id}/cart/${JSON.stringify(req.body)}`, (err, _, body) => {
res.send(`https://api.keyvalue.xyz/${req.params.id}/cart/${JSON.stringify(req.body)}`);
});});