Skip to content

Commit

Permalink
refactor: cleanup custom inspect methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jul 27, 2024
1 parent ef8aff3 commit 24b050a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 101 deletions.
20 changes: 20 additions & 0 deletions _examples/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
// Copyright 2018-2024 the oak authors. All rights reserved.

/**
* This is a simple example of a REST API using acorn and Deno KV storage.
*
* It contains the following routes:
*
* - `GET /` - A simple route that increments a count in a cookie and returns
* the count.
* - `GET /redirect` - A route that redirects to `/book/1` using route
* parameters.
* - `GET /book` - A route that returns a list of all books.
* - `GET /book/:id` - A route that returns a single book by its id.
* - `POST /book` - A route that creates a new book.
* - `PUT /book/:id` - A route that updates a book by its id, you must put a
* full book object in the body.
* - `PATCH /book/:id` - A route that updates a book by its id.
* - `DELETE /book/:id` - A route that deletes a book by its id.
*
* @module
*/

import { Router, Status, v } from "../mod.ts";
import { assert } from "@oak/commons/assert";

Expand Down
16 changes: 0 additions & 16 deletions request_event_cfw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@ export class CloudflareWorkerRequestEvent<
this.#resolve(response);
}

[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
return `${this.constructor.name} ${
inspect({
addr: this.#addr,
env: this.#env,
id: this.#id,
request: this.#request,
responded: this.#responded,
response: this.#response,
url: this.#url,
})
}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
Expand Down
35 changes: 19 additions & 16 deletions request_server_bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,6 @@ class BunRequestEvent<
this.#resolve(response);
}

[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
return `${this.constructor.name} ${
inspect({
addr: this.#addr,
env: this.env,
id: this.#id,
request: this.#request,
responded: this.#responded,
response: this.#response,
url: this.#url,
})
}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -278,4 +262,23 @@ export default class BunRequestServer<
}
return this.#stream[Symbol.asyncIterator]();
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
// deno-lint-ignore no-explicit-any
): any {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}

const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
return `${options.stylize(this.constructor.name, "special")} ${
inspect({ closed: this.#closed }, newOptions)
}`;
}
}
2 changes: 1 addition & 1 deletion request_server_deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "@std/assert/equals";
import DenoServer from "./request_server_deno.ts";

Deno.test({
name: "DenoServer should be closed initially",
name: "DenoServer - should be closed initially",
fn() {
const { signal } = new AbortController();
const server = new DenoServer({ signal });
Expand Down
46 changes: 0 additions & 46 deletions request_server_deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,6 @@ class DenoRequestEvent<
})
}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
// deno-lint-ignore no-explicit-any
): any {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}

const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
return `${options.stylize(this.constructor.name, "special")} ${
inspect({
addr: this.#addr,
env: this.#env,
id: this.#id,
request: this.#request,
responded: this.#responded,
response: this.#promise,
url: this.#url,
}, newOptions)
}`;
}
}

/**
Expand Down Expand Up @@ -298,23 +271,4 @@ export default class DenoServer<
): string {
return `${this.constructor.name} ${inspect({ closed: this.#closed })}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
options: any,
inspect: (value: unknown, options?: unknown) => string,
// deno-lint-ignore no-explicit-any
): any {
if (depth < 0) {
return options.stylize(`[${this.constructor.name}]`, "special");
}

const newOptions = Object.assign({}, options, {
depth: options.depth === null ? null : options.depth - 1,
});
return `${options.stylize(this.constructor.name, "special")} ${
inspect({ closed: this.#closed }, newOptions)
}`;
}
}
22 changes: 0 additions & 22 deletions request_server_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,6 @@ class NodeRequestEvent<Env extends Record<string, string>>
this.#resolve(response);
}

[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
return `${this.constructor.name} ${
inspect({
addr: this.addr,
env: this.env,
id: this.#id,
request: this.#request,
responded: this.#responded,
response: this.#promise,
url: this.#url,
})
}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -302,12 +286,6 @@ export default class NodeRequestServer<
return this.#stream[Symbol.asyncIterator]();
}

[Symbol.for("Deno.customInspect")](
inspect: (value: unknown) => string,
): string {
return `${this.constructor.name} ${inspect({ closed: this.#closed })}`;
}

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
// deno-lint-ignore no-explicit-any
Expand Down

0 comments on commit 24b050a

Please sign in to comment.