Skip to content

Commit 8edb87c

Browse files
committed
chore: make code cleaner
1 parent 90f9532 commit 8edb87c

File tree

20 files changed

+398
-156
lines changed

20 files changed

+398
-156
lines changed

packages/middleware-endpoint/src/adaptors/createConfigValueProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export const createConfigValueProvider = <Config extends Record<string, unknown>
2525
if (isClientContextParam) {
2626
// For client context parameters, check clientContextParams first
2727
const clientContextParams = config.clientContextParams as Record<string, unknown> | undefined;
28-
const nestedValue: unknown = clientContextParams?.[configKey] ?? clientContextParams?.[canonicalEndpointParamKey];
28+
const nestedValue: unknown = clientContextParams?.[canonicalEndpointParamKey];
2929
configValue = nestedValue ?? config[configKey] ?? config[canonicalEndpointParamKey];
3030
} else {
31-
// For built-in parameters, only check direct config properties
31+
// For built-in parameters and other config properties
3232
configValue = config[configKey] ?? config[canonicalEndpointParamKey];
3333
}
3434

packages/middleware-endpoint/src/adaptors/getEndpointFromInstructions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ export const resolveParams = async <
9090
endpointParams[name] = commandInput[instruction.name] as string | boolean;
9191
break;
9292
case "clientContextParams":
93-
endpointParams[name] = await createConfigValueProvider<Config>(instruction.name, name, clientConfig, true)();
94-
break;
9593
case "builtInParams":
96-
endpointParams[name] = await createConfigValueProvider<Config>(instruction.name, name, clientConfig, false)();
94+
endpointParams[name] = await createConfigValueProvider<Config>(
95+
instruction.name,
96+
name,
97+
clientConfig,
98+
instruction.type !== "builtInParams"
99+
)();
97100
break;
98101
case "operationContextParams":
99102
endpointParams[name] = instruction.get(commandInput);

private/my-local-model-schema/src/XYZServiceClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ export class XYZServiceClient extends __Client<
257257
getHttpAuthSchemeEndpointRuleSetPlugin(this.config, {
258258
httpAuthSchemeParametersProvider: defaultXYZServiceHttpAuthSchemeParametersProvider,
259259
identityProviderConfigProvider: async (config: XYZServiceClientResolvedConfig) =>
260-
new DefaultIdentityProviderConfig({}),
260+
new DefaultIdentityProviderConfig({
261+
"smithy.api#httpApiKeyAuth": config.apiKey,
262+
}),
261263
})
262264
);
263265
this.middlewareStack.use(getHttpSigningPlugin(this.config));

private/my-local-model-schema/src/auth/httpAuthExtensionConfiguration.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// smithy-typescript generated code
2-
import type { HttpAuthScheme } from "@smithy/types";
2+
import { type HttpAuthScheme, ApiKeyIdentity, ApiKeyIdentityProvider } from "@smithy/types";
33

44
import type { XYZServiceHttpAuthSchemeProvider } from "./httpAuthSchemeProvider";
55

@@ -11,6 +11,8 @@ export interface HttpAuthExtensionConfiguration {
1111
httpAuthSchemes(): HttpAuthScheme[];
1212
setHttpAuthSchemeProvider(httpAuthSchemeProvider: XYZServiceHttpAuthSchemeProvider): void;
1313
httpAuthSchemeProvider(): XYZServiceHttpAuthSchemeProvider;
14+
setApiKey(apiKey: ApiKeyIdentity | ApiKeyIdentityProvider): void;
15+
apiKey(): ApiKeyIdentity | ApiKeyIdentityProvider | undefined;
1416
}
1517

1618
/**
@@ -19,6 +21,7 @@ export interface HttpAuthExtensionConfiguration {
1921
export type HttpAuthRuntimeConfig = Partial<{
2022
httpAuthSchemes: HttpAuthScheme[];
2123
httpAuthSchemeProvider: XYZServiceHttpAuthSchemeProvider;
24+
apiKey: ApiKeyIdentity | ApiKeyIdentityProvider;
2225
}>;
2326

2427
/**
@@ -29,6 +32,7 @@ export const getHttpAuthExtensionConfiguration = (
2932
): HttpAuthExtensionConfiguration => {
3033
const _httpAuthSchemes = runtimeConfig.httpAuthSchemes!;
3134
let _httpAuthSchemeProvider = runtimeConfig.httpAuthSchemeProvider!;
35+
let _apiKey = runtimeConfig.apiKey;
3236
return {
3337
setHttpAuthScheme(httpAuthScheme: HttpAuthScheme): void {
3438
const index = _httpAuthSchemes.findIndex((scheme) => scheme.schemeId === httpAuthScheme.schemeId);
@@ -47,6 +51,12 @@ export const getHttpAuthExtensionConfiguration = (
4751
httpAuthSchemeProvider(): XYZServiceHttpAuthSchemeProvider {
4852
return _httpAuthSchemeProvider;
4953
},
54+
setApiKey(apiKey: ApiKeyIdentity | ApiKeyIdentityProvider): void {
55+
_apiKey = apiKey;
56+
},
57+
apiKey(): ApiKeyIdentity | ApiKeyIdentityProvider | undefined {
58+
return _apiKey;
59+
},
5060
};
5161
};
5262

@@ -57,5 +67,6 @@ export const resolveHttpAuthRuntimeConfig = (config: HttpAuthExtensionConfigurat
5767
return {
5868
httpAuthSchemes: config.httpAuthSchemes(),
5969
httpAuthSchemeProvider: config.httpAuthSchemeProvider(),
70+
apiKey: config.apiKey(),
6071
};
6172
};

private/my-local-model-schema/src/auth/httpAuthSchemeProvider.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// smithy-typescript generated code
2-
import type {
3-
HandlerExecutionContext,
4-
HttpAuthOption,
5-
HttpAuthScheme,
6-
HttpAuthSchemeParameters,
7-
HttpAuthSchemeParametersProvider,
8-
HttpAuthSchemeProvider,
9-
Provider,
2+
import { doesIdentityRequireRefresh, isIdentityExpired, memoizeIdentityProvider } from "@smithy/core";
3+
import {
4+
type HandlerExecutionContext,
5+
type HttpAuthOption,
6+
type HttpAuthScheme,
7+
type HttpAuthSchemeParameters,
8+
type HttpAuthSchemeParametersProvider,
9+
type HttpAuthSchemeProvider,
10+
type Provider,
11+
ApiKeyIdentity,
12+
ApiKeyIdentityProvider,
13+
HttpApiKeyAuthLocation,
1014
} from "@smithy/types";
1115
import { getSmithyContext, normalizeProvider } from "@smithy/util-middleware";
1216

@@ -41,9 +45,16 @@ export const defaultXYZServiceHttpAuthSchemeParametersProvider = async (
4145
};
4246
};
4347

44-
function createSmithyApiNoAuthHttpAuthOption(authParameters: XYZServiceHttpAuthSchemeParameters): HttpAuthOption {
48+
function createSmithyApiHttpApiKeyAuthHttpAuthOption(
49+
authParameters: XYZServiceHttpAuthSchemeParameters
50+
): HttpAuthOption {
4551
return {
46-
schemeId: "smithy.api#noAuth",
52+
schemeId: "smithy.api#httpApiKeyAuth",
53+
signingProperties: {
54+
name: "X-Api-Key",
55+
in: HttpApiKeyAuthLocation.HEADER,
56+
scheme: undefined,
57+
},
4758
};
4859
}
4960

@@ -59,7 +70,7 @@ export const defaultXYZServiceHttpAuthSchemeProvider: XYZServiceHttpAuthSchemePr
5970
const options: HttpAuthOption[] = [];
6071
switch (authParameters.operation) {
6172
default: {
62-
options.push(createSmithyApiNoAuthHttpAuthOption(authParameters));
73+
options.push(createSmithyApiHttpApiKeyAuthHttpAuthOption(authParameters));
6374
}
6475
}
6576
return options;
@@ -88,6 +99,11 @@ export interface HttpAuthSchemeInputConfig {
8899
* @internal
89100
*/
90101
httpAuthSchemeProvider?: XYZServiceHttpAuthSchemeProvider;
102+
103+
/**
104+
* The API key to use when making requests.
105+
*/
106+
apiKey?: ApiKeyIdentity | ApiKeyIdentityProvider;
91107
}
92108

93109
/**
@@ -113,6 +129,11 @@ export interface HttpAuthSchemeResolvedConfig {
113129
* @internal
114130
*/
115131
readonly httpAuthSchemeProvider: XYZServiceHttpAuthSchemeProvider;
132+
133+
/**
134+
* The API key to use when making requests.
135+
*/
136+
readonly apiKey?: ApiKeyIdentityProvider;
116137
}
117138

118139
/**
@@ -121,7 +142,9 @@ export interface HttpAuthSchemeResolvedConfig {
121142
export const resolveHttpAuthSchemeConfig = <T>(
122143
config: T & HttpAuthSchemeInputConfig
123144
): T & HttpAuthSchemeResolvedConfig => {
145+
const apiKey = memoizeIdentityProvider(config.apiKey, isIdentityExpired, doesIdentityRequireRefresh);
124146
return Object.assign(config, {
125147
authSchemePreference: normalizeProvider(config.authSchemePreference ?? []),
148+
apiKey,
126149
}) as T & HttpAuthSchemeResolvedConfig;
127150
};

private/my-local-model-schema/src/endpoint/EndpointParameters.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ export interface ClientInputEndpointParameters {
88
clientContextParams?: {
99
apiKey?: string | undefined | Provider<string | undefined>;
1010
customParam?: string | undefined | Provider<string | undefined>;
11+
enableFeature?: boolean | undefined | Provider<boolean | undefined>;
12+
debugMode?: boolean | undefined | Provider<boolean | undefined>;
13+
nonConflictingParam?: string | undefined | Provider<string | undefined>;
1114
};
1215
endpoint?: string | Provider<string> | Endpoint | Provider<Endpoint> | EndpointV2 | Provider<EndpointV2>;
13-
apiKey?: string | undefined | Provider<string | undefined>;
1416
customParam?: string | undefined | Provider<string | undefined>;
17+
enableFeature?: boolean | undefined | Provider<boolean | undefined>;
18+
debugMode?: boolean | undefined | Provider<boolean | undefined>;
19+
nonConflictingParam?: string | undefined | Provider<string | undefined>;
1520
}
1621

1722
export type ClientResolvedEndpointParameters = Omit<
@@ -22,23 +27,30 @@ export type ClientResolvedEndpointParameters = Omit<
2227
clientContextParams: {
2328
apiKey?: string | undefined | Provider<string | undefined>;
2429
customParam?: string | undefined | Provider<string | undefined>;
30+
enableFeature?: boolean | undefined | Provider<boolean | undefined>;
31+
debugMode?: boolean | undefined | Provider<boolean | undefined>;
32+
nonConflictingParam?: string | undefined | Provider<string | undefined>;
2533
};
2634
};
2735

2836
/**
2937
* @internal
3038
*/
3139
const clientContextParamDefaults = {
32-
apiKey: "default-api-key",
40+
nonConflictingParam: "non-conflict-default",
3341
customParam: "default-custom-value",
42+
debugMode: false,
43+
enableFeature: true,
3444
} as const;
3545

3646
export const resolveClientEndpointParameters = <T>(
3747
options: T & ClientInputEndpointParameters
3848
): T & ClientResolvedEndpointParameters => {
3949
return Object.assign(options, {
40-
apiKey: options.apiKey ?? "default-api-key",
4150
customParam: options.customParam ?? "default-custom-value",
51+
enableFeature: options.enableFeature ?? true,
52+
debugMode: options.debugMode ?? false,
53+
nonConflictingParam: options.nonConflictingParam ?? "non-conflict-default",
4254
defaultSigningName: "",
4355
clientContextParams: Object.assign(clientContextParamDefaults, options.clientContextParams),
4456
});
@@ -48,8 +60,12 @@ export const resolveClientEndpointParameters = <T>(
4860
* @internal
4961
*/
5062
export const commonParams = {
51-
apiKey: { type: "clientContextParams", name: "apiKey" },
63+
ApiKey: { type: "clientContextParams", name: "apiKey" },
64+
nonConflictingParam: { type: "clientContextParams", name: "nonConflictingParam" },
65+
region: { type: "clientContextParams", name: "region" },
5266
customParam: { type: "clientContextParams", name: "customParam" },
67+
debugMode: { type: "clientContextParams", name: "debugMode" },
68+
enableFeature: { type: "clientContextParams", name: "enableFeature" },
5369
endpoint: { type: "builtInParams", name: "endpoint" },
5470
} as const;
5571

@@ -58,6 +74,10 @@ export const commonParams = {
5874
*/
5975
export interface EndpointParameters extends __EndpointParameters {
6076
endpoint: string;
61-
apiKey?: string | undefined;
77+
ApiKey?: string | undefined;
78+
region?: string | undefined;
6279
customParam?: string | undefined;
80+
enableFeature?: boolean | undefined;
81+
debugMode?: boolean | undefined;
82+
nonConflictingParam?: string | undefined;
6383
}

private/my-local-model-schema/src/endpoint/endpointResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ruleSet } from "./ruleset";
77

88
const cache = new EndpointCache({
99
size: 50,
10-
params: ["endpoint"],
10+
params: ["ApiKey", "endpoint"],
1111
});
1212

1313
/**

private/my-local-model-schema/src/endpoint/ruleset.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,62 @@ export const ruleSet: RuleSetObject = {
1010
documentation: "The endpoint used to send the request.",
1111
type: "String",
1212
},
13-
apiKey: {
13+
ApiKey: {
14+
required: false,
15+
documentation: "ApiKey",
1416
type: "String",
15-
required: true,
16-
default: "default-api-key",
17-
documentation: "API key for service authentication",
17+
},
18+
region: {
19+
type: "String",
20+
required: false,
21+
documentation: "AWS region",
1822
},
1923
customParam: {
2024
type: "String",
2125
required: true,
2226
default: "default-custom-value",
2327
documentation: "Custom parameter for testing",
2428
},
29+
enableFeature: {
30+
type: "Boolean",
31+
required: true,
32+
default: true,
33+
documentation: "Feature toggle with default",
34+
},
35+
debugMode: {
36+
type: "Boolean",
37+
required: true,
38+
default: false,
39+
documentation: "Debug mode with default",
40+
},
41+
nonConflictingParam: {
42+
type: "String",
43+
required: true,
44+
default: "non-conflict-default",
45+
documentation: "Non-conflicting with default",
46+
},
2547
},
2648
rules: [
49+
{
50+
conditions: [
51+
{
52+
fn: "isSet",
53+
argv: [
54+
{
55+
ref: "ApiKey",
56+
},
57+
],
58+
},
59+
],
60+
endpoint: {
61+
url: "{endpoint}",
62+
properties: {},
63+
headers: {
64+
"x-api-key": ["{ApiKey}"],
65+
},
66+
},
67+
type: "endpoint",
68+
},
2769
{
2870
conditions: [],
2971
endpoint: {

private/my-local-model-schema/src/runtimeConfig.shared.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// smithy-typescript generated code
2-
import { NoAuthSigner } from "@smithy/core";
2+
import { HttpApiKeyAuthSigner } from "@smithy/core";
33
import { SmithyRpcV2CborProtocol } from "@smithy/core/cbor";
44
import { NoOpLogger } from "@smithy/smithy-client";
55
import type { IdentityProviderConfig } from "@smithy/types";
@@ -25,10 +25,9 @@ export const getRuntimeConfig = (config: XYZServiceClientConfig) => {
2525
httpAuthSchemeProvider: config?.httpAuthSchemeProvider ?? defaultXYZServiceHttpAuthSchemeProvider,
2626
httpAuthSchemes: config?.httpAuthSchemes ?? [
2727
{
28-
schemeId: "smithy.api#noAuth",
29-
identityProvider: (ipc: IdentityProviderConfig) =>
30-
ipc.getIdentityProvider("smithy.api#noAuth") || (async () => ({})),
31-
signer: new NoAuthSigner(),
28+
schemeId: "smithy.api#httpApiKeyAuth",
29+
identityProvider: (ipc: IdentityProviderConfig) => ipc.getIdentityProvider("smithy.api#httpApiKeyAuth"),
30+
signer: new HttpApiKeyAuthSigner(),
3231
},
3332
],
3433
logger: config?.logger ?? new NoOpLogger(),

private/my-local-model/src/XYZServiceClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ export class XYZServiceClient extends __Client<
253253
getHttpAuthSchemeEndpointRuleSetPlugin(this.config, {
254254
httpAuthSchemeParametersProvider: defaultXYZServiceHttpAuthSchemeParametersProvider,
255255
identityProviderConfigProvider: async (config: XYZServiceClientResolvedConfig) =>
256-
new DefaultIdentityProviderConfig({}),
256+
new DefaultIdentityProviderConfig({
257+
"smithy.api#httpApiKeyAuth": config.apiKey,
258+
}),
257259
})
258260
);
259261
this.middlewareStack.use(getHttpSigningPlugin(this.config));

0 commit comments

Comments
 (0)