burger-api 0.0.28 → 0.0.30
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Router } from "./router";
|
|
2
|
-
import type { ServerOptions } from "../types
|
|
2
|
+
import type { ServerOptions } from "../types";
|
|
3
3
|
/**
|
|
4
4
|
* Generates an OpenAPI 3.0 document from the router's routes and global options.
|
|
5
5
|
* @param router The router instance with loaded routes.
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ServerOptions, Middleware } from "./types
|
|
1
|
+
import type { ServerOptions, Middleware } from "./types";
|
|
2
2
|
export declare class Burger {
|
|
3
3
|
private options;
|
|
4
4
|
private server;
|
|
@@ -28,4 +28,4 @@ export declare class Burger {
|
|
|
28
28
|
serve(port?: number, cb?: () => void): Promise<void>;
|
|
29
29
|
}
|
|
30
30
|
export { setDir } from "./utils/index";
|
|
31
|
-
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, Middleware, } from "./types
|
|
31
|
+
export type { ServerOptions, RequestHandler, BurgerRequest, BurgerResponse, Middleware, } from "./types";
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export interface ServerOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The title of the API. This is an optional property that can be used
|
|
5
|
+
* to specify a custom title for the API documentation.
|
|
6
|
+
*/
|
|
7
|
+
title?: string;
|
|
8
|
+
/**
|
|
9
|
+
* The description of the API. This is an optional property that can be used
|
|
10
|
+
* to provide a brief overview of the API.
|
|
11
|
+
*/
|
|
12
|
+
description?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The directory path to load API routes from.
|
|
15
|
+
* If not specified, no API routes are loaded.
|
|
16
|
+
*/
|
|
17
|
+
apiDir?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The directory path to load Page routes from.
|
|
20
|
+
* If not specified, no Page routes are loaded.
|
|
21
|
+
* Page routes are not yet supported, but will be supported in the future.
|
|
22
|
+
*/
|
|
23
|
+
pageDir?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Global middleware to be executed before each request.
|
|
26
|
+
*/
|
|
27
|
+
globalMiddleware?: Middleware[];
|
|
28
|
+
/**
|
|
29
|
+
* The templating engine to use for rendering HTML templates.
|
|
30
|
+
*/
|
|
31
|
+
templatingEngine?: TemplatingEngine;
|
|
32
|
+
/**
|
|
33
|
+
* The version of the API. This is an optional property that can be used
|
|
34
|
+
* to specify the version of the API.
|
|
35
|
+
*/
|
|
36
|
+
version?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Enables or disables debug mode. This is an optional property
|
|
39
|
+
* that, when set to true, can be used to output additional debugging
|
|
40
|
+
* information to the console or logs to aid in development and troubleshooting.
|
|
41
|
+
*/
|
|
42
|
+
debug?: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface BurgerRequest extends Request {
|
|
45
|
+
/**
|
|
46
|
+
* Provides helper to access query parameters as URLSearchParams.
|
|
47
|
+
* This is a convenience property that allows you to access the query
|
|
48
|
+
* parameters of the request as a URLSearchParams object, which provides
|
|
49
|
+
* methods for working with the query parameters like `get(key)`,
|
|
50
|
+
* `getAll(key)`, `has(key)`, `keys()`, `values()`, `entries()`, and more.
|
|
51
|
+
*/
|
|
52
|
+
query: URLSearchParams;
|
|
53
|
+
/**
|
|
54
|
+
* Contains URL parameters extracted from the request path.
|
|
55
|
+
* This property is only present if the request path matches a route
|
|
56
|
+
* with dynamic parameters.
|
|
57
|
+
*
|
|
58
|
+
* For example, if the route is `/users/:id`, and the request path is
|
|
59
|
+
* `/users/123`, then the `params` property will be `{ id: '123' }`.
|
|
60
|
+
*/
|
|
61
|
+
params?: Record<string, string>;
|
|
62
|
+
/**
|
|
63
|
+
* Contains validated data for the request.
|
|
64
|
+
* This is an optional property that will only be present if
|
|
65
|
+
* a middleware has validated the request data and attached the
|
|
66
|
+
* validated data to the request.
|
|
67
|
+
*
|
|
68
|
+
* Properties:
|
|
69
|
+
* - `params`: Validated URL parameters.
|
|
70
|
+
* - `query`: Validated query string parameters.
|
|
71
|
+
* - `body`: Validated request body (if JSON).
|
|
72
|
+
*/
|
|
73
|
+
validated?: {
|
|
74
|
+
params?: unknown;
|
|
75
|
+
query?: unknown;
|
|
76
|
+
body?: unknown;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export interface BurgerResponse {
|
|
80
|
+
/**
|
|
81
|
+
* Sets a header value.
|
|
82
|
+
* @param name - The header name.
|
|
83
|
+
* @param value - The header value.
|
|
84
|
+
* @returns The current instance for chaining.
|
|
85
|
+
*/
|
|
86
|
+
header(name: string, value: string): this;
|
|
87
|
+
/**
|
|
88
|
+
* Removes a header.
|
|
89
|
+
* @param name - The header name to remove.
|
|
90
|
+
* @returns The current instance for chaining.
|
|
91
|
+
*/
|
|
92
|
+
removeHeader(name: string): this;
|
|
93
|
+
/**
|
|
94
|
+
* Sets the HTTP status code.
|
|
95
|
+
* @param status - The status code.
|
|
96
|
+
* @returns The current instance for chaining.
|
|
97
|
+
*/
|
|
98
|
+
status(status: number): this;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the response body.
|
|
101
|
+
* @param body - The body content.
|
|
102
|
+
* @returns The current instance for chaining.
|
|
103
|
+
*/
|
|
104
|
+
body(body: BodyInit): this;
|
|
105
|
+
/**
|
|
106
|
+
* Builds and returns the final Response object using the current state.
|
|
107
|
+
* This should be called only once when the response is finalized.
|
|
108
|
+
* @returns The final Response object.
|
|
109
|
+
*/
|
|
110
|
+
build(): Response;
|
|
111
|
+
/**
|
|
112
|
+
* Convenience method to send a JSON response.
|
|
113
|
+
* Sets the "Content-Type" header and serializes the data to JSON.
|
|
114
|
+
* @param data - The data to send.
|
|
115
|
+
* @returns The final Response object.
|
|
116
|
+
*/
|
|
117
|
+
json(data: unknown): Response;
|
|
118
|
+
/**
|
|
119
|
+
* Convenience method to send a plain text response.
|
|
120
|
+
* Sets the "Content-Type" header and returns a text response.
|
|
121
|
+
* @param data - The text to send.
|
|
122
|
+
* @returns The final Response object.
|
|
123
|
+
*/
|
|
124
|
+
text(data: string): Response;
|
|
125
|
+
/**
|
|
126
|
+
* Convenience method to send an HTML response.
|
|
127
|
+
* Sets the "Content-Type" header and returns an HTML response.
|
|
128
|
+
* @param data - The HTML string to send.
|
|
129
|
+
* @returns The final Response object.
|
|
130
|
+
*/
|
|
131
|
+
html(data: string): Response;
|
|
132
|
+
/**
|
|
133
|
+
* Convenience method to send a redirect response.
|
|
134
|
+
* Sets the "Location" header and status code.
|
|
135
|
+
* @param url - The URL to redirect to.
|
|
136
|
+
* @param status - The HTTP status code (default 302).
|
|
137
|
+
* @returns The current instance for chaining.
|
|
138
|
+
*/
|
|
139
|
+
redirect(url: string, status?: number): this;
|
|
140
|
+
/**
|
|
141
|
+
* Convenience method to send a file response using Bun.file.
|
|
142
|
+
* @param filePath - The file path to serve.
|
|
143
|
+
* @returns The final Response object.
|
|
144
|
+
*/
|
|
145
|
+
file(filePath: string): Response;
|
|
146
|
+
/**
|
|
147
|
+
* Returns a Response directly using the provided body and init settings.
|
|
148
|
+
* This bypasses the mutable state.
|
|
149
|
+
* @param body - The response body.
|
|
150
|
+
* @param init - Optional ResponseInit settings.
|
|
151
|
+
* @returns A new Response object.
|
|
152
|
+
*/
|
|
153
|
+
original(body?: BodyInit | null, init?: ResponseInit): Response;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Represents a request handler function.
|
|
157
|
+
* Request handler functions receive a BurgerRequest and BurgerResponse as
|
|
158
|
+
* arguments. They must return a Response object, which can be either a Promise
|
|
159
|
+
* that resolves with a Response object or a Response object directly.
|
|
160
|
+
*
|
|
161
|
+
* @param request - The incoming BurgerRequest object.
|
|
162
|
+
* @param response - The outgoing BurgerResponse object.
|
|
163
|
+
* @returns A Response object or a Promise that resolves with a Response object.
|
|
164
|
+
*/
|
|
165
|
+
export type RequestHandler = (request: BurgerRequest, response: BurgerResponse) => Promise<Response> | Response;
|
|
166
|
+
/**
|
|
167
|
+
* Represents a middleware function in the request handling pipeline.
|
|
168
|
+
* Middleware functions have the ability to modify the request and response
|
|
169
|
+
* objects, end the request-response cycle, or call the next middleware function
|
|
170
|
+
* in the chain. They are executed in the order they are defined.
|
|
171
|
+
*
|
|
172
|
+
* @param request - The incoming BurgerRequest object.
|
|
173
|
+
* @param response - The outgoing BurgerResponse object.
|
|
174
|
+
* @param next - A function to call the next middleware in the chain.
|
|
175
|
+
* @returns A Promise that resolves with the response.
|
|
176
|
+
*/
|
|
177
|
+
export type Middleware = (request: BurgerRequest, response: BurgerResponse, next: () => Promise<Response>) => Promise<Response>;
|
|
178
|
+
export interface RouteDefinition {
|
|
179
|
+
/**
|
|
180
|
+
* The path of the route.
|
|
181
|
+
*/
|
|
182
|
+
path: string;
|
|
183
|
+
/**
|
|
184
|
+
* An object containing the request handlers for each HTTP method.
|
|
185
|
+
* The keys are the HTTP method names (e.g. "GET", "POST", etc.).
|
|
186
|
+
* The values are the request handlers for that method.
|
|
187
|
+
*/
|
|
188
|
+
handlers: {
|
|
189
|
+
[method: string]: RequestHandler;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* An array of middleware functions to run before the request handler.
|
|
193
|
+
* The middleware functions will be run in the order they are specified.
|
|
194
|
+
*/
|
|
195
|
+
middleware?: Middleware[];
|
|
196
|
+
/**
|
|
197
|
+
* An optional route schema to validate the request data against.
|
|
198
|
+
* This property is set by the user when defining a route.
|
|
199
|
+
* The schema is used to validate the request data for each HTTP method.
|
|
200
|
+
* The keys are the HTTP method names (in lowercase) and the values are
|
|
201
|
+
* the Zod schema objects for that method.
|
|
202
|
+
*/
|
|
203
|
+
schema?: RouteSchema;
|
|
204
|
+
/**
|
|
205
|
+
* Optional OpenAPI metadata to generate documentation for the route.
|
|
206
|
+
* If provided, this property should define an object where each key
|
|
207
|
+
* is an HTTP method name (in lowercase), and the value is an object
|
|
208
|
+
* containing the OpenAPI metadata for that method.
|
|
209
|
+
*/
|
|
210
|
+
openapi?: openapi;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Define a type for the route schema.
|
|
214
|
+
* For each HTTP method (in lowercase), you can optionally define:
|
|
215
|
+
* - params: for URL parameters,
|
|
216
|
+
* - query: for query string parameters,
|
|
217
|
+
* - body: for the request body.
|
|
218
|
+
*/
|
|
219
|
+
export type RouteSchema = {
|
|
220
|
+
[method: string]: {
|
|
221
|
+
params?: z.ZodTypeAny;
|
|
222
|
+
query?: z.ZodTypeAny;
|
|
223
|
+
body?: z.ZodTypeAny;
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Optional OpenAPI metadata to generate documentation for the route.
|
|
228
|
+
* Each key is an HTTP method name (in lowercase) and the value is an object
|
|
229
|
+
* containing the OpenAPI metadata for that method.
|
|
230
|
+
*
|
|
231
|
+
* If the `openapi` property is not defined, the route will not be included in
|
|
232
|
+
* the generated OpenAPI documentation.
|
|
233
|
+
*
|
|
234
|
+
* See the OpenAPI specification for the possible properties and their
|
|
235
|
+
* descriptions.
|
|
236
|
+
*/
|
|
237
|
+
export type openapi = {
|
|
238
|
+
[method: string]: {
|
|
239
|
+
summary?: string;
|
|
240
|
+
description?: string;
|
|
241
|
+
tags?: string[];
|
|
242
|
+
operationId?: string;
|
|
243
|
+
deprecated?: boolean;
|
|
244
|
+
responses?: Record<string, any>;
|
|
245
|
+
externalDocs?: {
|
|
246
|
+
description?: string;
|
|
247
|
+
url?: string;
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
export type TemplatingEngine = (templatePath: string, props?: Record<string, any>) => Promise<string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { z } from "zod";
|