fix: address code review feedback
- Fix API endpoint paths: /events (not /api/events) to match actual NestJS routes - Convert script to ES modules (import/export) to match package.json type: module - Add detailed error messages for common HTTP status codes (401, 403, 404, 400) - Improve error handling with actionable guidance
This commit is contained in:
@@ -5,9 +5,9 @@
|
|||||||
* Interacts with Mosaic Stack's Events API for calendar management
|
* Interacts with Mosaic Stack's Events API for calendar management
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const https = require('https');
|
import https from 'https';
|
||||||
const http = require('http');
|
import http from 'http';
|
||||||
const { URL } = require('url');
|
import { URL } from 'url';
|
||||||
|
|
||||||
// Configuration
|
// Configuration
|
||||||
const API_URL = process.env.MOSAIC_API_URL || 'http://localhost:3001';
|
const API_URL = process.env.MOSAIC_API_URL || 'http://localhost:3001';
|
||||||
@@ -44,7 +44,20 @@ function apiRequest(method, path, body = null) {
|
|||||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
resolve(parsed);
|
resolve(parsed);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`HTTP ${res.statusCode}: ${parsed.message || data}`));
|
// Provide helpful error messages based on status code
|
||||||
|
let errorMsg = `HTTP ${res.statusCode}: ${parsed.message || data}`;
|
||||||
|
|
||||||
|
if (res.statusCode === 401) {
|
||||||
|
errorMsg += '\n → Check MOSAIC_API_TOKEN is valid';
|
||||||
|
} else if (res.statusCode === 403) {
|
||||||
|
errorMsg += '\n → Verify workspace permissions and MOSAIC_WORKSPACE_ID';
|
||||||
|
} else if (res.statusCode === 404) {
|
||||||
|
errorMsg += '\n → Resource not found. Check the event ID';
|
||||||
|
} else if (res.statusCode === 400) {
|
||||||
|
errorMsg += '\n → Invalid request. Check date formats and required fields';
|
||||||
|
}
|
||||||
|
|
||||||
|
reject(new Error(errorMsg));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(new Error(`Failed to parse response: ${data}`));
|
reject(new Error(`Failed to parse response: ${data}`));
|
||||||
@@ -105,7 +118,7 @@ async function createEvent(args) {
|
|||||||
if (args['project-id']) body.projectId = args['project-id'];
|
if (args['project-id']) body.projectId = args['project-id'];
|
||||||
if (args.metadata) body.metadata = JSON.parse(args.metadata);
|
if (args.metadata) body.metadata = JSON.parse(args.metadata);
|
||||||
|
|
||||||
const result = await apiRequest('POST', '/api/events', body);
|
const result = await apiRequest('POST', '/events', body);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +136,7 @@ async function listEvents(args) {
|
|||||||
if (args.limit) params.append('limit', args.limit);
|
if (args.limit) params.append('limit', args.limit);
|
||||||
|
|
||||||
const query = params.toString();
|
const query = params.toString();
|
||||||
const path = `/api/events${query ? '?' + query : ''}`;
|
const path = `/events${query ? '?' + query : ''}`;
|
||||||
|
|
||||||
const result = await apiRequest('GET', path);
|
const result = await apiRequest('GET', path);
|
||||||
return result;
|
return result;
|
||||||
@@ -137,7 +150,7 @@ async function getEvent(eventId) {
|
|||||||
throw new Error('Event ID required');
|
throw new Error('Event ID required');
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await apiRequest('GET', `/api/events/${eventId}`);
|
const result = await apiRequest('GET', `/events/${eventId}`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +173,7 @@ async function updateEvent(eventId, args) {
|
|||||||
if (args['project-id']) body.projectId = args['project-id'];
|
if (args['project-id']) body.projectId = args['project-id'];
|
||||||
if (args.metadata) body.metadata = JSON.parse(args.metadata);
|
if (args.metadata) body.metadata = JSON.parse(args.metadata);
|
||||||
|
|
||||||
const result = await apiRequest('PATCH', `/api/events/${eventId}`, body);
|
const result = await apiRequest('PATCH', `/events/${eventId}`, body);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +185,7 @@ async function deleteEvent(eventId) {
|
|||||||
throw new Error('Event ID required');
|
throw new Error('Event ID required');
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await apiRequest('DELETE', `/api/events/${eventId}`);
|
const result = await apiRequest('DELETE', `/events/${eventId}`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,11 +293,9 @@ Environment:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run if called directly
|
// Run if called directly
|
||||||
if (require.main === module) {
|
main();
|
||||||
main();
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
createEvent,
|
createEvent,
|
||||||
listEvents,
|
listEvents,
|
||||||
getEvent,
|
getEvent,
|
||||||
|
|||||||
Reference in New Issue
Block a user