Skip to content

Commit 4541bfb

Browse files
committed
feat: separate constant & type files
1 parent 5462354 commit 4541bfb

File tree

5 files changed

+92
-75
lines changed

5 files changed

+92
-75
lines changed

cloud-config.ts

Lines changed: 36 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
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

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

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

constants.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { FetchAllConfigsParams } from './types';
2+
3+
export const CLOUD_CONFIG_DEFAULT_GROUP = 'defaultGroup';
4+
export const CLOUD_CONFIG_DEFAULT_PROJECT = 'defaultProject';
5+
6+
export const IS_PROD = process.env.NODE_ENV === 'production';
7+
8+
export const CLOUD_CONFIG_API_ENDPOINT =
9+
process.env.NEXT_PUBLIC_CLOUD_CONFIG_API_ENDPOINT ||
10+
'http://localhost:3001/api';
11+
12+
export const CLOUD_CONFIG_ORG_ID =
13+
process.env.NEXT_PUBLIC_CLOUD_CONFIG_ORG_ID ||
14+
'Missing NEXT_PUBLIC_CLOUD_CONFIG_ORG_ID in .env';
15+
16+
export const CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET =
17+
process.env.NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET;
18+
19+
export const CLOUD_CONFIG_SERVER_ENCRYPT_SECRET =
20+
process.env.CLOUD_CONFIG_SERVER_ENCRYPT_SECRET;
21+
22+
export const CLOUD_CONFIG_FETCH_ALL_DEFAULT_VALUE: FetchAllConfigsParams = {
23+
orgId: CLOUD_CONFIG_ORG_ID,
24+
serverSide: false,
25+
accessToken: undefined,
26+
cache: 'default',
27+
apiPrefix: CLOUD_CONFIG_API_ENDPOINT,
28+
// cacheSeconds: 60,
29+
};

index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
export * from "./cloud-config";
1+
import { cloudConfig } from './cloud-config';
2+
3+
export * from './cloud-config';
4+
export * from './constants';
5+
export * from './types';
6+
7+
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.7",
3+
"version": "0.1.8",
44
"description": "This package allows you to use CloudConfig easily.",
55
"author": "Alex Zeng",
66
"license": "MIT",

types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface CloudConfigData {
2+
projectName: string;
3+
groupName: string;
4+
featureKey: string;
5+
value: unknown;
6+
valueType: string;
7+
prodEnabled?: boolean;
8+
devEnabled?: boolean;
9+
valueEncrypted?: boolean;
10+
}
11+
12+
export interface FetchAllConfigsParams {
13+
orgId?: string;
14+
serverSide?: boolean;
15+
accessToken?: string;
16+
cache?: RequestCache;
17+
apiPrefix?: string;
18+
cacheSeconds?: number;
19+
}

0 commit comments

Comments
 (0)