From bbb27f23445da79eced7716d57c7cc3054596774 Mon Sep 17 00:00:00 2001 From: G Date: Thu, 29 Aug 2024 16:48:57 +0000 Subject: [PATCH] print a json representation of the error response. Also i changed the name of the file to follow naming convention --- src/utils/axios-request.ts | 39 --------------------------------------- src/utils/axiosRequest.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/utils/requests/authenticateUser.ts | 11 +++++------ src/utils/requests/getPaymentTypes.ts | 2 +- src/utils/requests/orangePayment.ts | 2 +- src/utils/requests/transactions.ts | 2 +- src/utils/requests/userInformations.ts | 2 +- src/utils/requests/wavePayment.ts | 2 +- 8 files changed, 53 insertions(+), 50 deletions(-) delete mode 100644 src/utils/axios-request.ts create mode 100644 src/utils/axiosRequest.ts diff --git a/src/utils/axios-request.ts b/src/utils/axios-request.ts deleted file mode 100644 index e00b270..0000000 --- a/src/utils/axios-request.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { LOG } from '@logger'; -import axios, { AxiosError, AxiosResponse } from 'axios'; - - -const log = LOG.extend("AxiosRequest"); - -const baseUrl = process.env.EXPO_PUBLIC_API_URL; - -const client = axios.create({baseURL : baseUrl}); - - -const axiosRequest = async ({...options}) : Promise => { - // console.log("base Url", baseUrl); - // client.defaults.headers.common.Authorization = `Bearer ${""}`; - // client.defaults.headers.common['Content-Type'] = 'application/json'; - // console.log("client default", client.defaults); - // console.log("client datas", client.defaults.data); - log.debug("RequestOptions :: ", options); - - - const onSuccess = (response : T) => { - return response - }; - const onError = (error : AxiosError) => { - log.error(error); - throw error; - }; - - try { - const response : AxiosResponse = await client({ ...options }); - return onSuccess(response.data); - } catch (error : any) { - log.error("RequestError :: ", error); - log.error("RequestError Reponse :: ", error.response); - return onError(error); - } -} - -export default axiosRequest; \ No newline at end of file diff --git a/src/utils/axiosRequest.ts b/src/utils/axiosRequest.ts new file mode 100644 index 0000000..0d026f3 --- /dev/null +++ b/src/utils/axiosRequest.ts @@ -0,0 +1,43 @@ +import { LOG } from "@logger"; +import axios, { type AxiosError, type AxiosResponse } from "axios"; + +const log = LOG.extend("AxiosRequest"); + +const baseUrl = process.env.EXPO_PUBLIC_API_URL; + +// biome-ignore lint/style/useNamingConvention: +const client = axios.create({ baseURL: baseUrl }); + +const axiosRequest = async ({ ...options }): Promise => { + // console.log("base Url", baseUrl); + // client.defaults.headers.common.Authorization = `Bearer ${""}`; + // client.defaults.headers.common['Content-Type'] = 'application/json'; + // console.log("client default", client.defaults); + // console.log("client datas", client.defaults.data); + log.debug("RequestOptions :: ", options); + + const onSuccess = (response: T) => { + return response; + }; + const onError = (error: AxiosError) => { + log.error(error); + throw error; + }; + + try { + const response: AxiosResponse = await client({ ...options }); + return onSuccess(response.data); + } catch (error: unknown) { + if (axios.isAxiosError(error)) { + log.error("axiosRequest | Response :: ", JSON.stringify(error.response, null, 2)); + // log.error("Axios RequestError Reponse message:: ", error.message); + // log.error("Axios RequestError Reponse name:: ", error.response?.data); + } else { + log.error("axiosRequest | General RequestError :: ", error); + } + // biome-ignore lint/suspicious/noExplicitAny: + return onError(error as any); + } +}; + +export default axiosRequest; diff --git a/src/utils/requests/authenticateUser.ts b/src/utils/requests/authenticateUser.ts index cb79a32..f882c7e 100644 --- a/src/utils/requests/authenticateUser.ts +++ b/src/utils/requests/authenticateUser.ts @@ -1,11 +1,11 @@ -import { IauthenticationData } from "@/contexts/Types"; +import type { IauthenticationData } from "@/contexts/Types"; import { LOG } from "@logger"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; const log = LOG.extend("authenticateUser"); const authenticateUser = async ({ username, password }: { username: string; password: string }) => { - log.http({username, password}); + log.http({ username, password }); const response = await axiosRequest({ url: "/api/token/", method: "POST", @@ -15,8 +15,7 @@ const authenticateUser = async ({ username, password }: { username: string; pass }, }); log.http(JSON.stringify(response, null, 2)); - return response + return response; }; - -export default authenticateUser; \ No newline at end of file +export default authenticateUser; diff --git a/src/utils/requests/getPaymentTypes.ts b/src/utils/requests/getPaymentTypes.ts index b046ed3..f39d498 100644 --- a/src/utils/requests/getPaymentTypes.ts +++ b/src/utils/requests/getPaymentTypes.ts @@ -1,6 +1,6 @@ import { LOG } from "@logger"; import base64 from "react-native-base64"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; import type { IpaginatedResponse, IpaymentType } from "./types"; const basictoken = base64.encode("admin:admin"); diff --git a/src/utils/requests/orangePayment.ts b/src/utils/requests/orangePayment.ts index 17ece96..864860e 100644 --- a/src/utils/requests/orangePayment.ts +++ b/src/utils/requests/orangePayment.ts @@ -1,6 +1,6 @@ import { LOG } from "@logger"; import base64 from "react-native-base64"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; export interface IorangePaymentStarter { // biome-ignore lint/style/useNamingConvention: diff --git a/src/utils/requests/transactions.ts b/src/utils/requests/transactions.ts index 248e5cd..546cb16 100644 --- a/src/utils/requests/transactions.ts +++ b/src/utils/requests/transactions.ts @@ -1,6 +1,6 @@ import { LOG } from "@logger"; import base64 from "react-native-base64"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; import type { PaymentCode } from "./types"; const log = LOG.extend("transactions"); diff --git a/src/utils/requests/userInformations.ts b/src/utils/requests/userInformations.ts index 7df5354..6b818fc 100644 --- a/src/utils/requests/userInformations.ts +++ b/src/utils/requests/userInformations.ts @@ -1,6 +1,6 @@ import { LOG } from "@logger"; import base64 from "react-native-base64"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; import type { IuserInformations } from "./types"; const log = LOG.extend("getUserInformations"); diff --git a/src/utils/requests/wavePayment.ts b/src/utils/requests/wavePayment.ts index b37ac5d..79a4cb1 100644 --- a/src/utils/requests/wavePayment.ts +++ b/src/utils/requests/wavePayment.ts @@ -1,6 +1,6 @@ import { LOG } from "@logger"; import base64 from "react-native-base64"; -import axiosRequest from "../axios-request"; +import axiosRequest from "../axiosRequest"; const log = LOG.extend("wavePayment"); -- libgit2 0.27.1