Files
pedwfrontend/pages/api/proxy/[...route].js
T

111 lines
3.5 KiB
JavaScript

import axios from "axios";
import https from "https";
import CryptoJS from "crypto-js";
const WEBAPI_URL = "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
// const azureHeaders = {
// headers: {
// "OData-MaxVersion": "4.0",
// "OData-Version": "4.0",
// "Accept": "application/json",
// "Prefer": 'odata.include-annotations="*",return=representation',
// "Content-Type": "application/json",
// "ServiceBusAuthorization": SASToken,
// },
// };
const getSASToken = () => {
var m_ResourceURI = "dev-pedw-ns.servicebus.windows.net";
var m_Path = "dev-pedw-hc";
var m_SasKey = "Ml0Y/S/nfRcmIrHFRkO4jNm2J3iH52TSxPiak7+E1+Y=";
var m_SasKeyName = "devpedwnspolicy";
var encodedResourceUri = encodeURIComponent(
"https://" + m_ResourceURI + "/" + m_Path + "/"
);
var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
var t1 = new Date();
var expireInSeconds =
+(31 * 24 * 3600) + 3600 + (((t1.getTime() - t0.getTime()) / 1000) | 0);
//the line below is a hack that converts to UTF8
var plainSignature = JSON.parse(
JSON.stringify(encodedResourceUri + "\n" + expireInSeconds)
);
var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey);
var base64HashValue = CryptoJS.enc.Base64.stringify(hash);
var token =
"SharedAccessSignature sr=" +
encodedResourceUri +
"&sig=" +
encodeURIComponent(base64HashValue) +
"&se=" +
expireInSeconds +
"&skn=" +
m_SasKeyName;
return token;
};
export default async function ApiProxy(req, res) {
// console.log(req.method);
var updateBody = {
"pinswg_areaofsiteinhectaresdec": 21,
"pinswg_developmentaffectsettingifalisted": false,
"pinswg_floorspaceinsquaremeters": "123123124",
"pinswg_incarelatestoca": false,
"pinswg_isfloodinganissue": true,
"pinswg_isthesitewithinanaonb": false,
"pinswg_landuse": "846040002",
"pinswg_sitewithinsssi": true,
"pinswg_sitewithinagreenbelt": false,
"pinswg_siteviewablefromroad": false,
"pinswg_typeofplanningapplication": "846040000",
};
var data = JSON.stringify(req.body);
const SASToken = getSASToken();
// console.log(SASToken);
var config = {
method: req.method,
//url: WEBAPI_URL + updateFormCollection + "(" + incidentId + ")",
url: WEBAPI_URL + req.query.route[0],
headers: {
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"Accept": "application/json",
"Prefer": 'odata.include-annotations="*",return=representation',
"Authorization": SASToken,
"Content-Type": "application/json",
},
data: data,
};
console.log(config);
return new Promise((resolve, reject) => {
// console.log(config);
axios(config)
.then((response) => {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.setHeader("Cache-Control", "max-age=1800000");
res.end(JSON.stringify(response.data));
console.log("response data", response.data);
resolve();
})
.catch((error) => {
console.log("proxy response error", error);
res.json(error);
res.status(405).end();
return resolve();
});
});
}