diff options
Diffstat (limited to 'lib/server/filter/method.ts')
-rw-r--r-- | lib/server/filter/method.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/server/filter/method.ts b/lib/server/filter/method.ts new file mode 100644 index 0000000..7d6aa76 --- /dev/null +++ b/lib/server/filter/method.ts @@ -0,0 +1,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(); |