mirror of
https://github.com/denoland/deno.git
synced 2025-03-04 01:44:26 -05:00
feat: Add Deno.ServeDefaultExport type (#24879)
Closes https://github.com/denoland/deno/issues/23725
This commit is contained in:
parent
ae8d048b6c
commit
3e1f98236f
7 changed files with 61 additions and 0 deletions
29
cli/tsc/dts/lib.deno.ns.d.ts
vendored
29
cli/tsc/dts/lib.deno.ns.d.ts
vendored
|
@ -6256,6 +6256,35 @@ declare namespace Deno {
|
||||||
info: ServeHandlerInfo,
|
info: ServeHandlerInfo,
|
||||||
) => Response | Promise<Response>;
|
) => 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}.
|
/** Options which can be set when calling {@linkcode Deno.serve}.
|
||||||
*
|
*
|
||||||
* @category HTTP Server
|
* @category HTTP Server
|
||||||
|
|
6
tests/specs/serve/type_check/__test__.jsonc
Normal file
6
tests/specs/serve/type_check/__test__.jsonc
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"args": "serve --check --port 12345 main.ts",
|
||||||
|
"output": "main.out",
|
||||||
|
"tempDir": true,
|
||||||
|
"exitCode": 1
|
||||||
|
}
|
5
tests/specs/serve/type_check/main.out
Normal file
5
tests/specs/serve/type_check/main.out
Normal 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
|
4
tests/specs/serve/type_check/main.ts
Normal file
4
tests/specs/serve/type_check/main.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export default {
|
||||||
|
bad() {
|
||||||
|
},
|
||||||
|
} satisfies Deno.ServeDefaultExport;
|
6
tests/specs/serve/type_check2/__test__.jsonc
Normal file
6
tests/specs/serve/type_check2/__test__.jsonc
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"args": "serve --check --port 12345 main.ts",
|
||||||
|
"output": "main.out",
|
||||||
|
"tempDir": true,
|
||||||
|
"exitCode": 1
|
||||||
|
}
|
5
tests/specs/serve/type_check2/main.out
Normal file
5
tests/specs/serve/type_check2/main.out
Normal 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
|
6
tests/specs/serve/type_check2/main.ts
Normal file
6
tests/specs/serve/type_check2/main.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
export default {
|
||||||
|
fetch(request) {
|
||||||
|
console.log(request.doesnt_exist);
|
||||||
|
return new Response("Hello world!");
|
||||||
|
},
|
||||||
|
} satisfies Deno.ServeDefaultExport;
|
Loading…
Add table
Reference in a new issue