-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make handler, middleware, coreHandler, etc, functional again * Make CoreHandler match functional interface * Update MiddlewareStack to use functional interfaces * Update existing middleware to use functional interface * Update middleware, stack, client, and command interfaces to allow combining command and client stacks * Update SDK packages to use functional middleware interfaces
- Loading branch information
Showing
4 changed files
with
150 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
import { | ||
BuildHandler, | ||
BuildHandlerArguments, | ||
BuildMiddleware, | ||
BodyLengthCalculator, | ||
Handler, | ||
HandlerArguments | ||
} from '@aws/types'; | ||
|
||
export class ContentLengthMiddleware implements Handler<any, any, any> { | ||
constructor( | ||
private readonly bodyLengthCalculator: BodyLengthCalculator, | ||
private readonly next: Handler<any, any, any> | ||
) {} | ||
|
||
async handle(args: HandlerArguments<any, any>): Promise<any> { | ||
const request = args.request; | ||
|
||
export function contentLengthMiddleware< | ||
Input extends object, | ||
Output extends object, | ||
Stream | ||
>( | ||
bodyLengthCalculator: BodyLengthCalculator | ||
): BuildMiddleware<Input, Output, Stream> { | ||
return ( | ||
next: BuildHandler<Input, Output, Stream> | ||
): BuildHandler<Input, Output, Stream> => async ( | ||
args: BuildHandlerArguments<Input, Stream> | ||
): Promise<Output> => { | ||
const {request} = args; | ||
if (!request) { | ||
throw new Error('Unable to determine request content-length due to missing request.'); | ||
} | ||
|
||
if (request.body) { | ||
request.headers['Content-Length'] = String(this.bodyLengthCalculator(request.body)); | ||
const {body, headers} = request; | ||
if ( | ||
body && | ||
Object.keys(headers) | ||
.map(str => str.toLowerCase()) | ||
.indexOf('content-length') === -1 | ||
) { | ||
headers['Content-Length'] = String(bodyLengthCalculator(body)); | ||
} | ||
|
||
return this.next.handle({ | ||
request, | ||
...args | ||
return next({ | ||
...args, | ||
request | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.