test(docs): type check docs code examples (#3180)

This PR adds the initial seeds of type checking the code in all code
blocks for the Markdown Docs.

- Adds a unit test, disabled due to the many remaining errors, that can
help find incorrect code
- Fixes a bunch of syntax errors, type errors, and other code issues
found in the docs

## Future improvements

A lot of remaining issues is mostly related to missing imports, e.g.
`App`, `staticFiles`, `HttpError` etc. There's a couple of different
directions to solve this in future PR's.

- Add explicit imports to all docs examples (may become unnecessarily
verbose)
- Add code to docs code blocks, but with a hint to remove it when
rendering the markdown as HTML
   ```tsx
   import { App } from "fresh";
   // #docs-start

   // Everything above marker above is trimmed away
   const app = new App();
   ```
- Add a `types.d.ts` with "global" types used during type checking, so
globals, imports and so on can be handled globally

I do think that some examples probably should be more explicit with
imports, but it would be overkill for a lot of places such as `app.md`
and `context.md`.
This commit is contained in:
Christian Svensson
2025-08-05 23:56:04 +02:00
committed by GitHub
parent e014d80a0f
commit b7f1abb0d0
13 changed files with 142 additions and 24 deletions
+4 -4
View File
@@ -20,7 +20,7 @@ app.listen();
All items are applied from top to bottom. This means that when you defined a
middleware _after_ a `.get()` handler, it won't be included.
```ts main.ts
```tsx main.tsx
const app = new App()
.use((ctx) => {
// Will be called for all middlewares
@@ -278,9 +278,9 @@ Setting a middleware:
```ts
// top level error handler
app.onError("*", ctx => {
return new Response(String(ctx.error), { status: 500 })
}))
app.onError("*", (ctx) => {
return new Response(String(ctx.error), { status: 500 });
});
```
Setting a route:
+1 -1
View File
@@ -28,7 +28,7 @@ app.get("/", (ctx) => {
console.log("path: ", ctx.url.pathname);
const hasParam = ctx.url.searchParams.has("q");
return new Response(`Has q param: ${String(hasParam)});
return new Response(`Has q param: ${hasParam}`);
});
```
+1 -1
View File
@@ -25,7 +25,7 @@ if (Deno.args.includes("build")) {
```ts main.ts
import { App, staticFiles } from "fresh";
const app = new App({ root: import.meta.url })
const app = new App({ basePath: "/foo" })
.use(staticFiles())
.fsRoutes(); // This inserts all file based routes here
```
+5 -1
View File
@@ -29,7 +29,7 @@ An island can be used anywhere like a regular Preact component. Fresh will take
care of making it interactive on the client.
```tsx main.tsx
import MyIsland from "../islands/my-island.tsx";
import MyIsland from "./islands/my-island.tsx";
const app = new App()
.get("/", (ctx) => ctx.render(<MyIsland />));
@@ -94,6 +94,10 @@ In essence, Fresh allows you to mix static and interactive parts in your app in
a way that's most optimal for your app. We'll keep sending only the JavaScript
that is needed for the islands to the browser.
```tsx islands/other-island.tsx
export default (props: { foo: string }) => <>{props.foo}</>;
```
```tsx route/index.tsx
import MyIsland from "../islands/my-island.tsx";
import OtherIsland from "../islands/other-island.tsx";
+1 -1
View File
@@ -80,7 +80,7 @@ const middleware1 = define.middleware(async (ctx) => {
return await ctx.next();
});
const middleware1 = define.middleware(async (ctx) => {
const middleware2 = define.middleware(async (ctx) => {
console.log("B");
return await ctx.next();
});
+7 -7
View File
@@ -10,21 +10,21 @@ request.
const app = new App()
.get("/", () => new Response("hello")) // Responds to: GET /
.get("/other", () => new Response("other")) // Responds to: GET /other
.post("/upload", () => new Response("upload")); // Responds to: POST /upload
.post("/upload", () => new Response("upload")) // Responds to: POST /upload
.get("/books/:id", (ctx) => {
// Responds to: GET /books/my-book, /books/cool-book, etc
const id = ctx.params.id
return new Response(`Book id: ${id}`));
const id = ctx.params.id;
return new Response(`Book id: ${id}`);
})
.get("/blog/:post/comments", () => {
// Responds to: GET /blog/my-post/comments, /blog/hello/comments, etc
const post = ctx.params.post
return new Response(`Blog post comments for post: ${post}`)
const post = ctx.params.post;
return new Response(`Blog post comments for post: ${post}`);
})
.get("/foo/*", (ctx) => {
// Responds to: GET /foo/bar, /foo/bar/baz, etc
return new Response("foo"));
})
return new Response("foo");
});
```
Fresh supports the full
+2 -1
View File
@@ -252,9 +252,10 @@ function testApp() {
.get("/", () => new Response("hello"));
// Applies build snapshot to this app instance.
applySnapshot(app);
return app;
}
Deno.test("My Test", () => {
Deno.test("My Test", async () => {
const handler = testApp().handler();
const response = await handler(new Request("http://localhost"));
+2 -2
View File
@@ -13,8 +13,8 @@ web applications.
import { App } from "fresh";
const app = new App()
.get("/", () => new Response("hello world"));
.get("/jsx", ctx => ctx.render(<h1>render JSX!</h1>));
.get("/", () => new Response("hello world"))
.get("/jsx", (ctx) => ctx.render(<h1>render JSX!</h1>));
app.listen();
```
+2
View File
@@ -14,6 +14,8 @@ header. to HTTP requests. These allow the server to indicate which origins
from.
```ts main.ts
import { app, csrf } from "fresh";
const app = new App();
app.use(csrf());
+1 -1
View File
@@ -60,7 +60,7 @@ decided on your own when it's best to update to v4.
```ts dev.ts
import { Builder } from "fresh/dev";
import { tailwind } from "@fresh/plugin-tailwindcss-v3";
import { tailwind } from "@fresh/plugin-tailwind-v3";
tailwind(builder, {});
```
+3 -2
View File
@@ -19,7 +19,7 @@ import { App } from "fresh";
const middleware = define.middleware((ctx) => {
ctx.state.text = "middleware text";
return await ctx.next();
return ctx.next();
});
Deno.test("My middleware - sets ctx.state.text", async () => {
@@ -124,9 +124,10 @@ function testApp() {
// Applies build snapshot to this app instance.
applySnapshot(app);
return app;
}
Deno.test("My Test", () => {
Deno.test("My Test", async () => {
const handler = testApp().handler();
const response = await handler(new Request("http://localhost"));