Skip to content

Commit

Permalink
fix(middleware-flexible-checksums): use union for new config types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Sep 17, 2024
1 parent bcfee78 commit c43103f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = {
export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors<RequestChecksumCalculation> = {
environmentVariableSelector: (env) =>
stringUnionSelector(env, ENV_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation, SelectorType.ENV),
configFileSelector: (profile) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string> = {
export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors<ResponseChecksumValidation> = {
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,
};
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>;
requestChecksumCalculation?: RequestChecksumCalculation | Provider<RequestChecksumCalculation>;

/**
* Determines when checksum validation will be performed on response payloads.
*/
responseChecksumValidation?: string | Provider<string>;
responseChecksumValidation?: ResponseChecksumValidation | Provider<ResponseChecksumValidation>;
}

export interface FlexibleChecksumsResolvedConfig {
requestChecksumCalculation: Provider<string>;
responseChecksumValidation: Provider<string>;
requestChecksumCalculation: Provider<RequestChecksumCalculation>;
responseChecksumValidation: Provider<ResponseChecksumValidation>;
}

export const resolveFlexibleChecksumsConfig = <T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ export enum SelectorType {
*
* @internal
*/
export const stringUnionSelector = (
export const stringUnionSelector = <U extends object, K extends keyof U>(
obj: Record<string, string | undefined>,
key: string,
union: Record<string, string>,
union: U,
type: SelectorType
) => {
): U[K] | undefined => {
if (!(key in obj)) return undefined;

const value = obj[key]!.toUpperCase();
if (!Object.values(union).includes(value)) {
throw new TypeError(`Cannot load ${type} '${key}'. Expected one of ${Object.values(union)}, got '${obj[key]}'.`);
}

return value;
return value as U[K];
};

0 comments on commit c43103f

Please sign in to comment.