From c43103fb71e2894db3c895ff3c8ba25ba07e4fbd Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Tue, 17 Sep 2024 13:30:19 -0700 Subject: [PATCH] fix(middleware-flexible-checksums): use union for new config types (#6489) --- ...REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts | 2 +- ...RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts | 8 ++++---- .../src/resolveFlexibleChecksumsConfig.spec.ts | 11 ++++++++--- .../src/resolveFlexibleChecksumsConfig.ts | 15 ++++++++++----- .../src/stringUnionSelector.ts | 8 ++++---- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/packages/middleware-flexible-checksums/src/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts b/packages/middleware-flexible-checksums/src/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts index cf21cfcc5a86..8fd3ff0804b0 100644 --- a/packages/middleware-flexible-checksums/src/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts +++ b/packages/middleware-flexible-checksums/src/NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts @@ -6,7 +6,7 @@ import { SelectorType, stringUnionSelector } from "./stringUnionSelector"; export const ENV_REQUEST_CHECKSUM_CALCULATION = "AWS_REQUEST_CHECKSUM_CALCULATION"; export const CONFIG_REQUEST_CHECKSUM_CALCULATION = "request_checksum_calculation"; -export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors = { +export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors = { environmentVariableSelector: (env) => stringUnionSelector(env, ENV_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.ENV), configFileSelector: (profile) => diff --git a/packages/middleware-flexible-checksums/src/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts b/packages/middleware-flexible-checksums/src/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts index fcf839559c10..9fa568b33438 100644 --- a/packages/middleware-flexible-checksums/src/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts +++ b/packages/middleware-flexible-checksums/src/NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts @@ -1,15 +1,15 @@ import { LoadedConfigSelectors } from "@smithy/node-config-provider"; -import { DEFAULT_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation } from "./constants"; +import { DEFAULT_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation } from "./constants"; import { SelectorType, stringUnionSelector } from "./stringUnionSelector"; export const ENV_RESPONSE_CHECKSUM_VALIDATION = "AWS_RESPONSE_CHECKSUM_VALIDATION"; export const CONFIG_RESPONSE_CHECKSUM_VALIDATION = "response_checksum_validation"; -export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors = { +export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors = { environmentVariableSelector: (env) => - stringUnionSelector(env, ENV_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation, SelectorType.ENV), + stringUnionSelector(env, ENV_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.ENV), configFileSelector: (profile) => - stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation, SelectorType.CONFIG), + stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.CONFIG), default: DEFAULT_RESPONSE_CHECKSUM_VALIDATION, }; diff --git a/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.spec.ts b/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.spec.ts index 7bb1cd64f0d6..59d909a607f3 100644 --- a/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.spec.ts +++ b/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.spec.ts @@ -1,6 +1,11 @@ import { normalizeProvider } from "@smithy/util-middleware"; -import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, DEFAULT_RESPONSE_CHECKSUM_VALIDATION } from "./constants"; +import { + DEFAULT_REQUEST_CHECKSUM_CALCULATION, + DEFAULT_RESPONSE_CHECKSUM_VALIDATION, + RequestChecksumCalculation, + ResponseChecksumValidation, +} from "./constants"; import { resolveFlexibleChecksumsConfig } from "./resolveFlexibleChecksumsConfig"; jest.mock("@smithy/util-middleware"); @@ -25,8 +30,8 @@ describe(resolveFlexibleChecksumsConfig.name, () => { it("normalizes client checksums configuration", () => { const mockInput = { - requestChecksumCalculation: "WHEN_REQUIRED", - responseChecksumValidation: "WHEN_REQUIRED", + requestChecksumCalculation: RequestChecksumCalculation.WHEN_REQUIRED, + responseChecksumValidation: ResponseChecksumValidation.WHEN_REQUIRED, }; const resolvedConfig = resolveFlexibleChecksumsConfig(mockInput); expect(resolvedConfig).toEqual(mockInput); diff --git a/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.ts b/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.ts index b57f894b23fb..57cfd22e7ed9 100644 --- a/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.ts +++ b/packages/middleware-flexible-checksums/src/resolveFlexibleChecksumsConfig.ts @@ -1,23 +1,28 @@ import { Provider } from "@smithy/types"; import { normalizeProvider } from "@smithy/util-middleware"; -import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, DEFAULT_RESPONSE_CHECKSUM_VALIDATION } from "./constants"; +import { + DEFAULT_REQUEST_CHECKSUM_CALCULATION, + DEFAULT_RESPONSE_CHECKSUM_VALIDATION, + RequestChecksumCalculation, + ResponseChecksumValidation, +} from "./constants"; export interface FlexibleChecksumsInputConfig { /** * Determines when a checksum will be calculated for request payloads. */ - requestChecksumCalculation?: string | Provider; + requestChecksumCalculation?: RequestChecksumCalculation | Provider; /** * Determines when checksum validation will be performed on response payloads. */ - responseChecksumValidation?: string | Provider; + responseChecksumValidation?: ResponseChecksumValidation | Provider; } export interface FlexibleChecksumsResolvedConfig { - requestChecksumCalculation: Provider; - responseChecksumValidation: Provider; + requestChecksumCalculation: Provider; + responseChecksumValidation: Provider; } export const resolveFlexibleChecksumsConfig = ( diff --git a/packages/middleware-flexible-checksums/src/stringUnionSelector.ts b/packages/middleware-flexible-checksums/src/stringUnionSelector.ts index 2ad49baa50e8..4a557eded5c3 100644 --- a/packages/middleware-flexible-checksums/src/stringUnionSelector.ts +++ b/packages/middleware-flexible-checksums/src/stringUnionSelector.ts @@ -10,12 +10,12 @@ export enum SelectorType { * * @internal */ -export const stringUnionSelector = ( +export const stringUnionSelector = ( obj: Record, key: string, - union: Record, + union: U, type: SelectorType -) => { +): U[K] | undefined => { if (!(key in obj)) return undefined; const value = obj[key]!.toUpperCase(); @@ -23,5 +23,5 @@ export const stringUnionSelector = ( throw new TypeError(`Cannot load ${type} '${key}'. Expected one of ${Object.values(union)}, got '${obj[key]}'.`); } - return value; + return value as U[K]; };