[ci] format
This commit is contained in:
parent
ecbcc8c42c
commit
9fa013b4fa
8 changed files with 15 additions and 19 deletions
|
@ -7,9 +7,9 @@ const clientRoot = new URL('../dist/client/', import.meta.url);
|
||||||
|
|
||||||
async function handle(req, res) {
|
async function handle(req, res) {
|
||||||
ssrHandler(req, res, async (err) => {
|
ssrHandler(req, res, async (err) => {
|
||||||
if(err) {
|
if (err) {
|
||||||
res.writeHead(500);
|
res.writeHead(500);
|
||||||
res.end(err.stack)
|
res.end(err.stack);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,4 @@ import db from './db.json';
|
||||||
const products = db.products;
|
const products = db.products;
|
||||||
const productMap = new Map(products.map((product) => [product.id, product]));
|
const productMap = new Map(products.map((product) => [product.id, product]));
|
||||||
|
|
||||||
export {
|
export { products, productMap };
|
||||||
products,
|
|
||||||
productMap
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
|
|
||||||
// Normally this would be in a database.
|
// Normally this would be in a database.
|
||||||
export const userCartItems = new Map();
|
export const userCartItems = new Map();
|
||||||
|
|
|
@ -6,15 +6,15 @@ export function get(_params: any, request: Request) {
|
||||||
let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
|
let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
|
||||||
if (!userId || !userCartItems.has(userId)) {
|
if (!userId || !userCartItems.has(userId)) {
|
||||||
return {
|
return {
|
||||||
body: JSON.stringify({ items: [] })
|
body: JSON.stringify({ items: [] }),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let items = userCartItems.get(userId);
|
let items = userCartItems.get(userId);
|
||||||
let array = Array.from(items.values());
|
let array = Array.from(items.values());
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body: JSON.stringify({ items: array })
|
body: JSON.stringify({ items: array }),
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AddToCartItem {
|
interface AddToCartItem {
|
||||||
|
@ -41,7 +41,7 @@ export async function post(_params: any, request: Request) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
ok: true
|
ok: true,
|
||||||
})
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@ import { products } from '../../models/db';
|
||||||
|
|
||||||
export function get() {
|
export function get() {
|
||||||
return {
|
return {
|
||||||
body: JSON.stringify(products)
|
body: JSON.stringify(products),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,12 @@ export function get({ id: idStr }) {
|
||||||
const product = productMap.get(id);
|
const product = productMap.get(id);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body: JSON.stringify(product)
|
body: JSON.stringify(product),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 400,
|
status: 400,
|
||||||
statusText: 'Not found'
|
statusText: 'Not found',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ export function createRequest({ url, headers, method = 'GET', body = undefined,
|
||||||
const request = new Request(url.toString(), {
|
const request = new Request(url.toString(), {
|
||||||
method: method,
|
method: method,
|
||||||
headers: headersObj,
|
headers: headersObj,
|
||||||
body
|
body,
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperties(request, {
|
Object.defineProperties(request, {
|
||||||
|
|
|
@ -129,11 +129,11 @@ async function handleRequest(
|
||||||
}
|
}
|
||||||
|
|
||||||
let body: ArrayBuffer | undefined = undefined;
|
let body: ArrayBuffer | undefined = undefined;
|
||||||
if(!(req.method === 'GET' || req.method === 'HEAD')) {
|
if (!(req.method === 'GET' || req.method === 'HEAD')) {
|
||||||
let bytes: string[] = [];
|
let bytes: string[] = [];
|
||||||
await new Promise(resolve => {
|
await new Promise((resolve) => {
|
||||||
req.setEncoding('utf-8');
|
req.setEncoding('utf-8');
|
||||||
req.on('data', bts => bytes.push(bts));
|
req.on('data', (bts) => bytes.push(bts));
|
||||||
req.on('close', resolve);
|
req.on('close', resolve);
|
||||||
});
|
});
|
||||||
body = new TextEncoder().encode(bytes.join('')).buffer;
|
body = new TextEncoder().encode(bytes.join('')).buffer;
|
||||||
|
|
Loading…
Reference in a new issue