Skip to content

Commit 490ac3b

Browse files
committed
feat: add getWithDefault
1 parent 32b2cf6 commit 490ac3b

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ yarn add cloud-configuration
1717
## Basic Usage
1818

1919
```typescript
20-
import { fetchAllConfigs, getCloudConfig } from "cloud-configuration";
20+
import cloudConfig from "cloud-configuration";
2121

22-
const configs = await fetchAllConfigs();
22+
const configs = await cloudConfig.fetchAll();
2323

24-
const auFlagUrl = getCloudConfig({ configs, featureKey: "au_flag_url" });
24+
const auFlagUrl = cloudConfig.get({ configs, featureKey: "au_flag_url" }); // return value or null
25+
26+
const usFlagUrl = cloudConfig.getWithDefault({
27+
configs,
28+
featureKey: "us_flag_url",
29+
defaultValue: "https://example.com/us.png",
30+
}); // return typed value or default value, good for typescript
2531
```
2632

2733
## Local .env examples

cloud-config.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const couldConfigSecretClient =
3131

3232
const 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+
156165
interface 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

222235
const 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

234248
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.5",
3+
"version": "0.1.6",
44
"description": "This package allows you to use CloudConfig easily.",
55
"author": "Alex Zeng",
66
"license": "MIT",

0 commit comments

Comments
 (0)