import { Router, Request, Response } from 'express'
import cors from 'cors'
import { BaseApi } from './BaseApi.js'

const router = Router()
export default router

class ApiEndpointRoute extends BaseApi {
  async endpoint(req: Request, res: Response): Promise<void> {
    res.setHeader('content-type', 'application/json')
    res.json({
      message: 'API endpoint reached',
      method: req.method,
      headers: req.headers,
      url: req.url,
    })
  }
}

const api = new ApiEndpointRoute()
api.setCors(router)
const checkCorsFn = api._processCors
router.get('/endpoint', cors(checkCorsFn), api.endpoint.bind(api))
router.post('/endpoint', cors(checkCorsFn), api.endpoint.bind(api))
