Skip to content

Commit

Permalink
fix: allow headers in config
Browse files Browse the repository at this point in the history
  • Loading branch information
maxakuru committed Sep 30, 2024
1 parent d79b0f2 commit 627a77c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ export async function resolveConfig(ctx, tenant, overrides = {}) {
...paths.reduce((conf, key) => ({
...conf,
...confMap[key],
headers: {
...conf.headers,
...(confMap[key]?.headers ?? {}),
},
params: {
...conf.params,
...extractPathParams(key, suffix),
},
}), {
...(confMap.base ?? {}),
headers: confMap.base?.headers ?? {},
params: {},
}),
...overrides,
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function fetchProduct(sku, config) {
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
...config.headers,
},
});
if (!resp.ok) {
Expand Down Expand Up @@ -72,6 +73,7 @@ async function fetchVariants(sku, config) {
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
...config.headers,
},
});
if (!resp.ok) {
Expand Down Expand Up @@ -110,6 +112,7 @@ async function lookupProductSKU(urlkey, config) {
'Magento-Website-Code': config.magentoWebsiteCode,
'Magento-Store-View-Code': config.magentoStoreViewCode,
'Magento-Store-Code': config.magentoStoreCode,
...config.headers,
},
});
if (!resp.ok) {
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
coreEndpoint: string;
catalogEndpoint?: string;
params: Record<string, string>;
headers: Record<string, string>;
}

export interface Env {
Expand Down
35 changes: 35 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,39 @@ describe('config tests', () => {
assert.deepStrictEqual(config, {
apiKey: 'good',
params: { urlkey: 'my-url-key', sku: 'some-sku' },
headers: {},
pageType: 'product',
});
});

it('should combine headers objects', async () => {
const tenantConfigs = {
'test-tenant': {
base: {
apiKey: 'bad',
headers: {
foo: '1',
baz: '1',
},
},
'/us/p/{{urlkey}}/{{sku}}': {
pageType: 'product',
apiKey: 'good',
headers: {
foo: '2',
bar: '2',
},
},
},
};
const config = await resolveConfig(
TEST_CONTEXT('/us/p/my-url-key/some-sku', tenantConfigs),
'test-tenant',
);
assert.deepStrictEqual(config, {
apiKey: 'good',
params: { urlkey: 'my-url-key', sku: 'some-sku' },
headers: { foo: '2', baz: '1', bar: '2' },
pageType: 'product',
});
});
Expand All @@ -75,6 +108,7 @@ describe('config tests', () => {
assert.deepStrictEqual(config, {
apiKey: 'good',
params: { sku: 'some-sku' },
headers: {},
pageType: 'product',
});
});
Expand All @@ -100,6 +134,7 @@ describe('config tests', () => {
apiKey: 'good',
params: { sku: 'some-sku' },
pageType: 'product',
headers: {},
});
});
});

0 comments on commit 627a77c

Please sign in to comment.