fix all type errors

This commit is contained in:
Thomas Ruoff
2021-02-15 23:29:25 +01:00
parent 85a7b54a13
commit 3386fb3928
14 changed files with 123 additions and 132 deletions

View File

@@ -1,45 +1,47 @@
import {promises} from 'fs'
import { promises } from 'fs'
import { NextApiRequest, NextApiResponse } from 'next'
import {remove as storeRemove} from '../../../lib/store'
import { remove as storeRemove } from '../../../lib/store'
import { getPdfPath } from '../../../lib/utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { method } = req;
const { method } = req
switch (req.method) {
case 'GET':
try {
const {
query: { id: idArg },
} = req
switch (req.method) {
case 'GET':
try {
const {
query: { id: idArg },
} = req
const fileContent = await promises.readFile(getPdfPath(idArg))
res.setHeader('Content-Type', 'application/pdf')
res.status(200).send(fileContent);
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break;
case 'DELETE':
try {
const {
query: { id: idArg },
} = req
const id = Array.isArray(idArg) ? idArg[0] : idArg
storeRemove(idArg)
const fileContent = await promises.readFile(getPdfPath(id))
res.setHeader('Content-Type', 'application/pdf')
res.status(200).send(fileContent)
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break
case 'DELETE':
try {
const {
query: { id: idArg },
} = req
res.status(202).end()
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break;
default:
res.setHeader('Allow', ['GET', 'DELETE'])
res.status(405).end(`Method ${method} Not Allowed`)
}
storeRemove(idArg)
res.status(202).end()
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break
default:
res.setHeader('Allow', ['GET', 'DELETE'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}
export default handler;
export default handler