This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add NextJS 'GET' API endpoint for '/api/queryTypes'
## what - add 'GET' API endpoint for '/api/queryTypes' ## how - Fetch data from upstream Pi-hole API ## why - This API endpoint will be used to fetch 'query types' data using Redux Toolkit Query ## where - ./src/pages/api/queryTypes.ts ## usage
- Loading branch information
1 parent
a03bddf
commit 54c77ad
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import axios from 'axios'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import { withSessionRoute } from '@lib/AuthSession'; | ||
import logger from '@utils/logger'; | ||
import { queryTypes as queryTypesUrl } from '@utils/url/upstream'; | ||
import { IQueryTypes } from '@utils/url/upstream.types'; | ||
|
||
const apiUrl = '/api/queryTypes'; | ||
|
||
/** | ||
* Error message to return to the Requester | ||
*/ | ||
interface ErrorMessage { | ||
message: string; | ||
} | ||
|
||
/** | ||
* Summary 'formatted' data to return | ||
*/ | ||
export type IGetQueryTypesResponseData = IQueryTypes; | ||
|
||
/** | ||
* GET endpoint for /api/queryTypes | ||
* | ||
* @remarks | ||
* Returns forwarded destinations query data | ||
* | ||
* Must be authenticated | ||
* @param req - HTTP request provided by NextJS | ||
* @param res - HTTP response provided by NextJS | ||
*/ | ||
const handleGetQueryTypes = ( | ||
req: NextApiRequest, | ||
res: NextApiResponse<IGetQueryTypesResponseData | ErrorMessage>, | ||
) => { | ||
const getLogger = logger.scope(apiUrl, 'GET'); | ||
const { ipAddress, port, password } = req.session.authSession; | ||
const requestUrl = `http://${ipAddress}:${port}/${queryTypesUrl()}&auth=${password}`; | ||
|
||
axios | ||
.get<IQueryTypes>(requestUrl) | ||
.then((response) => { | ||
getLogger.info('data obtained from upstream'); | ||
getLogger.complete(`sending response: `, response.data); | ||
res.status(200).json(response.data); | ||
}) | ||
.catch((error) => { | ||
getLogger.error(`error returned when sending HTTP request to '${requestUrl}'`); | ||
res.status(500).json({ message: JSON.stringify(error) }); | ||
}); | ||
}; | ||
|
||
/** | ||
* Default method to run when executing this http api endpoint | ||
* | ||
* @remarks | ||
* HTTP API endpoint `/api/queryTypes` | ||
* | ||
* @remarks | ||
* HTTP method allowed: `GET` | ||
*/ | ||
const requestHandler = (req: NextApiRequest, res: NextApiResponse) => { | ||
const { method = '' } = req; | ||
|
||
// limit which HTTP methods are allowed | ||
switch (method) { | ||
case 'GET': { | ||
handleGetQueryTypes(req, res); | ||
break; | ||
} | ||
default: { | ||
logger.error({ | ||
prefix: apiUrl, | ||
message: `invalid HTTP method type '${method}'`, | ||
}); | ||
res.setHeader('Allow', ['GET']); | ||
res.status(405).end(`Method ${method} Not Allowed`); | ||
} | ||
} | ||
}; | ||
|
||
export default withSessionRoute(requestHandler); |