Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
feat(api): add NextJS 'GET' API endpoint for '/api/queryTypes'
Browse files Browse the repository at this point in the history
  ## 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
Clumsy-Coder committed Aug 20, 2023
1 parent a03bddf commit 54c77ad
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/pages/api/queryTypes.ts
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);

0 comments on commit 54c77ad

Please sign in to comment.