From 2b6add0b242f5953592c3d09591840f254822b82 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Fri, 12 Feb 2021 12:29:21 +0000 Subject: [PATCH] fix: explicit HTTPRequest.resourceType type defs Fixes #6854. --- src/common/HTTPRequest.ts | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/common/HTTPRequest.ts b/src/common/HTTPRequest.ts index f0683176166a2..2d9554342db5a 100644 --- a/src/common/HTTPRequest.ts +++ b/src/common/HTTPRequest.ts @@ -45,6 +45,26 @@ export interface ResponseForRequest { body: string | Buffer; } +/** + * Resource types for HTTPRequests as perceived by the rendering engine. + * + * @public + */ +export type ResourceType = + | 'document' + | 'stylesheet' + | 'image' + | 'media' + | 'font' + | 'script' + | 'texttrack' + | 'xhr' + | 'fetch' + | 'eventsource' + | 'websocket' + | 'manifest' + | 'other'; + /** * * Represents an HTTP request sent by a page. @@ -108,7 +128,7 @@ export class HTTPRequest { private _allowInterception: boolean; private _interceptionHandled = false; private _url: string; - private _resourceType: string; + private _resourceType: ResourceType; private _method: string; private _postData?: string; @@ -133,7 +153,7 @@ export class HTTPRequest { this._interceptionId = interceptionId; this._allowInterception = allowInterception; this._url = event.request.url; - this._resourceType = event.type.toLowerCase(); + this._resourceType = event.type.toLowerCase() as ResourceType; this._method = event.request.method; this._postData = event.request.postData; this._frame = frame; @@ -153,17 +173,8 @@ export class HTTPRequest { /** * Contains the request's resource type as it was perceived by the rendering * engine. - * @remarks - * @returns one of the following: `document`, `stylesheet`, `image`, `media`, - * `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, - * `manifest`, `other`. */ - resourceType(): string { - // TODO (@jackfranklin): protocol.d.ts has a type for this, but all the - // string values are uppercase. The Puppeteer docs explicitly say the - // potential values are all lower case, and the constructor takes the event - // type and calls toLowerCase() on it, so we can't reuse the type from the - // protocol.d.ts. Why do we lower case? + resourceType(): ResourceType { return this._resourceType; }