Skip to content

Commit

Permalink
Add support for multiple urlResolver functions (#143)
Browse files Browse the repository at this point in the history
* Add support for multiple urlResolver functions

* Replace urlResolver member with addUrlResolver method
  • Loading branch information
bigtimebuddy authored and englercj committed Dec 3, 2019
1 parent 2cad490 commit 55c76ce
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
29 changes: 18 additions & 11 deletions src/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ export class Loader
*/
static readonly DefaultMiddlewarePriority = 50;

/**
* A function that is called when preparing a url for use. This function
* can be used to modify the url just prior to `baseUrl` and `defaultQueryString`
* being applied.
*/
urlResolver: Loader.UrlResolverFn | null = null;

/**
* The progress percent of the loader going through the queue.
*/
Expand Down Expand Up @@ -141,6 +134,11 @@ export class Loader
*/
private _baseUrl = '';

/**
* The internal list of URL resolver functions called within `_prepareUrl`.
*/
private _urlResolvers: Loader.UrlResolverFn[] = [];

/**
* The middleware to run after loading each resource.
*/
Expand Down Expand Up @@ -420,18 +418,27 @@ export class Loader
this._queue.concurrency = concurrency;
}

/**
* Add a function that can be used to modify the url just prior
* to `baseUrl` and `defaultQueryString` being applied.
*/
addUrlResolver(func: Loader.UrlResolverFn): this
{
this._urlResolvers.push(func);
return this;
}

/**
* Prepares a url for usage based on the configuration of this object
*/
private _prepareUrl(url: string, baseUrl: string): string
{
let parsed = parseUri(url, { strictMode: true });

if (this.urlResolver)
{
url = this.urlResolver(url, parsed);
this._urlResolvers.forEach(resolver => {
url = resolver(url, parsed);
parsed = parseUri(url, { strictMode: true });
}
});

// Only add `baseUrl` for urls that are not absolute.
if (!parsed.protocol && url.indexOf('//') !== 0)
Expand Down
33 changes: 28 additions & 5 deletions test/spec/Loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,46 @@ describe('Loader', () => {
});
});

describe('#urlResolver', () => {
it('calls urlResolver', () => {
describe('#addUrlResolver', () => {
it('calls addUrlResolver', () => {
const spy = sinon.spy();

loader.urlResolver = () => { spy(); return ''; };
loader.addUrlResolver(() => { spy(); return ''; });
loader._prepareUrl('', '');

expect(spy).to.have.been.calledOnce;
});

it('uses the result of urlResolver', () => {
loader.urlResolver = (s) => s.replace('{token}', 'test');
it('uses the result of addUrlResolver', () => {
loader.addUrlResolver((s) => s.replace('{token}', 'test'));

const s = loader._prepareUrl('/{token}/', '/some/base/url');

expect(s).to.equal('/some/base/url/test/');
});

it('calls multiple urlResolver, in order', () => {
const spy1 = sinon.spy(s => s + '/foo');
const spy2 = sinon.spy(s => s + '/bar');

loader.addUrlResolver(spy1)
.addUrlResolver(spy2);

const s = loader._prepareUrl('init', '');

expect(spy1).to.have.been.calledOnce;
expect(spy2).to.have.been.calledOnce;
expect(s).to.equal('init/foo/bar');
});

it('supports multiple functions as urlResolver', () => {
loader.addUrlResolver((s) => s.replace('{token}', 'foo'))
.addUrlResolver((s) => s.replace('{token2}', 'bar'));

const s = loader._prepareUrl('/{token}/{token2}/', '/some/base/url');

expect(s).to.equal('/some/base/url/foo/bar/');
});
});

describe('#add', () => {
Expand Down

0 comments on commit 55c76ce

Please sign in to comment.