1
- // derived from https://github.com/aws/aws-sdk-js-v3/blob/b0e1422630 /packages/middleware-host-header /src/index .ts
1
+ // derived from https://github.com/aws/aws-sdk-js-v3/blob/e35f78c97fa6710ff9c444351893f0f06755e771 /packages/middleware-endpoint-discovery /src/endpointDiscoveryMiddleware .ts
2
2
3
3
import { HttpRequest } from "@aws-sdk/protocol-http" ;
4
4
import {
8
8
Pluggable ,
9
9
} from "@aws-sdk/types" ;
10
10
11
- interface PluginOptions {
11
+ interface HttpApiKeyAuthMiddlewareConfig {
12
12
/**
13
13
* Where to put the API key.
14
14
*
@@ -50,6 +50,10 @@ export interface HttpApiKeyAuthResolvedConfig {
50
50
apiKey ?: string ;
51
51
}
52
52
53
+ // We have to provide a resolve function when we have config, even if it doesn't
54
+ // actually do anything to the input value. "If any of inputConfig, resolvedConfig,
55
+ // or resolveFunction are set, then all of inputConfig, resolvedConfig, and
56
+ // resolveFunction must be set."
53
57
export function resolveHttpApiKeyAuthConfig < T > (
54
58
input : T & PreviouslyResolved & HttpApiKeyAuthInputConfig
55
59
) : T & HttpApiKeyAuthResolvedConfig {
@@ -71,33 +75,40 @@ export function resolveHttpApiKeyAuthConfig<T>(
71
75
*/
72
76
export const httpApiKeyAuthMiddleware =
73
77
< Input extends object , Output extends object > (
74
- resolvedConfig : HttpApiKeyAuthResolvedConfig ,
75
- options : PluginOptions
78
+ pluginConfig : HttpApiKeyAuthResolvedConfig ,
79
+ middlewareConfig : HttpApiKeyAuthMiddlewareConfig
76
80
) : BuildMiddleware < Input , Output > =>
77
81
( next ) =>
78
82
async ( args ) => {
79
83
if ( ! HttpRequest . isInstance ( args . request ) ) return next ( args ) ;
80
84
81
- const { request } = args ;
82
-
83
85
// This middleware will not be injected if the operation has the @optionalAuth trait.
84
- if ( ! resolvedConfig . apiKey ) {
86
+ if ( ! pluginConfig . apiKey ) {
85
87
throw new Error (
86
88
"API key authorization is required but no API key was provided in the client configuration"
87
89
) ;
88
90
}
89
91
90
- if ( options . in === "header" ) {
91
- // Set the header, even if it's already been set.
92
- request . headers [ options . name . toLowerCase ( ) ] = options . scheme
93
- ? `${ options . scheme } ${ resolvedConfig . apiKey } `
94
- : resolvedConfig . apiKey ;
95
- } else if ( options . in === "query" ) {
96
- // Set the query parameter, even if it's already been set.
97
- request . query [ options . name ] = resolvedConfig . apiKey ;
98
- }
99
-
100
- return next ( args ) ;
92
+ return next ( {
93
+ ...args ,
94
+ request : {
95
+ ...args . request ,
96
+ headers : {
97
+ ...args . request . headers ,
98
+ ...( middlewareConfig . in === "header" && {
99
+ // Set the header, even if it's already been set.
100
+ [ middlewareConfig . name . toLowerCase ( ) ] : middlewareConfig . scheme
101
+ ? `${ middlewareConfig . scheme } ${ pluginConfig . apiKey } `
102
+ : pluginConfig . apiKey ,
103
+ } ) ,
104
+ } ,
105
+ query : {
106
+ ...args . request . query ,
107
+ // Set the query parameter, even if it's already been set.
108
+ ...( middlewareConfig . in === "query" && { [ middlewareConfig . name ] : pluginConfig . apiKey } ) ,
109
+ } ,
110
+ } ,
111
+ } ) ;
101
112
} ;
102
113
103
114
export const httpApiKeyAuthMiddlewareOptions : BuildHandlerOptions &
@@ -110,12 +121,12 @@ export const httpApiKeyAuthMiddlewareOptions: BuildHandlerOptions &
110
121
} ;
111
122
112
123
export const getHttpApiKeyAuthPlugin = (
113
- resolvedConfig : HttpApiKeyAuthResolvedConfig ,
114
- options : PluginOptions
124
+ pluginConfig : HttpApiKeyAuthResolvedConfig ,
125
+ middlewareConfig : HttpApiKeyAuthMiddlewareConfig
115
126
) : Pluggable < any , any > => ( {
116
127
applyToStack : ( clientStack ) => {
117
128
clientStack . add (
118
- httpApiKeyAuthMiddleware ( resolvedConfig , options ) ,
129
+ httpApiKeyAuthMiddleware ( pluginConfig , middlewareConfig ) ,
119
130
httpApiKeyAuthMiddlewareOptions
120
131
) ;
121
132
} ,
0 commit comments