|
| 1 | +import Api from "api/api"; |
| 2 | +import axios, { AxiosInstance, AxiosRequestConfig, CancelToken } from "axios"; |
| 3 | +import { calculateFlowCode } from "./apiUtils"; |
| 4 | + |
| 5 | +export type ResponseType = { |
| 6 | + response: any; |
| 7 | +}; |
| 8 | + |
| 9 | +// Axios Configuration |
| 10 | +const lcHeaders = { |
| 11 | + "Lowcoder-Token": calculateFlowCode(), |
| 12 | + "Content-Type": "application/json" |
| 13 | +}; |
| 14 | + |
| 15 | +let axiosIns: AxiosInstance | null = null; |
| 16 | + |
| 17 | +const getAxiosInstance = (clientSecret?: string) => { |
| 18 | + if (axiosIns && !clientSecret) { |
| 19 | + return axiosIns; |
| 20 | + } |
| 21 | + |
| 22 | + const headers: Record<string, string> = { |
| 23 | + "Content-Type": "application/json", |
| 24 | + }; |
| 25 | + |
| 26 | + const apiRequestConfig: AxiosRequestConfig = { |
| 27 | + baseURL: "https://api-service.lowcoder.cloud/api/flow", |
| 28 | + headers, |
| 29 | + }; |
| 30 | + |
| 31 | + axiosIns = axios.create(apiRequestConfig); |
| 32 | + return axiosIns; |
| 33 | +}; |
| 34 | + |
| 35 | +class NewsApi extends Api { |
| 36 | + |
| 37 | + static async secureRequest(body: any, timeout: number = 6000): Promise<any> { |
| 38 | + let response; |
| 39 | + const axiosInstance = getAxiosInstance(); |
| 40 | + |
| 41 | + // Create a cancel token and set timeout for cancellation |
| 42 | + const source = axios.CancelToken.source(); |
| 43 | + const timeoutId = setTimeout(() => { |
| 44 | + source.cancel("Request timed out."); |
| 45 | + }, timeout); |
| 46 | + |
| 47 | + // Request configuration with cancel token |
| 48 | + const requestConfig: AxiosRequestConfig = { |
| 49 | + method: "POST", |
| 50 | + withCredentials: true, |
| 51 | + data: body, |
| 52 | + cancelToken: source.token, // Add cancel token |
| 53 | + }; |
| 54 | + |
| 55 | + try { |
| 56 | + response = await axiosInstance.request(requestConfig); |
| 57 | + } catch (error) { |
| 58 | + if (axios.isCancel(error)) { |
| 59 | + // Retry once after timeout cancellation |
| 60 | + try { |
| 61 | + // Reset the cancel token and retry |
| 62 | + const retrySource = axios.CancelToken.source(); |
| 63 | + const retryTimeoutId = setTimeout(() => { |
| 64 | + retrySource.cancel("Retry request timed out."); |
| 65 | + }, 10000); |
| 66 | + |
| 67 | + response = await axiosInstance.request({ |
| 68 | + ...requestConfig, |
| 69 | + cancelToken: retrySource.token, |
| 70 | + }); |
| 71 | + |
| 72 | + clearTimeout(retryTimeoutId); |
| 73 | + } catch (retryError) { |
| 74 | + console.warn("Error at Secure Flow Request. Retry failed:", retryError); |
| 75 | + throw retryError; |
| 76 | + } |
| 77 | + } else { |
| 78 | + console.warn("Error at Secure Flow Request:", error); |
| 79 | + throw error; |
| 80 | + } |
| 81 | + } finally { |
| 82 | + clearTimeout(timeoutId); // Clear the initial timeout |
| 83 | + } |
| 84 | + |
| 85 | + return response; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// API Functions |
| 90 | + |
| 91 | +// secure/get-youtube-videos |
| 92 | +// secure/get-github-releases |
| 93 | + |
| 94 | +export const getReleases = async () => { |
| 95 | + const apiBody = { |
| 96 | + path: "webhook/secure/get-github-releases", |
| 97 | + data: {}, |
| 98 | + method: "post", |
| 99 | + headers: lcHeaders |
| 100 | + }; |
| 101 | + try { |
| 102 | + const result = await NewsApi.secureRequest(apiBody); |
| 103 | + return result?.data[0]?.github?.length > 0 ? result.data[0].github as any[] : []; |
| 104 | + } catch (error) { |
| 105 | + console.error("Error getting news:", error); |
| 106 | + throw error; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +export const getYoutubeVideos = async () => { |
| 111 | + const apiBody = { |
| 112 | + path: "webhook/secure/get-youtube-videos", |
| 113 | + data: {}, |
| 114 | + method: "post", |
| 115 | + headers: lcHeaders |
| 116 | + }; |
| 117 | + try { |
| 118 | + const result = await NewsApi.secureRequest(apiBody); |
| 119 | + return result?.data[0]?.youtube?.length > 0 ? result.data[0].youtube as any[] : []; |
| 120 | + } catch (error) { |
| 121 | + console.error("Error getting news:", error); |
| 122 | + throw error; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +export const getHubspotContent = async () => { |
| 127 | + const apiBody = { |
| 128 | + path: "webhook/secure/get-hubspot-content", |
| 129 | + data: {}, |
| 130 | + method: "post", |
| 131 | + headers: lcHeaders |
| 132 | + }; |
| 133 | + try { |
| 134 | + const result = await NewsApi.secureRequest(apiBody); |
| 135 | + return result?.data[0]?.hubspot?.length > 0 ? result.data[0].hubspot as any[] : []; |
| 136 | + } catch (error) { |
| 137 | + console.error("Error getting news:", error); |
| 138 | + throw error; |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +export default NewsApi; |
0 commit comments