|
1 | | -# api-machine - REST API Server Framework |
| 1 | +# API-Machine |
2 | 2 |
|
3 | 3 | A lightweight, TypeScript-first REST API framework built on Express with a class-based routing architecture. |
4 | 4 |
|
@@ -85,83 +85,17 @@ The [`examples/`](examples/) directory contains comprehensive examples: |
85 | 85 | - Request body validation |
86 | 86 | - Structured error responses |
87 | 87 |
|
88 | | -## Configuration |
89 | | - |
90 | | -### Server Options |
91 | | -#### port `number = 5000` |
92 | | - |
93 | | -The port number on which the server will listen for incoming requests. |
94 | | - |
95 | | -#### maxPayloadSizeMB `number = 10` |
96 | | - |
97 | | -Maximum size in megabytes for JSON request payloads that the server will accept. |
98 | | - |
99 | | -#### maxUrlEncodedSizeMB `number = 1` |
100 | | - |
101 | | -Maximum size in megabytes for URL-encoded request payloads that the server will accept. |
102 | | - |
103 | | -#### log `LogInterface = console` |
104 | | - |
105 | | -Custom logger interface for handling server logging (e.g. `ts-tiny-log`). Must implement the LogInterface contract. |
106 | | - |
107 | | -#### securityHeaders `SecurityHeadersOptions` |
108 | | - |
109 | | -Configuration for HTTP security headers. By default, api-machine is **secure by default** with: |
110 | | -- X-Powered-By header removed (prevents server fingerprinting) |
111 | | -- X-Content-Type-Options: nosniff (prevents MIME sniffing) |
112 | | -- X-Frame-Options: DENY (prevents clickjacking) |
113 | | -- X-XSS-Protection: 1; mode=block (legacy XSS protection) |
114 | | - |
115 | | -See **[Security Headers Documentation](docs/security-headers.md)** for detailed configuration options and best practices. |
116 | | - |
117 | 88 | ### Example with Options |
118 | 89 |
|
119 | 90 | ```typescript |
120 | | -const server = new MyServer({ |
| 91 | +const server = new MyApiServer({ |
121 | 92 | port: 8080, |
122 | 93 | maxPayloadSizeMB: 20, |
123 | 94 | maxUrlEncodedSizeMB: 2, |
124 | 95 | log: myCustomLogger |
125 | 96 | }); |
126 | 97 | ``` |
127 | 98 |
|
128 | | -## API Summary |
129 | | - |
130 | | -### RestServer |
131 | | - |
132 | | -Abstract class for creating REST API servers. |
133 | | - |
134 | | -**Methods:** |
135 | | -- `start()`: Starts the server |
136 | | -- `stop()`: Stops the server |
137 | | - |
138 | | - |
139 | | -### BaseApiRouter |
140 | | - |
141 | | -Abstract class for creating route groups. |
142 | | - |
143 | | -**Properties:** |
144 | | -- `path`: The base path for the router (e.g., `/api`) |
145 | | - |
146 | | -**Methods:** |
147 | | -- `routes()`: Abstract method to define endpoints (must be implemented) |
148 | | - |
149 | | -### BaseApiEndpoint |
150 | | - |
151 | | -Abstract class for creating API endpoints. |
152 | | - |
153 | | -**Properties:** |
154 | | -- `path`: The endpoint path (default: `''`) |
155 | | -- `method`: The HTTP method (default: `GET`). Can be: |
156 | | - - `EndpointMethods.GET` |
157 | | - - `EndpointMethods.POST` |
158 | | - - `EndpointMethods.PATCH` |
159 | | - - `EndpointMethods.PUT` |
160 | | - - `EndpointMethods.DELETE` |
161 | | - |
162 | | -**Methods:** |
163 | | -- `handle(request, response, next)`: Abstract method to handle requests (must be implemented) |
164 | | - |
165 | 99 | #### Using Different HTTP Methods |
166 | 100 |
|
167 | 101 | api-machine provides convenience classes for each HTTP method with appropriate default status codes: |
@@ -227,55 +161,11 @@ class DeleteUserEndpoint extends DeleteEndpoint { |
227 | 161 | } |
228 | 162 | ``` |
229 | 163 |
|
230 | | -**Available Endpoint Classes:** |
231 | | -- `GetEndpoint` - GET requests (200 OK) |
232 | | -- `PostEndpoint` - POST requests (201 Created) |
233 | | -- `PutEndpoint` - PUT requests (200 OK) |
234 | | -- `PatchEndpoint` - PATCH requests (200 OK) |
235 | | -- `DeleteEndpoint` - DELETE requests (204 No Content) |
236 | | -- `HealthCheckEndpoint` - Pre-built health check endpoint (GET /health) |
237 | | - |
238 | | -You can also use `BaseApiEndpoint` and manually set the `method` and `statusCode` properties if needed for custom behavior. |
239 | | - |
240 | 164 | #### Pre-Built Endpoints |
241 | 165 |
|
242 | 166 | ##### HealthCheckEndpoint |
243 | 167 |
|
244 | | -A ready-to-use health check endpoint that returns system status information. Simply include it in your router: |
245 | | - |
246 | | -```typescript |
247 | | -import { BaseApiRouter, HealthCheckEndpoint } from 'api-machine'; |
248 | | - |
249 | | -class MyRouter extends BaseApiRouter { |
250 | | - override path = '/api'; |
251 | | - |
252 | | - async routes() { |
253 | | - return [ |
254 | | - HealthCheckEndpoint, // Available at GET /api/health |
255 | | - // ... other endpoints |
256 | | - ]; |
257 | | - } |
258 | | -} |
259 | | -``` |
260 | | - |
261 | | -**Response Format:** |
262 | | -```json |
263 | | -{ |
264 | | - "status": "ok", |
265 | | - "timestamp": "2025-11-08T12:00:00.000Z", |
266 | | - "uptime": 123.45, |
267 | | - "environment": "development" |
268 | | -} |
269 | | -``` |
270 | | - |
271 | | -**Customizing the Path:** |
272 | | -```typescript |
273 | | -class CustomHealthCheck extends HealthCheckEndpoint { |
274 | | - override path = '/status'; // Available at GET /api/status |
275 | | -} |
276 | | -``` |
277 | | - |
278 | | -For advanced usage, extending the health check with custom checks, and deployment examples (Kubernetes, Docker, monitoring), see the **[Health Check Endpoint Documentation](docs/health-check-endpoint.md)**. |
| 168 | +A ready-to-use health check endpoint that returns system status information. Simply include it in your router. For advanced usage, extending the health check with custom checks, and deployment examples (Kubernetes, Docker, monitoring), see the **[Health Check Endpoint Documentation](docs/health-check-endpoint.md)**. |
279 | 169 |
|
280 | 170 | ## Error Handling |
281 | 171 |
|
@@ -303,7 +193,7 @@ class GetUserEndpoint extends GetEndpoint { |
303 | 193 | ``` |
304 | 194 |
|
305 | 195 | **Key Features:** |
306 | | -- 29 built-in error classes covering HTTP status codes 400-451 |
| 196 | +- Built-in error classes covering HTTP status codes 400-451 |
307 | 197 | - Automatic JSON error responses with timestamps |
308 | 198 | - Support for custom headers (e.g., `WWW-Authenticate`, `Retry-After`) |
309 | 199 | - Optional `details` field for additional context |
@@ -376,7 +266,7 @@ See **[Authentication Documentation](docs/authentication.md)** for complete usag |
376 | 266 |
|
377 | 267 | ## Middleware |
378 | 268 |
|
379 | | -Routers and endpoints support Express-style middleware for logging, validation, and more. See [Middleware Support](docs/middleware.md) for usage and examples. |
| 269 | +Routers and endpoints support Express middleware for logging, validation, and more. See [Middleware Support](docs/middleware.md) for usage and examples. |
380 | 270 |
|
381 | 271 | ## Contributing & Development |
382 | 272 |
|
|
0 commit comments