You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
import fetch from "node-fetch";
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
import { getSession } from "next-auth/client";
|
|
|
|
global.fetch = fetch;
|
|
|
|
import config from "../../../config";
|
|
|
|
export default async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method !== "POST") {
|
|
res.status(405).json({error: "Method not allowed;."});
|
|
return;
|
|
}
|
|
|
|
if (typeof(req.body) !== "object" || typeof(req.body.query) !== "string") {
|
|
res.status(400).json({error: "Invalid request body (expected json object with query)"});
|
|
return;
|
|
}
|
|
|
|
const authHeaders = {};
|
|
const session = await getSession({req});
|
|
if (session != null && session.user != null) {
|
|
authHeaders["Authorization"] = `Bearer ${config.jwtToken(session.user.name, 15)}`;
|
|
}
|
|
|
|
const apires = await fetch(config.apiUrl("/graphql"), {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...authHeaders,
|
|
},
|
|
body: JSON.stringify(req.body),
|
|
});
|
|
|
|
res.setHeader("Content-Type", apires.headers.get("Content-Type"));
|
|
res.status(apires.status);
|
|
apires.body.pipe(res);
|
|
}
|