Skip to content

Commit a84cdd4

Browse files
committed
feat: add fetchCloudConfig
1 parent 02c71c8 commit a84cdd4

4 files changed

Lines changed: 89 additions & 9 deletions

File tree

cloud-config.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ export interface CloudConfigData {
1515
export const CLOUD_CONFIG_DEFAULT_GROUP = "defaultGroup";
1616
export const CLOUD_CONFIG_DEFAULT_PROJECT = "defaultProject";
1717

18+
export const IS_PROD = process.env.NODE_ENV === "production";
19+
20+
export const CLOUD_CONFIG_API_ENDPOINT =
21+
process.env.NEXT_PUBLIC_CLOUD_CONFIG_API_ENDPOINT ||
22+
"http://localhost:3001/api";
23+
1824
const couldConfigSecretClient =
1925
process.env.NEXT_PUBLIC_CLOUD_CONFIG_CLIENT_ENCRYPT_SECRET;
2026

2127
const couldConfigSecretServer = process.env.CLOUD_CONFIG_SERVER_ENCRYPT_SECRET;
2228

23-
export const IS_PROD = process.env.NODE_ENV === "production";
24-
2529
export const decryptData = (
2630
data: string,
2731
cryptSecret: string
@@ -130,3 +134,69 @@ export const getCloudConfig = <T>({
130134

131135
return config.value as T;
132136
};
137+
138+
export const fetchCloudConfig = async ({
139+
orgId,
140+
serverSide = false,
141+
accessToken,
142+
cache = "default",
143+
apiPrefix = CLOUD_CONFIG_API_ENDPOINT,
144+
}: {
145+
orgId: string;
146+
serverSide?: boolean;
147+
accessToken?: string;
148+
cache?: RequestCache;
149+
apiPrefix?: string;
150+
}) => {
151+
try {
152+
const startTime = Date.now();
153+
154+
const apiEndpoint = serverSide
155+
? `${apiPrefix}/server-config`
156+
: `${apiPrefix}/client-config?orgId=${orgId}`;
157+
158+
const requestData = serverSide
159+
? JSON.stringify({ orgId, accessToken })
160+
: undefined;
161+
const response = await fetch(apiEndpoint, {
162+
method: serverSide ? "POST" : "GET",
163+
body: requestData,
164+
headers: {
165+
"Content-Type": "application/json",
166+
},
167+
cache: cache,
168+
});
169+
if (!response.ok) {
170+
console.log("🚀 Debug fetchCloudConfig requestData:", requestData);
171+
172+
throw new Error(
173+
`😢 fetchCloudConfig failed: ${response.status}/${response.statusText} - ${apiEndpoint}`
174+
);
175+
}
176+
const duration = Date.now() - startTime;
177+
178+
console.log(
179+
`fetchCloudConfig in ${(duration / 1000).toFixed(2)} seconds ${
180+
duration > 2000 ? "💔" : "-"
181+
} ${apiEndpoint}`
182+
);
183+
184+
return (await response.json()) as CloudConfigData[];
185+
} catch (error) {
186+
console.log("💔💔💔 fetchCloudConfig error:", error);
187+
}
188+
189+
return null;
190+
};
191+
192+
export default {
193+
CLOUD_CONFIG_DEFAULT_GROUP,
194+
CLOUD_CONFIG_DEFAULT_PROJECT,
195+
IS_PROD,
196+
CLOUD_CONFIG_API_ENDPOINT,
197+
decryptData,
198+
parseSingleConfig,
199+
parseAllConfigs,
200+
getCloudConfig,
201+
fetchCloudConfig,
202+
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
"author": "Alex Zeng",
66
"license": "MIT",
77
"main": "index.js",
8+
"type": "module",
89
"scripts": {
910
"test": "echo \"Error: no test specified\" && exit 1"
1011
},
1112
"repository": {
1213
"type": "git",
13-
"url": "git+https://github.com/AlexStack/cloud-config-package.git"
14+
"url": "git+https://github.com/AlexStack/cloud-configuration.git"
1415
},
1516
"keywords": [],
1617
"bugs": {
17-
"url": "https://github.com/AlexStack/cloud-config-package/issues"
18+
"url": "https://github.com/AlexStack/cloud-configuration/issues"
1819
},
19-
"homepage": "https://github.com/AlexStack/cloud-config-package#readme",
20+
"homepage": "https://github.com/AlexStack/cloud-configuration#readme",
2021
"devDependencies": {
2122
"@types/crypto-js": "^4.1.2",
2223
"@types/node": "^20.8.2"

test/test-config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
import { decryptData } from "cloud-config";
1+
import { fetchCloudConfig } from "../cloud-config";
22

3-
console.log("test-config.ts", decryptData("U2FsdG", "test"));
3+
console.log(
4+
"test-config.ts",
5+
fetchCloudConfig({
6+
orgId: "U2FsdGVkX1/1dETBp2nec8Xe2KtA/V8ndJo/uDqXrpc=",
7+
})
8+
);

tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es5",
3+
"target": "ES2015",
44
"lib": ["dom", "dom.iterable", "esnext"],
55
"allowJs": true,
66
"skipLibCheck": true,
@@ -25,7 +25,11 @@
2525
}
2626
]
2727
},
28-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
28+
"ts-node": {
29+
// Tell ts-node CLI to install the --loader automatically
30+
"esm": true
31+
},
32+
"include": ["next-env.d.ts", "**/*.ts", "*.ts", "**/*.tsx", ".next/types/**/*.ts"],
2933
"exclude": ["node_modules"],
3034
"moduleResolution": ["node_modules", ".next", "node"]
3135
}

0 commit comments

Comments
 (0)