@@ -15,13 +15,17 @@ export interface CloudConfigData {
1515export const CLOUD_CONFIG_DEFAULT_GROUP = "defaultGroup" ;
1616export const CLOUD_CONFIG_DEFAULT_PROJECT = "defaultProject" ;
1717
18+ export const IS_PROD = process . env . NODE_ENV === "production" ;
19+
20+ export const CLOUD_CONFIG_API_ENDPOINT =
21+ process . env . NEXT_PUBLIC_CLOUD_CONFIG_API_ENDPOINT ||
22+ "http://localhost:3001/api" ;
23+
1824const couldConfigSecretClient =
1925 process . env . NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET ;
2026
2127const couldConfigSecretServer = process . env . CLOUD_CONFIG_SERVER_ENCRYPT_SECRET ;
2228
23- export const IS_PROD = process . env . NODE_ENV === "production" ;
24-
2529export const decryptData = (
2630 data : string ,
2731 cryptSecret : string
@@ -130,3 +134,69 @@ export const getCloudConfig = <T>({
130134
131135 return config . value as T ;
132136} ;
137+
138+ export const fetchCloudConfig = async ( {
139+ orgId,
140+ serverSide = false ,
141+ accessToken,
142+ cache = "default" ,
143+ apiPrefix = CLOUD_CONFIG_API_ENDPOINT ,
144+ } : {
145+ orgId : string ;
146+ serverSide ?: boolean ;
147+ accessToken ?: string ;
148+ cache ?: RequestCache ;
149+ apiPrefix ?: string ;
150+ } ) => {
151+ try {
152+ const startTime = Date . now ( ) ;
153+
154+ const apiEndpoint = serverSide
155+ ? `${ apiPrefix } /server-config`
156+ : `${ apiPrefix } /client-config?orgId=${ orgId } ` ;
157+
158+ const requestData = serverSide
159+ ? JSON . stringify ( { orgId, accessToken } )
160+ : undefined ;
161+ const response = await fetch ( apiEndpoint , {
162+ method : serverSide ? "POST" : "GET" ,
163+ body : requestData ,
164+ headers : {
165+ "Content-Type" : "application/json" ,
166+ } ,
167+ cache : cache ,
168+ } ) ;
169+ if ( ! response . ok ) {
170+ console . log ( "🚀 Debug fetchCloudConfig requestData:" , requestData ) ;
171+
172+ throw new Error (
173+ `😢 fetchCloudConfig failed: ${ response . status } /${ response . statusText } - ${ apiEndpoint } `
174+ ) ;
175+ }
176+ const duration = Date . now ( ) - startTime ;
177+
178+ console . log (
179+ `fetchCloudConfig in ${ ( duration / 1000 ) . toFixed ( 2 ) } seconds ${
180+ duration > 2000 ? "💔" : "-"
181+ } ${ apiEndpoint } `
182+ ) ;
183+
184+ return ( await response . json ( ) ) as CloudConfigData [ ] ;
185+ } catch ( error ) {
186+ console . log ( "💔💔💔 fetchCloudConfig error:" , error ) ;
187+ }
188+
189+ return null ;
190+ } ;
191+
192+ export default {
193+ CLOUD_CONFIG_DEFAULT_GROUP ,
194+ CLOUD_CONFIG_DEFAULT_PROJECT ,
195+ IS_PROD ,
196+ CLOUD_CONFIG_API_ENDPOINT ,
197+ decryptData,
198+ parseSingleConfig,
199+ parseAllConfigs,
200+ getCloudConfig,
201+ fetchCloudConfig,
202+ } ;
0 commit comments