Skip to content

Commit 5771800

Browse files
authored
Issue 26 custom headers (#27)
Adds optional custom headers to config Also adds tests for custom headers.
1 parent 8d9cd78 commit 5771800

File tree

11 files changed

+89
-3
lines changed

11 files changed

+89
-3
lines changed

src/@episerver/content-delivery/lib/apiClient.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/@episerver/content-delivery/lib/apiClient.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/@episerver/content-delivery/lib/config.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export declare type ContentDeliveryConfig = {
1212
* requests to the Content Delivery API.
1313
*/
1414
getAccessToken?: (path?: string) => Promise<string>;
15+
/**
16+
* Function to call to include custom headers in
17+
* requests to the Content Delivery API.
18+
*/
19+
getHeaders?: (path?: string) => Promise<Record<string, any>>;
1520
/**
1621
* Select all properties by default, unless otherwise
1722
* specified in each request to the Content Delivery API.

src/@episerver/content-delivery/lib/config.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/@episerver/content-delivery/lib/config.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/@episerver/content-delivery/src/apiClient.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ async function getHeaders(path: string, headers: ApiHeaders = {}, config: Conten
175175
}
176176
}
177177

178+
if (config.getHeaders) {
179+
const configHeaders = await config.getHeaders(path);
180+
if (configHeaders) {
181+
for (const name in configHeaders) {
182+
if (configHeaders[name] !== undefined) {
183+
result.set(name, configHeaders[name])
184+
}
185+
}
186+
}
187+
}
188+
178189
if (config.getAccessToken) {
179190
const accessToken = await config.getAccessToken(path);
180191
if (accessToken) {

src/@episerver/content-delivery/src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export type ContentDeliveryConfig = {
1414
*/
1515
getAccessToken?: (path?: string) => Promise<string>,
1616

17+
/**
18+
* Function to call to include custom headers in
19+
* requests to the Content Delivery API.
20+
*/
21+
getHeaders?: (path?: string) => Promise<Record<string, any>>,
22+
1723
/**
1824
* Select all properties by default, unless otherwise
1925
* specified in each request to the Content Delivery API.

src/@episerver/content-delivery/test/contentLoader.tests.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,17 @@ describe('ContentLoader', () => {
319319
});
320320
});
321321
});
322+
323+
describe("with custom headers", () => {
324+
it("should pass custom headers in request when applied", async () => {
325+
const customHeaderValue = "CustomHeaderValue";
326+
var cl = new ContentLoader({
327+
getHeaders: () =>
328+
Promise.resolve({ CustomHeaderName: customHeaderValue }),
329+
});
330+
const content = await cl.getContent(startPageId);
331+
332+
content.customHeader.should.equal(customHeaderValue);
333+
});
334+
});
322335
});

src/@episerver/content-delivery/test/contentResolver.tests.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,17 @@ describe('ContentResolver', () => {
150150
});
151151
});
152152
});
153+
154+
describe("with custom headers", () => {
155+
it("should pass custom headers in request when applied", async () => {
156+
const customHeaderValue = "CustomHeaderValue";
157+
var cr = new ContentResolver({
158+
getHeaders: () =>
159+
Promise.resolve({ CustomHeaderName: customHeaderValue }),
160+
});
161+
var result = await cr.resolveContent(baseUrl, true);
162+
163+
result.content.customHeader.should.equal(customHeaderValue);
164+
});
165+
});
153166
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using EPiServer.ContentApi.Core.Serialization;
2+
using EPiServer.ContentApi.Core.Serialization.Internal;
3+
using EPiServer.ContentApi.Core.Serialization.Models;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace Backend
8+
{
9+
public class CustomHeaderContentApiFilter : ContentApiModelFilter<ContentApiModel>
10+
{
11+
private readonly IHttpContextAccessor _httpContextAccessor;
12+
13+
public CustomHeaderContentApiFilter(IHttpContextAccessor httpContextAccessor)
14+
{
15+
_httpContextAccessor = httpContextAccessor;
16+
}
17+
18+
public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
19+
{
20+
if (_httpContextAccessor.HttpContext.Request.Headers.ContainsKey("CustomHeaderName"))
21+
{
22+
contentApiModel.Properties.Add("customHeader", _httpContextAccessor.HttpContext.Request.Headers["CustomHeaderName"][0]);
23+
}
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)