1
1
/* eslint-disable no-console */
2
- import AES from "crypto-js/aes" ;
3
- import encUtf8 from "crypto-js/enc-utf8" ;
4
-
5
- export interface CloudConfigData {
6
- projectName : string ;
7
- groupName : string ;
8
- featureKey : string ;
9
- value : unknown ;
10
- valueType : string ;
11
- prodEnabled ?: boolean ;
12
- devEnabled ?: boolean ;
13
- valueEncrypted ?: boolean ;
14
- }
15
-
16
- export const CLOUD_CONFIG_DEFAULT_GROUP = "defaultGroup" ;
17
- export const CLOUD_CONFIG_DEFAULT_PROJECT = "defaultProject" ;
18
-
19
- export const IS_PROD = process . env . NODE_ENV === "production" ;
20
-
21
- export const CLOUD_CONFIG_API_ENDPOINT =
22
- process . env . NEXT_PUBLIC_CLOUD_CONFIG_API_ENDPOINT ||
23
- "http://localhost:3001/api" ;
24
-
25
- export const CLOUD_CONFIG_ORG_ID =
26
- process . env . NEXT_PUBLIC_CLOUD_CONFIG_ORG_ID ||
27
- "Missing NEXT_PUBLIC_CLOUD_CONFIG_ORG_ID in .env" ;
28
-
29
- const couldConfigSecretClient =
30
- process . env . NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET ;
31
-
32
- const couldConfigSecretServer = process . env . CLOUD_CONFIG_SERVER_ENCRYPT_SECRET ;
2
+ import AES from 'crypto-js/aes' ;
3
+ import encUtf8 from 'crypto-js/enc-utf8' ;
4
+
5
+ import {
6
+ CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET ,
7
+ CLOUD_CONFIG_DEFAULT_GROUP ,
8
+ CLOUD_CONFIG_DEFAULT_PROJECT ,
9
+ CLOUD_CONFIG_FETCH_ALL_DEFAULT_VALUE ,
10
+ CLOUD_CONFIG_SERVER_ENCRYPT_SECRET ,
11
+ IS_PROD ,
12
+ } from './constants' ;
13
+ import { CloudConfigData , FetchAllConfigsParams } from './types' ;
33
14
34
15
export const decryptConfig = (
35
16
data : string ,
@@ -39,13 +20,13 @@ export const decryptConfig = (
39
20
const decryptedData = AES . decrypt ( data , cryptSecret ) ;
40
21
const decryptedText = decryptedData . toString ( encUtf8 ) ;
41
22
if ( ! decryptedText || decryptedText === data ) {
42
- return " Decrypt value failed! Make sure the encrypt secret is correct in env" ;
23
+ return ' Decrypt value failed! Make sure the encrypt secret is correct in env' ;
43
24
}
44
25
return decryptedText ;
45
26
} catch ( error ) {
46
- console . log ( " 😅😅😅 decryptConfig failed" , error ) ;
27
+ console . log ( ' 😅😅😅 decryptConfig failed' , error ) ;
47
28
}
48
- return " Decrypt value failed! Please check your encrypt secret settings in env" ;
29
+ return ' Decrypt value failed! Please check your encrypt secret settings in env' ;
49
30
} ;
50
31
51
32
export const parseSingleConfig = (
@@ -56,15 +37,15 @@ export const parseSingleConfig = (
56
37
return config ;
57
38
}
58
39
const cryptSecret = serverSideOnly
59
- ? couldConfigSecretServer
60
- : couldConfigSecretClient ;
40
+ ? CLOUD_CONFIG_SERVER_ENCRYPT_SECRET
41
+ : CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET ;
61
42
if ( ! cryptSecret ) {
62
43
// eslint-disable-next-line no-console
63
44
console . log (
64
45
`😅😅😅 Can't decrypt featureKey ${ config . featureKey } , Please set ${
65
46
serverSideOnly
66
- ? " CLOUD_CONFIG_SERVER_ENCRYPT_SECRET"
67
- : " NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET"
47
+ ? ' CLOUD_CONFIG_SERVER_ENCRYPT_SECRET'
48
+ : ' NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET'
68
49
} in .env`
69
50
) ;
70
51
return config ;
@@ -75,24 +56,24 @@ export const parseSingleConfig = (
75
56
}
76
57
77
58
let newValue ;
78
- if ( config . valueType === " json" ) {
59
+ if ( config . valueType === ' json' ) {
79
60
try {
80
61
newValue = JSON . parse ( decryptedValue ) ;
81
62
} catch ( error ) {
82
63
console . log (
83
- " 😅😅😅 JSON.parse(decryptedValue) error" ,
64
+ ' 😅😅😅 JSON.parse(decryptedValue) error' ,
84
65
config . value ,
85
66
error
86
67
) ;
87
68
}
88
69
}
89
- if ( config . valueType === " array" ) {
90
- newValue = decryptedValue . split ( "," ) . map ( ( tag ) => tag . trim ( ) ) ;
70
+ if ( config . valueType === ' array' ) {
71
+ newValue = decryptedValue . split ( ',' ) . map ( ( tag ) => tag . trim ( ) ) ;
91
72
}
92
- if ( config . valueType === " number" ) {
73
+ if ( config . valueType === ' number' ) {
93
74
newValue = parseFloat ( decryptedValue ) ;
94
75
}
95
- if ( config . valueType === " boolean" ) {
76
+ if ( config . valueType === ' boolean' ) {
96
77
newValue = Boolean ( decryptedValue ) ;
97
78
}
98
79
@@ -162,28 +143,12 @@ export const getConfigWithDefaultValue = <T>(
162
143
return value === null || value === undefined ? params . defaultValue : value ;
163
144
} ;
164
145
165
- interface FetchAllConfigsParams {
166
- orgId ?: string ;
167
- serverSide ?: boolean ;
168
- accessToken ?: string ;
169
- cache ?: RequestCache ;
170
- apiPrefix ?: string ;
171
- cacheSeconds ?: number ;
172
- }
173
-
174
- export const fetchAllConfigs = async (
175
- params : FetchAllConfigsParams = {
176
- orgId : CLOUD_CONFIG_ORG_ID ,
177
- serverSide : false ,
178
- accessToken : undefined ,
179
- cache : "default" ,
180
- apiPrefix : CLOUD_CONFIG_API_ENDPOINT ,
181
- // cacheSeconds: 60,
182
- }
183
- ) => {
146
+ export const fetchAllConfigs = async ( params ?: FetchAllConfigsParams ) => {
184
147
try {
185
- const { orgId, serverSide, accessToken, cache, apiPrefix, cacheSeconds } =
186
- params ;
148
+ const { orgId, serverSide, accessToken, cache, apiPrefix, cacheSeconds } = {
149
+ ...CLOUD_CONFIG_FETCH_ALL_DEFAULT_VALUE ,
150
+ ...params ,
151
+ } ;
187
152
188
153
const startTime = Date . now ( ) ;
189
154
@@ -196,18 +161,18 @@ export const fetchAllConfigs = async (
196
161
: undefined ;
197
162
198
163
const fetchInit = {
199
- method : serverSide ? " POST" : " GET" ,
164
+ method : serverSide ? ' POST' : ' GET' ,
200
165
body : requestData ,
201
166
headers : {
202
- " Content-Type" : " application/json" ,
167
+ ' Content-Type' : ' application/json' ,
203
168
} ,
204
169
cache : cacheSeconds !== undefined ? undefined : cache ,
205
170
next : { revalidate : cacheSeconds } ,
206
171
} ;
207
172
208
173
const response = await fetch ( apiEndpoint , fetchInit ) ;
209
174
if ( ! response . ok ) {
210
- console . log ( " 🚀 Debug fetchAllConfigs requestData:" , requestData ) ;
175
+ console . log ( ' 🚀 Debug fetchAllConfigs requestData:' , requestData ) ;
211
176
212
177
throw new Error (
213
178
`😢 fetchAllConfigs failed: ${ response . status } /${ response . statusText } - ${ apiEndpoint } `
@@ -217,21 +182,21 @@ export const fetchAllConfigs = async (
217
182
218
183
console . log (
219
184
`fetchAllConfigs in ${ ( duration / 1000 ) . toFixed ( 2 ) } seconds ${
220
- duration > 2000 ? "💔" : "-"
185
+ duration > 2000 ? '💔' : '-'
221
186
} ${ apiEndpoint } `
222
187
) ;
223
188
224
189
const configs = ( ( await response . json ( ) ) || [ ] ) as CloudConfigData [ ] ;
225
190
226
191
return parseAllConfigs ( configs , serverSide ) ;
227
192
} catch ( error ) {
228
- console . log ( " 💔💔💔 fetchAllConfigs error:" , error ) ;
193
+ console . log ( ' 💔💔💔 fetchAllConfigs error:' , error ) ;
229
194
}
230
195
231
196
return [ ] ;
232
197
} ;
233
198
234
- const cloudConfig = {
199
+ export const cloudConfig = {
235
200
// DEFAULT_GROUP: CLOUD_CONFIG_DEFAULT_GROUP,
236
201
// DEFAULT_PROJECT: CLOUD_CONFIG_DEFAULT_PROJECT,
237
202
// IS_PROD: IS_PROD,
@@ -243,5 +208,3 @@ const cloudConfig = {
243
208
getWithDefault : getConfigWithDefaultValue ,
244
209
fetchAll : fetchAllConfigs ,
245
210
} ;
246
-
247
- export default cloudConfig ;
0 commit comments