Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
docs(new): migrate HTTPResponse docs to TSDoc (#6085)
Co-authored-by: Changhao Han <changhaohan@chromium.org>
  • Loading branch information
2 people authored and mathiasbynens committed Jun 25, 2020
1 parent b993adb commit 4696f7a
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 52 deletions.
22 changes: 0 additions & 22 deletions new-docs/puppeteer.httpresponse._constructor_.md

This file was deleted.

22 changes: 0 additions & 22 deletions new-docs/puppeteer.httpresponse._resolvebody.md

This file was deleted.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.buffer.md
Expand Up @@ -13,3 +13,5 @@ buffer(): Promise<Buffer>;

Promise&lt;Buffer&gt;

Promise which resolves to a buffer with response body.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.frame.md
Expand Up @@ -13,3 +13,5 @@ frame(): Frame | null;

[Frame](./puppeteer.frame.md) \| null

A [Frame](./puppeteer.frame.md) that initiated this response, or `null` if navigating to error pages.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.fromcache.md
Expand Up @@ -13,3 +13,5 @@ fromCache(): boolean;

boolean

True if the response was served from either the browser's disk cache or memory cache.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.fromserviceworker.md
Expand Up @@ -13,3 +13,5 @@ fromServiceWorker(): boolean;

boolean

True if the response was served by a service worker.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.headers.md
Expand Up @@ -13,3 +13,5 @@ headers(): Record<string, string>;

Record&lt;string, string&gt;

An object with HTTP headers associated with the response. All header names are lower-case.

6 changes: 6 additions & 0 deletions new-docs/puppeteer.httpresponse.json.md
Expand Up @@ -13,3 +13,9 @@ json(): Promise<any>;

Promise&lt;any&gt;

Promise which resolves to a JSON representation of response body.

## Remarks

This method will throw if the response body is not parsable via `JSON.parse`<!-- -->.

9 changes: 4 additions & 5 deletions new-docs/puppeteer.httpresponse.md
Expand Up @@ -4,23 +4,22 @@

## HTTPResponse class

The HTTPResponse class represents responses which are received by the [Page](./puppeteer.page.md) class.

<b>Signature:</b>

```typescript
export declare class HTTPResponse
```

## Constructors
## Remarks

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(client, request, responsePayload)](./puppeteer.httpresponse._constructor_.md) | | Constructs a new instance of the <code>HTTPResponse</code> class |
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `HTTPResponse` class.

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [\_resolveBody(err)](./puppeteer.httpresponse._resolvebody.md) | | |
| [buffer()](./puppeteer.httpresponse.buffer.md) | | |
| [frame()](./puppeteer.httpresponse.frame.md) | | |
| [fromCache()](./puppeteer.httpresponse.fromcache.md) | | |
Expand Down
2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.ok.md
Expand Up @@ -13,3 +13,5 @@ ok(): boolean;

boolean

True if the response was successful (status in the range 200-299).

4 changes: 3 additions & 1 deletion new-docs/puppeteer.httpresponse.remoteaddress.md
Expand Up @@ -11,5 +11,7 @@ remoteAddress(): RemoteAddress;
```
<b>Returns:</b>

RemoteAddress
[RemoteAddress](./puppeteer.remoteaddress.md)

The IP address and port number used to connect to the remote server.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.request.md
Expand Up @@ -13,3 +13,5 @@ request(): HTTPRequest;

[HTTPRequest](./puppeteer.httprequest.md)

A matching [HTTPRequest](./puppeteer.httprequest.md) object.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.securitydetails.md
Expand Up @@ -13,3 +13,5 @@ securityDetails(): SecurityDetails | null;

[SecurityDetails](./puppeteer.securitydetails.md) \| null

[SecurityDetails](./puppeteer.securitydetails.md) if the response was received over the secure connection, or `null` otherwise.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.status.md
Expand Up @@ -13,3 +13,5 @@ status(): number;

number

The status code of the response (e.g., 200 for a success).

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.statustext.md
Expand Up @@ -13,3 +13,5 @@ statusText(): string;

string

The status text of the response (e.g. usually an "OK" for a success).

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.text.md
Expand Up @@ -13,3 +13,5 @@ text(): Promise<string>;

Promise&lt;string&gt;

Promise which resolves to a text representation of response body.

2 changes: 2 additions & 0 deletions new-docs/puppeteer.httpresponse.url.md
Expand Up @@ -13,3 +13,5 @@ url(): string;

string

The URL of the response.

3 changes: 2 additions & 1 deletion new-docs/puppeteer.md
Expand Up @@ -24,7 +24,7 @@
| [Frame](./puppeteer.frame.md) | |
| [FrameManager](./puppeteer.framemanager.md) | |
| [HTTPRequest](./puppeteer.httprequest.md) | |
| [HTTPResponse](./puppeteer.httpresponse.md) | |
| [HTTPResponse](./puppeteer.httpresponse.md) | The HTTPResponse class represents responses which are received by the [Page](./puppeteer.page.md) class. |
| [JSHandle](./puppeteer.jshandle.md) | |
| [Keyboard](./puppeteer.keyboard.md) | |
| [Mouse](./puppeteer.mouse.md) | The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport. |
Expand Down Expand Up @@ -55,6 +55,7 @@
| [KeyDefinition](./puppeteer.keydefinition.md) | Copyright 2017 Google Inc. All rights reserved.<!-- -->Licensed under the Apache License, Version 2.0 (the 'License'); you may not use this file except in compliance with the License. You may obtain a copy of the License at<!-- -->http://www.apache.org/licenses/LICENSE-2.0<!-- -->Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
| [Metrics](./puppeteer.metrics.md) | |
| [PressOptions](./puppeteer.pressoptions.md) | |
| [RemoteAddress](./puppeteer.remoteaddress.md) | |
| [SerializedAXNode](./puppeteer.serializedaxnode.md) | Represents a Node and the properties of it that are relevant to Accessibility. |
| [SnapshotOptions](./puppeteer.snapshotoptions.md) | |
| [WaitForTargetOptions](./puppeteer.waitfortargetoptions.md) | |
Expand Down
11 changes: 11 additions & 0 deletions new-docs/puppeteer.remoteaddress.ip.md
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [puppeteer](./puppeteer.md) &gt; [RemoteAddress](./puppeteer.remoteaddress.md) &gt; [ip](./puppeteer.remoteaddress.ip.md)

## RemoteAddress.ip property

<b>Signature:</b>

```typescript
ip: string;
```
20 changes: 20 additions & 0 deletions new-docs/puppeteer.remoteaddress.md
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [puppeteer](./puppeteer.md) &gt; [RemoteAddress](./puppeteer.remoteaddress.md)

## RemoteAddress interface


<b>Signature:</b>

```typescript
export interface RemoteAddress
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [ip](./puppeteer.remoteaddress.ip.md) | string | |
| [port](./puppeteer.remoteaddress.port.md) | number | |

11 changes: 11 additions & 0 deletions new-docs/puppeteer.remoteaddress.port.md
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [puppeteer](./puppeteer.md) &gt; [RemoteAddress](./puppeteer.remoteaddress.md) &gt; [port](./puppeteer.remoteaddress.port.md)

## RemoteAddress.port property

<b>Signature:</b>

```typescript
port: number;
```
72 changes: 71 additions & 1 deletion src/common/HTTPResponse.ts
Expand Up @@ -19,11 +19,20 @@ import { HTTPRequest } from './HTTPRequest';
import { SecurityDetails } from './SecurityDetails';
import Protocol from '../protocol';

interface RemoteAddress {
/**
* @public
*/
export interface RemoteAddress {
ip: string;
port: number;
}

/**
* The HTTPResponse class represents responses which are received by the
* {@link Page} class.
*
* @public
*/
export class HTTPResponse {
private _client: CDPSession;
private _request: HTTPRequest;
Expand All @@ -39,6 +48,9 @@ export class HTTPResponse {
private _headers: Record<string, string> = {};
private _securityDetails: SecurityDetails | null;

/**
* @internal
*/
constructor(
client: CDPSession,
request: HTTPRequest,
Expand Down Expand Up @@ -67,38 +79,70 @@ export class HTTPResponse {
: null;
}

/**
* @internal
*/
_resolveBody(err: Error | null): void {
return this._bodyLoadedPromiseFulfill(err);
}

/**
* @returns The IP address and port number used to connect to the remote
* server.
*/
remoteAddress(): RemoteAddress {
return this._remoteAddress;
}

/**
* @returns The URL of the response.
*/
url(): string {
return this._url;
}

/**
* @returns True if the response was successful (status in the range 200-299).
*/
ok(): boolean {
// TODO: document === 0 case?
return this._status === 0 || (this._status >= 200 && this._status <= 299);
}

/**
* @returns The status code of the response (e.g., 200 for a success).
*/
status(): number {
return this._status;
}

/**
* @returns The status text of the response (e.g. usually an "OK" for a
* success).
*/
statusText(): string {
return this._statusText;
}

/**
* @returns An object with HTTP headers associated with the response. All
* header names are lower-case.
*/
headers(): Record<string, string> {
return this._headers;
}

/**
* @returns {@link SecurityDetails} if the response was received over the
* secure connection, or `null` otherwise.
*/
securityDetails(): SecurityDetails | null {
return this._securityDetails;
}

/**
* @returns Promise which resolves to a buffer with response body.
*/
buffer(): Promise<Buffer> {
if (!this._contentPromise) {
this._contentPromise = this._bodyLoadedPromise.then(async (error) => {
Expand All @@ -115,28 +159,54 @@ export class HTTPResponse {
return this._contentPromise;
}

/**
* @returns Promise which resolves to a text representation of response body.
*/
async text(): Promise<string> {
const content = await this.buffer();
return content.toString('utf8');
}

/**
*
* @returns Promise which resolves to a JSON representation of response body.
*
* @remarks
*
* This method will throw if the response body is not parsable via
* `JSON.parse`.
*/
async json(): Promise<any> {
const content = await this.text();
return JSON.parse(content);
}

/**
* @returns A matching {@link HTTPRequest} object.
*/
request(): HTTPRequest {
return this._request;
}

/**
* @returns True if the response was served from either the browser's disk
* cache or memory cache.
*/
fromCache(): boolean {
return this._fromDiskCache || this._request._fromMemoryCache;
}

/**
* @returns True if the response was served by a service worker.
*/
fromServiceWorker(): boolean {
return this._fromServiceWorker;
}

/**
* @returns A {@link Frame} that initiated this response, or `null` if
* navigating to error pages.
*/
frame(): Frame | null {
return this._request.frame();
}
Expand Down

0 comments on commit 4696f7a

Please sign in to comment.