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
This commit is contained in:
@ -14,9 +14,12 @@ import * as url from 'url';
|
||||
import {INITIAL_CONFIG, PlatformConfig} from './tokens';
|
||||
|
||||
|
||||
function parseUrl(urlStr: string): {pathname: string, search: string, hash: string} {
|
||||
function parseUrl(urlStr: string) {
|
||||
const parsedUrl = url.parse(urlStr);
|
||||
return {
|
||||
hostname: parsedUrl.hostname || '',
|
||||
protocol: parsedUrl.protocol || '',
|
||||
port: parsedUrl.port || '',
|
||||
pathname: parsedUrl.pathname || '',
|
||||
search: parsedUrl.search || '',
|
||||
hash: parsedUrl.hash || '',
|
||||
@ -29,6 +32,9 @@ function parseUrl(urlStr: string): {pathname: string, search: string, hash: stri
|
||||
*/
|
||||
@Injectable()
|
||||
export class ServerPlatformLocation implements PlatformLocation {
|
||||
public readonly hostname: string = '/';
|
||||
public readonly protocol: string = '/';
|
||||
public readonly port: string = '/';
|
||||
public readonly pathname: string = '/';
|
||||
public readonly search: string = '';
|
||||
public readonly hash: string = '';
|
||||
@ -39,6 +45,9 @@ export class ServerPlatformLocation implements PlatformLocation {
|
||||
const config = _config as PlatformConfig | null;
|
||||
if (!!config && !!config.url) {
|
||||
const parsedUrl = parseUrl(config.url);
|
||||
this.hostname = parsedUrl.hostname;
|
||||
this.protocol = parsedUrl.protocol;
|
||||
this.port = parsedUrl.port;
|
||||
this.pathname = parsedUrl.pathname;
|
||||
this.search = parsedUrl.search;
|
||||
this.hash = parsedUrl.hash;
|
||||
|
Reference in New Issue
Block a user