1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat: Add Deno.ServeDefaultExport type (#24879)

Closes https://github.com/denoland/deno/issues/23725
This commit is contained in:
Bartek Iwańczuk 2024-08-05 22:19:09 +01:00 committed by GitHub
parent ae8d048b6c
commit 3e1f98236f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 61 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}

View file

@ -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

View file

@ -0,0 +1,4 @@
export default {
bad() {
},
} satisfies Deno.ServeDefaultExport;

View file

@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}

View file

@ -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

View file

@ -0,0 +1,6 @@
export default {
fetch(request) {
console.log(request.doesnt_exist);
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;