Skip to content

Commit 84bec05

Browse files
authored
feat: allow feature identification on the smithy context (#1424)
* feat: allow feature identification on the smithy context * remove credentials http and imds feature detection from Smithy * add test assertion
1 parent 5c4370a commit 84bec05

File tree

6 files changed

+97
-7
lines changed

6 files changed

+97
-7
lines changed

.changeset/sharp-horses-fry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/types": minor
3+
"@smithy/core": minor
4+
---
5+
6+
add feature identification map to smithy context

packages/core/src/setFeature.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { HandlerExecutionContext } from "@smithy/types";
2+
3+
import { setFeature } from "./setFeature";
4+
5+
describe(setFeature.name, () => {
6+
it("creates the context object path if needed", () => {
7+
const context: HandlerExecutionContext = {};
8+
setFeature(context, "RETRY_MODE_STANDARD", "E");
9+
expect(context).toEqual({
10+
__smithy_context: {
11+
features: {
12+
RETRY_MODE_STANDARD: "E",
13+
},
14+
},
15+
});
16+
});
17+
});

packages/core/src/setFeature.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { HandlerExecutionContext, SmithyFeatures } from "@smithy/types";
2+
3+
/**
4+
* @internal
5+
* Indicates to the request context that a given feature is active.
6+
*
7+
* @param context - handler execution context.
8+
* @param feature - readable name of feature.
9+
* @param value - encoding value of feature. This is required because the
10+
* specification asks the library not to include a runtime lookup of all
11+
* the feature identifiers.
12+
*/
13+
export function setFeature<F extends keyof SmithyFeatures>(
14+
context: HandlerExecutionContext,
15+
feature: F,
16+
value: SmithyFeatures[F]
17+
) {
18+
if (!context.__smithy_context) {
19+
context.__smithy_context = {
20+
features: {},
21+
};
22+
} else if (!context.__smithy_context.features) {
23+
context.__smithy_context.features = {};
24+
}
25+
context.__smithy_context.features![feature] = value;
26+
}

packages/types/src/feature-ids.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @internal
3+
*/
4+
export type SmithyFeatures = Partial<{
5+
RESOURCE_MODEL: "A";
6+
WAITER: "B";
7+
PAGINATOR: "C";
8+
RETRY_MODE_LEGACY: "D";
9+
RETRY_MODE_STANDARD: "E";
10+
RETRY_MODE_ADAPTIVE: "F";
11+
GZIP_REQUEST_COMPRESSION: "L";
12+
PROTOCOL_RPC_V2_CBOR: "M";
13+
ENDPOINT_OVERRIDE: "N";
14+
SIGV4A_SIGNING: "S";
15+
CREDENTIALS_CODE: "e";
16+
}>;

packages/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from "./endpoint";
1111
export * from "./endpoints";
1212
export * from "./eventStream";
1313
export * from "./extensions";
14+
export * from "./feature-ids";
1415
export * from "./http";
1516
export * from "./http/httpHandlerInitialization";
1617
export * from "./identity";

packages/types/src/middleware.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { AuthScheme, HttpAuthDefinition } from "./auth/auth";
2-
import { EndpointV2 } from "./endpoint";
3-
import { Logger } from "./logger";
4-
import { UserAgent } from "./util";
1+
import type { AuthScheme, HttpAuthDefinition } from "./auth/auth";
2+
import type { SelectedHttpAuthScheme } from "./auth/HttpAuthScheme";
3+
import type { Command } from "./command";
4+
import type { EndpointV2 } from "./endpoint";
5+
import type { SmithyFeatures } from "./feature-ids";
6+
import type { Logger } from "./logger";
7+
import type { UserAgent } from "./util";
58

69
/**
710
* @public
@@ -554,6 +557,7 @@ export interface HandlerExecutionContext {
554557
currentAuthConfig?: HttpAuthDefinition;
555558

556559
/**
560+
* @deprecated do not extend this field, it is a carryover from AWS SDKs.
557561
* Used by DynamoDbDocumentClient.
558562
*/
559563
dynamoDbDocumentClientOptions?: Partial<{
@@ -563,10 +567,30 @@ export interface HandlerExecutionContext {
563567

564568
/**
565569
* @internal
566-
* Context for Smithy properties
570+
* Context for Smithy properties.
571+
*/
572+
[SMITHY_CONTEXT_KEY]?: {
573+
service?: string;
574+
operation?: string;
575+
commandInstance?: Command<any, any, any, any, any>;
576+
selectedHttpAuthScheme?: SelectedHttpAuthScheme;
577+
features?: SmithyFeatures;
578+
/**
579+
* @deprecated
580+
* Do not assign arbitrary members to the Smithy Context,
581+
* fields should be explicitly declared here to avoid collisions.
582+
*/
583+
[key: string]: unknown;
584+
};
585+
586+
/**
587+
* @deprecated
588+
* Do not assign arbitrary members to the context, since
589+
* they can interfere with existing functionality.
590+
*
591+
* Additional members should instead be declared on the SMITHY_CONTEXT_KEY
592+
* or other reserved keys.
567593
*/
568-
[SMITHY_CONTEXT_KEY]?: Record<string, unknown>;
569-
570594
[key: string]: any;
571595
}
572596

0 commit comments

Comments
 (0)