-
Notifications
You must be signed in to change notification settings - Fork 1
/
intercept_response.ts
62 lines (59 loc) · 2.02 KB
/
intercept_response.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* This is an example of using the {@linkcode interceptResponse},
* {@linkcode skip}, and {@linkcode whenStatus} to to handle 40x responses.
*
* For the case of a `404 Not Found` response within the main cascade,
* it will be converted to a skipped response (null), and handled by
* the custom fallback handler.
*
* A `403 Forbidden` response will be caught by the {@linkcode whenStatus}
* interceptor and a custom forbidden message will be returned.
*
* This example uses the static file router, which unfortunately
* cannot run directly from JSR at present.
*
* You can run the example from a checkout of this repo:
*
* ```sh
* deno task example ./packages/examples/intercept_response.ts
* ```
*
* And try hitting the following URLs in your browser:
*
* - http://localhost:8000/hello.txt
* - http://localhost:8000/private
* - http://localhost:8000/nonexistent
*
* @module
*/
import { staticRoute } from "@http/route/static-route";
import { withFallback } from "@http/route/with-fallback";
import { interceptResponse } from "@http/interceptor/intercept-response";
import { skip } from "@http/interceptor/skip";
import { notFound } from "@http/response/not-found";
import { whenStatus } from "@http/interceptor/when-status";
import { cascade } from "@http/route/cascade";
import { byPattern } from "@http/route/by-pattern";
import { forbidden } from "@http/response/forbidden";
import { port } from "@http/host-deno-local/port";
const server = Deno.serve(
{ port: port() },
withFallback(
interceptResponse(
// This is the main set of handlers...
cascade(
byPattern("/private", () => forbidden()),
staticRoute("/", import.meta.resolve("./public")),
),
// These are the Response Interceptors...
skip(404),
whenStatus(
403,
() => forbidden("I'm sorry Dave, but I'm afraid you can't do that."),
),
),
// Custom Not Found handler
() => notFound("I'm sorry Dave, but I'm afraid I can't find that."),
),
) as Deno.HttpServer;
export default server;