Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## [0.10.0] - 2024-09-08

### Added

- support for `headers` in `ControllerMethodArg` as a shortcut to parse &
retrieve request headers

### Changed

- upgraded dependencies (`@oak/oak@17.0.0`, `@std/path@1.0.4`
`@std/assert@1.0.4`, `@std/testing@1.0.2`, `@std/io@0.224.7`)

- minor TypeScript syntax updates to better support Deno 2

## [0.9.0] - 2024-07-16

### Changed
Expand Down
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export { join } from "jsr:@std/path@^0.225.2";
export { join } from "jsr:@std/path@^1.0.4";

export { Router } from "jsr:@oak/oak@^16.1.0";
export { Router } from "jsr:@oak/oak@^17.0.0";

export type {
Application,
Context,
Next,
RouteContext,
} from "jsr:@oak/oak@^16.1.0";
} from "jsr:@oak/oak@^17.0.0";

import {
extendZodWithOpenApi,
Expand Down
14 changes: 7 additions & 7 deletions dev_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ export {
assertObjectMatch,
assertStringIncludes,
assertThrows,
} from "jsr:@std/assert@^1.0.0";
} from "jsr:@std/assert@^1.0.4";

export {
type BodyType,
type Middleware,
Request,
testing as oakTesting,
} from "jsr:@oak/oak@^16.1.0";
} from "jsr:@oak/oak@^17.0.0";

export { Body } from "jsr:@oak/oak@^16.1.0/body";
export { Body } from "jsr:@oak/oak@^17.0.0/body";

export {
assertSpyCall,
Expand All @@ -24,17 +24,17 @@ export {
spy,
type Stub,
stub,
} from "jsr:@std/testing@^0.225.3/mock";
} from "jsr:@std/testing@^1.0.2/mock";

export { assertSnapshot } from "jsr:@std/testing@^0.225.3/snapshot";
export { assertSnapshot } from "jsr:@std/testing@^1.0.2/snapshot";

export { Buffer } from "jsr:@std/io@^0.224.3";
export { Buffer } from "jsr:@std/io@^0.224.7";

export {
afterEach,
beforeEach,
describe,
it,
} from "jsr:@std/testing@^0.225.3/bdd";
} from "jsr:@std/testing@^1.0.2/bdd";

export { ZodObject } from "npm:zod@^3.23.8";
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dklab/oak-routing-ctrl",
"version": "0.9.0",
"version": "0.10.0",
"exports": {
".": "./mod.ts",
"./mod": "./mod.ts"
Expand Down
26 changes: 19 additions & 7 deletions src/ControllerMethodArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { ERR_UNSUPPORTED_CLASS_METHOD_DECORATOR_RUNTIME_BEHAVIOR } from "./Const
export type ControllerMethodArg =
| "param"
| "body"
| "query";
| "query"
| "headers";

// an enhanced version of a (decorated) method which
// is declared in the (decorated) Controller Class
Expand Down Expand Up @@ -127,22 +128,26 @@ function getEnhancedHandler(
try {
parsedReqBody = await _internal.parseOakReqBody(ctx);
} catch (e) {
return ctx.throw(400, `Unable to parse request body: ${e.message}`, {
stack: e.stack,
});
return ctx.throw(
400,
`Unable to parse request body: ${(e as Error).message}`,
{
stack: (e as Error).stack,
},
);
}

const parsedReqSearchParams: Record<string, string> = {};
try {
ctx.request.url.searchParams.forEach((value, key) =>
ctx.request.url.searchParams.forEach((value: string, key: string) =>
parsedReqSearchParams[key] = value
);
} catch (e) {
return ctx.throw(
400,
`Unable to parse request search params: ${e.message}`,
`Unable to parse request search params: ${(e as Error).message}`,
{
stack: e.stack,
stack: (e as Error).stack,
},
);
}
Expand All @@ -164,6 +169,13 @@ function getEnhancedHandler(
// search query a.k.a URLSearchParams
decoratedArgs.push(parsedReqSearchParams);
break;
case p === "headers": {
// request headers
const headers: Record<string, string> = {};
ctx.request.headers.forEach((v: string, k: string) => headers[k] = v);
decoratedArgs.push(headers);
break;
}
case ["ctx", "context"].includes(p):
// `ctx` or `context` is supported by default (as the last argument)
// but can also be declared explicitly
Expand Down
Loading