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
|
||||
*/
|
||||
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const { URL } = require('url');
|
||||
import https from 'https';
|
||||
import http from 'http';
|
||||
import { URL } from 'url';
|
||||
|
||||
// Configuration
|
||||
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) {
|
||||
resolve(parsed);
|
||||
} 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) {
|
||||
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.metadata) body.metadata = JSON.parse(args.metadata);
|
||||
|
||||
const result = await apiRequest('POST', '/api/events', body);
|
||||
const result = await apiRequest('POST', '/events', body);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -123,7 +136,7 @@ async function listEvents(args) {
|
||||
if (args.limit) params.append('limit', args.limit);
|
||||
|
||||
const query = params.toString();
|
||||
const path = `/api/events${query ? '?' + query : ''}`;
|
||||
const path = `/events${query ? '?' + query : ''}`;
|
||||
|
||||
const result = await apiRequest('GET', path);
|
||||
return result;
|
||||
@@ -137,7 +150,7 @@ async function getEvent(eventId) {
|
||||
throw new Error('Event ID required');
|
||||
}
|
||||
|
||||
const result = await apiRequest('GET', `/api/events/${eventId}`);
|
||||
const result = await apiRequest('GET', `/events/${eventId}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -160,7 +173,7 @@ async function updateEvent(eventId, args) {
|
||||
if (args['project-id']) body.projectId = args['project-id'];
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -172,7 +185,7 @@ async function deleteEvent(eventId) {
|
||||
throw new Error('Event ID required');
|
||||
}
|
||||
|
||||
const result = await apiRequest('DELETE', `/api/events/${eventId}`);
|
||||
const result = await apiRequest('DELETE', `/events/${eventId}`);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -280,11 +293,9 @@ Environment:
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
main();
|
||||
|
||||
module.exports = {
|
||||
export {
|
||||
createEvent,
|
||||
listEvents,
|
||||
getEvent,
|
||||
|
||||
Reference in New Issue
Block a user