Skip to content

Commit 27d8a0e

Browse files
Added more JPL APIs
1 parent 57de492 commit 27d8a0e

File tree

8 files changed

+984
-89
lines changed

8 files changed

+984
-89
lines changed

src/handlers/jpl/cad.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import axios from 'axios';
2+
import { addResource } from '../../index.js';
3+
4+
/**
5+
* Handler for JPL SB Close Approach (CAD) API
6+
*
7+
* This API provides data about asteroid and comet close approaches to planets
8+
* in the past and future.
9+
*
10+
* @param args Request parameters
11+
* @returns API response
12+
*/
13+
export async function cadHandler(args: Record<string, any>) {
14+
try {
15+
// Base URL for the CAD API
16+
const baseUrl = 'https://ssd-api.jpl.nasa.gov/cad.api';
17+
18+
// Validate parameters if needed
19+
// Parameters are fairly flexible in this API, so minimal validation is needed
20+
21+
// Make the API request
22+
const response = await axios.get(baseUrl, { params: args });
23+
const data = response.data;
24+
25+
// Create a resource URI that represents this query
26+
let resourceUri: string;
27+
let resourceName: string;
28+
29+
if (args.des) {
30+
// Query for a specific object
31+
resourceUri = `jpl://cad/object/${args.des}`;
32+
resourceName = `Close approaches for object ${args.des}`;
33+
} else if (args.spk) {
34+
// Query for a specific object by SPK-ID
35+
resourceUri = `jpl://cad/object/${args.spk}`;
36+
resourceName = `Close approaches for object ${args.spk}`;
37+
} else {
38+
// Query for close approaches with constraints
39+
const constraints = Object.entries(args)
40+
.map(([key, value]) => `${key}=${value}`)
41+
.join('&');
42+
43+
resourceUri = `jpl://cad/list${constraints ? '?' + constraints : ''}`;
44+
45+
// Create a readable name based on date range and body
46+
const dateMin = args['date-min'] || 'now';
47+
const dateMax = args['date-max'] || '+60';
48+
const body = args.body || 'Earth';
49+
50+
resourceName = `Close approaches to ${body} from ${dateMin} to ${dateMax}`;
51+
}
52+
53+
// Add response to resources
54+
addResource(resourceUri, {
55+
name: resourceName,
56+
mimeType: "application/json",
57+
text: JSON.stringify(data, null, 2)
58+
});
59+
60+
// Format the response
61+
return {
62+
content: [{
63+
type: "text",
64+
text: JSON.stringify(data, null, 2)
65+
}]
66+
};
67+
} catch (error: any) {
68+
return {
69+
content: [{
70+
type: "text",
71+
text: `Error accessing JPL SB Close Approach API: ${error.message}`
72+
}],
73+
isError: true
74+
};
75+
}
76+
}
77+
78+
// Export default for dynamic imports
79+
export default cadHandler;

src/handlers/jpl/horizons.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import axios from 'axios';
2+
import { addResource } from '../../index.js';
3+
4+
/**
5+
* Handler for JPL Horizons API
6+
*
7+
* This API provides ephemeris data for solar system objects (planets, moons, asteroids, comets, etc.).
8+
*
9+
* @param args Request parameters
10+
* @returns API response
11+
*/
12+
export async function horizonsHandler(args: Record<string, any>) {
13+
try {
14+
// Base URL for the Horizons API
15+
const baseUrl = 'https://ssd.jpl.nasa.gov/api/horizons.api';
16+
17+
// Make the API request
18+
const response = await axios.get(baseUrl, { params: args });
19+
const data = response.data;
20+
21+
// Create a resource URI that represents this query
22+
let resourceUri = 'jpl://horizons';
23+
let resourceName = 'JPL Horizons ephemeris data';
24+
25+
// Customize resource name based on the request type
26+
if (args.COMMAND) {
27+
// Add the object identifier to the resource URI
28+
resourceUri += `/object/${encodeURIComponent(args.COMMAND)}`;
29+
30+
// Update resource name based on EPHEM_TYPE
31+
if (args.EPHEM_TYPE === 'OBSERVER') {
32+
resourceName = `${args.COMMAND} observer ephemeris data`;
33+
} else if (args.EPHEM_TYPE === 'VECTORS') {
34+
resourceName = `${args.COMMAND} vector ephemeris data`;
35+
} else if (args.EPHEM_TYPE === 'ELEMENTS') {
36+
resourceName = `${args.COMMAND} orbital elements data`;
37+
} else {
38+
resourceName = `${args.COMMAND} ephemeris data`;
39+
}
40+
41+
// Add time range info if available
42+
if (args.START_TIME && args.STOP_TIME) {
43+
resourceName += ` (${args.START_TIME} to ${args.STOP_TIME})`;
44+
}
45+
}
46+
47+
// Add query parameters to the URI
48+
const queryParams = Object.entries(args)
49+
.filter(([key]) => key !== 'COMMAND' && key !== 'format')
50+
.map(([key, value]) => `${key}=${encodeURIComponent(String(value))}`)
51+
.join('&');
52+
53+
if (queryParams) {
54+
resourceUri += `?${queryParams}`;
55+
}
56+
57+
// Add response to resources
58+
addResource(resourceUri, {
59+
name: resourceName,
60+
mimeType: "application/json",
61+
text: JSON.stringify(data, null, 2)
62+
});
63+
64+
// Format the response
65+
return {
66+
content: [{
67+
type: "text",
68+
text: JSON.stringify(data, null, 2)
69+
}]
70+
};
71+
} catch (error: any) {
72+
return {
73+
content: [{
74+
type: "text",
75+
text: `Error accessing JPL Horizons API: ${error.message}`
76+
}],
77+
isError: true
78+
};
79+
}
80+
}
81+
82+
// Export default for dynamic imports
83+
export default horizonsHandler;

src/handlers/jpl/jd_cal.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import axios from 'axios';
2+
import { addResource } from '../../index.js';
3+
4+
/**
5+
* Handler for JPL Julian Date Calendar Conversion API
6+
*
7+
* This API converts between Julian dates and calendar dates (UTC)
8+
*
9+
* @param args Request parameters
10+
* @returns API response
11+
*/
12+
export async function jdCalHandler(args: Record<string, any>) {
13+
try {
14+
// Base URL for the JD Calendar API
15+
const baseUrl = 'https://ssd-api.jpl.nasa.gov/jd_cal.api';
16+
17+
// Validate parameters
18+
if (!args.jd && !args.cd) {
19+
return {
20+
content: [{
21+
type: "text",
22+
text: "Error: Either a Julian date (jd) or calendar date (cd) must be provided."
23+
}],
24+
isError: true
25+
};
26+
}
27+
28+
// Make the API request
29+
const response = await axios.get(baseUrl, { params: args });
30+
const data = response.data;
31+
32+
// Add response to resources
33+
const resourceUri = `jpl://jd_cal/${args.jd || args.cd}`;
34+
addResource(resourceUri, {
35+
name: `Julian Date / Calendar Date Conversion: ${args.jd || args.cd}`,
36+
mimeType: "application/json",
37+
text: JSON.stringify(data, null, 2)
38+
});
39+
40+
// Format the response
41+
return {
42+
content: [{
43+
type: "text",
44+
text: JSON.stringify(data, null, 2)
45+
}]
46+
};
47+
} catch (error: any) {
48+
return {
49+
content: [{
50+
type: "text",
51+
text: `Error accessing JPL Julian Date Calendar API: ${error.message}`
52+
}],
53+
isError: true
54+
};
55+
}
56+
}
57+
58+
// Export default for dynamic imports
59+
export default jdCalHandler;

src/handlers/jpl/nhats.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import axios from 'axios';
2+
import { addResource } from '../../index.js';
3+
4+
/**
5+
* Handler for JPL NHATS API (Human-accessible NEOs data)
6+
*
7+
* This API provides data from the NASA/JPL NHATS database about Near-Earth Objects (NEOs)
8+
* that are potentially accessible by human missions.
9+
*
10+
* @param args Request parameters
11+
* @returns API response
12+
*/
13+
export async function nhatsHandler(args: Record<string, any>) {
14+
try {
15+
// Base URL for the NHATS API
16+
const baseUrl = 'https://ssd-api.jpl.nasa.gov/nhats.api';
17+
18+
// Validate parameters if needed
19+
// Parameters are fairly flexible in this API, so minimal validation is needed
20+
21+
// Make the API request
22+
const response = await axios.get(baseUrl, { params: args });
23+
const data = response.data;
24+
25+
// Create a resource URI that represents this query
26+
let resourceUri: string;
27+
28+
if (args.des) {
29+
// Object mode - query for a specific object
30+
resourceUri = `jpl://nhats/object/${args.des}`;
31+
} else if (args.spk) {
32+
// Object mode - query for a specific object by SPK-ID
33+
resourceUri = `jpl://nhats/object/${args.spk}`;
34+
} else {
35+
// Summary mode - query for a list of objects with constraints
36+
const constraints = Object.entries(args)
37+
.map(([key, value]) => `${key}=${value}`)
38+
.join('&');
39+
40+
resourceUri = `jpl://nhats/summary${constraints ? '?' + constraints : ''}`;
41+
}
42+
43+
// Add response to resources
44+
addResource(resourceUri, {
45+
name: args.des || args.spk
46+
? `NHATS data for object: ${args.des || args.spk}`
47+
: 'NHATS summary data',
48+
mimeType: "application/json",
49+
text: JSON.stringify(data, null, 2)
50+
});
51+
52+
// Format the response
53+
return {
54+
content: [{
55+
type: "text",
56+
text: JSON.stringify(data, null, 2)
57+
}]
58+
};
59+
} catch (error: any) {
60+
return {
61+
content: [{
62+
type: "text",
63+
text: `Error accessing JPL NHATS API: ${error.message}`
64+
}],
65+
isError: true
66+
};
67+
}
68+
}
69+
70+
// Export default for dynamic imports
71+
export default nhatsHandler;

0 commit comments

Comments
 (0)