1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import {
Either,
HttpMethod,
IEither,
type ITraceable,
LogLevel,
PenguenoError,
type PenguenoRequest,
type RequestFilter,
type ServerTrace,
TraceUtil,
} from '@emprespresso/pengueno';
export const requireMethod =
(methods: Array<HttpMethod>): RequestFilter<HttpMethod> =>
(req: ITraceable<PenguenoRequest, ServerTrace>) =>
req
.flatMap(TraceUtil.withFunctionTrace(requireMethod))
.map((t): IEither<PenguenoError, HttpMethod> => {
const {
req: { method },
} = t.get();
if (!methods.includes(method)) {
const msg = "that's not how you pet me (⋟﹏⋞)~";
t.trace.traceScope(LogLevel.WARN).trace(msg);
return Either.left(new PenguenoError(msg, 405));
}
return Either.right(method);
})
.get();
|