11/* 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' ;
3314
3415export const decryptConfig = (
3516 data : string ,
@@ -39,13 +20,13 @@ export const decryptConfig = (
3920 const decryptedData = AES . decrypt ( data , cryptSecret ) ;
4021 const decryptedText = decryptedData . toString ( encUtf8 ) ;
4122 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' ;
4324 }
4425 return decryptedText ;
4526 } catch ( error ) {
46- console . log ( " 😅😅😅 decryptConfig failed" , error ) ;
27+ console . log ( ' 😅😅😅 decryptConfig failed' , error ) ;
4728 }
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' ;
4930} ;
5031
5132export const parseSingleConfig = (
@@ -56,15 +37,15 @@ export const parseSingleConfig = (
5637 return config ;
5738 }
5839 const cryptSecret = serverSideOnly
59- ? couldConfigSecretServer
60- : couldConfigSecretClient ;
40+ ? CLOUD_CONFIG_SERVER_ENCRYPT_SECRET
41+ : CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET ;
6142 if ( ! cryptSecret ) {
6243 // eslint-disable-next-line no-console
6344 console . log (
6445 `😅😅😅 Can't decrypt featureKey ${ config . featureKey } , Please set ${
6546 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'
6849 } in .env`
6950 ) ;
7051 return config ;
@@ -75,24 +56,24 @@ export const parseSingleConfig = (
7556 }
7657
7758 let newValue ;
78- if ( config . valueType === " json" ) {
59+ if ( config . valueType === ' json' ) {
7960 try {
8061 newValue = JSON . parse ( decryptedValue ) ;
8162 } catch ( error ) {
8263 console . log (
83- " 😅😅😅 JSON.parse(decryptedValue) error" ,
64+ ' 😅😅😅 JSON.parse(decryptedValue) error' ,
8465 config . value ,
8566 error
8667 ) ;
8768 }
8869 }
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 ( ) ) ;
9172 }
92- if ( config . valueType === " number" ) {
73+ if ( config . valueType === ' number' ) {
9374 newValue = parseFloat ( decryptedValue ) ;
9475 }
95- if ( config . valueType === " boolean" ) {
76+ if ( config . valueType === ' boolean' ) {
9677 newValue = Boolean ( decryptedValue ) ;
9778 }
9879
@@ -162,28 +143,12 @@ export const getConfigWithDefaultValue = <T>(
162143 return value === null || value === undefined ? params . defaultValue : value ;
163144} ;
164145
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 ) => {
184147 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+ } ;
187152
188153 const startTime = Date . now ( ) ;
189154
@@ -196,18 +161,18 @@ export const fetchAllConfigs = async (
196161 : undefined ;
197162
198163 const fetchInit = {
199- method : serverSide ? " POST" : " GET" ,
164+ method : serverSide ? ' POST' : ' GET' ,
200165 body : requestData ,
201166 headers : {
202- " Content-Type" : " application/json" ,
167+ ' Content-Type' : ' application/json' ,
203168 } ,
204169 cache : cacheSeconds !== undefined ? undefined : cache ,
205170 next : { revalidate : cacheSeconds } ,
206171 } ;
207172
208173 const response = await fetch ( apiEndpoint , fetchInit ) ;
209174 if ( ! response . ok ) {
210- console . log ( " 🚀 Debug fetchAllConfigs requestData:" , requestData ) ;
175+ console . log ( ' 🚀 Debug fetchAllConfigs requestData:' , requestData ) ;
211176
212177 throw new Error (
213178 `😢 fetchAllConfigs failed: ${ response . status } /${ response . statusText } - ${ apiEndpoint } `
@@ -217,21 +182,21 @@ export const fetchAllConfigs = async (
217182
218183 console . log (
219184 `fetchAllConfigs in ${ ( duration / 1000 ) . toFixed ( 2 ) } seconds ${
220- duration > 2000 ? "💔" : "-"
185+ duration > 2000 ? '💔' : '-'
221186 } ${ apiEndpoint } `
222187 ) ;
223188
224189 const configs = ( ( await response . json ( ) ) || [ ] ) as CloudConfigData [ ] ;
225190
226191 return parseAllConfigs ( configs , serverSide ) ;
227192 } catch ( error ) {
228- console . log ( " 💔💔💔 fetchAllConfigs error:" , error ) ;
193+ console . log ( ' 💔💔💔 fetchAllConfigs error:' , error ) ;
229194 }
230195
231196 return [ ] ;
232197} ;
233198
234- const cloudConfig = {
199+ export const cloudConfig = {
235200 // DEFAULT_GROUP: CLOUD_CONFIG_DEFAULT_GROUP,
236201 // DEFAULT_PROJECT: CLOUD_CONFIG_DEFAULT_PROJECT,
237202 // IS_PROD: IS_PROD,
@@ -243,5 +208,3 @@ const cloudConfig = {
243208 getWithDefault : getConfigWithDefaultValue ,
244209 fetchAll : fetchAllConfigs ,
245210} ;
246-
247- export default cloudConfig ;
0 commit comments