UNPKG

@iota-big3/sdk-gateway

Version:

Universal API Gateway with protocol translation, intelligent routing, rate limiting, health checking, and caching

75 lines 2.51 kB
"use strict"; /** * @iota-big3/sdk-gateway * Clean HTTP Adapter - Phase 2d Rebuild */ Object.defineProperty(exports, "__esModule", { value: true }); exports.HTTPAdapter = void 0; const tslib_1 = require("tslib"); const express_1 = tslib_1.__importDefault(require("express")); class HTTPAdapter { constructor() { this.protocol = 'http'; } async initializeAsync(config) { this.gateway = config.gateway; this.app = (0, express_1.default)(); // Middleware this.app.use(express_1.default.json()); this.app.use(express_1.default.urlencoded({ extended: true })); // Gateway handler this.app?.all('*', async (req, res) => { try { const gatewayRequest = this.convertRequest(req); const gatewayResponse = await this.gateway.handleRequest(gatewayRequest); this.sendResponse(res, gatewayResponse); } catch (error) { res.status(500).json({ error: error instanceof Error ? error.message : 'Unknown error' }); } }); // Start server const port = config.port || 8080; this.server = this.app.listen(port); // HTTP adapter listening on port ${port} } convertRequest(req) { return { id: req.headers['x-request-id'] || `req-${Date.now()}`, method: req.method, path: req.path, headers: req.headers, query: req.query, body: req.body, protocol: 'http' }; } sendResponse(res, gatewayResponse) { // Set headers Object.entries(gatewayResponse.headers).forEach(([key, value]) => { res.setHeader(key, value); }); // Send response res.status(gatewayResponse.statusCode).json(gatewayResponse.body); } async handleRequestAsync(request) { // This would make the actual HTTP request to the backend service // For now, returning a mock response return { statusCode: 200, headers: { 'content-type': 'application/json' }, body: { message: 'Response from service' } }; } async shutdownAsync() { if (this.server) { await new Promise((resolve) => { this.server.close(() => resolve()); }); } } } exports.HTTPAdapter = HTTPAdapter; //# sourceMappingURL=http.adapter.js.map