1+ /* eslint-disable no-console */
12import AES from "crypto-js/aes" ;
23import encUtf8 from "crypto-js/enc-utf8" ;
34
@@ -34,11 +35,16 @@ export const decryptData = (
3435 data : string ,
3536 cryptSecret : string
3637) : string | null => {
37- const decryptedText = AES . decrypt ( data , cryptSecret ) ;
38- if ( ! decryptedText ) {
39- return null ;
38+ try {
39+ const decryptedData = AES . decrypt ( data , cryptSecret ) ;
40+ const decryptedText = decryptedData . toString ( encUtf8 ) ;
41+ if ( ! decryptedText || decryptedText === data ) {
42+ return "Decrypt value failed! Make sure the encrypt secret is correct in env" ;
43+ }
44+ } catch ( error ) {
45+ console . log ( "😅😅😅 decryptData failed" , error ) ;
4046 }
41- return decryptedText . toString ( encUtf8 ) ;
47+ return "Decrypt value failed! Please check your encrypt secret settings in env" ;
4248} ;
4349
4450export const parseSingleConfig = (
@@ -52,9 +58,8 @@ export const parseSingleConfig = (
5258 ? couldConfigSecretServer
5359 : couldConfigSecretClient ;
5460 if ( ! cryptSecret ) {
55- // eslint-disable-next-line no-console
5661 console . log (
57- `Can't decrypt featureKey ${ config . featureKey } , Please set ${
62+ `😅😅😅 Can't decrypt featureKey ${ config . featureKey } , Please set ${
5863 serverSideOnly
5964 ? "CLOUD_CONFIG_SERVER_ENCRYPT_SECRET"
6065 : "NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET"
@@ -72,8 +77,11 @@ export const parseSingleConfig = (
7277 try {
7378 newValue = JSON . parse ( decryptedValue ) ;
7479 } catch ( error ) {
75- // eslint-disable-next-line no-console
76- console . log ( "JSON.parse(decryptedValue) error" , config . value , error ) ;
80+ console . log (
81+ "😅😅😅 JSON.parse(decryptedValue) error" ,
82+ config . value ,
83+ error
84+ ) ;
7785 }
7886 }
7987 if ( config . valueType === "array" ) {
@@ -101,19 +109,21 @@ export const parseAllConfigs = (
101109 return configs . map ( ( config ) => parseSingleConfig ( config , serverSideOnly ) ) ;
102110} ;
103111
104- interface GetCloudConfigParams {
112+ interface GetCloudConfigParams < T > {
105113 featureKey : string ;
106114 groupName ?: string ;
107115 projectName ?: string ;
108116 configs : CloudConfigData [ ] ;
117+ defaultValue ?: T ;
109118}
110119
111120export const getCloudConfig = < T > ( {
112121 featureKey,
113122 groupName = CLOUD_CONFIG_DEFAULT_GROUP ,
114123 projectName = CLOUD_CONFIG_DEFAULT_PROJECT ,
115124 configs,
116- } : GetCloudConfigParams ) => {
125+ defaultValue,
126+ } : GetCloudConfigParams < T > ) => {
117127 const config = configs . find ( ( item ) => {
118128 if ( item . featureKey !== featureKey ) {
119129 return false ;
@@ -126,33 +136,46 @@ export const getCloudConfig = <T>({
126136 }
127137 return true ;
128138 } ) ;
139+ const newDefaultValue = defaultValue === undefined ? null : defaultValue ;
129140 if ( ! config ) {
130- return null ;
141+ return newDefaultValue ;
131142 }
132143 if ( IS_PROD && ! config . prodEnabled ) {
133- return null ;
144+ return newDefaultValue ;
134145 }
135146 if ( ! IS_PROD && ! config . devEnabled ) {
136- return null ;
147+ return newDefaultValue ;
148+ }
149+ if ( config . value === null || config . value === undefined ) {
150+ return newDefaultValue ;
137151 }
138152
139153 return config . value as T ;
140154} ;
141155
142- export const fetchAllConfigs = async ( {
143- orgId = CLOUD_CONFIG_ORG_ID ,
144- serverSide = false ,
145- accessToken,
146- cache = "default" ,
147- apiPrefix = CLOUD_CONFIG_API_ENDPOINT ,
148- } : {
156+ interface FetchAllConfigsParams {
149157 orgId ?: string ;
150158 serverSide ?: boolean ;
151159 accessToken ?: string ;
152160 cache ?: RequestCache ;
153161 apiPrefix ?: string ;
154- } ) => {
162+ cacheSeconds ?: number ;
163+ }
164+
165+ export const fetchAllConfigs = async (
166+ params : FetchAllConfigsParams = {
167+ orgId : CLOUD_CONFIG_ORG_ID ,
168+ serverSide : false ,
169+ accessToken : undefined ,
170+ cache : "default" ,
171+ apiPrefix : CLOUD_CONFIG_API_ENDPOINT ,
172+ cacheSeconds : 60 ,
173+ }
174+ ) => {
155175 try {
176+ const { orgId, serverSide, accessToken, cache, apiPrefix, cacheSeconds } =
177+ params ;
178+
156179 const startTime = Date . now ( ) ;
157180
158181 const apiEndpoint = serverSide
@@ -169,6 +192,7 @@ export const fetchAllConfigs = async ({
169192 "Content-Type" : "application/json" ,
170193 } ,
171194 cache : cache ,
195+ // next: { revalidate: cacheSeconds },
172196 } ) ;
173197 if ( ! response . ok ) {
174198 console . log ( "🚀 Debug fetchAllConfigs requestData:" , requestData ) ;
@@ -195,7 +219,7 @@ export const fetchAllConfigs = async ({
195219 return [ ] ;
196220} ;
197221
198- export default {
222+ const cloudConfig = {
199223 CLOUD_CONFIG_DEFAULT_GROUP ,
200224 CLOUD_CONFIG_DEFAULT_PROJECT ,
201225 IS_PROD ,
@@ -206,3 +230,5 @@ export default {
206230 getCloudConfig,
207231 fetchAllConfigs,
208232} ;
233+
234+ export default cloudConfig ;
0 commit comments