Skip to content

Commit

Permalink
feat: use flag to replace httprequest instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored and trivikr committed Jan 3, 2020
1 parent 8d55098 commit b584032
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
3 changes: 2 additions & 1 deletion packages/middleware-content-length/package.json
Expand Up @@ -14,7 +14,8 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/types": "^0.1.0-preview.7",
"@aws-sdk/types": "^0.1.0-preview.5",
"@aws-sdk/protocol-http": "^0.1.0-preview.1",
"tslib": "^1.8.0"
},
"devDependencies": {
Expand Down
32 changes: 17 additions & 15 deletions packages/middleware-content-length/src/index.ts
Expand Up @@ -4,9 +4,9 @@ import {
BuildMiddleware,
BodyLengthCalculator,
MetadataBearer,
BuildHandlerOutput,
HttpRequest
BuildHandlerOutput
} from "@aws-sdk/types";
import { HttpRequest } from "@aws-sdk/protocol-http";

export function contentLengthMiddleware(
bodyLengthCalculator: BodyLengthCalculator
Expand All @@ -18,19 +18,21 @@ export function contentLengthMiddleware(
): Promise<BuildHandlerOutput<Output>> => {
let request = { ...args.request };
//TODO: cast request with instanceof
const { body, headers } = <HttpRequest>request;
if (
body &&
Object.keys(headers)
.map(str => str.toLowerCase())
.indexOf("content-length") === -1
) {
const length = bodyLengthCalculator(body);
if (length !== undefined) {
(<HttpRequest>request).headers = {
...(<HttpRequest>request).headers,
"Content-Length": String(length)
};
if (HttpRequest.isHttpRequest(request)) {
const { body, headers } = request;
if (
body &&
Object.keys(headers)
.map(str => str.toLowerCase())
.indexOf("content-length") === -1
) {
const length = bodyLengthCalculator(body);
if (length !== undefined) {
request.headers = {
...request.headers,
"Content-Length": String(length)
};
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/protocol-http/package.json
Expand Up @@ -16,7 +16,8 @@
"license": "Apache-2.0",
"dependencies": {
"tslib": "^1.8.0",
"@aws-sdk/types": "^0.1.0-preview.4"
"@aws-sdk/types": "^0.1.0-preview.4",
"@aws-sdk/util-uri-escape": "^0.1.0-preview.3"
},
"devDependencies": {
"@types/jest": "^24.0.12",
Expand Down
6 changes: 6 additions & 0 deletions packages/protocol-http/src/httpRequest.ts
Expand Up @@ -18,6 +18,8 @@ export class HttpRequest implements HttpMessage, HttpEndpoint {
public query: QueryParameterBag;
public headers: HeaderBag;
public body?: any;
public isHttpRequest = true;

constructor(options: HttpRequestOptions) {
this.method = options.method || "GET";
this.hostname = options.hostname || "localhost";
Expand All @@ -37,6 +39,10 @@ export class HttpRequest implements HttpMessage, HttpEndpoint {
: "/";
}

static isInstance(request: unknown): request is HttpRequest {
return (request as HttpRequest).isHttpRequest;
}

getFormatedUrl(): string {
let hostname = this.hostname;
if (this.port) {
Expand Down

0 comments on commit b584032

Please sign in to comment.