import type { IncomingMessage, ServerResponse } from 'http'

export function cors(req: IncomingMessage, res: ServerResponse, next: () => void) {
	res.setHeader('Access-Control-Allow-Origin', '*')
	res.setHeader('Access-Control-Request-Method', '*')
	res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT')
	res.setHeader('Access-Control-Allow-Headers', '*')
	if (req.method === 'OPTIONS') {
		res.writeHead(200)
		res.end()
		return
	}
	next()
}
