updated mock to use routes

This commit is contained in:
Peter Morton 2025-05-28 18:26:03 -05:00
parent 07b7cee3b1
commit d9934cd2ad

View File

@ -1,24 +1,69 @@
(async () => {
const caseId = "003abc000123xyzabc";
const app = {
routes: [],
route: function (regexp, fn) {
this.routes.push({ regexp: new RegExp(regexp), function: fn });
},
run: function (req, res) {
const path =
req.params[0].split(
`/ProxyScript/run/${req.params.workspaceId}/${req.params.branch}/${req.params.route}`
)[1] || "/";
try {
console.log({ URL: req.url, body: req.body });
if (req.url.endsWith("token")) {
res.json({ access_token: "TOKEN" });
} else if (req.url.endsWith(caseId)) {
res.json({
id: caseId,
CaseNumber: 1024,
});
} else {
// create a case
res.json({ id: caseId });
for (var route of this.routes) {
if (route.regexp.test(path)) {
route.function(req, res, route.regexp.exec(path));
return;
}
}
} catch (error) {
res.json({
error: {
message: error.message,
},
// If no route matches, return a 404 status
const status = { path: req.params[0], status: 404 };
res.status(404).send(status);
},
};
const caseId = "003abc000123xyzabc";
app.route("/services/oauth2/token", async (req, res) => {
try {
return res.status(200).send({ access_token: "TOKEN" });
} catch {
console.error(error);
return res.status(500).send({
status: 500,
});
}
})();
});
app.route(
"^(?:/services/data/v57.0/sobjects/Case)(?:/$)?$",
async (req, res) => {
try {
res.json({ id: caseId });
} catch (error) {
console.error(error);
return res.status(500).send({
status: 500,
});
}
}
);
app.route(
"^(?:/services/data/v57.0/sobjects/Case/([^/]+))(?:/$)?$",
async (req, res, match) => {
try {
if (match.length > 1) {
res.json({
id: caseId,
CaseNumber: 1024,
});
}
} catch (error) {
console.error(error);
return res.status(500).send({
status: 500,
});
}
}
);
app.run(req, res);