Skip to content

Commit b635fe8

Browse files
jasonadenbenlesh
authored andcommittedApr 24, 2019
feat(common): add APIs to read component pieces of URL (#30055)
Without this change, the framework doesn't surface URL parts such as hostname, protocol, and port. This makes it difficult to rebuild a complete URL. This change provides new APIs to read these values. PR Close #30055
1 parent b44b143 commit b635fe8

File tree

6 files changed

+40
-2
lines changed

6 files changed

+40
-2
lines changed
 

‎packages/common/src/location/platform_location.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export abstract class PlatformLocation {
3535
abstract onPopState(fn: LocationChangeListener): void;
3636
abstract onHashChange(fn: LocationChangeListener): void;
3737

38+
abstract get protocol(): string;
39+
abstract get hostname(): string;
40+
abstract get port(): string;
3841
abstract get pathname(): string;
3942
abstract get search(): string;
4043
abstract get hash(): string;

‎packages/platform-browser/src/browser/location/browser_platform_location.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export class BrowserPlatformLocation extends PlatformLocation {
4949
getDOM().getGlobalEventTarget(this._doc, 'window').addEventListener('hashchange', fn, false);
5050
}
5151

52+
get protocol(): string { return this.location.protocol; }
53+
get hostname(): string { return this.location.hostname; }
54+
get port(): string { return this.location.port; }
5255
get pathname(): string { return this.location.pathname; }
5356
get search(): string { return this.location.search; }
5457
get hash(): string { return this.location.hash; }

‎packages/platform-server/src/location.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import * as url from 'url';
1414
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
1515

1616

17-
function parseUrl(urlStr: string): {pathname: string, search: string, hash: string} {
17+
function parseUrl(urlStr: string) {
1818
const parsedUrl = url.parse(urlStr);
1919
return {
20+
hostname: parsedUrl.hostname || '',
21+
protocol: parsedUrl.protocol || '',
22+
port: parsedUrl.port || '',
2023
pathname: parsedUrl.pathname || '',
2124
search: parsedUrl.search || '',
2225
hash: parsedUrl.hash || '',
@@ -29,6 +32,9 @@ function parseUrl(urlStr: string): {pathname: string, search: string, hash: stri
2932
*/
3033
@Injectable()
3134
export class ServerPlatformLocation implements PlatformLocation {
35+
public readonly hostname: string = '/';
36+
public readonly protocol: string = '/';
37+
public readonly port: string = '/';
3238
public readonly pathname: string = '/';
3339
public readonly search: string = '';
3440
public readonly hash: string = '';
@@ -39,6 +45,9 @@ export class ServerPlatformLocation implements PlatformLocation {
3945
const config = _config as PlatformConfig | null;
4046
if (!!config && !!config.url) {
4147
const parsedUrl = parseUrl(config.url);
48+
this.hostname = parsedUrl.hostname;
49+
this.protocol = parsedUrl.protocol;
50+
this.port = parsedUrl.port;
4251
this.pathname = parsedUrl.pathname;
4352
this.search = parsedUrl.search;
4453
this.hash = parsedUrl.hash;

‎packages/platform-server/test/integration_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ class HiddenModule {
516516
expect(location.hash).toBe('#hash');
517517
});
518518
});
519+
it('parses component pieces of a URL', () => {
520+
platformDynamicServer([{
521+
provide: INITIAL_CONFIG,
522+
useValue: {document: '<app></app>', url: 'http://test.com:80/deep/path?query#hash'}
523+
}])
524+
.bootstrapModule(ExampleModule)
525+
.then(appRef => {
526+
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
527+
expect(location.hostname).toBe('test.com');
528+
expect(location.protocol).toBe('http:');
529+
expect(location.port).toBe('80');
530+
expect(location.pathname).toBe('/deep/path');
531+
expect(location.search).toBe('?query');
532+
expect(location.hash).toBe('#hash');
533+
});
534+
});
519535
it('handles empty search and hash portions of the url', () => {
520536
platformDynamicServer([{
521537
provide: INITIAL_CONFIG,

‎packages/platform-webworker/src/web_workers/worker/platform_location.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ export class WebWorkerPlatformLocation extends PlatformLocation {
7474

7575
onHashChange(fn: LocationChangeListener): void { this._hashChangeListeners.push(fn); }
7676

77-
get pathname(): string { return this._location ? this._location.pathname ! : '<unknown>'; }
77+
get hostname(): string { return this._location ? this._location.host ! : '<unknown>'; }
78+
79+
get port(): string { return this._location ? this._location.port ! : '<unknown>'; }
80+
81+
get protocol(): string { return this._location ? this._location.protocol ! : '<unknown>'; }
7882

7983
get search(): string { return this._location ? this._location.search : '<unknown>'; }
8084

‎tools/public_api_guard/common/common.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,10 @@ export declare class PercentPipe implements PipeTransform {
377377

378378
export declare abstract class PlatformLocation {
379379
abstract readonly hash: string;
380+
abstract readonly hostname: string;
380381
abstract readonly pathname: string;
382+
abstract readonly port: string;
383+
abstract readonly protocol: string;
381384
abstract readonly search: string;
382385
abstract back(): void;
383386
abstract forward(): void;

0 commit comments

Comments
 (0)
Please sign in to comment.