burger-api 0.0.28
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 +225 -0
- package/dist/src/core/openapi.d.ts +17 -0
- package/dist/src/core/openapi.js +133 -0
- package/dist/src/core/request.d.ts +40 -0
- package/dist/src/core/request.js +58 -0
- package/dist/src/core/response.d.ts +92 -0
- package/dist/src/core/response.js +140 -0
- package/dist/src/core/router.d.ts +69 -0
- package/dist/src/core/router.js +197 -0
- package/dist/src/core/server.d.ts +24 -0
- package/dist/src/core/server.js +60 -0
- package/dist/src/core/swagger-ui.d.ts +1 -0
- package/dist/src/core/swagger-ui.js +33 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/index.js +137 -0
- package/dist/src/middleware/validator.d.ts +11 -0
- package/dist/src/middleware/validator.js +90 -0
- package/dist/src/utils/error.d.ts +17 -0
- package/dist/src/utils/error.js +209 -0
- package/dist/src/utils/index.d.ts +20 -0
- package/dist/src/utils/index.js +41 -0
- package/package.json +75 -0
package/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# burger-api [](https://github.com/isfhan/burger-api)
|
|
2
|
+
|
|
3
|
+
**burger-api** is a modern, open source API framework built on [Bun.js](https://bun.sh). It combines the simplicity of file-based routing (inspired by Next.js) with powerful features like built-in middleware, Zod-based schema validation, and automatic OpenAPI generation. Designed for high performance and ease-of-use, burger-api leverages Bun’s native modules to deliver blazing-fast API responses while keeping your codebase clean and maintainable.
|
|
4
|
+
|
|
5
|
+
**This project is under active development and should not be used in production yet.**
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
burger-api is built to offer a robust developer experience through:
|
|
10
|
+
|
|
11
|
+
- **Bun-Native Performance:**
|
|
12
|
+
Leverages Bun’s high-performance HTTP server.
|
|
13
|
+
|
|
14
|
+
- **File-Based Routing:**
|
|
15
|
+
Automatically registers API and page routes from your file structure using a clear naming convention.
|
|
16
|
+
|
|
17
|
+
- **Middleware Architecture:**
|
|
18
|
+
Supports both global and route-specific middleware with a familiar `req, res, next` pattern.
|
|
19
|
+
|
|
20
|
+
- **Type-Safe Validation:**
|
|
21
|
+
Utilizes Zod for request validation, ensuring full type safety and automatic error reporting.
|
|
22
|
+
|
|
23
|
+
- **Automatic OpenAPI Generation:**
|
|
24
|
+
Generates a complete OpenAPI 3.0 specification (with support for tags, summaries, descriptions, operationId, deprecated status, externalDocs, and more) directly from your routes and Zod schemas.
|
|
25
|
+
|
|
26
|
+
- **Swagger UI Integration:**
|
|
27
|
+
Provides an out-of-the-box Swagger UI endpoint for interactive API documentation.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
### Core Features
|
|
32
|
+
|
|
33
|
+
- **Bun-Native HTTP Server:**
|
|
34
|
+
Built on Bun’s native APIs for exceptional performance.
|
|
35
|
+
|
|
36
|
+
- **File-Based Routing Engine:**
|
|
37
|
+
Automatically scans directories to register routes.
|
|
38
|
+
|
|
39
|
+
- Supports dynamic routes via folder names like `[id]` for `/api/product/:id`.
|
|
40
|
+
- Differentiates between API routes (`route.ts`) and page routes (`page.ts`).
|
|
41
|
+
|
|
42
|
+
- **Request & Response Enhancements:**
|
|
43
|
+
|
|
44
|
+
- `BurgerRequest` extends the native Request to include query, params, and validated data.
|
|
45
|
+
- `BurgerResponse` provides methods to set headers, status, and build responses (JSON, text, HTML, redirects, files).
|
|
46
|
+
|
|
47
|
+
- **Middleware System:**
|
|
48
|
+
|
|
49
|
+
- **Global Middleware:** Runs on every request.
|
|
50
|
+
- **Route-Specific Middleware:** Defined in individual route files.
|
|
51
|
+
- **Validation Middleware:** Automatically validates request data using Zod schemas exported from routes.
|
|
52
|
+
|
|
53
|
+
- **Zod-Based Schema Validation:**
|
|
54
|
+
Automatically validates request params, query, and body using Zod.
|
|
55
|
+
|
|
56
|
+
- Supports preprocessing (e.g., converting URL parameters from string to number).
|
|
57
|
+
|
|
58
|
+
- **Automatic OpenAPI Specification Generation:**
|
|
59
|
+
Generates an OpenAPI 3.0 document using route metadata and Zod schemas.
|
|
60
|
+
|
|
61
|
+
- **OpenAPI Options:**
|
|
62
|
+
- Global metadata (title, description, version)
|
|
63
|
+
- Per-route metadata (summary, description, tags, operationId, deprecated, externalDocs)
|
|
64
|
+
- Auto-generated parameters and requestBody schemas (using `zod-to-json-schema` for detailed inline JSON schema definitions)
|
|
65
|
+
|
|
66
|
+
- **Swagger UI Integration:**
|
|
67
|
+
Provides a `/docs` endpoint serving an interactive Swagger UI that reads from `/openapi.json`.
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
Install burger-api via bun:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
bun add burger-api
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## How to Use burger-api
|
|
78
|
+
|
|
79
|
+
### **Basic Usage Example**
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { Burger, setDir } from "burger-api";
|
|
83
|
+
|
|
84
|
+
// Global middleware example: a simple logger.
|
|
85
|
+
const globalLogger = async (req, res, next) => {
|
|
86
|
+
console.log(`[Global Logger] ${req.method} ${req.url}`);
|
|
87
|
+
return next();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const burger = new Burger({
|
|
91
|
+
apiDir: setDir(__dirname, "api"),
|
|
92
|
+
title: "My Custom API",
|
|
93
|
+
description: "Custom API with auto-generated docs and validation",
|
|
94
|
+
version: "1.0.0",
|
|
95
|
+
globalMiddleware: [globalLogger],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Start the server on port 4000 with a callback
|
|
99
|
+
burger.serve(4000, (port) => {
|
|
100
|
+
console.log(`Server is running on port ${port}`);
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### **File-Based Routing Examples**
|
|
105
|
+
|
|
106
|
+
- **Static API Route:**
|
|
107
|
+
Place a file at `src/api/route.ts` to handle the root API endpoint (e.g., `/api`).
|
|
108
|
+
|
|
109
|
+
- **Dynamic API Route:**
|
|
110
|
+
For routes with dynamic segments, create folders with square brackets. For example:
|
|
111
|
+
```
|
|
112
|
+
src/api/product/
|
|
113
|
+
├── route.ts // Handles /api/product
|
|
114
|
+
└── [id]/
|
|
115
|
+
└── route.ts // Handles /api/product/:id
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### **Route File Example**
|
|
119
|
+
|
|
120
|
+
Below is an example route file demonstrating schema validation, route-specific middleware, and OpenAPI metadata.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
// examples/demo/api/product/[id]/route.ts
|
|
124
|
+
|
|
125
|
+
import { z } from "zod";
|
|
126
|
+
import type { BurgerRequest, BurgerResponse } from "burger-api";
|
|
127
|
+
|
|
128
|
+
// OpenAPI metadata for this route
|
|
129
|
+
export const openapi = {
|
|
130
|
+
get: {
|
|
131
|
+
summary: "Get Product Details",
|
|
132
|
+
description: "Retrieves product details by product ID.",
|
|
133
|
+
tags: ["Product"],
|
|
134
|
+
operationId: "getProductDetails",
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// Validation Schemas for GET and POST requests
|
|
139
|
+
export const schema = {
|
|
140
|
+
get: {
|
|
141
|
+
params: z.object({
|
|
142
|
+
id: z.preprocess(
|
|
143
|
+
(val) => (typeof val === "string" ? parseInt(val, 10) : val),
|
|
144
|
+
z.number().min(1, "ID is required")
|
|
145
|
+
),
|
|
146
|
+
}),
|
|
147
|
+
query: z.object({
|
|
148
|
+
search: z.string().optional(),
|
|
149
|
+
}),
|
|
150
|
+
},
|
|
151
|
+
post: {
|
|
152
|
+
body: z.object({
|
|
153
|
+
name: z.string().min(1, "Name is required"),
|
|
154
|
+
price: z.number().positive("Price must be positive"),
|
|
155
|
+
}),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Route-specific middleware
|
|
160
|
+
export const middleware = [
|
|
161
|
+
async (
|
|
162
|
+
req: BurgerRequest,
|
|
163
|
+
res: BurgerResponse,
|
|
164
|
+
next: () => Promise<Response>
|
|
165
|
+
) => {
|
|
166
|
+
console.log("[Product Middleware] Executing product route middleware");
|
|
167
|
+
return next();
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
// GET handler: returns product details
|
|
172
|
+
export async function GET(
|
|
173
|
+
req: BurgerRequest,
|
|
174
|
+
res: BurgerResponse,
|
|
175
|
+
params: { id: number }
|
|
176
|
+
) {
|
|
177
|
+
console.log("[GET] Product route invoked");
|
|
178
|
+
const validatedParams = (req.validated?.params as { id: number }) || params;
|
|
179
|
+
const query = req.validated?.query || Object.fromEntries(req.query.entries());
|
|
180
|
+
return res.json({
|
|
181
|
+
id: validatedParams.id,
|
|
182
|
+
query,
|
|
183
|
+
name: "Sample Product",
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// POST handler: creates a new product
|
|
188
|
+
export async function POST(req: BurgerRequest, res: BurgerResponse) {
|
|
189
|
+
console.log("[POST] Product route invoked");
|
|
190
|
+
const body = req.validated?.body || (await req.json());
|
|
191
|
+
return res.json(body);
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### **API Documentation Endpoints**
|
|
196
|
+
|
|
197
|
+
- **OpenAPI JSON:**
|
|
198
|
+
Access `http://localhost:4000/openapi.json` to view the auto-generated OpenAPI specification.
|
|
199
|
+
|
|
200
|
+
- **Swagger UI:**
|
|
201
|
+
Access `http://localhost:4000/docs` to view interactive API documentation via Swagger UI.
|
|
202
|
+
|
|
203
|
+
## Contributing
|
|
204
|
+
|
|
205
|
+
We welcome contributions from the community! If you have suggestions or improvements, please open an issue or submit a pull request. Let’s build something amazing together.
|
|
206
|
+
|
|
207
|
+
## License
|
|
208
|
+
|
|
209
|
+
This project is licensed under the MIT License.
|
|
210
|
+
|
|
211
|
+
## FAQs & Additional Resources
|
|
212
|
+
|
|
213
|
+
- **How do I add custom middleware?**
|
|
214
|
+
You can pass an array of global middleware in the Burger options or export route-specific middleware in your route files.
|
|
215
|
+
|
|
216
|
+
- **How does file-based routing work?**
|
|
217
|
+
Place your route files under `src/api/` (or `src/pages/`) using folder and file naming conventions (e.g., `[id]` for dynamic routes).
|
|
218
|
+
|
|
219
|
+
- **How is validation handled?**
|
|
220
|
+
burger-api uses Zod for schema validation. Define your schemas in your route files and they are automatically used to validate incoming requests.
|
|
221
|
+
|
|
222
|
+
- **How can I customize the OpenAPI documentation?**
|
|
223
|
+
Override the default auto-generated summaries, descriptions, tags, and operationIds by exporting an `openapi` object in your route files.
|
|
224
|
+
|
|
225
|
+
_burger-api_ aims to revolutionize your API development experience with simplicity, speed, and cutting-edge features. Happy coding!
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Router } from "./router";
|
|
2
|
+
import type { ServerOptions } from "../types/index.d";
|
|
3
|
+
/**
|
|
4
|
+
* Generates an OpenAPI 3.0 document from the router's routes and global options.
|
|
5
|
+
* @param router The router instance with loaded routes.
|
|
6
|
+
* @param options Global options, including OpenAPI metadata.
|
|
7
|
+
* @returns The OpenAPI document as a JavaScript object.
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateOpenAPIDocument(router: Router, options: ServerOptions): {
|
|
10
|
+
openapi: string;
|
|
11
|
+
info: {
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
version: string;
|
|
15
|
+
};
|
|
16
|
+
paths: Record<string, any>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
/**
|
|
3
|
+
* Builds an array of OpenAPI 3.0 parameters based on the Zod schema.
|
|
4
|
+
* For each property in the Zod schema, an OpenAPI parameter is constructed
|
|
5
|
+
* with the same name and required flag. The schema of the parameter is set
|
|
6
|
+
* to a string type.
|
|
7
|
+
* @param zodSchema - The Zod schema to construct parameters from.
|
|
8
|
+
* @param location - The location of the parameter. Must be either "path" or "query".
|
|
9
|
+
* @returns An array of OpenAPI 3.0 parameter objects.
|
|
10
|
+
*/
|
|
11
|
+
function buildParameters(zodSchema, location) {
|
|
12
|
+
const parameters = [];
|
|
13
|
+
if (zodSchema &&
|
|
14
|
+
zodSchema._def &&
|
|
15
|
+
typeof zodSchema._def.shape === "function") {
|
|
16
|
+
// Get the shape of the Zod schema
|
|
17
|
+
const shape = zodSchema._def.shape();
|
|
18
|
+
for (const key in shape) {
|
|
19
|
+
// Get the definition of the field
|
|
20
|
+
const fieldDef = shape[key];
|
|
21
|
+
// Determine if the field is optional
|
|
22
|
+
const isOptional = fieldDef._def.typeName === "ZodOptional";
|
|
23
|
+
parameters.push({
|
|
24
|
+
// Set the name of the parameter
|
|
25
|
+
name: key,
|
|
26
|
+
// Type of the parameter path or query
|
|
27
|
+
in: location,
|
|
28
|
+
// Set the required flag
|
|
29
|
+
required: !isOptional,
|
|
30
|
+
// Set the schema type
|
|
31
|
+
schema: { type: "string" },
|
|
32
|
+
// Description of the parameter
|
|
33
|
+
description: `${location} parameter ${key}`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return parameters;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Builds a request body object for OpenAPI based on a Zod schema.
|
|
41
|
+
* Converts the Zod schema into JSON schema and constructs an OpenAPI
|
|
42
|
+
* requestBody object with content type "application/json".
|
|
43
|
+
* @param zodSchema - The Zod schema to convert into JSON schema.
|
|
44
|
+
* @returns An OpenAPI requestBody object, or undefined if no schema is provided.
|
|
45
|
+
*/
|
|
46
|
+
function buildRequestBody(zodSchema) {
|
|
47
|
+
if (!zodSchema)
|
|
48
|
+
return undefined;
|
|
49
|
+
const jsonSchema = zodToJsonSchema(zodSchema);
|
|
50
|
+
return {
|
|
51
|
+
content: {
|
|
52
|
+
"application/json": {
|
|
53
|
+
schema: jsonSchema,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
description: "Request body",
|
|
57
|
+
required: true,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts a route path from colon-based dynamic segments to OpenAPI's curly brace syntax.
|
|
62
|
+
*
|
|
63
|
+
* @param routePath The original route path with colon-based dynamic segments (e.g., "/user/:id").
|
|
64
|
+
* @returns The converted route path with curly brace syntax (e.g., "/user/{id}").
|
|
65
|
+
*/
|
|
66
|
+
function convertPathForOpenAPI(routePath) {
|
|
67
|
+
// Replace occurrences of :param with {param}
|
|
68
|
+
return routePath.replace(/:([a-zA-Z0-9_]+)/g, "{$1}");
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Generates an OpenAPI 3.0 document from the router's routes and global options.
|
|
72
|
+
* @param router The router instance with loaded routes.
|
|
73
|
+
* @param options Global options, including OpenAPI metadata.
|
|
74
|
+
* @returns The OpenAPI document as a JavaScript object.
|
|
75
|
+
*/
|
|
76
|
+
export function generateOpenAPIDocument(router, options) {
|
|
77
|
+
const openapiDoc = {
|
|
78
|
+
openapi: "3.0.0",
|
|
79
|
+
info: {
|
|
80
|
+
title: options.title || "Burger API",
|
|
81
|
+
description: options.description || "Burger API documentation",
|
|
82
|
+
version: options.version || "1.0.0",
|
|
83
|
+
},
|
|
84
|
+
paths: {},
|
|
85
|
+
};
|
|
86
|
+
// Iterate over each route in the router.
|
|
87
|
+
for (const route of router.routes) {
|
|
88
|
+
// Convert colon-based dynamic segments to OpenAPI's {param} syntax.
|
|
89
|
+
const openApiPath = convertPathForOpenAPI(route.path);
|
|
90
|
+
// Initialize path object if necessary
|
|
91
|
+
openapiDoc.paths[openApiPath] = openapiDoc.paths[openApiPath] || {};
|
|
92
|
+
// For each HTTP method in the route, add an OpenAPI operation.
|
|
93
|
+
for (const method in route.handlers) {
|
|
94
|
+
// Convert HTTP method to lowercase
|
|
95
|
+
const lowerMethod = method.toLowerCase();
|
|
96
|
+
// Use provided openapi metadata if available; else, fallback to auto-generated values.
|
|
97
|
+
const methodMeta = route.openapi?.[lowerMethod] || {};
|
|
98
|
+
// Generate an operationId: e.g., "get_api_product"
|
|
99
|
+
const operationId = methodMeta.operationId ||
|
|
100
|
+
`${lowerMethod}_${route.path.replace(/[\/:]/g, "_")}`;
|
|
101
|
+
// Build parameters for both path and query from the schema.
|
|
102
|
+
let parameters = [];
|
|
103
|
+
if (route.schema && route.schema[lowerMethod]) {
|
|
104
|
+
const schemaDef = route.schema[lowerMethod];
|
|
105
|
+
parameters = [
|
|
106
|
+
...buildParameters(schemaDef.params, "path"),
|
|
107
|
+
...buildParameters(schemaDef.query, "query"),
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
// Build requestBody if a body schema exists.
|
|
111
|
+
let requestBody = undefined;
|
|
112
|
+
if (route.schema && route.schema[lowerMethod]?.body) {
|
|
113
|
+
requestBody = buildRequestBody(route.schema[lowerMethod].body);
|
|
114
|
+
}
|
|
115
|
+
openapiDoc.paths[openApiPath][lowerMethod] = {
|
|
116
|
+
operationId,
|
|
117
|
+
summary: methodMeta.summary || `Summary for ${method} ${route.path}`,
|
|
118
|
+
description: methodMeta.description || "",
|
|
119
|
+
tags: methodMeta.tags || [],
|
|
120
|
+
deprecated: methodMeta.deprecated || false,
|
|
121
|
+
parameters: parameters,
|
|
122
|
+
requestBody: requestBody,
|
|
123
|
+
responses: methodMeta.responses || {
|
|
124
|
+
"200": {
|
|
125
|
+
description: "Successful response",
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
externalDocs: methodMeta.externalDocs || undefined,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return openapiDoc;
|
|
133
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare class HttpRequest {
|
|
2
|
+
private _request;
|
|
3
|
+
private _query?;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new HttpRequest object from the given Request object.
|
|
6
|
+
* @param request - The Request object to wrap.
|
|
7
|
+
*/
|
|
8
|
+
constructor(request: Request);
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves the query parameters from the request URL.
|
|
11
|
+
* Initializes the query parameters if they are not already set.
|
|
12
|
+
* @returns The URLSearchParams object containing the query parameters.
|
|
13
|
+
*/
|
|
14
|
+
get query(): URLSearchParams;
|
|
15
|
+
/**
|
|
16
|
+
* Gets the URL of the request.
|
|
17
|
+
* @returns The URL as a string.
|
|
18
|
+
*/
|
|
19
|
+
get url(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Gets the HTTP method of the request.
|
|
22
|
+
* @returns The HTTP method as a string.
|
|
23
|
+
*/
|
|
24
|
+
get method(): string;
|
|
25
|
+
/**
|
|
26
|
+
* Gets the headers of the request.
|
|
27
|
+
* @returns The headers as a Headers object.
|
|
28
|
+
*/
|
|
29
|
+
get headers(): Headers;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the body of the request as JSON.
|
|
32
|
+
* @returns The body as a Promise of the parsed JSON.
|
|
33
|
+
*/
|
|
34
|
+
json<T = any>(): Promise<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the original Request object.
|
|
37
|
+
* @returns The original Request object.
|
|
38
|
+
*/
|
|
39
|
+
get original(): Request;
|
|
40
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export class HttpRequest {
|
|
2
|
+
_request;
|
|
3
|
+
_query;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new HttpRequest object from the given Request object.
|
|
6
|
+
* @param request - The Request object to wrap.
|
|
7
|
+
*/
|
|
8
|
+
constructor(request) {
|
|
9
|
+
this._request = request;
|
|
10
|
+
this._query = new URL(request.url).searchParams;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves the query parameters from the request URL.
|
|
14
|
+
* Initializes the query parameters if they are not already set.
|
|
15
|
+
* @returns The URLSearchParams object containing the query parameters.
|
|
16
|
+
*/
|
|
17
|
+
get query() {
|
|
18
|
+
if (!this._query) {
|
|
19
|
+
this._query = new URL(this._request.url).searchParams;
|
|
20
|
+
}
|
|
21
|
+
return this._query;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the URL of the request.
|
|
25
|
+
* @returns The URL as a string.
|
|
26
|
+
*/
|
|
27
|
+
get url() {
|
|
28
|
+
return this._request.url;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets the HTTP method of the request.
|
|
32
|
+
* @returns The HTTP method as a string.
|
|
33
|
+
*/
|
|
34
|
+
get method() {
|
|
35
|
+
return this._request.method;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Gets the headers of the request.
|
|
39
|
+
* @returns The headers as a Headers object.
|
|
40
|
+
*/
|
|
41
|
+
get headers() {
|
|
42
|
+
return this._request.headers;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Gets the body of the request as JSON.
|
|
46
|
+
* @returns The body as a Promise of the parsed JSON.
|
|
47
|
+
*/
|
|
48
|
+
async json() {
|
|
49
|
+
return this._request.json();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns the original Request object.
|
|
53
|
+
* @returns The original Request object.
|
|
54
|
+
*/
|
|
55
|
+
get original() {
|
|
56
|
+
return this._request;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export declare class HttpResponse {
|
|
2
|
+
/**
|
|
3
|
+
* The status code of the response, which defaults to 200 (OK). This
|
|
4
|
+
* property can be modified before calling build() to construct the
|
|
5
|
+
* Response object.
|
|
6
|
+
*/
|
|
7
|
+
private _status;
|
|
8
|
+
/**
|
|
9
|
+
* The headers of the response, which can be modified before calling build()
|
|
10
|
+
* to construct the Response object.
|
|
11
|
+
*/
|
|
12
|
+
private _headers;
|
|
13
|
+
/**
|
|
14
|
+
* The body of the response, which can be any valid BodyInit type or null.
|
|
15
|
+
*/
|
|
16
|
+
private _body;
|
|
17
|
+
/**
|
|
18
|
+
* Set a header on the response.
|
|
19
|
+
* @param name - The header name.
|
|
20
|
+
* @param value - The header value.
|
|
21
|
+
* @returns The response object.
|
|
22
|
+
*/
|
|
23
|
+
header(name: string, value: string): this;
|
|
24
|
+
/**
|
|
25
|
+
* Remove a header from the response.
|
|
26
|
+
* @param name - The header name.
|
|
27
|
+
* @returns The response object.
|
|
28
|
+
*/
|
|
29
|
+
removeHeader(name: string): this;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the HTTP status code of the response.
|
|
32
|
+
* @param status - The status code.
|
|
33
|
+
* @returns The response object.
|
|
34
|
+
*/
|
|
35
|
+
status(status: number): this;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the body of the response.
|
|
38
|
+
* @param body - The content to be used as the response body.
|
|
39
|
+
* @returns The response object for method chaining.
|
|
40
|
+
*/
|
|
41
|
+
body(body: BodyInit): this;
|
|
42
|
+
/**
|
|
43
|
+
* Sets the content type to "application/json" and serializes the provided data
|
|
44
|
+
* into a JSON string to set as the response body.
|
|
45
|
+
* @param data - The data to be serialized and sent as the response body.
|
|
46
|
+
* @returns The finalized Response object after building.
|
|
47
|
+
*/
|
|
48
|
+
json(data: unknown): Response;
|
|
49
|
+
/**
|
|
50
|
+
* Sets the content type to "text/plain" and assigns the provided text data
|
|
51
|
+
* as the response body.
|
|
52
|
+
* @param data - The text to be sent as the response body.
|
|
53
|
+
* @returns The finalized Response object after building.
|
|
54
|
+
*/
|
|
55
|
+
text(data: string): Response;
|
|
56
|
+
/**
|
|
57
|
+
* Sets the content type to "text/html" and assigns the provided HTML data
|
|
58
|
+
* as the response body.
|
|
59
|
+
* @param data - The HTML string to be sent as the response body.
|
|
60
|
+
* @returns The finalized Response object after building.
|
|
61
|
+
*/
|
|
62
|
+
html(data: string): Response;
|
|
63
|
+
/**
|
|
64
|
+
* Sets the HTTP status code and removes the "Location" header from the
|
|
65
|
+
* response.
|
|
66
|
+
* @param status - The status code.
|
|
67
|
+
* @returns The response object.
|
|
68
|
+
*/
|
|
69
|
+
redirect(url: string, status?: number): this;
|
|
70
|
+
/**
|
|
71
|
+
* Convenience method to send a file response using Bun.file.
|
|
72
|
+
* Attempts to serve the specified file and set it as the response body.
|
|
73
|
+
* If the file cannot be found or an error occurs, logs the error and
|
|
74
|
+
* returns a 404 response with "File not found" as the body.
|
|
75
|
+
* @param filePath - The file path to serve.
|
|
76
|
+
* @returns The finalized Response object.
|
|
77
|
+
*/
|
|
78
|
+
file(filePath: string): Response;
|
|
79
|
+
/**
|
|
80
|
+
* Returns a Response directly using the provided body and init settings.
|
|
81
|
+
* This bypasses the mutable state.
|
|
82
|
+
* @param body - The response body.
|
|
83
|
+
* @param init - Optional ResponseInit settings.
|
|
84
|
+
* @returns A new Response object.
|
|
85
|
+
*/
|
|
86
|
+
original(body?: BodyInit | null, init?: ResponseInit): Response;
|
|
87
|
+
/**
|
|
88
|
+
* Builds the final Response object using the current state.
|
|
89
|
+
* @returns The finalized Response object.
|
|
90
|
+
*/
|
|
91
|
+
build(): Response;
|
|
92
|
+
}
|