Skip to content

Commit aed99ec

Browse files
committed
fix: use kv for configs
1 parent 07b9947 commit aed99ec

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

src/config.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
/**
1414
* @type {Record<string, Record<string, Config>>}
1515
*/
16-
const TENANT_CONFIGS = {
17-
visualcomfort: {
18-
base: {
19-
apiKey: '59878b5d8af24fe9a354f523f5a0bb62',
20-
magentoEnvironmentId: '97034e45-43a5-48ab-91ab-c9b5a98623a8',
21-
magentoWebsiteCode: 'base',
22-
magentoStoreViewCode: 'default',
23-
coreEndpoint: 'https://www.visualcomfort.com/graphql',
24-
},
25-
'/us/p/{{urlkey}}/{{sku}}': {
26-
pageType: 'product',
27-
apiKey: '59878b5d8af24fe9a354f523f5a0bb62',
28-
magentoEnvironmentId: '97034e45-43a5-48ab-91ab-c9b5a98623a8',
29-
magentoWebsiteCode: 'base',
30-
magentoStoreViewCode: 'default',
31-
coreEndpoint: 'https://www.visualcomfort.com/graphql',
32-
},
33-
},
34-
};
16+
// const TENANT_CONFIGS = {
17+
// visualcomfort: {
18+
// base: {
19+
// apiKey: '59878b5d8af24fe9a354f523f5a0bb62',
20+
// magentoEnvironmentId: '97034e45-43a5-48ab-91ab-c9b5a98623a8',
21+
// magentoWebsiteCode: 'base',
22+
// magentoStoreViewCode: 'default',
23+
// coreEndpoint: 'https://www.visualcomfort.com/graphql',
24+
// },
25+
// '/us/p/{{urlkey}}/{{sku}}': {
26+
// pageType: 'product',
27+
// apiKey: '59878b5d8af24fe9a354f523f5a0bb62',
28+
// magentoEnvironmentId: '97034e45-43a5-48ab-91ab-c9b5a98623a8',
29+
// magentoWebsiteCode: 'base',
30+
// magentoStoreViewCode: 'default',
31+
// coreEndpoint: 'https://www.visualcomfort.com/graphql',
32+
// },
33+
// },
34+
// };
3535

3636
/**
3737
* @param {string[]} patterns
@@ -59,10 +59,12 @@ function extractPathParams(pattern, path) {
5959
* @param {Context} ctx
6060
* @param {string} tenant
6161
* @param {Partial<Config>} [overrides={}]
62-
* @returns {Config|null}
62+
* @returns {Promise<Config|null>}
6363
*/
64-
export function resolveConfig(ctx, tenant, overrides, configs = TENANT_CONFIGS) {
65-
const confMap = configs[tenant];
64+
export async function resolveConfig(ctx, tenant, overrides = {}) {
65+
const confMap = await ctx.env.CONFIGS.get(tenant, 'json');
66+
console.log('confMap: ', tenant, confMap, confMap);
67+
console.log('config list: ', await ctx.env.CONFIGS.list());
6668
if (!confMap) {
6769
return null;
6870
}

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export default {
147147
}
148148

149149
const overrides = Object.fromEntries(ctx.url.searchParams.entries());
150-
const config = resolveConfig(ctx, tenant, overrides);
150+
const config = await resolveConfig(ctx, tenant, overrides);
151151
if (!config) {
152152
return errorResponse(404, 'config not found');
153153
}

src/types.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ExecutionContext } from "@cloudflare/workers-types/experimental";
1+
import type { ExecutionContext, KVNamespace } from "@cloudflare/workers-types/experimental";
22

33
declare global {
44
export interface Config {
@@ -14,12 +14,16 @@ declare global {
1414
export interface Env {
1515
VERSION: string;
1616
ENVIRONMENT: string;
17-
[key: string]: string;
17+
18+
// KV namespaces
19+
CONFIGS: KVNamespace<string>;
20+
21+
[key: string]: string | KVNamespace<string>;
1822
}
1923

2024
export interface Context extends ExecutionContext {
2125
url: URL;
22-
env: Record<string, string>;
26+
env: Env;
2327
log: Console;
2428
info: {
2529
method: string;

wrangler.toml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ send_metrics = false
77

88
build = { command = "npm install && node build.js" }
99

10+
kv_namespaces = [
11+
{ binding = "CONFIGS", id = "bb91e12a65a8462282396f32e63406f1", preview_id = "bb91e12a65a8462282396f32e63406f1" }
12+
]
13+
1014
[vars]
1115
VERSION = "@@VERSION@@-dev"
1216
ENVIRONMENT = "dev"

0 commit comments

Comments
 (0)