Skip to content

Commit

Permalink
feat: add support for includeBy and excludeBy options in Resource
Browse files Browse the repository at this point in the history
Co-authored-by: William Killerud <william.killerud@schibsted.com>
  • Loading branch information
tor0405 and wkillerud committed Mar 20, 2024
1 parent 5aed9d6 commit 51eb442
Show file tree
Hide file tree
Showing 2 changed files with 673 additions and 378 deletions.
48 changes: 33 additions & 15 deletions lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class PodiumClientResource {
this.#metrics = new Metrics();
this.#state = state;

this.#metrics.on('error', error => {
this.#metrics.on('error', (error) => {
log.error(
'Error emitted by metric stream in @podium/client module',
error,
Expand All @@ -57,7 +57,10 @@ export default class PodiumClientResource {
}

async fetch(incoming = {}, reqOptions = {}) {
if (!utils.validateIncoming(incoming)) throw new TypeError('you must pass an instance of "HttpIncoming" as the first argument to the .fetch() method');
if (!utils.validateIncoming(incoming))
throw new TypeError(
'you must pass an instance of "HttpIncoming" as the first argument to the .fetch() method',
);
const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);

if (this.#options.excludeBy) {
Expand All @@ -66,10 +69,12 @@ export default class PodiumClientResource {
*/
const exclucedDeviceTypes = this.#options.excludeBy.deviceType;
if (Array.isArray(exclucedDeviceTypes)) {
const deviceTypeHeader = incoming.request.headers['x-podium-device-type'];
const deviceTypeHeader =
incoming.request.headers['x-podium-device-type'];

for (let i = 0; i < exclucedDeviceTypes.length; i += 1) {
const shouldSkip = exclucedDeviceTypes[i] === deviceTypeHeader;
const shouldSkip =
exclucedDeviceTypes[i] === deviceTypeHeader;
if (shouldSkip) {
return new Response({
headers: {},
Expand All @@ -89,8 +94,13 @@ export default class PodiumClientResource {
*/
const includeDeviceTypes = this.#options.includeBy.deviceType;
if (Array.isArray(includeDeviceTypes)) {
const deviceTypeHeader = incoming.request.headers['x-podium-device-type'];
const shouldRequest = includeDeviceTypes.includes(deviceTypeHeader);
const deviceTypeHeader =
incoming.request.headers['x-podium-device-type'];

const shouldRequest =
!deviceTypeHeader ||
includeDeviceTypes.includes(deviceTypeHeader);

if (!shouldRequest) {
return new Response({
headers: {},
Expand All @@ -105,14 +115,13 @@ export default class PodiumClientResource {

this.#state.setInitializingState();

const { manifest, headers, redirect, isFallback } = await this.#resolver.resolve(
outgoing,
);
const { manifest, headers, redirect, isFallback } =
await this.#resolver.resolve(outgoing);

const chunks = [];
// eslint-disable-next-line no-restricted-syntax
for await (const chunk of outgoing) {
chunks.push(chunk)
chunks.push(chunk);
}

const content = !outgoing.redirect
Expand All @@ -122,14 +131,23 @@ export default class PodiumClientResource {
return new Response({
headers,
content,
css: utils.filterAssets(isFallback ? "fallback" : "content", manifest.css),
js: utils.filterAssets(isFallback ? "fallback" : "content", manifest.js),
css: utils.filterAssets(
isFallback ? 'fallback' : 'content',
manifest.css,
),
js: utils.filterAssets(
isFallback ? 'fallback' : 'content',
manifest.js,
),
redirect,
});
}

stream(incoming = {}, reqOptions = {}) {
if (!utils.validateIncoming(incoming)) throw new TypeError('you must pass an instance of "HttpIncoming" as the first argument to the .stream() method');
if (!utils.validateIncoming(incoming))
throw new TypeError(
'you must pass an instance of "HttpIncoming" as the first argument to the .stream() method',
);
const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);
this.#state.setInitializingState();
this.#resolver.resolve(outgoing);
Expand All @@ -139,7 +157,7 @@ export default class PodiumClientResource {
refresh(incoming = {}, reqOptions = {}) {
const outgoing = new HttpOutgoing(this.#options, reqOptions, incoming);
this.#state.setInitializingState();
return this.#resolver.refresh(outgoing).then(obj => obj);
return this.#resolver.refresh(outgoing).then((obj) => obj);
}

[inspect]() {
Expand All @@ -153,4 +171,4 @@ export default class PodiumClientResource {
get [Symbol.toStringTag]() {
return 'PodiumClientResource';
}
};
}
Loading

0 comments on commit 51eb442

Please sign in to comment.