Skip to content

Commit ebbb4e7

Browse files
committed
Update routing, simplify internal types
1 parent d3ebe40 commit ebbb4e7

35 files changed

Lines changed: 451 additions & 583 deletions

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,79 +34,81 @@ Create a directory to hold your API methods (default: `hyper-api` in your projec
3434
```
3535
my-project/
3636
├── hyper-api/
37-
│ ├── users.get.ts
38-
│ ├── users.post.ts
37+
│ ├── users+get.ts
38+
│ ├── users+post.ts
3939
│ └── products/
40-
│ ├── [id].get.ts
41-
│ └── search.get.ts
40+
│ ├── [id]+get.ts
41+
│ └── search+get.ts
4242
├── index.ts
4343
└── package.json
4444
```
4545

46-
HyperAPI uses file names to determine routes and HTTP methods:
46+
HyperAPI uses file names to determine routes and HTTP methods.
4747

4848
#### Static Routes
4949

5050
File name will require an exact match. For example:
5151

5252
| File name | Route pattern | Matched requests |
5353
| - | - | - |
54-
| `/users.ts` | `/users` | `GET /users` |
54+
| `/users+get.ts` | `/users` | `GET /users` |
55+
| `/users+post.ts` | `/users` | `POST /users` |
5556

5657
#### Index Routes
5758

5859
Files named as `index.ts` do not add `index` to the route.
5960

6061
| File name | Route pattern | Matched requests |
6162
| - | - | - |
62-
| `/index.ts` | `/` | `GET /` |
63-
| `/account/index.ts` | `/account` | `GET /account` |
63+
| `/index+get.ts` | `/` | `GET /` |
64+
| `/account/index+get.ts` | `/account` | `GET /account` |
6465

6566
#### Route parameters
6667

6768
Wrap any route parameter with `[]` to capture it.
6869

6970
| File name | Route pattern | Matched requests |
7071
| - | - | - |
71-
| `/users/[id].ts` | `/users/:id` | `GET /users/123` with `{ id: "123" }` <br> `GET /users/foo` with `{ id: "foo" }` |
72+
| `/users/[id]+get.ts` | `/users/:id` | `GET /users/123` with `{ id: "123" }` <br> `GET /users/foo` with `{ id: "foo" }` |
7273

7374
#### Optional route parameters
7475

7576
Make a route parameter optional by wrapping it name with `[[]]` (double brackets).
7677

7778
| File name | Route pattern | Matched requests |
7879
| - | - | - |
79-
| `/users/[[id]].ts` | `/users/:id?` | `GET /users` with `{}` <br> `GET /users/123` with `{ id: "123" }` <br> `GET /users/foo` with `{ id: "foo" }` |
80+
| `/users/[[id]]+get.ts` | `/users/:id?` | `GET /users` with `{}` <br> `GET /users/123` with `{ id: "123" }` <br> `GET /users/foo` with `{ id: "foo" }` |
8081

8182
You can mix parameters (both required and optional) in a single route segment:
8283

8384
| File name | Route pattern | Matched requests |
8485
| - | - | - |
85-
| `/files/[name].[[ext]].ts` | `/files/:name.:ext?` | `GET /files/image.png` with `{ name: "image", ext: "png" }` <br> `GET /files/README` with `{ name: "README" }` |
86+
| `/files/[name].[[ext]]+get.ts` | `/files/:name.:ext?` | `GET /files/image.png` with `{ name: "image", ext: "png" }` <br> `GET /files/README` with `{ name: "README" }` |
8687

8788
#### Catch-all parameters
8889

8990
Route parameter names as `[...name]` consumes all remaining path segments. This route can only be used in a file name.
9091

9192
| File name | Route pattern | Matched requests | Unmatched requests |
9293
| - | - | - | - |
93-
| `/docs/[...path].ts` | `/docs/:path+` | `GET /docs/foo` with `{ path: "foo" }` <br> `GET /docs/a/b/c` with `{ path: "a/b/c" }` | `GET /docs` |
94+
| `/docs/[...path]+get.ts` | `/docs/:path+` | `GET /docs/foo` with `{ path: "foo" }` <br> `GET /docs/a/b/c` with `{ path: "a/b/c" }` | `GET /docs` |
9495

9596
#### Optional catch-all parameters
9697

9798
Route parameter names as `[[...name]]` consumes all remaining path segments, but also matches routes with no segments. This route can only be used in a file name.
9899

99100
| File name | Route pattern | Matched requests |
100101
| - | - | - |
101-
| `/docs/[[...path]].ts` | `/docs/:path+` | `GET /docs` with `{}` <br> `GET /docs/foo` with `{ path: "foo" }` <br> `GET /docs/a/b/c` with `{ path: "a/b/c" }` |
102+
| `/docs/[[...path]]+get.ts` | `/docs/:path+` | `GET /docs` with `{}` <br> `GET /docs/foo` with `{ path: "foo" }` <br> `GET /docs/a/b/c` with `{ path: "a/b/c" }` |
102103

103104
#### HTTP methods
104105

105-
Add HTTP method before an extension to make route match only given HTTP method. By default, route will serve any incoming HTTP method.
106+
Add HTTP method before an extension to make route match only given HTTP method. If no method is specified, the route will match special `UNDEF` method which is used by drivers that do not rely on HTTP stack such as WebSockets or [Tasq](https://github.com/kirick-ts/tasq).
106107

107108
| File name | Route pattern | Matched requests | Unmatched requests |
108109
| - | - | - | - |
109-
| `/user/[id].get.ts` | `/user/:id` | `GET /user/1` with `{ id: "1" }` | `POST /user/1` <br> `DELETE /user/1` |
110+
| `/user/[id]+get.ts` | `/user/:id` | `GET /user/1` with `{ id: "1" }` | `POST /user/1` <br> `DELETE /user/1` |
111+
| `/user/[id].ts` | `/user/:id` | `UNDEF /user/1` with `{ id: "1" }` | `GET /user/1` <br> `POST /user/1` <br> `DELETE /user/1` |
110112

111113
But you can not use HTTP method in the filename if there is no name for the route. If you want to serve `POST /account`, create file `/account.post.ts` or `account/index.post.ts`, not just `account/post.ts`.
112114

@@ -131,7 +133,7 @@ console.log('API server running on http://localhost:3000');
131133

132134
### 3. Create your API handlers
133135

134-
Example of a basic endpoint (`hyper-api/hello.get.ts`):
136+
Example of a basic endpoint (`hyper-api/hello+get.ts`):
135137

136138
```typescript
137139
import * as v from 'valibot';

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"files": {
99
"ignoreUnknown": false,
10-
"includes": ["**", "!**/dist/**", "!**/*.html", "!**/*.ejs", "!**/*.vue"]
10+
"includes": ["**", "!**/dist/**"]
1111
},
1212
"formatter": {
1313
"enabled": true,

0 commit comments

Comments
 (0)