|
| 1 | +import { |
| 2 | + BuildHandler, |
| 3 | + BuildHandlerArguments, |
| 4 | + BuildHandlerOptions, |
| 5 | + BuildHandlerOutput, |
| 6 | + BuildMiddleware, |
| 7 | + MetadataBearer, |
| 8 | + Pluggable |
| 9 | +} from "@aws-sdk/types"; |
| 10 | +import { HttpRequest } from "@aws-sdk/protocol-http"; |
| 11 | +import { ResolvedPredictEndpointMiddlewareConfig } from "./configurations"; |
| 12 | + |
| 13 | +export function predictEndpointMiddleware( |
| 14 | + options: ResolvedPredictEndpointMiddlewareConfig |
| 15 | +): BuildMiddleware<any, any> { |
| 16 | + return <Output extends MetadataBearer>( |
| 17 | + next: BuildHandler<any, Output> |
| 18 | + ): BuildHandler<any, Output> => async ( |
| 19 | + args: BuildHandlerArguments<any> |
| 20 | + ): Promise<BuildHandlerOutput<Output>> => { |
| 21 | + const { input } = args; |
| 22 | + let { request } = args; |
| 23 | + if (HttpRequest.isInstance(request)) { |
| 24 | + if (input.PredictEndpoint) { |
| 25 | + const endpoint = options.urlParser(input.predictEndpoint); |
| 26 | + request = { |
| 27 | + ...request, |
| 28 | + hostname: endpoint.hostname, |
| 29 | + path: endpoint.path, |
| 30 | + port: endpoint.port, |
| 31 | + protocol: endpoint.protocol, |
| 32 | + query: endpoint.query |
| 33 | + }; |
| 34 | + } |
| 35 | + } |
| 36 | + return next({ |
| 37 | + ...args, |
| 38 | + request |
| 39 | + }); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export const predictEndpointMiddlewareOptions: BuildHandlerOptions = { |
| 44 | + step: "build", |
| 45 | + tags: ["PREDICT_ENDPOINT"], |
| 46 | + name: "predictEndpointMiddleware" |
| 47 | +}; |
| 48 | + |
| 49 | +export const getPredictEndpointPlugin = ( |
| 50 | + config: ResolvedPredictEndpointMiddlewareConfig |
| 51 | +): Pluggable<any, any> => ({ |
| 52 | + applyToStack: clientStack => { |
| 53 | + clientStack.add( |
| 54 | + predictEndpointMiddleware(config), |
| 55 | + predictEndpointMiddlewareOptions |
| 56 | + ); |
| 57 | + } |
| 58 | +}); |
0 commit comments