@@ -31,7 +31,7 @@ const couldConfigSecretClient =
3131
3232const couldConfigSecretServer = process . env . CLOUD_CONFIG_SERVER_ENCRYPT_SECRET ;
3333
34- export const decryptData = (
34+ export const decryptConfig = (
3535 data : string ,
3636 cryptSecret : string
3737) : string | null => {
@@ -41,8 +41,9 @@ export const decryptData = (
4141 if ( ! decryptedText || decryptedText === data ) {
4242 return "Decrypt value failed! Make sure the encrypt secret is correct in env" ;
4343 }
44+ return decryptedText ;
4445 } catch ( error ) {
45- console . log ( "😅😅😅 decryptData failed" , error ) ;
46+ console . log ( "😅😅😅 decryptConfig failed" , error ) ;
4647 }
4748 return "Decrypt value failed! Please check your encrypt secret settings in env" ;
4849} ;
@@ -58,6 +59,7 @@ export const parseSingleConfig = (
5859 ? couldConfigSecretServer
5960 : couldConfigSecretClient ;
6061 if ( ! cryptSecret ) {
62+ // eslint-disable-next-line no-console
6163 console . log (
6264 `😅😅😅 Can't decrypt featureKey ${ config . featureKey } , Please set ${
6365 serverSideOnly
@@ -67,7 +69,7 @@ export const parseSingleConfig = (
6769 ) ;
6870 return config ;
6971 }
70- const decryptedValue = decryptData ( config . value as string , cryptSecret ) ;
72+ const decryptedValue = decryptConfig ( config . value as string , cryptSecret ) ;
7173 if ( ! decryptedValue ) {
7274 return config ;
7375 }
@@ -153,6 +155,13 @@ export const getCloudConfig = <T>({
153155 return config . value as T ;
154156} ;
155157
158+ export const getConfigWithDefaultValue = < T > (
159+ params : GetCloudConfigParams < T > & { defaultValue : T }
160+ ) => {
161+ const value = getCloudConfig ( params ) ;
162+ return value === null || value === undefined ? params . defaultValue : value ;
163+ } ;
164+
156165interface FetchAllConfigsParams {
157166 orgId ?: string ;
158167 serverSide ?: boolean ;
@@ -185,15 +194,19 @@ export const fetchAllConfigs = async (
185194 const requestData = serverSide
186195 ? JSON . stringify ( { orgId, accessToken } )
187196 : undefined ;
188- const response = await fetch ( apiEndpoint , {
197+
198+ const fetchInit = {
189199 method : serverSide ? "POST" : "GET" ,
190200 body : requestData ,
191201 headers : {
192202 "Content-Type" : "application/json" ,
193203 } ,
194204 cache : cache ,
195- // next: { revalidate: cacheSeconds },
196- } ) ;
205+ next : { revalidate : cacheSeconds } ,
206+ } ;
207+
208+ const response = await fetch ( apiEndpoint , fetchInit ) ;
209+
197210 if ( ! response . ok ) {
198211 console . log ( "🚀 Debug fetchAllConfigs requestData:" , requestData ) ;
199212
@@ -220,15 +233,16 @@ export const fetchAllConfigs = async (
220233} ;
221234
222235const cloudConfig = {
223- CLOUD_CONFIG_DEFAULT_GROUP ,
224- CLOUD_CONFIG_DEFAULT_PROJECT ,
225- IS_PROD ,
226- CLOUD_CONFIG_API_ENDPOINT ,
227- decryptData,
228- parseSingleConfig,
229- parseAllConfigs,
230- getCloudConfig,
231- fetchAllConfigs,
236+ // DEFAULT_GROUP: CLOUD_CONFIG_DEFAULT_GROUP,
237+ // DEFAULT_PROJECT: CLOUD_CONFIG_DEFAULT_PROJECT,
238+ // IS_PROD: IS_PROD,
239+ // API_ENDPOINT: CLOUD_CONFIG_API_ENDPOINT,
240+ decrypt : decryptConfig ,
241+ parseSingle : parseSingleConfig ,
242+ parseAll : parseAllConfigs ,
243+ get : getCloudConfig ,
244+ getWithDefault : getConfigWithDefaultValue ,
245+ fetchAll : fetchAllConfigs ,
232246} ;
233247
234248export default cloudConfig ;
0 commit comments