add legacy endpoint

This commit is contained in:
Matthias Nannt
2023-03-30 13:20:33 +02:00
parent 22d4120a2d
commit e2d76a6ba3
5 changed files with 134 additions and 0 deletions

View File

@@ -22,6 +22,20 @@ const nextConfig = {
},
],
},
{
// matching all API routes
source: "/api/capture/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" },
{ key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
{
key: "Access-Control-Allow-Headers",
value:
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
},
],
},
];
},
};

View File

@@ -0,0 +1,39 @@
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const formId = req.query.formId?.toString();
// CORS
if (req.method === "OPTIONS") {
res.status(200).end();
}
// POST/capture/forms/[formId]/schema
// Update form schema
// Required fields in body: -
// Optional fields in body: customerId, data
else if (req.method === "POST") {
if (process.env.FORMBRICKS_LEGACY_HOST) {
const response = await fetch(
`${process.env.FORMBRICKS_LEGACY_HOST}/api/capture/forms/${formId}/schema`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.FORMBRICKS_LEGACY_HOST}`,
},
body: JSON.stringify(req.body),
}
);
const responseData = await response.json();
res.json(responseData);
} else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}
// Unknown HTTP Method
else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}

View File

@@ -0,0 +1,41 @@
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const formId = req.query.formId?.toString();
const submissionId = req.query.submissionId?.toString();
// CORS
if (req.method === "OPTIONS") {
res.status(200).end();
}
// PUT /capture/forms/[formId]/submissions/[submissionId]
// Extend an existing form submission
// Required fields in body: -
// Optional fields in body: customerId, data
else if (req.method === "PUT") {
// redirect request and make a request to legacy.formbricks.com
if (process.env.FORMBRICKS_LEGACY_HOST) {
const response = await fetch(
`${process.env.FORMBRICKS_LEGACY_HOST}/api/capture/forms/${formId}/submissions/${submissionId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.FORMBRICKS_LEGACY_HOST}`,
},
body: JSON.stringify(req.body),
}
);
const responseData = await response.json();
res.json(responseData);
} else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}
// Unknown HTTP Method
else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}

View File

@@ -0,0 +1,39 @@
import type { NextApiRequest, NextApiResponse } from "next";
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const formId = req.query.formId?.toString();
// CORS
if (req.method === "OPTIONS") {
res.status(200).end();
}
// POST/capture/forms/[formId]/submissions
// Create a new form submission
// Required fields in body: -
// Optional fields in body: customerId, data
else if (req.method === "POST") {
if (process.env.FORMBRICKS_LEGACY_HOST) {
const response = await fetch(
`${process.env.FORMBRICKS_LEGACY_HOST}/api/capture/forms/${formId}/submissions`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.FORMBRICKS_LEGACY_HOST}`,
},
body: JSON.stringify(req.body),
}
);
const responseData = await response.json();
res.json(responseData);
} else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}
// Unknown HTTP Method
else {
throw new Error(`The HTTP ${req.method} method is not supported by this route.`);
}
}

View File

@@ -5,6 +5,7 @@
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**"],
"env": [
"FORMBRICKS_LEGACY_HOST",
"GITHUB_ID",
"GITHUB_SECRET",
"MAIL_FROM",