|
| 1 | +import { Hono } from 'hono'; |
| 2 | +import { HTTPException } from 'hono/http-exception'; |
| 3 | + |
| 4 | +const ITEMS: Record<string, { name: string; stock: number; price: number }> = { |
| 5 | + 'self-watering-plant': { name: 'Self-Watering Plant', stock: 5, price: 2500 }, |
| 6 | + 'solar-powered-cyberdeck': { name: 'Solar-Powered Cyberdeck', stock: 0, price: 5000 }, |
| 7 | +}; |
| 8 | + |
| 9 | +// Inventory service — standalone sub-app used as a data source. |
| 10 | +// Mounted on the main app AND called internally via .request() from the storefront. |
| 11 | +const inventoryApp = new Hono(); |
| 12 | + |
| 13 | +inventoryApp.get('/item/:productId', c => { |
| 14 | + const productId = c.req.param('productId'); |
| 15 | + const item = ITEMS[productId]; |
| 16 | + if (!item) { |
| 17 | + throw new HTTPException(404, { message: `Item ${productId} not found` }); |
| 18 | + } |
| 19 | + return c.json({ productId, ...item }); |
| 20 | +}); |
| 21 | + |
| 22 | +inventoryApp.get('/item/:productId/stock', c => { |
| 23 | + const productId = c.req.param('productId'); |
| 24 | + const item = ITEMS[productId]; |
| 25 | + if (!item) { |
| 26 | + throw new HTTPException(404, { message: `Stock check failed: ${productId}` }); |
| 27 | + } |
| 28 | + return c.json({ productId, inStock: item.stock > 0, quantity: item.stock }); |
| 29 | +}); |
| 30 | + |
| 31 | +// Storefront service — orchestrates internal .request() calls to inventoryApp. |
| 32 | +const storefrontApp = new Hono(); |
| 33 | + |
| 34 | +storefrontApp.use('/*', async function storefrontAuth(_c, next) { |
| 35 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 36 | + await next(); |
| 37 | +}); |
| 38 | + |
| 39 | +// Single internal fetch: look up one product |
| 40 | +storefrontApp.get('/product/:productId', async c => { |
| 41 | + const res = await inventoryApp.request(`/item/${c.req.param('productId')}`); |
| 42 | + if (!res.ok) { |
| 43 | + throw new HTTPException(404, { message: 'Product not found' }); |
| 44 | + } |
| 45 | + const item = await res.json(); |
| 46 | + return c.json({ product: item, source: 'storefront' }); |
| 47 | +}); |
| 48 | + |
| 49 | +// Parallel internal fetches: compare two products via Promise.all |
| 50 | +storefrontApp.get('/compare/:productId1/:productId2', async c => { |
| 51 | + const [res1, res2] = await Promise.all([ |
| 52 | + inventoryApp.request(`/item/${c.req.param('productId1')}`), |
| 53 | + inventoryApp.request(`/item/${c.req.param('productId2')}`), |
| 54 | + ]); |
| 55 | + if (!res1.ok || !res2.ok) { |
| 56 | + throw new HTTPException(404, { message: 'One or more products not found' }); |
| 57 | + } |
| 58 | + const [item1, item2] = (await Promise.all([res1.json(), res2.json()])) as [ |
| 59 | + Record<string, number>, |
| 60 | + Record<string, number>, |
| 61 | + ]; |
| 62 | + return c.json({ |
| 63 | + items: [item1, item2], |
| 64 | + priceDifference: Math.abs(item1.price - item2.price), |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +// Sequential chained fetches: look up item, then check its stock |
| 69 | +storefrontApp.get('/product/:productId/availability', async c => { |
| 70 | + const itemRes = await inventoryApp.request(`/item/${c.req.param('productId')}`); |
| 71 | + if (!itemRes.ok) { |
| 72 | + throw new HTTPException(404, { message: 'Product not found' }); |
| 73 | + } |
| 74 | + const item: Record<string, string | number> = await itemRes.json(); |
| 75 | + |
| 76 | + const stockRes = await inventoryApp.request(`/item/${item.productId}/stock`); |
| 77 | + const stock: Record<string, string | number | boolean> = await stockRes.json(); |
| 78 | + |
| 79 | + return c.json({ |
| 80 | + product: item.name, |
| 81 | + available: stock.inStock, |
| 82 | + quantity: stock.quantity, |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +// Error propagation: internal 404 causes the handler to throw a plain Error |
| 87 | +storefrontApp.get('/product-or-throw/:productId', async c => { |
| 88 | + const res = await inventoryApp.request(`/item/${c.req.param('productId')}`); |
| 89 | + if (!res.ok) { |
| 90 | + throw new Error(`Failed to fetch product: ${c.req.param('productId')}`); |
| 91 | + } |
| 92 | + return c.json({ product: await res.json() }); |
| 93 | +}); |
| 94 | + |
| 95 | +const multiFetchRoutes = new Hono(); |
| 96 | +multiFetchRoutes.route('/inventory', inventoryApp); |
| 97 | +multiFetchRoutes.route('/storefront', storefrontApp); |
| 98 | + |
| 99 | +export { multiFetchRoutes }; |
0 commit comments