diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-07-27 17:03:10 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-07-27 18:30:30 -0700 |
| commit | 9970036d203ba2d0a46b35ba6fad21d49441cdd4 (patch) | |
| tree | a585d13933bf4149dcb07e28526063d071453105 /lib/server/filter/method.ts | |
| download | pengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.tar.gz pengueno-9970036d203ba2d0a46b35ba6fad21d49441cdd4.zip | |
hai
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(); |
