1
+ import Api from "api/api" ;
2
+ import axios , { AxiosInstance , AxiosPromise , AxiosRequestConfig } from "axios" ;
3
+ import { calculateFlowCode } from "./apiUtils" ;
4
+
5
+ export interface SearchParams {
6
+ query :string ;
7
+ asset :string ;
8
+ per_page :number ;
9
+ page :1 ;
10
+ sort :string ;
11
+ formats ?:string ;
12
+ price ?:string ;
13
+ }
14
+
15
+ export type ResponseType = {
16
+ response :any ;
17
+ } ;
18
+
19
+ const lcHeaders = {
20
+ "Lowcoder-Token" :calculateFlowCode ( ) ,
21
+ "Content-Type" :"application/json"
22
+ } ;
23
+
24
+ let axiosIns :AxiosInstance | null = null ;
25
+
26
+ const getAxiosInstance = ( clientSecret ?:string ) => {
27
+ if ( axiosIns && ! clientSecret ) {
28
+ return axiosIns ;
29
+ }
30
+
31
+ const headers :Record < string , string > = {
32
+ "Content-Type" :"application/json" ,
33
+ } ;
34
+
35
+ const apiRequestConfig :AxiosRequestConfig = {
36
+ baseURL :"https://api-service.lowcoder.cloud/api/flow" ,
37
+ headers,
38
+ } ;
39
+
40
+ axiosIns = axios . create ( apiRequestConfig ) ;
41
+ return axiosIns ;
42
+ }
43
+
44
+ class IconFlowApi extends Api {
45
+
46
+ static async secureRequest ( body :any , timeout :number = 6000 ) :Promise < any > {
47
+ let response ;
48
+ const axiosInstance = getAxiosInstance ( ) ;
49
+
50
+ // Create a cancel token and set timeout for cancellation
51
+ const source = axios . CancelToken . source ( ) ;
52
+ const timeoutId = setTimeout ( ( ) => {
53
+ source . cancel ( "Request timed out." ) ;
54
+ } , timeout ) ;
55
+
56
+ // Request configuration with cancel token
57
+ const requestConfig :AxiosRequestConfig = {
58
+ method :"POST" ,
59
+ withCredentials :true ,
60
+ data :body ,
61
+ cancelToken :source . token , // Add cancel token
62
+ } ;
63
+
64
+ try {
65
+ response = await axiosInstance . request ( requestConfig ) ;
66
+ } catch ( error ) {
67
+ if ( axios . isCancel ( error ) ) {
68
+ // Retry once after timeout cancellation
69
+ try {
70
+ // Reset the cancel token and retry
71
+ const retrySource = axios . CancelToken . source ( ) ;
72
+ const retryTimeoutId = setTimeout ( ( ) => {
73
+ retrySource . cancel ( "Retry request timed out." ) ;
74
+ } , 20000 ) ;
75
+
76
+ response = await axiosInstance . request ( {
77
+ ...requestConfig ,
78
+ cancelToken :retrySource . token ,
79
+ } ) ;
80
+
81
+ clearTimeout ( retryTimeoutId ) ;
82
+ } catch ( retryError ) {
83
+ console . warn ( "Error at Secure Flow Request. Retry failed:" , retryError ) ;
84
+ throw retryError ;
85
+ }
86
+ } else {
87
+ console . warn ( "Error at Secure Flow Request:" , error ) ;
88
+ throw error ;
89
+ }
90
+ } finally {
91
+ clearTimeout ( timeoutId ) ; // Clear the initial timeout
92
+ }
93
+
94
+ return response ;
95
+ }
96
+
97
+ }
98
+
99
+ export const searchAssets = async ( searchParameters :SearchParams ) => {
100
+ const apiBody = {
101
+ path :"webhook/scout/search-asset" ,
102
+ data :searchParameters ,
103
+ method :"post" ,
104
+ headers :lcHeaders
105
+ } ;
106
+ try {
107
+ const result = await IconFlowApi . secureRequest ( apiBody ) ;
108
+ return result ?. data ?. response ?. items ?. total > 0 ?result . data . response . items as any :null ;
109
+ } catch ( error ) {
110
+ console . error ( "Error searching Design Assets:" , error ) ;
111
+ throw error ;
112
+ }
113
+ } ;
114
+
115
+ export const getAssetLinks = async ( uuid :string , params :Record < string , string > ) => {
116
+ const apiBody = {
117
+ path :"webhook/scout/get-asset-links" ,
118
+ data :{ "uuid" :uuid , "params" :params } ,
119
+ method :"post" ,
120
+ headers :lcHeaders
121
+ } ;
122
+ try {
123
+ const result = await IconFlowApi . secureRequest ( apiBody ) ;
124
+
125
+ return result ?. data ?. response ?. download ?. url . length > 0 ?result . data . response . download as any :null ;
126
+ } catch ( error ) {
127
+ console . error ( "Error searching Design Assets:" , error ) ;
128
+ throw error ;
129
+ }
130
+ } ;
131
+
132
+
133
+ /*
134
+
135
+ static async search(params: SearchParams): Promise<any> {
136
+ let response;
137
+ try {
138
+ response = await getAxiosInstance().request({
139
+ url: '/v3/search',
140
+ method: "GET",
141
+ withCredentials: false,
142
+ params: {
143
+ ...params,
144
+ },
145
+ });
146
+ } catch (error) {
147
+ console.error(error);
148
+ }
149
+ return response?.data.response.items;
150
+ }
151
+
152
+ static async download(uuid: string, params: Record<string, string>): Promise<any> {
153
+ const response = await getAxiosInstance(clientSecret).request({
154
+ url: `/v3/items/${uuid}/api-download?format=${params.format}`,
155
+ method: "POST",
156
+ withCredentials: false,
157
+ });
158
+ return response?.data.response.download;
159
+ }
160
+
161
+ */
162
+
163
+ export default IconFlowApi ;