This commit is contained in:
65
utils/apiEnvelope.js
Normal file
65
utils/apiEnvelope.js
Normal file
@@ -0,0 +1,65 @@
|
||||
function envelopeResponses(_req, res, next) {
|
||||
const originalJson = res.json.bind(res);
|
||||
|
||||
res.json = (payload) => {
|
||||
if (isApiEnvelope(payload)) {
|
||||
return originalJson(payload);
|
||||
}
|
||||
|
||||
const isError = res.statusCode >= 400;
|
||||
const message = extractMessage(payload, isError);
|
||||
const errors = extractErrors(payload, isError, message);
|
||||
const data = isError ? null : payload;
|
||||
|
||||
return originalJson({
|
||||
status: isError ? "error" : "success",
|
||||
data,
|
||||
message,
|
||||
errors,
|
||||
});
|
||||
};
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
function isApiEnvelope(payload) {
|
||||
return Boolean(
|
||||
payload &&
|
||||
typeof payload === "object" &&
|
||||
!Array.isArray(payload) &&
|
||||
Object.prototype.hasOwnProperty.call(payload, "status") &&
|
||||
Object.prototype.hasOwnProperty.call(payload, "data") &&
|
||||
Object.prototype.hasOwnProperty.call(payload, "message") &&
|
||||
Object.prototype.hasOwnProperty.call(payload, "errors")
|
||||
);
|
||||
}
|
||||
|
||||
function extractMessage(payload, isError) {
|
||||
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
||||
if (typeof payload.message === "string" && payload.message.length) {
|
||||
return payload.message;
|
||||
}
|
||||
if (typeof payload.error === "string" && payload.error.length) {
|
||||
return payload.error;
|
||||
}
|
||||
}
|
||||
|
||||
return isError ? "Request failed" : "OK";
|
||||
}
|
||||
|
||||
function extractErrors(payload, isError, message) {
|
||||
if (!isError) return [];
|
||||
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
|
||||
if (Array.isArray(payload.errors)) {
|
||||
return payload.errors;
|
||||
}
|
||||
if (payload.error) {
|
||||
return [payload.error];
|
||||
}
|
||||
}
|
||||
return message ? [message] : [];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
envelopeResponses,
|
||||
};
|
||||
45
utils/bbox.js
Normal file
45
utils/bbox.js
Normal file
@@ -0,0 +1,45 @@
|
||||
function getBBox(geometry) {
|
||||
let minLng = Infinity, minLat = Infinity;
|
||||
let maxLng = -Infinity, maxLat = -Infinity;
|
||||
|
||||
function process(coords) {
|
||||
if (typeof coords[0] === "number") {
|
||||
const [lng, lat] = coords;
|
||||
|
||||
minLng = Math.min(minLng, lng);
|
||||
minLat = Math.min(minLat, lat);
|
||||
maxLng = Math.max(maxLng, lng);
|
||||
maxLat = Math.max(maxLat, lat);
|
||||
} else {
|
||||
coords.forEach(process);
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 handle theo type cho rõ ràng
|
||||
switch (geometry.type) {
|
||||
case "Point":
|
||||
process(geometry.coordinates);
|
||||
break;
|
||||
|
||||
case "MultiPoint":
|
||||
case "LineString":
|
||||
process(geometry.coordinates);
|
||||
break;
|
||||
|
||||
case "MultiLineString":
|
||||
case "Polygon":
|
||||
process(geometry.coordinates);
|
||||
break;
|
||||
|
||||
case "MultiPolygon":
|
||||
process(geometry.coordinates);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("Unsupported geometry type: " + geometry.type);
|
||||
}
|
||||
|
||||
return { minLng, minLat, maxLng, maxLat };
|
||||
}
|
||||
|
||||
module.exports = { getBBox };
|
||||
Reference in New Issue
Block a user