Skip to content

Commit 1acf8a0

Browse files
committed
fix: move to kv for configs
1 parent aed99ec commit 1acf8a0

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

src/config.js

+4-25
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,6 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
/**
14-
* @type {Record<string, Record<string, Config>>}
15-
*/
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-
// };
35-
3613
/**
3714
* @param {string[]} patterns
3815
* @param {string} path
@@ -63,11 +40,13 @@ function extractPathParams(pattern, path) {
6340
*/
6441
export async function resolveConfig(ctx, tenant, overrides = {}) {
6542
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());
6843
if (!confMap) {
6944
return null;
7045
}
46+
if (typeof confMap !== 'object') {
47+
ctx.log.warn('invalid config for tenant', tenant);
48+
return null;
49+
}
7150

7251
// order paths by preference
7352
const suffix = `/${ctx.url.pathname.split('/').slice(3).join('/')}`;

test/config.test.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ import { resolveConfig } from '../src/config.js';
1515

1616
/**
1717
* @param {string} path
18+
* @param {Record<string, Config>} configMap
1819
* @returns {Context}
1920
*/
20-
const TEST_CONTEXT = (path) => ({
21-
env: {},
21+
const TEST_CONTEXT = (path, configMap) => ({
22+
env: {
23+
CONFIGS: {
24+
get: async (tenant) => configMap[tenant],
25+
},
26+
},
2227
log: console,
2328
url: new URL(`https://www.example.com/tenant/content${path}`),
2429
info: {
@@ -28,7 +33,7 @@ const TEST_CONTEXT = (path) => ({
2833
});
2934

3035
describe('config tests', () => {
31-
it('should extract path params', () => {
36+
it('should extract path params', async () => {
3237
const tenantConfigs = {
3338
'test-tenant': {
3439
base: {
@@ -40,15 +45,18 @@ describe('config tests', () => {
4045
},
4146
},
4247
};
43-
const config = resolveConfig(TEST_CONTEXT('/us/p/my-url-key/some-sku'), 'test-tenant', undefined, tenantConfigs);
48+
const config = await resolveConfig(
49+
TEST_CONTEXT('/us/p/my-url-key/some-sku', tenantConfigs),
50+
'test-tenant',
51+
);
4452
assert.deepStrictEqual(config, {
4553
apiKey: 'good',
4654
params: { urlkey: 'my-url-key', sku: 'some-sku' },
4755
pageType: 'product',
4856
});
4957
});
5058

51-
it('should allow wildcard path segments', () => {
59+
it('should allow wildcard path segments', async () => {
5260
const tenantConfigs = {
5361
'test-tenant': {
5462
base: {
@@ -60,15 +68,18 @@ describe('config tests', () => {
6068
},
6169
},
6270
};
63-
const config = resolveConfig(TEST_CONTEXT('/us/p/something-here/some-sku'), 'test-tenant', undefined, tenantConfigs);
71+
const config = await resolveConfig(
72+
TEST_CONTEXT('/us/p/something-here/some-sku', tenantConfigs),
73+
'test-tenant',
74+
);
6475
assert.deepStrictEqual(config, {
6576
apiKey: 'good',
6677
params: { sku: 'some-sku' },
6778
pageType: 'product',
6879
});
6980
});
7081

71-
it('should allow overrides', () => {
82+
it('should allow overrides', async () => {
7283
const tenantConfigs = {
7384
'test-tenant': {
7485
base: {
@@ -80,7 +91,11 @@ describe('config tests', () => {
8091
},
8192
},
8293
};
83-
const config = resolveConfig(TEST_CONTEXT('/us/p/some-sku'), 'test-tenant', { apiKey: 'good' }, tenantConfigs);
94+
const config = await resolveConfig(
95+
TEST_CONTEXT('/us/p/some-sku', tenantConfigs),
96+
'test-tenant',
97+
{ apiKey: 'good' },
98+
);
8499
assert.deepStrictEqual(config, {
85100
apiKey: 'good',
86101
params: { sku: 'some-sku' },

0 commit comments

Comments
 (0)