feat: Refactor TypedServer to use SmartServe and introduce new request handlers
- Removed legacy servertools and Express dependencies in favor of SmartServe. - Introduced DevToolsHandler and TypedRequestHandler for handling specific routes. - Added support for custom route registration with regex parsing. - Implemented sitemap and feed handling with dedicated helper classes. - Enhanced HTML response handling with reload script injection. - Updated UtilityServiceServer and UtilityWebsiteServer to utilize new TypedServer API. - Removed deprecated compression options and Express-based route handling. - Added comprehensive request handling for various endpoints including robots.txt, manifest.json, and sitemap. - Improved error handling and response formatting across the server.
This commit is contained in:
43
ts/controllers/controller.typedrequest.ts
Normal file
43
ts/controllers/controller.typedrequest.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
/**
|
||||
* TypedRequest handler for type-safe RPC endpoint
|
||||
*/
|
||||
export class TypedRequestHandler {
|
||||
private typedRouter: plugins.typedrequest.TypedRouter;
|
||||
|
||||
constructor(typedRouter: plugins.typedrequest.TypedRouter) {
|
||||
this.typedRouter = typedRouter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a request - returns Response if handled, null otherwise
|
||||
*/
|
||||
public async handle(request: Request): Promise<Response | null> {
|
||||
const url = new URL(request.url);
|
||||
const path = url.pathname;
|
||||
|
||||
if (path === '/typedrequest' && request.method === 'POST') {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const response = await this.typedRouter.routeAndAddResponse(body);
|
||||
|
||||
return new Response(plugins.smartjson.stringify(response), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Invalid request' }), {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user