PromucFlow_constructor/app/client/src/api/Api.ts
Vemparala Surya Vamsi 56ef4309e1
chore: capture performance of parsing api response (#37081)
## Description
Capture performance of parsing api responses.

Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags="@tag.All"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/11512264036>
> Commit: 92f9d882b4f0dd40b1b3e38cb817b371b8288a68
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=11512264036&attempt=2"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Fri, 25 Oct 2024 06:47:52 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit


- **New Features**
- Introduced a telemetry feature for enhanced monitoring of API requests
and responses.

- **Bug Fixes**
- Improved error handling by logging issues when default transformation
functions are not found.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-10-25 12:18:16 +05:30

145 lines
3.9 KiB
TypeScript

import axios from "axios";
import type { AxiosInstance, AxiosRequestConfig } from "axios";
import {
apiRequestInterceptor,
apiFailureResponseInterceptor,
apiSuccessResponseInterceptor,
} from "./interceptors";
import { REQUEST_TIMEOUT_MS } from "ee/constants/ApiConstants";
import { convertObjectToQueryParams } from "utils/URLUtils";
import { startAndEndSpanForFn } from "UITelemetry/generateTraces";
export const apiRequestConfig = {
baseURL: "/api/",
timeout: REQUEST_TIMEOUT_MS,
headers: {
"Content-Type": "application/json",
},
withCredentials: true,
};
const axiosInstance: AxiosInstance = axios.create();
axiosInstance.defaults.transformResponse = [
function (...args) {
const transformResponseAr = axios.defaults.transformResponse;
// Pick up the transformFn from axios defaults and wrap it in with telemetry code so that we can capture how long it takes parse an api response
if (Array.isArray(transformResponseAr) && transformResponseAr?.[0]) {
const transfromFn = transformResponseAr?.[0];
const resp = startAndEndSpanForFn(
"axios.transformApiResponse",
{ url: this.url },
() => transfromFn.call(this, ...args),
);
return resp;
} else {
// eslint-disable-next-line no-console
console.error("could not find the api transformerFn");
// return the data as it is.
return args[0];
}
},
];
axiosInstance.interceptors.request.use(apiRequestInterceptor);
axiosInstance.interceptors.response.use(
apiSuccessResponseInterceptor,
apiFailureResponseInterceptor,
);
class Api {
static async get(
url: string,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryParams?: any,
config: AxiosRequestConfig = {},
) {
return axiosInstance.get(url + convertObjectToQueryParams(queryParams), {
...apiRequestConfig,
...config,
});
}
static async post(
url: string,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: any,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryParams?: any,
config: AxiosRequestConfig = {},
) {
return axiosInstance.post(
url + convertObjectToQueryParams(queryParams),
body,
{
...apiRequestConfig,
...config,
},
);
}
static async put(
url: string,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: any,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryParams?: any,
config: AxiosRequestConfig = {},
) {
return axiosInstance.put(
url + convertObjectToQueryParams(queryParams),
body,
{
...apiRequestConfig,
...config,
},
);
}
static async patch(
url: string,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body?: any,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryParams?: any,
config: AxiosRequestConfig = {},
) {
return axiosInstance.patch(
url + convertObjectToQueryParams(queryParams),
body,
{
...apiRequestConfig,
...config,
},
);
}
static async delete(
url: string,
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryParams?: any,
config: AxiosRequestConfig = {},
) {
return axiosInstance.delete(url + convertObjectToQueryParams(queryParams), {
...apiRequestConfig,
...config,
});
}
}
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
export default Api;