Skip to content

Commit 89e2284

Browse files
committed
added tests, pruned dependencies, updated proj config
1 parent adaa0e0 commit 89e2284

File tree

10 files changed

+102
-51
lines changed

10 files changed

+102
-51
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"editor.tabSize": 2,
33
"deno.enable": true,
44
"editor.formatOnSave": true,
5-
"editor.defaultFormatter": "denoland.vscode-deno"
5+
"editor.defaultFormatter": "denoland.vscode-deno",
6+
"[typescript]": { "editor.defaultFormatter": "denoland.vscode-deno" }
67
}

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
## Unreleased
22

3+
### Added
4+
5+
- Laxer usage of `ControllerMethodArgs` decorator: now allowing `queries`,
6+
`params`, `header` as literal arguments, so that things still work even if
7+
users accidentally / deliberately use the undocumented singular / plural forms
8+
39
### Changed
410

511
- switched from `deps.ts` and `dev_deps.ts` to `deno.jsonc`
6-
- revamped documentation
7-
- code format
12+
- revamped documentation (JSDoc)
13+
- code format & code format settings for VS Code users
14+
- upgraded dependencies (`zod@^3.24.1`)
815

916
## [0.12.2] - 2024-12-06
1017

deno.jsonc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
{
22
"imports": {
33
"@asteasolutions/zod-to-openapi": "npm:@asteasolutions/zod-to-openapi@^7.3.0",
4-
"@dklab/oak-routing-ctrl": "jsr:@dklab/oak-routing-ctrl@^0.12.2",
54
"@oak/oak": "jsr:@oak/oak@^17.1.3",
65
"@std/assert": "jsr:@std/assert@^1.0.9",
76
"@std/io": "jsr:@std/io@^0.225.0",
87
"@std/path": "jsr:@std/path@^1.0.8",
98
"@std/testing": "jsr:@std/testing@^1.0.6",
10-
"zod": "npm:zod@^3.23.8"
9+
"zod": "npm:zod@^3.24.1"
1110
},
1211
"tasks": {
1312
"pretty": "deno lint --ignore=docs && deno check . && deno fmt",
1413
"test": "deno test -RE",
1514
"check-doc": "deno check --doc .",
1615
"doc": "deno doc --html mod.ts"
16+
},
17+
"fmt": {
18+
"useTabs": false,
19+
"indentWidth": 2,
20+
"semiColons": true,
21+
"singleQuote": false,
22+
"proseWrap": "always"
1723
}
1824
}

deno.lock

Lines changed: 6 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ControllerMethodArgs.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ function getEnhancedHandler(
180180
for (const p of consumerDesirableParams) {
181181
switch (true) {
182182
case p === "param":
183+
case p === "params" as ControllerMethodArg: // undocumented on purpose
183184
// path param
184185
decoratedArgs.push(ctx.params);
185186
break;
@@ -188,10 +189,13 @@ function getEnhancedHandler(
188189
decoratedArgs.push(parsedReqBody);
189190
break;
190191
case p === "query":
192+
case p === "queries" as ControllerMethodArg: // undocumented on purpose
191193
// search query a.k.a URLSearchParams
192194
decoratedArgs.push(parsedReqSearchParams);
193195
break;
194-
case p === "headers": {
196+
case p === "headers":
197+
case p === "header" as ControllerMethodArg: // undocumented on purpose
198+
{
195199
// request headers
196200
const headers: Record<string, string> = {};
197201
ctx.request.headers.forEach((v: string, k: string) => headers[k] = v);

src/ControllerMethodArgs_test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,44 @@ Deno.test("getEnhancedHandler - not declaring any param", async () => {
887887
assertSpyCalls(testHandler, 1);
888888
});
889889

890+
Deno.test("getEnhancedHandler - declaring undocumented params", async () => {
891+
const spyParseOakRequestBody = spy(_internal, "parseOakReqBody");
892+
// deno-lint-ignore no-explicit-any
893+
const testHandler = spy((..._rest: any[]) => 44);
894+
// deno-lint-ignore ban-types
895+
const enhancedHandler: Function = _internal.getEnhancedHandler(
896+
testHandler,
897+
"context" as ControllerMethodArg,
898+
"params" as ControllerMethodArg,
899+
"queries" as ControllerMethodArg,
900+
"header" as ControllerMethodArg,
901+
"hiddenFeature" as ControllerMethodArg,
902+
);
903+
const ctx = createMockContext({
904+
path: "/hello/world",
905+
params: { lorem: "undocumented usage", hiddenFeature: "84" },
906+
headers: [["X-Foo", "Bearer Bar"]],
907+
});
908+
Object.defineProperty(ctx.request, "body", {
909+
get: () => createMockRequestBody("binary"),
910+
});
911+
Object.defineProperty(ctx.request.url, "searchParams", {
912+
value: new Map([["ipsum", "dolor"]]),
913+
});
914+
await enhancedHandler(ctx);
915+
const [context, param, query, headers, hiddenFeature] =
916+
testHandler.calls[0].args;
917+
assertEquals(param, { lorem: "undocumented usage", hiddenFeature: "84" });
918+
assertEquals(query, { ipsum: "dolor" });
919+
assertEquals(hiddenFeature, "84");
920+
assertEquals(context, ctx);
921+
assertEquals(headers, { "x-foo": "Bearer Bar" });
922+
assertEquals(testHandler.calls[0].returned, 44);
923+
assertSpyCalls(testHandler, 1);
924+
assertSpyCalls(spyParseOakRequestBody, 1);
925+
spyParseOakRequestBody.restore();
926+
});
927+
890928
/**
891929
* @NOTE if/when `oak` supports such a method, better import from there instead
892930
*/

src/__snapshots__/useOakServer_test.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ snapshot[`useOakServer - fully decorated Controller 1`] = `
5757
path: "/test/baz/:zaz",
5858
regexp: /^\\/test\\/baz(?:\\/([^\\/#\\?]+?))[\\/#\\?]?\$/i,
5959
},
60+
{
61+
methods: [
62+
"HEAD",
63+
"GET",
64+
],
65+
middleware: [
66+
[AsyncFunction (anonymous)],
67+
],
68+
options: {
69+
end: undefined,
70+
ignoreCaptures: undefined,
71+
sensitive: undefined,
72+
strict: undefined,
73+
},
74+
paramNames: [
75+
"zaz_zaz",
76+
],
77+
path: "/test/dolor/:zaz_zaz",
78+
regexp: /^\\/test\\/dolor(?:\\/([^\\/#\\?]+?))[\\/#\\?]?\$/i,
79+
},
6080
{
6181
methods: [
6282
"PUT",

src/useOakServer_test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class TestController {
6666
baz(query: Record<string, string>, param: Record<string, string>) {
6767
return `hello, path /baz/${param.zaz} with query ${query.someKey}`;
6868
}
69+
@Get("/dolor/:zaz_zaz")
70+
@ControllerMethodArgs("params" as ControllerMethodArg) // intentional coercing to test undocumented usage
71+
dolor(param: Record<string, string>) {
72+
return `hello, path /dolor/${param.zaz_zaz}`;
73+
}
6974
@Put("/taz/:someId")
7075
@ControllerMethodArgs("body")
7176
taz(body: ArrayBuffer, ctx: RouteContext<"/taz/:someId">) {
@@ -188,6 +193,12 @@ Deno.test({
188193
mockRequestPathParams: { zaz: "jaz" },
189194
expectedResponse: "hello, path /baz/jaz with query chaz",
190195
},
196+
{
197+
caseDescription: "handler with 'params' as undocumented usage",
198+
method: "get",
199+
mockRequestPathParams: { zaz_zaz: "jaz_jaz" },
200+
expectedResponse: "hello, path /dolor/jaz_jaz",
201+
},
191202
{
192203
caseDescription: "handler for a request with a binary payload",
193204
method: "put",

src/useOas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
OpenAPIRegistry,
66
type RouteConfig,
77
} from "@asteasolutions/zod-to-openapi";
8-
import { type OpenAPIObjectConfig } from "npm:@asteasolutions/zod-to-openapi@^7.2.0/dist/v3.0/openapi-generator";
8+
import { type OpenAPIObjectConfig } from "@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator";
99
import { debug } from "./utils/logger.ts";
1010
import { inspect } from "./utils/inspect.ts";
1111

src/utils/schema_utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
extendZodWithOpenApi,
33
type ResponseConfig,
44
type RouteConfig,
5-
} from "npm:@asteasolutions/zod-to-openapi@^7.2.0";
5+
} from "@asteasolutions/zod-to-openapi";
66

77
/**
88
* Open API Schema interface, usable when composing the request/response
@@ -36,7 +36,7 @@ export type OakOpenApiSpec =
3636
};
3737

3838
// must import from `npm:` instead of from `deno.land` to be compatible with `@asteasolutions/zod-to-openapi`
39-
import { z as slowTypedZ } from "npm:zod@^3.23.8";
39+
import { z as slowTypedZ } from "zod";
4040
extendZodWithOpenApi(slowTypedZ);
4141
type SubsetOfZ = Pick<
4242
typeof slowTypedZ,

0 commit comments

Comments
 (0)