burger-api 0.9.6 → 1.0.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +201 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +47 -0
- package/dist/src/errors/http-error.js +146 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +263 -50
- package/dist/src/index.js +919 -332
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +817 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +201 -0
- package/dist/src/router/router.js +600 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +172 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +2 -9
- package/dist/src/utils/wildcard.js +20 -58
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +79 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +219 -0
- package/dist/src/ws/adapter.js +623 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +156 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +81 -0
- package/dist/src/ws/platform.js +113 -0
- package/dist/src/ws/router.d.ts +48 -0
- package/dist/src/ws/router.js +174 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +193 -0
- package/dist/src/ws/types.d.ts +380 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -59
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
package/README.md
CHANGED
|
@@ -1,278 +1,121 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<a href="https://burger-api.com">
|
|
3
|
-
<img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342"
|
|
3
|
+
<img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342" alt="BurgerAPI"/>
|
|
4
4
|
</a>
|
|
5
5
|
</div>
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://www.npmjs.com/package/burger-api"><img src="https://img.shields.io/badge/version-1.0.0--beta-orange.svg" alt="Version 1.0.0-beta" /></a>
|
|
9
|
+
<a href="https://github.com/isfhan/burger-api/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="MIT License" /></a>
|
|
10
|
+
<a href="https://bun.sh"><img src="https://img.shields.io/badge/Bun-1.4.4%2B-black?logo=bun" alt="Bun 1.4.4+" /></a>
|
|
11
|
+
</p>
|
|
11
12
|
|
|
12
|
-
**burger-api** is a
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
burger-api leverages Bun's native modules to deliver blazing-fast API responses
|
|
17
|
-
while keeping your codebase clean and maintainable.
|
|
13
|
+
**burger-api** is a fast, file-based API framework for Bun, for TypeScript and
|
|
14
|
+
JavaScript. Write routes as files, validate with the schema library you like,
|
|
15
|
+
and get OpenAPI docs for free. Deploy to Bun, Node, Cloudflare Workers, Deno or
|
|
16
|
+
Vercel.
|
|
18
17
|
|
|
19
|
-
**
|
|
20
|
-
|
|
18
|
+
> **1.0.0-beta is a new BurgerAPI**, rewritten from the ground up. Coming from
|
|
19
|
+
> `0.9.7`? Read the [migration guide](https://burger-api.com/docs/migration).
|
|
20
|
+
> Getting an error? Update to the latest version first
|
|
21
|
+
> (`bun add burger-api@latest` and `bun add -g @burger-api/cli@latest`). Still
|
|
22
|
+
> broken? [Open an issue](https://github.com/isfhan/burger-api/issues).
|
|
21
23
|
|
|
22
|
-
##
|
|
23
|
-
|
|
24
|
-
- [Overview](#-overview)
|
|
25
|
-
- [CLI Tool](#-cli-tool)
|
|
26
|
-
- [Changelog](#-changelog)
|
|
27
|
-
- [What's Coming Next](#-whats-coming-next)
|
|
28
|
-
- [Contributing](#-contributing)
|
|
29
|
-
- [License](#-license)
|
|
30
|
-
|
|
31
|
-
## 📖 Documentation
|
|
32
|
-
|
|
33
|
-
For detailed documentation and examples, visit the
|
|
34
|
-
[BurgerAPI official docs](https://burger-api.com/).
|
|
35
|
-
|
|
36
|
-
## 🚀 Overview
|
|
37
|
-
|
|
38
|
-
burger-api is built to offer a robust developer experience through:
|
|
39
|
-
|
|
40
|
-
- ⚡ **Bun-Native Performance:**
|
|
41
|
-
Leverages Bun's high-performance HTTP server.
|
|
42
|
-
|
|
43
|
-
- 📁 **File-Based Routing:**
|
|
44
|
-
Automatically registers API routes from your file structure using a clear
|
|
45
|
-
naming convention.
|
|
46
|
-
|
|
47
|
-
- 🔄 **Middleware Architecture:**
|
|
48
|
-
Supports both global and route-specific middleware. Simple, powerful
|
|
49
|
-
middleware system with three return types:
|
|
50
|
-
|
|
51
|
-
- `Response` - Stop here, send this response
|
|
52
|
-
- `Function` - Transform the final response after handler runs
|
|
53
|
-
- `undefined` - Continue to next middleware
|
|
54
|
-
|
|
55
|
-
- ✅ **Type-Safe Validation:**
|
|
56
|
-
Utilizes Zod for request validation, ensuring full type safety and automatic
|
|
57
|
-
error reporting.
|
|
58
|
-
|
|
59
|
-
- 📚 **Automatic OpenAPI Generation:**
|
|
60
|
-
Generates a complete OpenAPI 3.0 specification directly from your routes and
|
|
61
|
-
Zod schemas.
|
|
62
|
-
|
|
63
|
-
- 🔍 **Swagger UI Integration:**
|
|
64
|
-
Out-of-the-box Swagger UI endpoint for interactive API documentation.
|
|
65
|
-
|
|
66
|
-
## 🛠️ CLI Tool (Newly Added)
|
|
67
|
-
|
|
68
|
-
burger-api comes with a powerful CLI tool that makes it easy to scaffold new
|
|
69
|
-
projects and manage middleware. Install it globally to get started:
|
|
70
|
-
|
|
71
|
-
### Installation
|
|
72
|
-
|
|
73
|
-
**Option 1: Bun Global Installation (Recommended if you have Bun installed)**
|
|
74
|
-
|
|
75
|
-
````bash
|
|
76
|
-
# Global installation
|
|
77
|
-
bun add -g @burger-api/cli
|
|
78
|
-
````
|
|
79
|
-
**Or use with bunx (No Installation Needed):**
|
|
80
|
-
|
|
81
|
-
````bash
|
|
82
|
-
bunx @burger-api/cli create my-project
|
|
83
|
-
````
|
|
84
|
-
|
|
85
|
-
**Option 2: Standalone Executable (Alternative Installation Method)**
|
|
86
|
-
|
|
87
|
-
**macOS, Linux, WSL:**
|
|
88
|
-
|
|
89
|
-
```bash
|
|
90
|
-
curl -fsSL https://burger-api.com/install.sh | bash
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
**Windows PowerShell:**
|
|
94
|
-
|
|
95
|
-
```powershell
|
|
96
|
-
irm https://burger-api.com/install.ps1 | iex
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
**Option 3: Manual Download (Alternative Installation Method)**
|
|
100
|
-
|
|
101
|
-
1. Download the executable for your platform from
|
|
102
|
-
[GitHub Releases](https://github.com/isfhan/burger-api/releases/latest)
|
|
103
|
-
2. Add to PATH
|
|
104
|
-
3. Make executable (Linux/macOS): `chmod +x burger-api`
|
|
105
|
-
|
|
106
|
-
**Verify Installation (To Check if the CLI is Installed Correctly)**
|
|
107
|
-
|
|
108
|
-
```bash
|
|
109
|
-
burger-api --version
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
**Package Details:**
|
|
113
|
-
|
|
114
|
-
- **npm package:**
|
|
115
|
-
[`@burger-api/cli`](https://www.npmjs.com/package/@burger-api/cli)
|
|
116
|
-
- **Requires:** Bun >=1.3.0
|
|
117
|
-
|
|
118
|
-
### Quick Start
|
|
24
|
+
## Get started
|
|
119
25
|
|
|
120
26
|
```bash
|
|
121
|
-
|
|
122
|
-
burger-api create my-api
|
|
123
|
-
|
|
124
|
-
# Navigate to your project
|
|
27
|
+
bunx @burger-api/cli create my-api
|
|
125
28
|
cd my-api
|
|
126
|
-
|
|
127
|
-
# Start development server
|
|
128
29
|
bun run dev
|
|
129
30
|
```
|
|
130
31
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
The CLI makes it easy to add production-ready middleware to your project. Browse
|
|
134
|
-
available middleware and install them with a single command:
|
|
32
|
+
Or add it to an existing project:
|
|
135
33
|
|
|
136
34
|
```bash
|
|
137
|
-
|
|
138
|
-
burger-api list
|
|
139
|
-
|
|
140
|
-
# Add middleware to your project
|
|
141
|
-
burger-api add cors logger rate-limiter
|
|
35
|
+
bun add burger-api
|
|
142
36
|
```
|
|
143
37
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
- **`cors`** - Cross-Origin Resource Sharing for handling cross-origin
|
|
147
|
-
requests
|
|
148
|
-
- **`logger`** - Request/response logging with detailed information
|
|
149
|
-
- **`rate-limiter`** - Request rate limiting to prevent API abuse
|
|
150
|
-
- **`jwt-auth`** - JWT authentication for securing your API endpoints
|
|
151
|
-
- **`api-key-auth`** - API key authentication for server-to-server
|
|
152
|
-
communication
|
|
153
|
-
- **`compression`** - Response compression (gzip/deflate) to reduce bandwidth
|
|
154
|
-
- **`security-headers`** - Security HTTP headers to protect against common
|
|
155
|
-
attacks
|
|
156
|
-
- **`cache`** - HTTP caching headers for improved performance
|
|
157
|
-
- **`timeout`** - Request timeout to prevent long-running requests
|
|
158
|
-
- **`body-size-limiter`** - Request body size limits to prevent large payload
|
|
159
|
-
attacks
|
|
160
|
-
|
|
161
|
-
After installing middleware, the CLI will show you exactly how to use it in your
|
|
162
|
-
project. For more information, visit the
|
|
163
|
-
[CLI documentation](../../packages/cli/README.md).
|
|
164
|
-
|
|
165
|
-
## 📣 Changelog
|
|
166
|
-
|
|
167
|
-
### Latest Version: 0.9.6 (March 18, 2026)
|
|
168
|
-
|
|
169
|
-
- 🚀 Production builds work better and use the same route rules as
|
|
170
|
-
development.
|
|
171
|
-
- 📦 You can pass route lists into the server so production does not scan
|
|
172
|
-
files.
|
|
173
|
-
- 🧪 Example tests and test scripts are improved; run framework and CLI
|
|
174
|
-
tests from the repo root.
|
|
175
|
-
|
|
176
|
-
### Previous Version: 0.7.0 (November 13, 2025)
|
|
38
|
+
Requires Bun 1.4.4+ (or Node 24.22.1+ with
|
|
39
|
+
[`@burger-api/node-server`](https://www.npmjs.com/package/@burger-api/node-server)).
|
|
177
40
|
|
|
178
|
-
|
|
41
|
+
## Example
|
|
179
42
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
- Clearer return types: Response, Function, or undefined
|
|
189
|
-
- Removed complex "around" middleware pattern
|
|
190
|
-
- Dedicated fast paths for 0, 1, and 2 middlewares
|
|
191
|
-
- Better JIT optimization
|
|
192
|
-
|
|
193
|
-
- 📦 **Monorepo Structure:**
|
|
194
|
-
|
|
195
|
-
- Converted to Bun workspace monorepo
|
|
196
|
-
- Core framework in `packages/burger-api`
|
|
197
|
-
- CLI tool in `packages/cli` (under development)
|
|
198
|
-
- Ecosystem middleware at root level
|
|
199
|
-
|
|
200
|
-
- 🔧 **Developer Experience:**
|
|
201
|
-
- 100% backward compatible
|
|
202
|
-
- Clearer documentation
|
|
203
|
-
- Easier to understand codebase
|
|
204
|
-
|
|
205
|
-
### Previous Version: 0.5.2 (November 9, 2025)
|
|
206
|
-
|
|
207
|
-
- 🔧 **Internal Improvements:**
|
|
208
|
-
- Refactored wildcard parameter extraction logic into reusable utility
|
|
209
|
-
functions
|
|
210
|
-
- Added test suites and README files for all example projects
|
|
211
|
-
|
|
212
|
-
### Previous Version: 0.5.0 (November 1, 2025)
|
|
43
|
+
```
|
|
44
|
+
src/
|
|
45
|
+
├── index.ts
|
|
46
|
+
└── api/
|
|
47
|
+
└── users/
|
|
48
|
+
├── route.ts # handlers
|
|
49
|
+
└── schema.ts # validation
|
|
50
|
+
```
|
|
213
51
|
|
|
214
|
-
|
|
52
|
+
```ts
|
|
53
|
+
// src/index.ts
|
|
54
|
+
import { Burger } from 'burger-api';
|
|
215
55
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
- Injects a minimal OPTIONS handler that returns a 204 No Content response
|
|
220
|
-
- Works for all HTTP methods that trigger CORS preflight (POST, PUT,
|
|
221
|
-
DELETE, PATCH)
|
|
222
|
-
- Does not inject if the route already has an OPTIONS handler
|
|
56
|
+
const app = new Burger({ apiDir: './src/api' });
|
|
57
|
+
app.serve(4000);
|
|
58
|
+
```
|
|
223
59
|
|
|
224
|
-
|
|
225
|
-
|
|
60
|
+
```ts
|
|
61
|
+
// src/api/users/schema.ts
|
|
62
|
+
import { z } from 'zod';
|
|
226
63
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
64
|
+
export const POST = {
|
|
65
|
+
body: z.object({ name: z.string(), email: z.email() }),
|
|
66
|
+
};
|
|
67
|
+
```
|
|
231
68
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
69
|
+
```ts
|
|
70
|
+
// src/api/users/route.ts
|
|
71
|
+
import { defineRoute } from 'burger-api';
|
|
72
|
+
import { POST as PostSchema } from './schema';
|
|
235
73
|
|
|
236
|
-
|
|
74
|
+
export const POST = defineRoute(PostSchema, (ctx) => {
|
|
75
|
+
const user = ctx.validated.body; // typed from schema.ts
|
|
76
|
+
return Response.json({ created: user }, { status: 201 });
|
|
77
|
+
});
|
|
78
|
+
```
|
|
237
79
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
it
|
|
241
|
-
- Create routes that handle multiple path segments automatically
|
|
242
|
-
- Access all matched path parts through `wildcardParams` in your request
|
|
243
|
-
- Routes are matched in order: exact paths first, then dynamic routes
|
|
244
|
-
(like `[id]`), then wildcards last
|
|
245
|
-
- Works inside dynamic routes too (example: `/api/users/[userId]/[...]`)
|
|
246
|
-
- View wildcard routes in OpenAPI docs and Swagger UI
|
|
80
|
+
`POST /api/users` now validates the body, returns a clear `422` error when it
|
|
81
|
+
is wrong, and shows up in the docs at `/docs`.
|
|
247
82
|
|
|
248
|
-
|
|
83
|
+
## Features
|
|
249
84
|
|
|
250
|
-
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
85
|
+
- **File-based routing:** `[param]` and `[...]` wildcard folders; `HEAD`,
|
|
86
|
+
`OPTIONS` and `405` handled for you.
|
|
87
|
+
- **Hooks:** `onRequest`, `transform`, `beforeRoute`, `afterRoute`,
|
|
88
|
+
`mapResponse` and `onError`, global in `src/hooks.ts` or per route.
|
|
89
|
+
- **Validation:** any Standard Schema library (Zod, Valibot, ArkType) for body,
|
|
90
|
+
query, params, headers, cookies and responses. Errors follow RFC 9457.
|
|
91
|
+
- **OpenAPI 3.1:** generated from your routes and schemas, with Swagger UI,
|
|
92
|
+
Scalar or Redoc.
|
|
93
|
+
- **Plugins and providers:** add hooks with `burger.usePlugin()`, share services
|
|
94
|
+
with `burger.provide()`.
|
|
95
|
+
- **WebSockets:** file-based routes under `src/websocket/`.
|
|
96
|
+
- **Pages:** HTML pages and static assets under `src/pages/`.
|
|
97
|
+
- **Deploy anywhere:** `app.serve()` on Bun, `toFetchHandler(app)` for Node,
|
|
98
|
+
Cloudflare Workers, Deno and Vercel.
|
|
99
|
+
- **JavaScript is first-class:** the same conventions in `.js` and `.mjs`.
|
|
255
100
|
|
|
256
|
-
|
|
257
|
-
[Changelog](./packages/burger-api/CHANGELOG.md) file.
|
|
101
|
+
## Ecosystem
|
|
258
102
|
|
|
259
|
-
|
|
103
|
+
Official hooks and plugins you can copy into your project with the CLI:
|
|
260
104
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
105
|
+
```bash
|
|
106
|
+
burger-api add cors logger rate-limiter jwt-auth
|
|
107
|
+
```
|
|
264
108
|
|
|
265
|
-
|
|
109
|
+
- **Hooks:** cors, logger, rate-limiter, cache, compression, security-headers,
|
|
110
|
+
timeout, body-size-limiter
|
|
111
|
+
- **Plugins:** jwt-auth, api-key, basic-auth, session, oidc, env
|
|
266
112
|
|
|
267
|
-
|
|
268
|
-
improvements, please open an issue or submit a pull request. Let's build
|
|
269
|
-
something amazing together.
|
|
113
|
+
## Documentation
|
|
270
114
|
|
|
271
|
-
|
|
115
|
+
- **Docs:** [burger-api.com](https://burger-api.com)
|
|
116
|
+
- **CLI:** [`@burger-api/cli`](https://www.npmjs.com/package/@burger-api/cli)
|
|
117
|
+
- **Changelog:** [CHANGELOG.md](https://github.com/isfhan/burger-api/blob/main/packages/burger-api/CHANGELOG.md)
|
|
272
118
|
|
|
273
|
-
|
|
274
|
-
[LICENSE](./packages/burger-api/LICENSE) file for details.
|
|
119
|
+
## License
|
|
275
120
|
|
|
276
|
-
|
|
277
|
-
people do anything they want with your code as long as they provide attribution
|
|
278
|
-
back to you and don't hold you liable.
|
|
121
|
+
[MIT](https://github.com/isfhan/burger-api/blob/main/LICENSE)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RuntimeAdapter, ServerHandle } from '../types.js';
|
|
2
|
+
import type { BunAdapterStartOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The primary, optimized runtime adapter. Wraps `Bun.serve` + Bun's native
|
|
5
|
+
* `routes` map for static dispatch, with `Router.fetch` as the fallback for
|
|
6
|
+
* dynamic/wildcard routes (hybrid router).
|
|
7
|
+
*
|
|
8
|
+
* The only place in the framework that touches a Bun-specific server
|
|
9
|
+
* bootstrap. Loaded lazily by `Server` on first `serve()`, so non-Bun bundles
|
|
10
|
+
* never import this module.
|
|
11
|
+
*/
|
|
12
|
+
export declare class BunAdapter implements RuntimeAdapter {
|
|
13
|
+
start(opts: BunAdapterStartOptions): ServerHandle;
|
|
14
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { serve } from 'bun';
|
|
2
|
+
import { renderUncaught } from '../../errors/http-error.js';
|
|
3
|
+
/**
|
|
4
|
+
* The primary, optimized runtime adapter. Wraps `Bun.serve` + Bun's native
|
|
5
|
+
* `routes` map for static dispatch, with `Router.fetch` as the fallback for
|
|
6
|
+
* dynamic/wildcard routes (hybrid router).
|
|
7
|
+
*
|
|
8
|
+
* The only place in the framework that touches a Bun-specific server
|
|
9
|
+
* bootstrap. Loaded lazily by `Server` on first `serve()`, so non-Bun bundles
|
|
10
|
+
* never import this module.
|
|
11
|
+
*/
|
|
12
|
+
export class BunAdapter {
|
|
13
|
+
start(opts) {
|
|
14
|
+
const serverOptions = {
|
|
15
|
+
hostname: opts.hostname,
|
|
16
|
+
routes: opts.staticRoutes,
|
|
17
|
+
fetch: async (request, server) => {
|
|
18
|
+
try {
|
|
19
|
+
return await opts.fetch(request, server);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
// Safety net for errors that escape the pipeline.
|
|
23
|
+
// HTTPError subclasses are caught by dispatchOnError.
|
|
24
|
+
// This catches the rest — same renderer as the fetch path.
|
|
25
|
+
return renderUncaught(error, request, opts.debug);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
error(error) {
|
|
29
|
+
// Server-level fallback (normal request errors flow through
|
|
30
|
+
// `fetch`): log server-side only; never echo `error.message`
|
|
31
|
+
// to clients.
|
|
32
|
+
console.error(error);
|
|
33
|
+
return new Response('Internal Server Error', {
|
|
34
|
+
status: 500,
|
|
35
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
port: opts.port,
|
|
39
|
+
};
|
|
40
|
+
if (opts.maxRequestBodySize !== undefined) {
|
|
41
|
+
serverOptions.maxRequestBodySize = opts.maxRequestBodySize;
|
|
42
|
+
}
|
|
43
|
+
// Add WebSocket handlers if provided
|
|
44
|
+
if (opts.websocket) {
|
|
45
|
+
serverOptions.websocket = opts.websocket;
|
|
46
|
+
}
|
|
47
|
+
let server;
|
|
48
|
+
try {
|
|
49
|
+
server = serve(serverOptions);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
// A busy port is an operator problem: throw one clear error the
|
|
53
|
+
// caller (app entry, CLI child) can surface; never kill the
|
|
54
|
+
// process from inside the adapter.
|
|
55
|
+
if (error?.code === 'EADDRINUSE') {
|
|
56
|
+
throw new Error(`Port ${opts.port} is already in use. Stop the other process or set PORT / --port.`, { cause: error });
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
// Hand the raw server to the framework once: the router records it as
|
|
61
|
+
// the lazy `ctx.ip` source (no per-request WeakMap writes).
|
|
62
|
+
opts.onServer?.(server);
|
|
63
|
+
if (opts.onListen) {
|
|
64
|
+
opts.onListen();
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
console.log(`🍔 BurgerAPI is running at: http://${opts.hostname || 'localhost'}:${opts.port}`);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
stop: () => server.stop(),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HTMLBundle } from 'bun';
|
|
2
|
+
import type { CompiledHandler, NativeMethodHandlers } from '../../router/types.js';
|
|
3
|
+
import type { RequestHandler } from '../../types/index.js';
|
|
4
|
+
import type { AdapterStartOptions } from '../types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Bun-only adapter options: native page `HTMLBundle`s and the Bun WebSocket
|
|
7
|
+
* `serve` option, kept under `adapter/bun/` so shared adapter types stay free
|
|
8
|
+
* of `bun` imports.
|
|
9
|
+
*/
|
|
10
|
+
export type BunAdapterStartOptions = AdapterStartOptions & {
|
|
11
|
+
/** Static routes; page routes may also be `HTMLBundle`s (Bun-only). */
|
|
12
|
+
staticRoutes: Record<string, CompiledHandler | HTMLBundle | NativeMethodHandlers | RequestHandler>;
|
|
13
|
+
/** Optional WebSocket handlers (Bun-specific). */
|
|
14
|
+
websocket?: unknown;
|
|
15
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { AdapterStartOptions, RuntimeAdapter, ServerHandle, } from './types.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { CompiledHandler, NativeMethodHandlers } from '../router/types.js';
|
|
2
|
+
import type { FetchHandler, RequestHandler } from '../types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options the runtime passes to an adapter to boot the server.
|
|
5
|
+
*
|
|
6
|
+
* Shared, WinterCG-safe contract: no Bun types. An adapter only translates how
|
|
7
|
+
* a `Request` enters and a `Response` leaves, keeping the core runtime-agnostic.
|
|
8
|
+
* Bun-only options live on `BunAdapterStartOptions` (`adapter/bun/types.ts`).
|
|
9
|
+
*/
|
|
10
|
+
export interface AdapterStartOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Static routes for the runtime's native dispatch (Bun's `routes` map).
|
|
13
|
+
* Built by the router as method objects; page routes may be plain handlers.
|
|
14
|
+
*/
|
|
15
|
+
staticRoutes: Record<string, CompiledHandler | RequestHandler | NativeMethodHandlers>;
|
|
16
|
+
/** The `fetch` fallback for dynamic/wildcard routes (Router.fetch). */
|
|
17
|
+
fetch: FetchHandler;
|
|
18
|
+
/** The port to listen on. */
|
|
19
|
+
port: number;
|
|
20
|
+
/** Optional hostname to bind. */
|
|
21
|
+
hostname?: string;
|
|
22
|
+
/** Debug flag, forwarded for error rendering. */
|
|
23
|
+
debug?: boolean;
|
|
24
|
+
/** Maximum request body size in bytes (runtime default when unset). */
|
|
25
|
+
maxRequestBodySize?: number;
|
|
26
|
+
/** Optional callback invoked once the server is listening. */
|
|
27
|
+
onListen?: () => void;
|
|
28
|
+
/**
|
|
29
|
+
* Optional callback with the raw runtime server handle right after it
|
|
30
|
+
* starts; used to record the server once for lazy `ctx.ip` resolution.
|
|
31
|
+
*/
|
|
32
|
+
onServer?: (server: unknown) => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A running server handle. Used only to stop the server (tests / graceful
|
|
36
|
+
* shutdown).
|
|
37
|
+
*/
|
|
38
|
+
export interface ServerHandle {
|
|
39
|
+
stop(): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The runtime adapter seam. A concrete adapter wraps one runtime's server
|
|
43
|
+
* bootstrap (`Bun.serve`, `Deno.serve`, `node:http`, etc.); only this surface
|
|
44
|
+
* touches runtime-specific APIs. Deploy targets that export a `fetch` handler
|
|
45
|
+
* (Workers, Vercel, Deno, Node 24+) use `toFetchHandler()` instead.
|
|
46
|
+
*/
|
|
47
|
+
export interface RuntimeAdapter {
|
|
48
|
+
start(opts: AdapterStartOptions): ServerHandle;
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-Standard (WinterCG) fetch entry.
|
|
3
|
+
*
|
|
4
|
+
* `toFetchHandler(burger)` turns a `Burger` app into a portable
|
|
5
|
+
* `(request, ...env) => Promise<Response>` handler that runs anywhere
|
|
6
|
+
* `Request`/`Response` exist:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* // Cloudflare Workers / Vercel
|
|
10
|
+
* export default { fetch: toFetchHandler(burger) };
|
|
11
|
+
*
|
|
12
|
+
* // Deno
|
|
13
|
+
* Deno.serve(toFetchHandler(burger));
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* WinterCG targets must pass AOT routes (`apiRoutes`); pages are Bun-only.
|
|
17
|
+
* A filesystem scan happens only in Bun dev when `apiDir` is set without
|
|
18
|
+
* `apiRoutes`, so a scan-based app fails on non-Bun runtimes. No Bun imports
|
|
19
|
+
* reach this module.
|
|
20
|
+
*/
|
|
21
|
+
import type { Burger } from '../../index.js';
|
|
22
|
+
import type { BurgerEnv, BurgerExecutionContext } from '../../context/context.js';
|
|
23
|
+
/**
|
|
24
|
+
* The portable entry shape: a Web-Standard `Request` in, a `Response` out.
|
|
25
|
+
*
|
|
26
|
+
* `env` and `executionCtx` are the second/third arguments a WinterCG host
|
|
27
|
+
* supplies (`fetch(request, env, ctx)`); they are bound onto every
|
|
28
|
+
* `BurgerContext` (`ctx.env`, `ctx.executionCtx`). Extra positional arguments
|
|
29
|
+
* are accepted and ignored for forward compatibility.
|
|
30
|
+
*/
|
|
31
|
+
export type FetchHandlerEntry = (request: Request, env?: BurgerEnv, executionCtx?: BurgerExecutionContext, ...rest: unknown[]) => Promise<Response>;
|
|
32
|
+
/**
|
|
33
|
+
* Returns a Web-Standard fetch handler for the given app.
|
|
34
|
+
*
|
|
35
|
+
* Routes are prepared lazily on the first call (AOT `apiRoutes`, or a one-time
|
|
36
|
+
* filesystem scan in Bun dev); later calls dispatch directly. Prefer AOT
|
|
37
|
+
* `apiRoutes` on WinterCG targets (no filesystem access).
|
|
38
|
+
*/
|
|
39
|
+
export declare function toFetchHandler(burger: Burger): FetchHandlerEntry;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-Standard (WinterCG) fetch entry.
|
|
3
|
+
*
|
|
4
|
+
* `toFetchHandler(burger)` turns a `Burger` app into a portable
|
|
5
|
+
* `(request, ...env) => Promise<Response>` handler that runs anywhere
|
|
6
|
+
* `Request`/`Response` exist:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* // Cloudflare Workers / Vercel
|
|
10
|
+
* export default { fetch: toFetchHandler(burger) };
|
|
11
|
+
*
|
|
12
|
+
* // Deno
|
|
13
|
+
* Deno.serve(toFetchHandler(burger));
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* WinterCG targets must pass AOT routes (`apiRoutes`); pages are Bun-only.
|
|
17
|
+
* A filesystem scan happens only in Bun dev when `apiDir` is set without
|
|
18
|
+
* `apiRoutes`, so a scan-based app fails on non-Bun runtimes. No Bun imports
|
|
19
|
+
* reach this module.
|
|
20
|
+
*/
|
|
21
|
+
import { renderUncaught } from '../../errors/http-error.js';
|
|
22
|
+
/**
|
|
23
|
+
* Returns a Web-Standard fetch handler for the given app.
|
|
24
|
+
*
|
|
25
|
+
* Routes are prepared lazily on the first call (AOT `apiRoutes`, or a one-time
|
|
26
|
+
* filesystem scan in Bun dev); later calls dispatch directly. Prefer AOT
|
|
27
|
+
* `apiRoutes` on WinterCG targets (no filesystem access).
|
|
28
|
+
*/
|
|
29
|
+
export function toFetchHandler(burger) {
|
|
30
|
+
let prepared = null;
|
|
31
|
+
/** Lazily prepares the handler; a failed attempt is not cached. */
|
|
32
|
+
const prepare = () => {
|
|
33
|
+
if (!prepared) {
|
|
34
|
+
prepared = burger.fetchHandler().catch((error) => {
|
|
35
|
+
// Reset so the next request retries the scan/compile instead
|
|
36
|
+
// of replaying the rejection forever.
|
|
37
|
+
prepared = null;
|
|
38
|
+
throw error;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return prepared;
|
|
42
|
+
};
|
|
43
|
+
return async (request, env, executionCtx) => {
|
|
44
|
+
try {
|
|
45
|
+
const handler = await prepare();
|
|
46
|
+
return await handler(request, env, executionCtx);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
// One top-level safety net, shared with the Bun adapter: every
|
|
50
|
+
// runtime answers RFC 9457, never an unhandled rejection.
|
|
51
|
+
return renderUncaught(error, request);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-Standard (WinterCG) adapter entry.
|
|
3
|
+
*
|
|
4
|
+
* Contains no Bun references: the portable fetch surface used by Cloudflare
|
|
5
|
+
* Workers, Vercel, Deno, and Node 24+.
|
|
6
|
+
*/
|
|
7
|
+
export { toFetchHandler } from './fetch-handler.js';
|
|
8
|
+
export type { FetchHandlerEntry } from './fetch-handler.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RouteAccessInfo } from '../context/types.js';
|
|
2
|
+
import type { RouteDefinition } from '../types/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Analyzes one route definition and returns a frozen `RouteAccessInfo`.
|
|
5
|
+
*
|
|
6
|
+
* `extraSources` are functions that run for this route but are not part of
|
|
7
|
+
* the definition (plugin hooks, app-level hooks, transform factories); they
|
|
8
|
+
* are scanned so a "known" result accounts for every framework-known reader.
|
|
9
|
+
*/
|
|
10
|
+
export declare function analyzeRouteAccess(def: RouteDefinition, debug?: boolean, extraSources?: unknown[]): RouteAccessInfo;
|