Skip to content

Commit

Permalink
feat: Add Deno.ServeDefaultExport type (#24879)
Browse files Browse the repository at this point in the history
Closes #23725
  • Loading branch information
bartlomieju authored Aug 5, 2024
1 parent ae8d048 commit 3e1f982
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,35 @@ declare namespace Deno {
info: ServeHandlerInfo,
) => Response | Promise<Response>;

/** Interface that module run with `deno serve` subcommand must conform to.
*
* To ensure your code is type-checked properly, make sure to add `satisfies Deno.ServeDefaultExport`
* to the `export default { ... }` like so:
*
* ```ts
* export default {
* fetch(req) {
* return new Response("Hello world");
* }
* } satisfies Deno.ServeDefaultExport;
* ```
*
* @category HTTP Server
*/
export interface ServeDefaultExport {
/** A handler for HTTP requests. Consumes a request and returns a response.
*
* If a handler throws, the server calling the handler will assume the impact
* of the error is isolated to the individual request. It will catch the error
* and if necessary will close the underlying connection.
*
* @category HTTP Server
*/
fetch: (
request: Request,
) => Response | Promise<Response>;
}

/** Options which can be set when calling {@linkcode Deno.serve}.
*
* @category HTTP Server
Expand Down
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}
5 changes: 5 additions & 0 deletions tests/specs/serve/type_check/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check [WILDCARD]
error: TS2353 [ERROR]: Object literal may only specify known properties, and 'bad' does not exist in type 'ServeDefaultExport'.
bad() {
~~~
at [WILDCARD]main.ts:2:3
4 changes: 4 additions & 0 deletions tests/specs/serve/type_check/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
bad() {
},
} satisfies Deno.ServeDefaultExport;
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check2/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}
5 changes: 5 additions & 0 deletions tests/specs/serve/type_check2/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check [WILDCARD]
error: TS2339 [ERROR]: Property 'doesnt_exist' does not exist on type 'Request'.
console.log(request.doesnt_exist);
~~~~~~~~~~~~
at [WILDCARD]main.ts:3:25
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check2/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
fetch(request) {
console.log(request.doesnt_exist);
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;

0 comments on commit 3e1f982

Please # to comment.