docs(common): update API docs for unified location service for upgrading (#30567)

PR Close #30567
This commit is contained in:
Brandon 2019-05-20 12:54:26 -05:00 committed by Jason Aden
parent 697046c2b2
commit 0d52a27f41
3 changed files with 130 additions and 57 deletions

View File

@ -22,7 +22,10 @@ const DEFAULT_PORTS: {[key: string]: number} = {
}; };
/** /**
* Docs TBD. * Location service that provides a drop-in replacement for the $location service
* provided in AngularJS.
*
* @see [Using the Angular Unified Location Service](guide/upgrade#using-the-unified-angular-location-service)
* *
* @publicApi * @publicApi
*/ */
@ -321,12 +324,17 @@ export class $locationShim {
} }
/** /**
* Register URL change listeners. This API can be used to catch updates performed by the * Registers listeners for URL changes. This API is used to catch updates performed by the
* AngularJS framework. These changes are a subset of the `$locationChangeStart/Success` events * AngularJS framework. These changes are a subset of the `$locationChangeStart` and
* as those events fire when AngularJS updates it's internally referenced version of the browser * `$locationChangeSuccess` events which fire when AngularJS updates its internally-referenced
* URL. It's possible for `$locationChange` events to happen, but for the browser URL * version of the browser URL.
*
* It's possible for `$locationChange` events to happen, but for the browser URL
* (window.location) to remain unchanged. This `onChange` callback will fire only when AngularJS * (window.location) to remain unchanged. This `onChange` callback will fire only when AngularJS
* actually updates the browser URL (window.location). * actually updates the browser URL (window.location).
*
* @param fn The callback function that is triggered for the listener when the URL changes.
* @param err The callback function that is triggered when an error occurs.
*/ */
onChange( onChange(
fn: (url: string, state: unknown, oldUrl: string, oldState: unknown) => void, fn: (url: string, state: unknown, oldUrl: string, oldState: unknown) => void,
@ -346,6 +354,11 @@ export class $locationShim {
}); });
} }
/**
* Parses the provided URL, and sets the current URL to the parsed result.
*
* @param url The URL string.
*/
$$parse(url: string) { $$parse(url: string) {
let pathUrl: string|undefined; let pathUrl: string|undefined;
if (url.startsWith('/')) { if (url.startsWith('/')) {
@ -366,6 +379,12 @@ export class $locationShim {
this.composeUrls(); this.composeUrls();
} }
/**
* Parses the provided URL and its relative URL.
*
* @param url The full URL string.
* @param relHref A URL string relative to the full URL string.
*/
$$parseLinkUrl(url: string, relHref?: string|null): boolean { $$parseLinkUrl(url: string, relHref?: string|null): boolean {
// When relHref is passed, it should be a hash and is handled separately // When relHref is passed, it should be a hash and is handled separately
if (relHref && relHref[0] === '#') { if (relHref && relHref[0] === '#') {
@ -413,9 +432,8 @@ export class $locationShim {
} }
/** /**
* This method is getter only. * Retrieves the full URL representation with all segments encoded according to
* * rules specified in
* Return full URL representation with all segments encoded according to rules specified in
* [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt). * [RFC 3986](http://www.ietf.org/rfc/rfc3986.txt).
* *
* *
@ -428,12 +446,8 @@ export class $locationShim {
absUrl(): string { return this.$$absUrl; } absUrl(): string { return this.$$absUrl; }
/** /**
* This method is getter / setter. * Retrieves the current URL, or sets a new URL. When setting a URL,
* * changes the path, search, and hash, and returns a reference to its own instance.
* Return URL (e.g. `/path?a=b#hash`) when called without any parameter.
*
* Change path, search and hash, when called with parameter and return `$location`.
*
* *
* ```js * ```js
* // given URL http://example.com/#/some/path?foo=bar&baz=xoxo * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
@ -463,10 +477,7 @@ export class $locationShim {
} }
/** /**
* This method is getter only. * Retrieves the protocol of the current URL.
*
* Return protocol of current URL.
*
* *
* ```js * ```js
* // given URL http://example.com/#/some/path?foo=bar&baz=xoxo * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
@ -477,11 +488,9 @@ export class $locationShim {
protocol(): string { return this.$$protocol; } protocol(): string { return this.$$protocol; }
/** /**
* This method is getter only. * Retrieves the protocol of the current URL.
* *
* Return host of current URL. * In contrast to the non-AngularJS version `location.host` which returns `hostname:port`, this
*
* Note: compared to the non-AngularJS version `location.host` which returns `hostname:port`, this
* returns the `hostname` portion only. * returns the `hostname` portion only.
* *
* *
@ -500,10 +509,7 @@ export class $locationShim {
host(): string { return this.$$host; } host(): string { return this.$$host; }
/** /**
* This method is getter only. * Retrieves the port of the current URL.
*
* Return port of current URL.
*
* *
* ```js * ```js
* // given URL http://example.com/#/some/path?foo=bar&baz=xoxo * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
@ -514,16 +520,12 @@ export class $locationShim {
port(): number|null { return this.$$port; } port(): number|null { return this.$$port; }
/** /**
* This method is getter / setter. * Retrieves the path of the current URL, or changes the path and returns a reference to its own
* instance.
* *
* Return path of current URL when called without any parameter. * Paths should always begin with forward slash (/). This method adds the forward slash
*
* Change path when called with parameter and return `$location`.
*
* Note: Path should always begin with forward slash (/), this method will add the forward slash
* if it is missing. * if it is missing.
* *
*
* ```js * ```js
* // given URL http://example.com/#/some/path?foo=bar&baz=xoxo * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo
* let path = $location.path(); * let path = $location.path();
@ -548,11 +550,8 @@ export class $locationShim {
} }
/** /**
* This method is getter / setter. * Retrieves a map of the search parameters of the current URL, or changes a search
* * part and returns a reference to its own instance.
* Return search part (as object) of current URL when called without any parameter.
*
* Change search part when called with parameter and return `$location`.
* *
* *
* ```js * ```js
@ -585,8 +584,7 @@ export class $locationShim {
* If `paramValue` is `true`, the property specified via the first argument will be added with no * If `paramValue` is `true`, the property specified via the first argument will be added with no
* value nor trailing equal sign. * value nor trailing equal sign.
* *
* @return {Object} If called with no arguments returns the parsed `search` object. If called with * @return {Object} The parsed `search` object of the current URL, or the changed `search` object.
* one or more arguments returns `$location` object itself.
*/ */
search(): {[key: string]: unknown}; search(): {[key: string]: unknown};
search(search: string|number|{[key: string]: unknown}): this; search(search: string|number|{[key: string]: unknown}): this;
@ -633,12 +631,8 @@ export class $locationShim {
} }
/** /**
* This method is getter / setter. * Retrieves the current hash fragment, or changes the hash fragment and returns a reference to
* * its own instance.
* Returns the hash fragment when called without any parameters.
*
* Changes the hash fragment when called with a parameter and returns `$location`.
*
* *
* ```js * ```js
* // given URL http://example.com/#/some/path?foo=bar&baz=xoxo#hashValue * // given URL http://example.com/#/some/path?foo=bar&baz=xoxo#hashValue
@ -660,7 +654,7 @@ export class $locationShim {
} }
/** /**
* If called, all changes to $location during the current `$digest` will replace the current * Changes to `$location` during the current `$digest` will replace the current
* history record, instead of adding a new one. * history record, instead of adding a new one.
*/ */
replace(): this { replace(): this {
@ -669,15 +663,13 @@ export class $locationShim {
} }
/** /**
* This method is getter / setter. * Retrieves the history state object when called without any parameter.
*
* Return the history state object when called without any parameter.
* *
* Change the history state object when called with one parameter and return `$location`. * Change the history state object when called with one parameter and return `$location`.
* The state object is later passed to `pushState` or `replaceState`. * The state object is later passed to `pushState` or `replaceState`.
* *
* NOTE: This method is supported only in HTML5 mode and only in browsers supporting * This method is supported only in HTML5 mode and only in browsers supporting
* the HTML5 History API (i.e. methods `pushState` and `replaceState`). If you need to support * the HTML5 History API methods such as `pushState` and `replaceState`. If you need to support
* older browsers (like IE9 or Android < 4.0), don't use this method. * older browsers (like IE9 or Android < 4.0), don't use this method.
* *
*/ */
@ -694,7 +686,8 @@ export class $locationShim {
} }
/** /**
* Docs TBD. * The factory function used to create an instance of the `$locationShim` in Angular,
* and provides an API-compatiable `$locationProvider` for AngularJS.
* *
* @publicApi * @publicApi
*/ */
@ -704,6 +697,9 @@ export class $locationShimProvider {
private platformLocation: PlatformLocation, private urlCodec: UrlCodec, private platformLocation: PlatformLocation, private urlCodec: UrlCodec,
private locationStrategy: LocationStrategy) {} private locationStrategy: LocationStrategy) {}
/**
* Factory method that returns an instance of the $locationShim
*/
$get() { $get() {
return new $locationShim( return new $locationShim(
this.ngUpgrade.$injector, this.location, this.platformLocation, this.urlCodec, this.ngUpgrade.$injector, this.location, this.platformLocation, this.urlCodec,

View File

@ -20,15 +20,31 @@ import {AngularJSUrlCodec, UrlCodec} from './params';
* @publicApi * @publicApi
*/ */
export interface LocationUpgradeConfig { export interface LocationUpgradeConfig {
/**
* Configures whether the location upgrade module should use the `HashLocationStrategy`
* or the `PathLocationStrategy`
*/
useHash?: boolean; useHash?: boolean;
/**
* Configures the hash prefix used in the URL when using the `HashLocationStrategy`
*/
hashPrefix?: string; hashPrefix?: string;
/**
* Configures the URL codec for encoding and decoding URLs. Default is the `AngularJSCodec`
*/
urlCodec?: typeof UrlCodec; urlCodec?: typeof UrlCodec;
/**
* Configures the base href when used in server-side rendered applications
*/
serverBaseHref?: string; serverBaseHref?: string;
/**
* Configures the base href when used in client-side rendered applications
*/
appBaseHref?: string; appBaseHref?: string;
} }
/** /**
* Is used in DI to configure the location upgrade package. * A provider token used to configure the location upgrade module.
* *
* @publicApi * @publicApi
*/ */
@ -38,7 +54,9 @@ export const LOCATION_UPGRADE_CONFIGURATION =
const APP_BASE_HREF_RESOLVED = new InjectionToken<string>('APP_BASE_HREF_RESOLVED'); const APP_BASE_HREF_RESOLVED = new InjectionToken<string>('APP_BASE_HREF_RESOLVED');
/** /**
* Module used for configuring Angular's LocationUpgradeService. * `NgModule` used for providing and configuring Angular's Unified Location Service for upgrading.
*
* @see [Using the Unified Angular Location Service](guide/upgrade#using-the-unified-angular-location-service)
* *
* @publicApi * @publicApi
*/ */

View File

@ -12,21 +12,80 @@
* @publicApi * @publicApi
**/ **/
export abstract class UrlCodec { export abstract class UrlCodec {
/**
* Encodes the path from the provided string
*
* @param path The path string
*/
abstract encodePath(path: string): string; abstract encodePath(path: string): string;
/**
* Decodes the path from the provided string
*
* @param path The path string
*/
abstract decodePath(path: string): string; abstract decodePath(path: string): string;
/**
* Encodes the search string from the provided string or object
*
* @param path The path string or object
*/
abstract encodeSearch(search: string|{[k: string]: unknown}): string; abstract encodeSearch(search: string|{[k: string]: unknown}): string;
/**
* Decodes the search objects from the provided string
*
* @param path The path string
*/
abstract decodeSearch(search: string): {[k: string]: unknown}; abstract decodeSearch(search: string): {[k: string]: unknown};
/**
* Encodes the hash from the provided string
*
* @param path The hash string
*/
abstract encodeHash(hash: string): string; abstract encodeHash(hash: string): string;
/**
* Decodes the hash from the provided string
*
* @param path The hash string
*/
abstract decodeHash(hash: string): string; abstract decodeHash(hash: string): string;
/**
* Normalizes the URL from the provided string
*
* @param path The URL string
*/
abstract normalize(href: string): string; abstract normalize(href: string): string;
/**
* Normalizes the URL from the provided string, search, hash, and base URL parameters
*
* @param path The URL path
* @param search The search object
* @param hash The has string
* @param baseUrl The base URL for the URL
*/
abstract normalize(path: string, search: {[k: string]: unknown}, hash: string, baseUrl?: string): abstract normalize(path: string, search: {[k: string]: unknown}, hash: string, baseUrl?: string):
string; string;
/**
* Checks whether the two strings are equal
* @param valA First string for comparison
* @param valB Second string for comparison
*/
abstract areEqual(valA: string, valB: string): boolean; abstract areEqual(valA: string, valB: string): boolean;
/**
* Parses the URL string based on the base URL
*
* @param url The full URL string
* @param base The base for the URL
*/
abstract parse(url: string, base?: string): { abstract parse(url: string, base?: string): {
href: string, href: string,
protocol: string, protocol: string,
@ -40,8 +99,8 @@ export abstract class UrlCodec {
} }
/** /**
* A `AngularJSUrlCodec` that uses logic from AngularJS to serialize and parse URLs * A `UrlCodec` that uses logic from AngularJS to serialize and parse URLs
* and URL parameters * and URL parameters.
* *
* @publicApi * @publicApi
*/ */