Files
pedwfrontend/pages/api/endpoint/getformdata_api.js
T

46 lines
1.4 KiB
JavaScript

import axios from "axios";
import { azureHeaders } from "../../../actions/core/headers";
import { consoleLogger } from "../../../actions/core/logger";
import { getToken } from "../../../actions/core/token";
import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
const WEBAPI_URL =
process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
export default async function ApiProxy(req, res) {
const whichForm = req.query.whichForm;
if (typeof whichForm !== "string" || whichForm.trim().length === 0) {
return respondError(res, {
status: 400,
code: "WHICH_FORM_REQUIRED",
message: "whichForm is required"
});
}
try {
const token = await getToken();
const queryUrl =
"systemforms?$select=formid,name,formxml,type,objecttypecode&$filter=(objecttypecode eq 'pinswg_" +
whichForm +
"' and type eq 2)&$count=true&$top=201";
const { data } = await axios.get(
WEBAPI_URL + queryUrl + hashAPIPath(queryUrl),
azureHeaders(token.access_token)
);
return respondSuccess(res, data);
} catch (error) {
consoleLogger(error);
return respondError(res, {
status: 400,
code: "FORM_DATA_FETCH_FAILED",
message: "Failed to fetch form data"
});
}
}