|
| 1 | +import * as express from 'express'; |
| 2 | +import { BurnBlockListResponse } from '@stacks/stacks-blockchain-api-types'; |
| 3 | +import { getETagCacheHandler, setETagCacheHeaders } from '../../controllers/cache-controller'; |
| 4 | +import { asyncHandler } from '../../async-handler'; |
| 5 | +import { PgStore } from '../../../datastore/pg-store'; |
| 6 | +import { parseDbBurnBlock, validRequestParams, validRequestQuery } from './helpers'; |
| 7 | +import { |
| 8 | + BlockPaginationQueryParams, |
| 9 | + BurnBlockParams, |
| 10 | + CompiledBlockPaginationParams, |
| 11 | + CompiledBurnBlockParams, |
| 12 | +} from './schemas'; |
| 13 | + |
| 14 | +export function createV2BurnBlocksRouter(db: PgStore): express.Router { |
| 15 | + const router = express.Router(); |
| 16 | + const cacheHandler = getETagCacheHandler(db); |
| 17 | + |
| 18 | + router.get( |
| 19 | + '/', |
| 20 | + cacheHandler, |
| 21 | + asyncHandler(async (req, res) => { |
| 22 | + if (!validRequestQuery(req, res, CompiledBlockPaginationParams)) return; |
| 23 | + const query = req.query as BlockPaginationQueryParams; |
| 24 | + |
| 25 | + const { limit, offset, results, total } = await db.getBurnBlocks(query); |
| 26 | + const response: BurnBlockListResponse = { |
| 27 | + limit, |
| 28 | + offset, |
| 29 | + total, |
| 30 | + results: results.map(r => parseDbBurnBlock(r)), |
| 31 | + }; |
| 32 | + setETagCacheHeaders(res); |
| 33 | + res.json(response); |
| 34 | + }) |
| 35 | + ); |
| 36 | + |
| 37 | + router.get( |
| 38 | + '/:height_or_hash', |
| 39 | + cacheHandler, |
| 40 | + asyncHandler(async (req, res) => { |
| 41 | + if (!validRequestParams(req, res, CompiledBurnBlockParams)) return; |
| 42 | + const params = req.params as BurnBlockParams; |
| 43 | + |
| 44 | + const block = await db.getBurnBlock(params); |
| 45 | + if (!block) { |
| 46 | + res.status(404).json({ errors: 'Not found' }); |
| 47 | + return; |
| 48 | + } |
| 49 | + setETagCacheHeaders(res); |
| 50 | + res.json(parseDbBurnBlock(block)); |
| 51 | + }) |
| 52 | + ); |
| 53 | + |
| 54 | + return router; |
| 55 | +} |
0 commit comments