systemprompt-mcp-reddit
Version:
A specialized Model Context Protocol (MCP) server that enables you to search, read, and interact with Reddit content, leveraging an AI Agent to help with each operation.
32 lines • 1.42 kB
JavaScript
import { RedditError } from "../../types/reddit.js";
export class RedditFetchService {
constructor(baseUrl, authService, rateLimitDelay) {
this.baseUrl = baseUrl;
this.authService = authService;
this.rateLimitDelay = rateLimitDelay;
}
async redditFetch(endpoint, options = {}) {
const headers = await this.authService.getAuthHeaders();
// Add rate limiting delay
await new Promise((resolve) => setTimeout(resolve, this.rateLimitDelay));
const response = await fetch(`${this.baseUrl}${endpoint}`, {
...options,
headers: {
...options.headers,
...headers,
},
});
if (!response.ok) {
if (response.status === 429) {
// Handle rate limiting
const retryAfter = parseInt(response.headers.get("Retry-After") || "60", 10);
await new Promise((resolve) => setTimeout(resolve, retryAfter * 1000));
return this.redditFetch(endpoint, options);
}
const errorData = await response.json().catch(() => null);
throw new RedditError(`Reddit API error: ${response.status} ${response.statusText}${errorData ? ` - ${JSON.stringify(errorData)}` : ""}`, "API_ERROR", errorData);
}
return response.json();
}
}
//# sourceMappingURL=reddit-fetch-service.js.map