Adding new logging function
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
import fetch from "node-fetch";
|
||||
import fetchEbayReadToken from "../utils/fetchEbayReadToken.js"; // Adjust the import path according to your project structure
|
||||
import { LoggingLevel, smartLogging } from "../utils/helper.js";
|
||||
|
||||
export const itemLookup = async (req, res) => {
|
||||
smartLogging(LoggingLevel.Debug, ``)
|
||||
const productCode = req.query.productCode;
|
||||
const oauthToken = await fetchEbayReadToken();
|
||||
console.log(productCode);
|
||||
let oauthToken;
|
||||
try{
|
||||
oauthToken = await fetchEbayReadToken();
|
||||
}catch(e){
|
||||
smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`);
|
||||
}
|
||||
// console.log(productCode);
|
||||
smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`);
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`,
|
||||
@@ -28,7 +36,7 @@ export const itemLookup = async (req, res) => {
|
||||
|
||||
res.status(200).send(price);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data from eBay Browse API:", error);
|
||||
smartLogging(LoggingLevel.Error, `Error fetching data from eBay Browse API: ${error}`);
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
import dataRoutes from "./routes/dataRoutes.js";
|
||||
import inventoryRoutes from "./routes/inventoryRoutes.js";
|
||||
import { LoggingLevel, setLevel, smartLogging } from "./utils/helper.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
@@ -16,5 +17,7 @@ app.use(express.json());
|
||||
app.use("/api/data", dataRoutes);
|
||||
app.use("/api/inventory", inventoryRoutes);
|
||||
|
||||
//Set Logging Level
|
||||
setLevel(LoggingLevel.AppTrace); //This should get passed in from the Docker-Compose.
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
||||
app.listen(PORT, () => smartLogging(LoggingLevel.Logging, `Server running on port ${PORT}`));
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls
|
||||
import fetch from "node-fetch";
|
||||
import { LoggingLevel, smartLogging } from "./helper.js";
|
||||
|
||||
const fetchEbayReadToken = async () => {
|
||||
smartLogging(LoggingLevel.AppTrace, `fetchEbayReadToken`);
|
||||
const ebayClientId = process.env.EBAY_CLIENT_ID;
|
||||
const ebayClientSecret = process.env.EBAY_CLIENT_SECRET;
|
||||
const credentials = Buffer.from(
|
||||
@@ -9,6 +11,7 @@ const fetchEbayReadToken = async () => {
|
||||
).toString("base64");
|
||||
|
||||
try {
|
||||
smartLogging(LoggingLevel.Debug, `Fetching Token from eBay`);
|
||||
const response = await fetch(
|
||||
"https://api.ebay.com/identity/v1/oauth2/token",
|
||||
{
|
||||
@@ -31,7 +34,7 @@ const fetchEbayReadToken = async () => {
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
} catch (error) {
|
||||
console.error("Error fetching eBay OAuth token:", error);
|
||||
smartLogging(LoggingLevel.Error, `Error fetching eBay OAuth token: ${error}`);
|
||||
throw error; // Throw the error to be handled by the caller
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import fetch from "node-fetch";
|
||||
import { LoggingLevel, smartLogging } from "./helper.js";
|
||||
|
||||
const fetchEbayUserToken = async (authorizationCode) => {
|
||||
smartLogging(LoggingLevel.AppTrace, `fetchEbayUserToken`);
|
||||
const clientId = process.env.EBAY_CLIENT_ID;
|
||||
const clientSecret = process.env.EBAY_CLIENT_SECRET;
|
||||
const redirectUri = process.env.EBAY_REDIRECT_URI; // Make sure this matches the URI registered with eBay
|
||||
@@ -33,7 +35,7 @@ const fetchEbayUserToken = async (authorizationCode) => {
|
||||
const data = await response.json();
|
||||
return data.access_token; // This is the User access token
|
||||
} catch (error) {
|
||||
console.error("Error fetching eBay user token:", error);
|
||||
smartLogging(LoggingLevel.Error,`Error fetching eBay user token: ${JSON.stringify(error)}`);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
46
api/utils/helper.js
Normal file
46
api/utils/helper.js
Normal file
@@ -0,0 +1,46 @@
|
||||
export let setlvl = undefined;
|
||||
|
||||
export function setLevel(level){
|
||||
console.log(`Setting logging level to: ${level}`);
|
||||
setlvl = level;
|
||||
}
|
||||
|
||||
export function smartLogging(level, msg){
|
||||
if(setlvl == undefined){ //If no level is set treat it as console.log()
|
||||
console.log(msg);
|
||||
}else if(level <= setlvl){
|
||||
|
||||
switch (level) {
|
||||
case LoggingLevel.Error:
|
||||
console.error(msg);
|
||||
break;
|
||||
case LoggingLevel.Warning:
|
||||
console.warn(msg);
|
||||
break;
|
||||
case LoggingLevel.Logging:
|
||||
console.log(msg);
|
||||
break;
|
||||
case LoggingLevel.Debug:
|
||||
console.debug(msg);
|
||||
case LoggingLevel.AppTrace:
|
||||
console.debug(msg);
|
||||
break;
|
||||
case LoggingLevel.Trace:
|
||||
console.trace(msg);
|
||||
break;
|
||||
default:
|
||||
console.log(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const LoggingLevel = {
|
||||
Error: "10",
|
||||
Warning: "9",
|
||||
Logging: "8",
|
||||
Debug: "7",
|
||||
AppTrace: "6",
|
||||
Trace: "5"
|
||||
}
|
||||
Reference in New Issue
Block a user