1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 03:34:35 +00:00
Files
server/api/src/middlewares/header.ts
2022-06-21 18:28:13 +03:00

30 lines
969 B
TypeScript

import { Request, Response } from 'express'
export const verifyAcceptHeader = (req: Request, res: Response) => {
const acceptHeader = req.headers.accept
if (!acceptHeader) {
return res.status(406).json(headerIsNotPresentMessage('Accept'))
} else if (acceptHeader !== 'application/json') {
return res.status(406).json(headerIsNotValidMessage('Accept'))
}
}
export const verifyContentTypeHeader = (req: Request, res: Response) => {
const contentTypeHeader = req.headers['content-type']
if (!contentTypeHeader) {
return res.status(406).json(headerIsNotPresentMessage('Content-Type'))
} else if (contentTypeHeader !== 'application/json') {
return res.status(406).json(headerIsNotValidMessage('Content-Type'))
}
}
export const headerIsNotPresentMessage = (header: string) => ({
message: `${header} header is not present.`
})
export const headerIsNotValidMessage = (header: string) => ({
message: `${header} header is not valid.`
})