Skip to content

Commit 2623c72

Browse files
authored
Merge pull request #14 from adobe-rnd/headers
fix: allow headers in config
2 parents d79b0f2 + 627a77c commit 2623c72

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/config.js

+5
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ export async function resolveConfig(ctx, tenant, overrides = {}) {
6161
...paths.reduce((conf, key) => ({
6262
...conf,
6363
...confMap[key],
64+
headers: {
65+
...conf.headers,
66+
...(confMap[key]?.headers ?? {}),
67+
},
6468
params: {
6569
...conf.params,
6670
...extractPathParams(key, suffix),
6771
},
6872
}), {
6973
...(confMap.base ?? {}),
74+
headers: confMap.base?.headers ?? {},
7075
params: {},
7176
}),
7277
...overrides,

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async function fetchProduct(sku, config) {
3333
'Magento-Website-Code': config.magentoWebsiteCode,
3434
'Magento-Store-View-Code': config.magentoStoreViewCode,
3535
'Magento-Store-Code': config.magentoStoreCode,
36+
...config.headers,
3637
},
3738
});
3839
if (!resp.ok) {
@@ -72,6 +73,7 @@ async function fetchVariants(sku, config) {
7273
'Magento-Website-Code': config.magentoWebsiteCode,
7374
'Magento-Store-View-Code': config.magentoStoreViewCode,
7475
'Magento-Store-Code': config.magentoStoreCode,
76+
...config.headers,
7577
},
7678
});
7779
if (!resp.ok) {
@@ -110,6 +112,7 @@ async function lookupProductSKU(urlkey, config) {
110112
'Magento-Website-Code': config.magentoWebsiteCode,
111113
'Magento-Store-View-Code': config.magentoStoreViewCode,
112114
'Magento-Store-Code': config.magentoStoreCode,
115+
...config.headers,
113116
},
114117
});
115118
if (!resp.ok) {

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare global {
1212
coreEndpoint: string;
1313
catalogEndpoint?: string;
1414
params: Record<string, string>;
15+
headers: Record<string, string>;
1516
}
1617

1718
export interface Env {

test/config.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ describe('config tests', () => {
5252
assert.deepStrictEqual(config, {
5353
apiKey: 'good',
5454
params: { urlkey: 'my-url-key', sku: 'some-sku' },
55+
headers: {},
56+
pageType: 'product',
57+
});
58+
});
59+
60+
it('should combine headers objects', async () => {
61+
const tenantConfigs = {
62+
'test-tenant': {
63+
base: {
64+
apiKey: 'bad',
65+
headers: {
66+
foo: '1',
67+
baz: '1',
68+
},
69+
},
70+
'/us/p/{{urlkey}}/{{sku}}': {
71+
pageType: 'product',
72+
apiKey: 'good',
73+
headers: {
74+
foo: '2',
75+
bar: '2',
76+
},
77+
},
78+
},
79+
};
80+
const config = await resolveConfig(
81+
TEST_CONTEXT('/us/p/my-url-key/some-sku', tenantConfigs),
82+
'test-tenant',
83+
);
84+
assert.deepStrictEqual(config, {
85+
apiKey: 'good',
86+
params: { urlkey: 'my-url-key', sku: 'some-sku' },
87+
headers: { foo: '2', baz: '1', bar: '2' },
5588
pageType: 'product',
5689
});
5790
});
@@ -75,6 +108,7 @@ describe('config tests', () => {
75108
assert.deepStrictEqual(config, {
76109
apiKey: 'good',
77110
params: { sku: 'some-sku' },
111+
headers: {},
78112
pageType: 'product',
79113
});
80114
});
@@ -100,6 +134,7 @@ describe('config tests', () => {
100134
apiKey: 'good',
101135
params: { sku: 'some-sku' },
102136
pageType: 'product',
137+
headers: {},
103138
});
104139
});
105140
});

0 commit comments

Comments
 (0)