Skip to content

Commit 32b2cf6

Browse files
committed
feat: improve getCloudConfig
1 parent d40e1f6 commit 32b2cf6

3 files changed

Lines changed: 63 additions & 26 deletions

File tree

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,24 @@ yarn add cloud-configuration
1919
```typescript
2020
import { fetchAllConfigs, getCloudConfig } from "cloud-configuration";
2121

22-
const configs = await fetchAllConfigs({
23-
orgId: "U2FsdGVkX1/1dETBp2nec8Xe2KtA/V8ndJo/uDqXrpc=",
24-
});
22+
const configs = await fetchAllConfigs();
2523

2624
const auFlagUrl = getCloudConfig({ configs, featureKey: "au_flag_url" });
2725
```
2826

27+
## Local .env examples
28+
29+
```bash
30+
CLOUD_CONFIG_SERVER_ENCRYPT_SECRET=S_+2/QGV3Xz
31+
CLOUD_CONFIG_SERVER_ACCESS_TOKEN=PRIVATE_U2FsdGVkX1/V6tnPtjtXIy9pX9oVKt1M73fasTvAsFpaQtvZg==
32+
NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET=C_/uTPZ+2Qrr
33+
NEXT_PUBLIC_CLOUD_CONFIG_ORG_ID=U2FsdGVkX1/1dETBp2nedJo/uDqXrpc=
34+
```
35+
36+
## Whole package size
37+
38+
- package size: < 10 kB
39+
2940
## License
3041

3142
MIT

cloud-config.ts

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import AES from "crypto-js/aes";
23
import 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

4450
export 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

111120
export 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;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cloud-configuration",
3-
"version": "0.1.3",
3+
"version": "0.1.5",
44
"description": "This package allows you to use CloudConfig easily.",
55
"author": "Alex Zeng",
66
"license": "MIT",

0 commit comments

Comments
 (0)