Skip to content

Split endpoints by file #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 3 additions & 87 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,14 @@
import { Hono } from 'hono';
import { fetchThreadReplies, fetchUserProfile, fetchUserProfileThreads } from '../lib/fetch';
import apiRoute from './routes';


const port = +(Bun.env.PORT ?? 3000);

console.log('Initializing API server on port', port);

const app = new Hono();



// Endpoint to get user profiles based on userName
app.get('/api/users', async (context) => {
try {
// Extract the userName query parameter from the request
const userName = context.req.query('userName');

// If the userName is missing, return a "Missing userName" error response with status code 400
if (!userName) return context.text('Missing userName', 400);

// Fetch the user profile using the provided userName
const data = await fetchUserProfile({ userName });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});

// Endpoint to get a specific user profile based on userId
app.get('/api/users/:userId', async (context) => {
try {
// Extract the userId from the request parameters
const userId = context.req.param('userId');

// If the userName is missing, return a "Missing userId" error response with status code 400
if (!userId) return context.text('Missing userId', 400);

// Fetch the user profile using the provided userId
const data = await fetchUserProfile({ userId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});

// Endpoint to get replies from a specific thread
app.get('/api/threads/:threadId/replies', async (context) => {
try {
// Extract the threadId from the request parameters
const threadId = context.req.param('threadId');

// If the userName is missing, return a "Missing threadId" error response with status code 400
if (!threadId) return context.text('Missing threadId', 400);

// Fetch the thread replies using the provided threadId
const data = await fetchThreadReplies({ threadId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});


// Endpoint to get user profile threads
app.get('/api/users/:userId/threads', async (context) => {
try {
// Extract the userId from the request parameters
const userId = context.req.param('userId');

// If the userName is missing, return a "Missing userId" error response with status code 400
if (!userId) return context.text('Missing userId', 400);

// Fetch the user profile threads using the provided userId
const data = await fetchUserProfileThreads({ userId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});




app.route('/api',apiRoute)

app.use('*', async (c) => {
c.notFound();
Expand Down
10 changes: 10 additions & 0 deletions src/api/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Hono } from 'hono'
import threadsRoute from './threads'
import usersRoute from './users'

const apiRoute = new Hono();

apiRoute.route('/users', usersRoute)
apiRoute.route('/threads', threadsRoute)

export default apiRoute;
29 changes: 29 additions & 0 deletions src/api/routes/threads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Hono } from "hono";
import { fetchThreadReplies, fetchUserProfileThreads } from "../../lib";

const route = new Hono();

// Endpoint to get replies for a thread
route.get('/threads/:threadId/replies', async (context) => {
try {
// Extract the threadId from the request parameters
const threadId = context.req.param('threadId');

// If the userName is missing, return a "Missing threadId" error response with status code 400
if (!threadId) return context.text('Missing threadId', 400);

// Fetch the thread replies using the provided threadId
const data = await fetchThreadReplies({ threadId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});




export default route;
66 changes: 66 additions & 0 deletions src/api/routes/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Hono } from "hono";
import { fetchUserProfile, fetchUserProfileThreads } from "../../lib";

const route = new Hono();

// Endpoint to get user profiles based on userName
route.get('/', async (context) => {
try {
// Extract the userName query parameter from the request
const userName = context.req.query('userName');

// If the userName is missing, return a "Missing userName" error response with status code 400
if (!userName) return context.text('Missing userName', 400);

// Fetch the user profile using the provided userName
const data = await fetchUserProfile({ userName });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});

// Endpoint to get a specific user profile based on userId
route.get('/:userId', async (context) => {
try {
// Extract the userId from the request parameters
const userId = context.req.param('userId');

// If the userName is missing, return a "Missing userId" error response with status code 400
if (!userId) return context.text('Missing userId', 400);

// Fetch the user profile using the provided userId
const data = await fetchUserProfile({ userId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});

// Endpoint to get user profile threads
route.get('/:userId/threads', async (context) => {
try {
// Extract the userId from the request parameters
const userId = context.req.param('userId');

// If the userName is missing, return a "Missing userId" error response with status code 400
if (!userId) return context.text('Missing userId', 400);

// Fetch the user profile threads using the provided userId
const data = await fetchUserProfileThreads({ userId });

// Return the fetched data as a JSON response
return context.json(data);
} catch (error) {
// If an error occurs, respond with a 500 status code and an "Internal Server Error" message
return context.text('Internal Server Error', 500);
}
});

export default route