Skip to content

Commit 2969bab

Browse files
committed
fixup! feat: support @httpApiKeyAuth trait
1 parent 7684950 commit 2969bab

File tree

1 file changed

+32
-21
lines changed
  • smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/integration

1 file changed

+32
-21
lines changed

smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen/integration/http-api-key-auth.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
22

33
import { HttpRequest } from "@aws-sdk/protocol-http";
44
import {
@@ -8,7 +8,7 @@ import {
88
Pluggable,
99
} from "@aws-sdk/types";
1010

11-
interface PluginOptions {
11+
interface HttpApiKeyAuthMiddlewareConfig {
1212
/**
1313
* Where to put the API key.
1414
*
@@ -50,6 +50,10 @@ export interface HttpApiKeyAuthResolvedConfig {
5050
apiKey?: string;
5151
}
5252

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."
5357
export function resolveHttpApiKeyAuthConfig<T>(
5458
input: T & PreviouslyResolved & HttpApiKeyAuthInputConfig
5559
): T & HttpApiKeyAuthResolvedConfig {
@@ -71,33 +75,40 @@ export function resolveHttpApiKeyAuthConfig<T>(
7175
*/
7276
export const httpApiKeyAuthMiddleware =
7377
<Input extends object, Output extends object>(
74-
resolvedConfig: HttpApiKeyAuthResolvedConfig,
75-
options: PluginOptions
78+
pluginConfig: HttpApiKeyAuthResolvedConfig,
79+
middlewareConfig: HttpApiKeyAuthMiddlewareConfig
7680
): BuildMiddleware<Input, Output> =>
7781
(next) =>
7882
async (args) => {
7983
if (!HttpRequest.isInstance(args.request)) return next(args);
8084

81-
const { request } = args;
82-
8385
// This middleware will not be injected if the operation has the @optionalAuth trait.
84-
if (!resolvedConfig.apiKey) {
86+
if (!pluginConfig.apiKey) {
8587
throw new Error(
8688
"API key authorization is required but no API key was provided in the client configuration"
8789
);
8890
}
8991

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+
});
101112
};
102113

103114
export const httpApiKeyAuthMiddlewareOptions: BuildHandlerOptions &
@@ -110,12 +121,12 @@ export const httpApiKeyAuthMiddlewareOptions: BuildHandlerOptions &
110121
};
111122

112123
export const getHttpApiKeyAuthPlugin = (
113-
resolvedConfig: HttpApiKeyAuthResolvedConfig,
114-
options: PluginOptions
124+
pluginConfig: HttpApiKeyAuthResolvedConfig,
125+
middlewareConfig: HttpApiKeyAuthMiddlewareConfig
115126
): Pluggable<any, any> => ({
116127
applyToStack: (clientStack) => {
117128
clientStack.add(
118-
httpApiKeyAuthMiddleware(resolvedConfig, options),
129+
httpApiKeyAuthMiddleware(pluginConfig, middlewareConfig),
119130
httpApiKeyAuthMiddlewareOptions
120131
);
121132
},

0 commit comments

Comments
 (0)