Skip to content

Commit

Permalink
Fix: bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaind committed Oct 14, 2024
1 parent 8ef14c4 commit 5054804
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
22 changes: 20 additions & 2 deletions index.js → api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,44 @@ const initializePrism = async () => {
const specPath = path.join(__dirname, '../pos.yml');
const specContent = fs.readFileSync(specPath, 'utf8');
const operations = await getHttpOperationsFromResource(specContent);
prism = createInstance({ config: { mock: { dynamic: true } } }, { operations });
prism = createInstance(
{ config: { mock: { dynamic: true } } },
{ components: { logger: { info() {}, error() {}, warn() {} } }, operations }
);
};

module.exports = async (req, res) => {
if (!prism) {
await initializePrism();
}

// Parse the URL and remove the '/api' prefix
const parsedUrl = parse(req.url, true);
const requestPath = parsedUrl.pathname.replace(/^\/api/, '') || '/';

// Set up the Prism request object
const request = {
method: req.method,
url: {
path: parsedUrl.pathname,
path: requestPath,
query: parsedUrl.query,
},
headers: req.headers,
body: req,
};

// CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

// Handle preflight requests
if (req.method === 'OPTIONS') {
res.statusCode = 204;
res.end();
return;
}

try {
const response = await prism.request(request);
res.statusCode = response.statusCode;
Expand Down
16 changes: 16 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"routes": [
{
"src": "/api/(.*)",
"dest": "api/index.js"
},
{
"handle": "filesystem"
},
{
"src": "/(.*)",
"dest": "/index.html"
}
]
}

0 comments on commit 5054804

Please sign in to comment.