Skip to content

Commit

Permalink
fix: update expose for status routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jul 17, 2024
1 parent d388765 commit 5616d15
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
5 changes: 3 additions & 2 deletions context.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the oak authors. All rights reserved.

import { assertEquals } from "@std/assert/assert-equals";
import { assertEquals } from "@std/assert/equals";
import { Schema } from "./schema.ts";
import { MockRequestEvent } from "./testing_utils.ts";

Expand All @@ -18,14 +18,15 @@ Deno.test({
},
);
const responseHeaders = new Headers();
const schema = new Schema();
const schema = new Schema(undefined, false);
const context = new Context(
requestEvent,
responseHeaders,
true,
{ item: "123" },
schema,
undefined,
false,
);
assertEquals(context.addr, {
hostname: "localhost",
Expand Down
2 changes: 1 addition & 1 deletion request_server_deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the oak authors. All rights reserved.

import { assertEquals } from "@std/assert/assert-equals";
import { assertEquals } from "@std/assert/equals";
import DenoServer from "./request_server_deno.ts";

Deno.test({
Expand Down
1 change: 1 addition & 0 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,7 @@ export class Router<
handler,
schemaDescriptor,
this.#keys,
this.#expose,
);
this.#logger.debug(`adding status route for ${status.join(", ")}`);
this.#statusRoutes.add(route);
Expand Down
40 changes: 23 additions & 17 deletions schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { isHttpError } from "@oak/commons/http_errors";
import { assert } from "@std/assert/assert";
import { assertEquals } from "@std/assert/assert-equals";
import { assertRejects } from "@std/assert/assert-rejects";
import { assertEquals } from "@std/assert/equals";
import { assertRejects } from "@std/assert/rejects";
import * as v from "@valibot/valibot";
import { MockRequestEvent } from "./testing_utils.ts";

Expand All @@ -12,7 +12,7 @@ import { Schema } from "./schema.ts";
Deno.test({
name: "Schema - empty schema should passthrough values for querystring",
async fn() {
const schema = new Schema();
const schema = new Schema(undefined, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1&b=2", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -26,7 +26,7 @@ Deno.test({
Deno.test({
name: "Schema - empty schema should passthrough values for body",
async fn() {
const schema = new Schema();
const schema = new Schema(undefined, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1&b=2", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -40,7 +40,7 @@ Deno.test({
Deno.test({
name: "Schema - empty schema should passthrough values for response",
async fn() {
const schema = new Schema();
const schema = new Schema(undefined, false);
const result = await schema.validateResponse({ hello: "world" });
assertEquals(result, { output: { hello: "world" } });
},
Expand All @@ -51,7 +51,7 @@ Deno.test({
async fn() {
const schema = new Schema({
querystring: v.object({ a: v.string(), b: v.string() }),
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1&b=2", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -65,7 +65,7 @@ Deno.test({
Deno.test({
name: "Schema - body schema should validate body",
async fn() {
const schema = new Schema({ body: v.object({ c: v.number() }) });
const schema = new Schema({ body: v.object({ c: v.number() }) }, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1&b=2", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -79,7 +79,10 @@ Deno.test({
Deno.test({
name: "Schema - response schema should validate response",
async fn() {
const schema = new Schema({ response: v.object({ hello: v.string() }) });
const schema = new Schema(
{ response: v.object({ hello: v.string() }) },
false,
);
const result = await schema.validateResponse({ hello: "world" });
assertEquals(result, { output: { hello: "world" } });
},
Expand All @@ -90,7 +93,7 @@ Deno.test({
async fn() {
const schema = new Schema({
querystring: v.object({ a: v.string(), b: v.string() }),
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -107,7 +110,7 @@ Deno.test({
Deno.test({
name: "Schema - invalid body should reject with 400",
async fn() {
const schema = new Schema({ body: v.object({ c: v.string() }) });
const schema = new Schema({ body: v.object({ c: v.string() }) }, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1&b=2", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -124,7 +127,10 @@ Deno.test({
Deno.test({
name: "Schema - invalid response should should reject with 500",
async fn() {
const schema = new Schema({ response: v.object({ hello: v.number() }) });
const schema = new Schema(
{ response: v.object({ hello: v.number() }) },
false,
);
const error = await assertRejects(async () => {
await schema.validateResponse({ hello: "world" });
});
Expand All @@ -143,7 +149,7 @@ Deno.test({
assertEquals(issues.length, 1);
return new Response("Invalid querystring", { status: 400 });
},
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -165,7 +171,7 @@ Deno.test({
assertEquals(issues.length, 1);
return new Response("Invalid querystring", { status: 400 });
},
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -187,7 +193,7 @@ Deno.test({
assertEquals(issues.length, 1);
return new Response("Invalid querystring", { status: 400 });
},
});
}, false);
const result = await schema.validateResponse({ hello: "world" });
assert(result.invalidResponse instanceof Response);
assertEquals(result.invalidResponse.status, 400);
Expand All @@ -202,7 +208,7 @@ Deno.test({
invalidHandler() {
throw new Error("Boom");
},
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -226,7 +232,7 @@ Deno.test({
invalidHandler() {
throw new Error("Boom");
},
});
}, false);
const requestEvent = new MockRequestEvent("http://localhost/?a=1", {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand All @@ -250,7 +256,7 @@ Deno.test({
invalidHandler() {
throw new Error("Boom");
},
});
}, false);
const error = await assertRejects(async () => {
await schema.validateResponse({ hello: "world" });
});
Expand Down
8 changes: 6 additions & 2 deletions status_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class StatusRoute<
QSSchema extends QueryStringSchema = QueryStringSchema,
QueryParams extends InferOutput<QSSchema> = InferOutput<QSSchema>,
> {
#expose: boolean;
#handler: StatusHandler<Status, Env, QSSchema, QueryParams>;
#keys?: KeyRing;
#schema: Schema<QSSchema, BodySchema, BodySchema>;
Expand All @@ -130,12 +131,14 @@ export class StatusRoute<
status: (Status | StatusRange)[],
handler: StatusHandler<Status, Env, QSSchema, QueryParams>,
schemaDescriptor: SchemaDescriptor<QSSchema, BodySchema, BodySchema>,
keys?: KeyRing,
keys: KeyRing | undefined,
expose: boolean,
) {
this.#status = status;
this.#handler = handler;
this.#keys = keys;
this.#schema = new Schema(schemaDescriptor);
this.#expose = expose;
this.#schema = new Schema(schemaDescriptor, expose);
}

/**
Expand Down Expand Up @@ -163,6 +166,7 @@ export class StatusRoute<
undefined,
this.#schema,
this.#keys,
this.#expose,
);
const result = await this.#handler(context, response.status, response);
if (result instanceof Response) {
Expand Down

0 comments on commit 5616d15

Please sign in to comment.