From a66eda91fd1374692e3e04108c847cbc7a24cea7 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 17 Sep 2024 19:36:11 +0000 Subject: [PATCH 1/3] fix(middleware-flexible-checksums): use union for new config types --- .../src/resolveFlexibleChecksumsConfig.spec.ts | 11 ++++++++--- .../src/resolveFlexibleChecksumsConfig.ts | 15 ++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) 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 = ( From 226c7313aa72fa0ac7006cd84e16bac6a404ef3f Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:09:54 +0000 Subject: [PATCH 2/3] chore: use union type on LoadedConfigSelectors --- ...NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts | 4 +++- ...NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts | 10 ++++++---- .../src/stringUnionSelector.ts | 2 +- 3 files changed, 10 insertions(+), 6 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..f0c334ec0232 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,10 +6,12 @@ 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) => + // @ts-expect-error Type 'string | undefined' is not assignable to type 'RequestChecksumCalculation | undefined'. stringUnionSelector(env, ENV_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.ENV), configFileSelector: (profile) => + // @ts-expect-error Type 'string | undefined' is not assignable to type 'RequestChecksumCalculation | undefined'. stringUnionSelector(profile, CONFIG_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.CONFIG), default: DEFAULT_REQUEST_CHECKSUM_CALCULATION, }; 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..d283a2509e2b 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,17 @@ 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), + // @ts-expect-error Type 'string | undefined' is not assignable to type 'ResponseChecksumValidation | undefined'. + stringUnionSelector(env, ENV_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.ENV), configFileSelector: (profile) => - stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation, SelectorType.CONFIG), + // @ts-expect-error Type 'string | undefined' is not assignable to type 'ResponseChecksumValidation | undefined'. + stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.CONFIG), default: DEFAULT_RESPONSE_CHECKSUM_VALIDATION, }; diff --git a/packages/middleware-flexible-checksums/src/stringUnionSelector.ts b/packages/middleware-flexible-checksums/src/stringUnionSelector.ts index 2ad49baa50e8..c17c6eba97b1 100644 --- a/packages/middleware-flexible-checksums/src/stringUnionSelector.ts +++ b/packages/middleware-flexible-checksums/src/stringUnionSelector.ts @@ -15,7 +15,7 @@ export const stringUnionSelector = ( key: string, union: Record, type: SelectorType -) => { +): (typeof union)[keyof typeof union] | undefined => { if (!(key in obj)) return undefined; const value = obj[key]!.toUpperCase(); From 3ced843d9ae22f546ba13639db920797050bcdf5 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 17 Sep 2024 20:18:39 +0000 Subject: [PATCH 3/3] fix: use generic in stringUnionSelector --- .../NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS.ts | 2 -- .../NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS.ts | 2 -- .../src/stringUnionSelector.ts | 8 ++++---- 3 files changed, 4 insertions(+), 8 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 f0c334ec0232..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 @@ -8,10 +8,8 @@ export const CONFIG_REQUEST_CHECKSUM_CALCULATION = "request_checksum_calculation export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors = { environmentVariableSelector: (env) => - // @ts-expect-error Type 'string | undefined' is not assignable to type 'RequestChecksumCalculation | undefined'. stringUnionSelector(env, ENV_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.ENV), configFileSelector: (profile) => - // @ts-expect-error Type 'string | undefined' is not assignable to type 'RequestChecksumCalculation | undefined'. stringUnionSelector(profile, CONFIG_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.CONFIG), default: DEFAULT_REQUEST_CHECKSUM_CALCULATION, }; 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 d283a2509e2b..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 @@ -8,10 +8,8 @@ export const CONFIG_RESPONSE_CHECKSUM_VALIDATION = "response_checksum_validation export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors = { environmentVariableSelector: (env) => - // @ts-expect-error Type 'string | undefined' is not assignable to type 'ResponseChecksumValidation | undefined'. stringUnionSelector(env, ENV_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.ENV), configFileSelector: (profile) => - // @ts-expect-error Type 'string | undefined' is not assignable to type 'ResponseChecksumValidation | undefined'. stringUnionSelector(profile, CONFIG_RESPONSE_CHECKSUM_VALIDATION, ResponseChecksumValidation, SelectorType.CONFIG), default: DEFAULT_RESPONSE_CHECKSUM_VALIDATION, }; diff --git a/packages/middleware-flexible-checksums/src/stringUnionSelector.ts b/packages/middleware-flexible-checksums/src/stringUnionSelector.ts index c17c6eba97b1..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 -): (typeof union)[keyof typeof union] | undefined => { +): 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]; };