UNPKG

mock-openai-api

Version:

A mock OpenAI Compatible Provider API server

72 lines 2.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = __importDefault(require("express")); const cors_1 = __importDefault(require("cors")); const routes_1 = __importDefault(require("./routes")); const app = (0, express_1.default)(); // Middleware app.use((0, cors_1.default)()); app.use(express_1.default.json({ limit: "10mb" })); app.use(express_1.default.urlencoded({ extended: true })); // Request logging middleware (conditional) app.use((req, res, next) => { if (global.verboseLogging) { console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`); console.log(`Original URL: ${req.originalUrl}`); console.log(`Base URL: ${req.baseUrl}`); if (req.body && Object.keys(req.body).length > 0) { console.log("Request body:", JSON.stringify(req.body, null, 2)); } } next(); }); // Add a simple root endpoint for debugging app.get("/", (req, res) => { res.json({ message: "Mock OpenAI API Server", status: "running", timestamp: new Date().toISOString(), availableEndpoints: [ "GET /health", "GET /v1/models", "POST /v1/chat/completions", "POST /v1/images/generations", ], }); }); // Routes app.use("/", routes_1.default); // 404 handler app.use((req, res) => { if (global.verboseLogging) { console.log(`404 - Path not found: ${req.originalUrl}`); console.log(`Method: ${req.method}`); console.log(`Headers:`, req.headers); } res.status(404).json({ error: { message: `Path not found: ${req.originalUrl}`, type: "not_found_error", code: "path_not_found", method: req.method, path: req.path, originalUrl: req.originalUrl, }, }); }); // Global error handler app.use((err, req, res, next) => { console.error("Global error:", err); res.status(500).json({ error: { message: "Internal server error", type: "api_error", code: "internal_error", }, }); }); exports.default = app; //# sourceMappingURL=app.js.map