Skip to content

Commit

Permalink
feat(redirect): add RedirectConfig.all method that lists all redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Apr 9, 2021
1 parent 926b2f5 commit e79a62b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/DynamicRedirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,38 @@ class DynamicRedirect {
}

async fetch() {
try {
let url = new URL(this._src);
if (!this._src.endsWith('.json')) {
if (!this._data) {
try {
let url = new URL(this._src);
if (!this._src.endsWith('.json')) {
// load via runtime (todo: do this via a plugin)
// eslint-disable-next-line no-underscore-dangle
const namespace = process.env.__OW_NAMESPACE || 'helix';
url = new URL(`https://adobeioruntime.net/api/v1/web/${namespace}/helix-services/data-embed@v1`);
url.searchParams.append('src', this._src);
}
const res = await fetch(url.href, {
headers: {
'x-request-id': this._transactionID,
},
});
const text = await res.text();
if (res.ok) {
this._data = JSON.parse(text);
if ('data' in this._data) {
this._data = this._data.data;
const namespace = process.env.__OW_NAMESPACE || 'helix';
url = new URL(`https://adobeioruntime.net/api/v1/web/${namespace}/helix-services/data-embed@v1`);
url.searchParams.append('src', this._src);
}
const res = await fetch(url.href, {
headers: {
'x-request-id': this._transactionID,
},
});
const text = await res.text();
if (res.ok) {
this._data = JSON.parse(text);
if ('data' in this._data) {
this._data = this._data.data;
}
this._data = this._data.map(clean);
}
this._data = this._data.map(clean);
this._logger.info(`loaded lookup table from ${this._src}`);
} catch (e) {
this._logger.warn(`failed to get ${this._src} ${e.message}`);
}
this._logger.info(`loaded lookup table from ${this._src}`);
} catch (e) {
this._logger.warn(`failed to get ${this._src} ${e.message}`);
}
}

async match(path) {
if (!this._data) {
await this.fetch();
}
await this.fetch();
if (this._data) {
const hit = this._data.find((entry) => entry.from === path
|| entry.from === path.replace(/[ äӓ]/g, encodeURIComponent));
Expand All @@ -107,6 +107,14 @@ class DynamicRedirect {
}
return null;
}

async all() {
await this.fetch();
if (this._data) {
return this._data;
}
return [];
}
}

module.exports = DynamicRedirect;
4 changes: 4 additions & 0 deletions src/Redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Redirect {
type: this.type,
} : retval;
}

all() {
return [this];
}
}

module.exports = Redirect;
5 changes: 5 additions & 0 deletions src/RedirectConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class RedirectConfig extends SchemaDerivedConfig {
const resolved = await Promise.all(this.redirects.map((redirect) => redirect.match(path)));
return resolved.find((o) => o);
}

async all() {
const resolved = await Promise.all(this.redirects.map((redirect) => redirect.all()));
return resolved.flat();
}
}

module.exports = RedirectConfig;
3 changes: 3 additions & 0 deletions test/redirectconfigs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ describe('Redirects Config Loading (from GitHub)', () => {
url: '/en/new',
type: 'permanent',
});

const all = await config.all();
assert.equal(all.length, 13);
}).timeout(10000);
});

Expand Down

0 comments on commit e79a62b

Please sign in to comment.