Files
Angular-Docs-Cn/packages/common/http/src/response.ts
T
Zhicheng WANG 8663fa04a8 Merge remote-tracking branch 'en/master' into aio
# Conflicts:
#	.nvmrc
#	aio/content/cli/index.md
#	aio/content/guide/ajs-quick-reference.md
#	aio/content/guide/animations.md
#	aio/content/guide/aot-compiler.md
#	aio/content/guide/app-shell.md
#	aio/content/guide/architecture-components.md
#	aio/content/guide/architecture-modules.md
#	aio/content/guide/architecture-services.md
#	aio/content/guide/architecture.md
#	aio/content/guide/attribute-directives.md
#	aio/content/guide/bazel.md
#	aio/content/guide/bootstrapping.md
#	aio/content/guide/browser-support.md
#	aio/content/guide/build.md
#	aio/content/guide/cheatsheet.md
#	aio/content/guide/cli-builder.md
#	aio/content/guide/comparing-observables.md
#	aio/content/guide/complex-animation-sequences.md
#	aio/content/guide/component-interaction.md
#	aio/content/guide/component-styles.md
#	aio/content/guide/creating-libraries.md
#	aio/content/guide/dependency-injection-in-action.md
#	aio/content/guide/dependency-injection-navtree.md
#	aio/content/guide/dependency-injection-providers.md
#	aio/content/guide/dependency-injection.md
#	aio/content/guide/deployment.md
#	aio/content/guide/deprecations.md
#	aio/content/guide/displaying-data.md
#	aio/content/guide/dynamic-component-loader.md
#	aio/content/guide/dynamic-form.md
#	aio/content/guide/elements.md
#	aio/content/guide/entry-components.md
#	aio/content/guide/feature-modules.md
#	aio/content/guide/file-structure.md
#	aio/content/guide/form-validation.md
#	aio/content/guide/forms-overview.md
#	aio/content/guide/forms.md
#	aio/content/guide/frequent-ngmodules.md
#	aio/content/guide/glossary.md
#	aio/content/guide/hierarchical-dependency-injection.md
#	aio/content/guide/http.md
#	aio/content/guide/i18n.md
#	aio/content/guide/ivy.md
#	aio/content/guide/language-service.md
#	aio/content/guide/lazy-loading-ngmodules.md
#	aio/content/guide/lifecycle-hooks.md
#	aio/content/guide/module-types.md
#	aio/content/guide/ngmodule-api.md
#	aio/content/guide/ngmodule-faq.md
#	aio/content/guide/ngmodule-vs-jsmodule.md
#	aio/content/guide/ngmodules.md
#	aio/content/guide/npm-packages.md
#	aio/content/guide/observables-in-angular.md
#	aio/content/guide/observables.md
#	aio/content/guide/pipes.md
#	aio/content/guide/providers.md
#	aio/content/guide/reactive-forms.md
#	aio/content/guide/releases.md
#	aio/content/guide/reusable-animations.md
#	aio/content/guide/route-animations.md
#	aio/content/guide/router.md
#	aio/content/guide/rx-library.md
#	aio/content/guide/schematics-authoring.md
#	aio/content/guide/schematics-for-libraries.md
#	aio/content/guide/schematics.md
#	aio/content/guide/security.md
#	aio/content/guide/service-worker-communications.md
#	aio/content/guide/service-worker-config.md
#	aio/content/guide/service-worker-devops.md
#	aio/content/guide/service-worker-getting-started.md
#	aio/content/guide/service-worker-intro.md
#	aio/content/guide/set-document-title.md
#	aio/content/guide/setup-local.md
#	aio/content/guide/sharing-ngmodules.md
#	aio/content/guide/singleton-services.md
#	aio/content/guide/static-query-migration.md
#	aio/content/guide/structural-directives.md
#	aio/content/guide/styleguide.md
#	aio/content/guide/template-syntax.md
#	aio/content/guide/testing.md
#	aio/content/guide/transition-and-triggers.md
#	aio/content/guide/typescript-configuration.md
#	aio/content/guide/universal.md
#	aio/content/guide/updating.md
#	aio/content/guide/upgrade-setup.md
#	aio/content/guide/upgrade.md
#	aio/content/guide/user-input.md
#	aio/content/guide/using-libraries.md
#	aio/content/guide/visual-studio-2015.md
#	aio/content/guide/web-worker.md
#	aio/content/guide/workspace-config.md
#	aio/content/marketing/events.html
#	aio/content/marketing/resources.json
#	aio/content/navigation.json
#	aio/content/start/data.md
#	aio/content/start/deployment.md
#	aio/content/start/forms.md
#	aio/content/start/index.md
#	aio/content/start/routing.md
#	aio/content/tutorial/index.md
#	aio/content/tutorial/toh-pt0.md
#	aio/content/tutorial/toh-pt1.md
#	aio/content/tutorial/toh-pt2.md
#	aio/content/tutorial/toh-pt3.md
#	aio/content/tutorial/toh-pt4.md
#	aio/content/tutorial/toh-pt5.md
#	aio/content/tutorial/toh-pt6.md
#	aio/package.json
#	aio/src/app/app.component.spec.ts
#	aio/src/app/custom-elements/api/api-list.component.html
#	aio/src/app/layout/doc-viewer/doc-viewer.component.ts
#	aio/src/app/layout/mode-banner/mode-banner.component.ts
#	aio/src/app/layout/nav-item/nav-item.component.html
#	aio/src/app/shared/toc.service.ts
#	aio/src/styles/0-base/_typography.scss
#	aio/src/styles/2-modules/_api-pages.scss
#	aio/tools/transforms/templates/api/lib/memberHelpers.html
#	aio/yarn.lock
#	packages/common/http/src/headers.ts
#	packages/common/http/src/interceptor.ts
#	packages/common/http/src/params.ts
#	packages/common/http/src/response.ts
#	packages/common/src/common_module.ts
#	packages/common/src/directives/ng_for_of.ts
#	packages/common/src/location/location.ts
#	packages/common/src/pipes/date_pipe.ts
#	packages/core/src/change_detection/pipe_transform.ts
#	packages/core/src/di/injectable.ts
#	packages/core/src/metadata/di.ts
#	packages/core/src/metadata/directives.ts
#	packages/core/src/metadata/ng_module.ts
#	packages/core/src/render3/component_ref.ts
#	packages/forms/src/directives/reactive_directives/form_control_name.ts
#	packages/forms/src/form_builder.ts
#	packages/forms/src/model.ts
#	packages/forms/src/validators.ts
#	packages/router/src/config.ts
#	packages/router/src/directives/router_outlet.ts
#	packages/router/src/events.ts
#	packages/router/src/router.ts
#	packages/router/src/router_module.ts
#	packages/router/src/router_state.ts
#	packages/router/src/shared.ts
2020-01-24 15:55:22 +08:00

419 lines
12 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {HttpHeaders} from './headers';
/**
* Type enumeration for the different kinds of `HttpEvent`.
*
* 不同种类的 `HttpEvent` 的枚举类型。
*
* @publicApi
*/
export enum HttpEventType {
/**
* The request was sent out over the wire.
*
* 该请求已经在线路上发出了。
*/
Sent,
/**
* An upload progress event was received.
*
* 收到了上传进度事件。
*/
UploadProgress,
/**
* The response status code and headers were received.
*
* 收到了响应状态码和响应头。
*/
ResponseHeader,
/**
* A download progress event was received.
*
* 收到了下载进度事件。
*/
DownloadProgress,
/**
* The full response including the body was received.
*
* 收到了包括响应体在内的完整响应对象。
*/
Response,
/**
* A custom event from an interceptor or a backend.
*
* 来自拦截器或后端的自定义事件。
*/
User,
}
/**
* Base interface for progress events.
*
*
* 进度事件的基础接口。
* @publicApi
*/
export interface HttpProgressEvent {
/**
* Progress event type is either upload or download.
*
* 进度事件的类型或者是上传或者是下载。
*/
type: HttpEventType.DownloadProgress|HttpEventType.UploadProgress;
/**
* Number of bytes uploaded or downloaded.
*
* 已经上传或下载的字节数。
*/
loaded: number;
/**
* Total number of bytes to upload or download. Depending on the request or
* response, this may not be computable and thus may not be present.
*
* 要上传或下载的总字节数。它可能是无法计算出来的,因此也就可能不存在,取决于是请求还是响应。
*/
total?: number;
}
/**
* A download progress event.
*
* 下载进度事件。
*
* @publicApi
*/
export interface HttpDownloadProgressEvent extends HttpProgressEvent {
type: HttpEventType.DownloadProgress;
/**
* The partial response body as downloaded so far.
*
* 到目前为止已经下载的那部分响应体。
*
* Only present if the responseType was `text`.
*
* 只有当 responseType 是 `text` 时才会出现。
*/
partialText?: string;
}
/**
* An upload progress event.
*
* 上传进度事件。
*
* @publicApi
*/
export interface HttpUploadProgressEvent extends HttpProgressEvent {
type: HttpEventType.UploadProgress;
}
/**
* An event indicating that the request was sent to the server. Useful
* when a request may be retried multiple times, to distinguish between
* retries on the final event stream.
*
* 用于表示请求已经发到服务器的事件。
* 当请求可能被多次接受时很有用,以区分出最终事件流上的重试行为。
*
* @publicApi
*/
export interface HttpSentEvent { type: HttpEventType.Sent; }
/**
* A user-defined event.
*
* 用户定义的事件。
*
* Grouping all custom events under this type ensures they will be handled
* and forwarded by all implementations of interceptors.
*
* 把所有自定义事件都分组在此类型下,以确保它们会被所有的拦截器所处理和转发。
*
* @publicApi
*/
export interface HttpUserEvent<T> { type: HttpEventType.User; }
/**
* An error that represents a failed attempt to JSON.parse text coming back
* from the server.
*
* 一个错误对象,用来表示当视图用 JSON.parse 对从服务器返回的文本进行解析时出错。
*
* It bundles the Error object with the actual response body that failed to parse.
*
* 它把 `Error` 对象和解析出错的实际响应体绑在一起。
*
*/
export interface HttpJsonParseError {
error: Error;
text: string;
}
/**
* Union type for all possible events on the response stream.
*
* 响应流中所有可能出现的事件的联合类型。
*
* Typed according to the expected type of the response.
*
* 其类型取决于所期待的响应类型。
*
* @publicApi
*/
export type HttpEvent<T> =
HttpSentEvent | HttpHeaderResponse | HttpResponse<T>| HttpProgressEvent | HttpUserEvent<T>;
/**
* Base class for both `HttpResponse` and `HttpHeaderResponse`.
*
* `HttpResponse` 和 `HttpHeaderResponse` 的共同基类。
*
* @publicApi
*/
export abstract class HttpResponseBase {
/**
* All response headers.
*
* 所有响应头。
*/
readonly headers: HttpHeaders;
/**
* Response status code.
*
* 响应的状态码。
*/
readonly status: number;
/**
* Textual description of response status code.
*
* 响应状态码的文本描述。
*
* Do not depend on this.
*
* 请不要在代码中依赖它。
*/
readonly statusText: string;
/**
* URL of the resource retrieved, or null if not available.
*
* 所接收的资源的 URL,如果不可用则为 `null`。
*/
readonly url: string|null;
/**
* Whether the status code falls in the 2xx range.
*
* 状态码是否位于 2xx 范围内。
*/
readonly ok: boolean;
/**
* Type of the response, narrowed to either the full response or the header.
*
* 响应对象的类型,窄化为完整的响应对象或只有响应头。
*/
// TODO(issue/24571): remove '!'.
readonly type !: HttpEventType.Response | HttpEventType.ResponseHeader;
/**
* Super-constructor for all responses.
*
* 所有响应体的上级(super)构造器。
*
* The single parameter accepted is an initialization hash. Any properties
* of the response passed there will override the default values.
*
* 接受的唯一参数是一个初始化哈希值。所传进来的响应对象的任何属性都会覆盖这些默认值。
*/
constructor(
init: {
headers?: HttpHeaders,
status?: number,
statusText?: string,
url?: string,
},
defaultStatus: number = 200, defaultStatusText: string = 'OK') {
// If the hash has values passed, use them to initialize the response.
// Otherwise use the default values.
this.headers = init.headers || new HttpHeaders();
this.status = init.status !== undefined ? init.status : defaultStatus;
this.statusText = init.statusText || defaultStatusText;
this.url = init.url || null;
// Cache the ok value to avoid defining a getter.
this.ok = this.status >= 200 && this.status < 300;
}
}
/**
* A partial HTTP response which only includes the status and header data,
* but no response body.
*
* 一个部分 HTTP 请求,它只包括状态和响应头数据,但没有响应体。
*
* `HttpHeaderResponse` is a `HttpEvent` available on the response
* event stream, only when progress events are requested.
*
* `HttpHeaderResponse` 是一个可用于响应事件流的 `HttpEvent`,只有在要求了进度事件时才有效。
*
* @publicApi
*/
export class HttpHeaderResponse extends HttpResponseBase {
/**
* Create a new `HttpHeaderResponse` with the given parameters.
*
* 根据给定的参数创建新的 `HttpHeaderResponse` 对象。
*/
constructor(init: {
headers?: HttpHeaders,
status?: number,
statusText?: string,
url?: string,
} = {}) {
super(init);
}
readonly type: HttpEventType.ResponseHeader = HttpEventType.ResponseHeader;
/**
* Copy this `HttpHeaderResponse`, overriding its contents with the
* given parameter hash.
*
* 复制这个 `HttpHeaderResponse`,使用给定的参数哈希对象覆盖其内容。
*/
clone(update: {headers?: HttpHeaders; status?: number; statusText?: string; url?: string;} = {}):
HttpHeaderResponse {
// Perform a straightforward initialization of the new HttpHeaderResponse,
// overriding the current parameters with new ones if given.
return new HttpHeaderResponse({
headers: update.headers || this.headers,
status: update.status !== undefined ? update.status : this.status,
statusText: update.statusText || this.statusText,
url: update.url || this.url || undefined,
});
}
}
/**
* A full HTTP response, including a typed response body (which may be `null`
* if one was not returned).
*
* 一个完整的 HTTP 响应对象,包括一个带类型的响应体(如果没返回内容,则为 `null`)。
*
* `HttpResponse` is a `HttpEvent` available on the response event
* stream.
*
*
* `HttpResponse` 是一个用于事件响应流的 `HttpEvent`。
* @publicApi
*/
export class HttpResponse<T> extends HttpResponseBase {
/**
* The response body, or `null` if one was not returned.
*
* 响应体,如果没有返回内容则为 `null`。
*/
readonly body: T|null;
/**
* Construct a new `HttpResponse`.
*
* 构造一个新的 `HttpResponse`。
*/
constructor(init: {
body?: T | null, headers?: HttpHeaders; status?: number; statusText?: string; url?: string;
} = {}) {
super(init);
this.body = init.body !== undefined ? init.body : null;
}
readonly type: HttpEventType.Response = HttpEventType.Response;
clone(): HttpResponse<T>;
clone(update: {headers?: HttpHeaders; status?: number; statusText?: string; url?: string;}):
HttpResponse<T>;
clone<V>(update: {
body?: V | null, headers?: HttpHeaders; status?: number; statusText?: string; url?: string;
}): HttpResponse<V>;
clone(update: {
body?: any | null; headers?: HttpHeaders; status?: number; statusText?: string; url?: string;
} = {}): HttpResponse<any> {
return new HttpResponse<any>({
body: (update.body !== undefined) ? update.body : this.body,
headers: update.headers || this.headers,
status: (update.status !== undefined) ? update.status : this.status,
statusText: update.statusText || this.statusText,
url: update.url || this.url || undefined,
});
}
}
/**
* A response that represents an error or failure, either from a
* non-successful HTTP status, an error while executing the request,
* or some other failure which occurred during the parsing of the response.
*
* 一个用于表示错误或失败的响应对象,或者来自执行请求时发生的错误给出的失败的 HTTP 状态码,或者来自在解析响应对象期间发生的其它错误。
*
* Any error returned on the `Observable` response stream will be
* wrapped in an `HttpErrorResponse` to provide additional context about
* the state of the HTTP layer when the error occurred. The error property
* will contain either a wrapped Error object or the error response returned
* from the server.
*
*
* 任何从 `Observable` 响应流中返回的错误都会被包装成 `HttpErrorResponse` 对象,以便在发生错误时,提供关于 HTTP 层状态的额外上下文信息。
* 该错误或者包含一个包装好的错误对象,或者包含一个从服务端返回的错误响应体。
*
* @publicApi
*/
export class HttpErrorResponse extends HttpResponseBase implements Error {
readonly name = 'HttpErrorResponse';
readonly message: string;
readonly error: any|null;
/**
* Errors are never okay, even when the status code is in the 2xx success range.
*
* 只要是错误,其 `ok` 就永远为 `false`,就算其 HTTP 状态码是 2xx 也一样。
*/
readonly ok = false;
constructor(init: {
error?: any; headers?: HttpHeaders; status?: number; statusText?: string; url?: string;
}) {
// Initialize with a default status of 0 / Unknown Error.
super(init, 0, 'Unknown Error');
// If the response was successful, then this was a parse error. Otherwise, it was
// a protocol-level failure of some sort. Either the request failed in transit
// or the server returned an unsuccessful status code.
if (this.status >= 200 && this.status < 300) {
this.message = `Http failure during parsing for ${init.url || '(unknown url)'}`;
} else {
this.message =
`Http failure response for ${init.url || '(unknown url)'}: ${init.status} ${init.statusText}`;
}
this.error = init.error || null;
}
}