Skip to content

Commit

Permalink
feat(core): CHECKOUT-3747 Configure instance with host URL
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Nov 14, 2018
1 parent ef94387 commit a173807
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/create-form-poster.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import FormBuilder from './form-builder';
import FormPoster from './form-poster';
import FormPosterOptions from './form-poster-options';

export default function createFormPoster(): FormPoster {
export default function createFormPoster(options?: FormPosterOptions): FormPoster {
const formBuilder = new FormBuilder();
const formPoster = new FormPoster(formBuilder);
const formPoster = new FormPoster(formBuilder, options);

return formPoster;
}
3 changes: 3 additions & 0 deletions src/form-poster-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface FormPosterOptions {
host?: string;
}
15 changes: 13 additions & 2 deletions src/form-poster.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import FormBuilder from './form-builder';
import FormPosterOptions from './form-poster-options';
import { isAbsoluteUrl, joinPaths } from './url-utils';

export default class FormPoster {
/**
* @internal
*/
constructor(
private _formBuilder: FormBuilder
private _formBuilder: FormBuilder,
private _options?: FormPosterOptions
) {}

postForm(url: string, data: { [key: string]: any }, callback?: () => void): void {
const form = this._formBuilder.build(url, data);
const form = this._formBuilder.build(this._prependHost(url), data);

window.addEventListener('unload', function handleUnload() {
window.removeEventListener('unload', handleUnload);
Expand All @@ -24,4 +27,12 @@ export default class FormPoster {
form.submit();
document.body.removeChild(form);
}

private _prependHost(url: string): string {
if (!this._options || !this._options.host || isAbsoluteUrl(url)) {
return url;
}

return joinPaths(this._options.host, url);
}
}
15 changes: 15 additions & 0 deletions src/url-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function isAbsoluteUrl(url: string): boolean {
return /^https?:\/\//.test(url);
}

export function joinPaths(pathA: string, pathB: string): string {
return `${removeTrailingSlash(pathA)}/${removeLeadingSlash(pathB)}`;
}

function removeTrailingSlash(path: string): string {
return path.replace(/\/$/, '');
}

function removeLeadingSlash(path: string): string {
return path.replace(/^\//, '');
}
11 changes: 10 additions & 1 deletion test/form-poster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ describe('FormPoster', () => {
formPoster.postForm(url, data);

expect(formBuilder.build)
.toHaveBeenCalled();
.toHaveBeenCalledWith(url, data);

expect(form.submit)
.toHaveBeenCalled();
});

it('builds form with host appended to URL if provided', () => {
formPoster = new FormPoster(formBuilder, { host: 'https://foobar.com/' });

formPoster.postForm(url, data);

expect(formBuilder.build)
.toHaveBeenCalledWith('https://foobar.com/url/123', data);
});

it('triggers the callback after posting the data', () => {
const callback = jasmine.createSpy();

Expand Down
31 changes: 31 additions & 0 deletions test/url-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isAbsoluteUrl, joinPaths } from '../src/url-utils';

describe('isAbsoluteUrl()', () => {
it('returns true if absolute URL', () => {
expect(isAbsoluteUrl('https://foobar.com/hello/world'))
.toBeTruthy();

expect(isAbsoluteUrl('http://foobar.com/hello/world'))
.toBeTruthy();
});

it('returns false if relative URL', () => {
expect(isAbsoluteUrl('/hello/world'))
.toBeFalsy();

expect(isAbsoluteUrl('/'))
.toBeFalsy();
});
});

describe('joinPaths()', () => {
it('joins two paths together', () => {
expect(joinPaths('https://foobar.com', 'hello/world'))
.toEqual('https://foobar.com/hello/world');
});

it('strips out trailing and leading slashes automatically', () => {
expect(joinPaths('https://foobar.com/', '/hello/world'))
.toEqual('https://foobar.com/hello/world');
});
});

0 comments on commit a173807

Please sign in to comment.