84 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import axios, { AxiosError } from 'axios';
 | |
| import serversideConfig from '../../serversideConfig';
 | |
| import https from 'https';
 | |
| 
 | |
| let customers = [];
 | |
| let customerObject = [];
 | |
| let errorMsg = '';
 | |
| 
 | |
| export default defineEventHandler(async (event) => {
 | |
| 
 | |
|     const agent = new https.Agent({
 | |
|         rejectUnauthorized: false,
 | |
|     });
 | |
| 
 | |
|     const axiosInstance = axios.create({
 | |
|         headers: {
 | |
|             'Content-Type': 'application/json',
 | |
|             Accept: "*",
 | |
|         },
 | |
|         httpsAgent: agent
 | |
|     });
 | |
| 
 | |
|     if (event.path.startsWith("/api/getAllCustomers")) {
 | |
|         // get all customers object from backend
 | |
|         try {
 | |
|             let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/customers`);
 | |
|             customers = res.data;
 | |
|         } catch (err) {
 | |
|             if (axios.isAxiosError(err)) {
 | |
|                 const axiosError = err as AxiosError;
 | |
| 
 | |
|                 if (axiosError.response) {
 | |
|                     // Axios error
 | |
|                     console.error(axiosError.response.data.message);
 | |
|                     errorMsg = axiosError.response.data.message;
 | |
|                 } else if (axiosError.request) {
 | |
|                     console.log(err)
 | |
|                     // If error was caused by the request
 | |
|                     console.error(axiosError.request);
 | |
|                 } else {
 | |
|                     // Other errors
 | |
|                     console.error('Error', axiosError.message);
 | |
|                 }
 | |
|             } else {
 | |
|                 // No AxiosError
 | |
|                 console.error('Error', err);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     if (event.path.startsWith("/api/getCustomerByName")) {
 | |
|         // get customer  object from backend
 | |
|         let itemId = null;
 | |
|         const path = event._path;
 | |
|         const pathSegments = path.split('/');
 | |
|         itemId = pathSegments[pathSegments.length - 1];
 | |
| 
 | |
|         try {
 | |
|             let res = await axiosInstance.get(`https://${serversideConfig.url}:${serversideConfig.port}/customerName/${itemId}`);
 | |
|             customerObject = res.data;
 | |
|         } catch (err) {
 | |
|             if (axios.isAxiosError(err)) {
 | |
|                 const axiosError = err as AxiosError;
 | |
| 
 | |
|                 if (axiosError.response) {
 | |
|                     // Axios error
 | |
|                     console.error(axiosError.response.data.message);
 | |
|                     errorMsg = axiosError.response.data.message;
 | |
|                 } else if (axiosError.request) {
 | |
|                     // If error was caused by the request
 | |
|                     console.error(axiosError.request);
 | |
|                 } else {
 | |
|                     // Other errors
 | |
|                     console.error('Error', axiosError.message);
 | |
|                 }
 | |
|             } else {
 | |
|                 // No AxiosError
 | |
|                 console.error('Error', err);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| })
 | |
| 
 | |
| export { customers, customerObject, errorMsg }; |