Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middleware-flexible-checksums): use union for new config types #6489

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: the type U[K] is guaranteed by the thrown error. value must be a value of union.

};
Loading