diff --git a/src/api/index.ts b/src/api/index.ts index 6925d65..ac4021f 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,6 @@ import { Hono } from 'hono'; -import { fetchThreadReplies, fetchUserProfile, fetchUserProfileThreads } from '../lib/fetch'; +import apiRoute from './routes'; + const port = +(Bun.env.PORT ?? 3000); @@ -7,92 +8,7 @@ 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(); diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts new file mode 100644 index 0000000..29a9bc4 --- /dev/null +++ b/src/api/routes/index.ts @@ -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; diff --git a/src/api/routes/threads.ts b/src/api/routes/threads.ts new file mode 100644 index 0000000..8c48f4f --- /dev/null +++ b/src/api/routes/threads.ts @@ -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; \ No newline at end of file diff --git a/src/api/routes/users.ts b/src/api/routes/users.ts new file mode 100644 index 0000000..2978897 --- /dev/null +++ b/src/api/routes/users.ts @@ -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 \ No newline at end of file