personal-site/pages/api/test-route.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-10-24 17:59:00 +00:00
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
import Cors from "cors";
const cors = Cors({
methods: ["GET"],
origin: "*",
});
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
2024-10-05 11:11:28 +00:00
function runMiddleware(
req: NextApiRequest,
res: NextApiResponse,
fn: Function
) {
2023-10-24 17:59:00 +00:00
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
2024-10-05 11:11:28 +00:00
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
2023-10-24 17:59:00 +00:00
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", "*");
2024-10-05 11:11:28 +00:00
res.setHeader(
"Access-Control-Allow-Methods",
"GET,OPTIONS,PATCH,DELETE,POST,PUT"
);
res.setHeader(
"Access-Control-Allow-Headers",
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
);
2023-10-24 17:59:00 +00:00
res.json({
title: "Hello There",
message: "General Kenobi",
});
}