From 32e3f6ffe2af1e8ab670dd40e76efa6ab883f28d Mon Sep 17 00:00:00 2001 From: George Fu Date: Thu, 7 Mar 2024 16:51:57 -0500 Subject: [PATCH] fix(types): use SerdeFunctions type as serde plugin input, but call serde functions as type SerdeContext (#1183) * fix(types): use optional endpoint for inputs to serializer middleware * docs: add comments describing cast of SerdeFunctions to CommandSerdeContext * fix: add default types (any) for serde plugin * docs: remove code comments that no longer apply --- .changeset/many-stingrays-bow.md | 5 ++++ .../src/deserializerMiddleware.ts | 20 +++++++++++---- packages/middleware-serde/src/serdePlugin.ts | 13 ++++++---- .../src/serializerMiddleware.ts | 25 +++++++++++++------ 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 .changeset/many-stingrays-bow.md diff --git a/.changeset/many-stingrays-bow.md b/.changeset/many-stingrays-bow.md new file mode 100644 index 00000000000..2e38e8322a3 --- /dev/null +++ b/.changeset/many-stingrays-bow.md @@ -0,0 +1,5 @@ +--- +"@smithy/middleware-serde": patch +--- + +use SerdeFunctions as input type and SerdeContext as resolved type for serde plugin diff --git a/packages/middleware-serde/src/deserializerMiddleware.ts b/packages/middleware-serde/src/deserializerMiddleware.ts index ab4c2cf7b11..4e27d68496d 100644 --- a/packages/middleware-serde/src/deserializerMiddleware.ts +++ b/packages/middleware-serde/src/deserializerMiddleware.ts @@ -5,17 +5,20 @@ import { DeserializeMiddleware, HandlerExecutionContext, ResponseDeserializer, + SerdeContext, SerdeFunctions, } from "@smithy/types"; /** * @internal - * - * 3rd type parameter is deprecated and unused. */ -export const deserializerMiddleware = ( +export const deserializerMiddleware = < + Input extends object = any, + Output extends object = any, + CommandSerdeContext extends SerdeContext = any +>( options: SerdeFunctions, - deserializer: ResponseDeserializer + deserializer: ResponseDeserializer ): DeserializeMiddleware => ( next: DeserializeHandler, context: HandlerExecutionContext @@ -24,7 +27,14 @@ export const deserializerMiddleware = > => { const { response } = await next(args); try { - const parsed = await deserializer(response, options); + /** + * [options] is upgraded from SerdeFunctions to CommandSerdeContext, + * since the generated deserializer expects CommandSerdeContext. + * + * This is okay because options is from the same client's resolved config, + * and the deserializer doesn't need the `endpoint` field. + */ + const parsed = await deserializer(response, options as CommandSerdeContext); return { response, output: parsed as Output, diff --git a/packages/middleware-serde/src/serdePlugin.ts b/packages/middleware-serde/src/serdePlugin.ts index c6db5c247f7..e82de8c3cca 100644 --- a/packages/middleware-serde/src/serdePlugin.ts +++ b/packages/middleware-serde/src/serdePlugin.ts @@ -1,13 +1,13 @@ import { DeserializeHandlerOptions, Endpoint, - EndpointBearer, MetadataBearer, MiddlewareStack, Pluggable, Provider, RequestSerializer, ResponseDeserializer, + SerdeContext, SerdeFunctions, SerializeHandlerOptions, UrlParser, @@ -43,12 +43,15 @@ export type V1OrV2Endpoint = { /** * @internal * - * Note: 2nd type parameter is deprecated and unused. */ -export function getSerdePlugin( +export function getSerdePlugin< + InputType extends object = any, + CommandSerdeContext extends SerdeContext = any, + OutputType extends MetadataBearer = any +>( config: V1OrV2Endpoint & SerdeFunctions, - serializer: RequestSerializer, - deserializer: ResponseDeserializer + serializer: RequestSerializer, + deserializer: ResponseDeserializer ): Pluggable { return { applyToStack: (commandStack: MiddlewareStack) => { diff --git a/packages/middleware-serde/src/serializerMiddleware.ts b/packages/middleware-serde/src/serializerMiddleware.ts index 777bc4f1aa8..f4e2ac12246 100644 --- a/packages/middleware-serde/src/serializerMiddleware.ts +++ b/packages/middleware-serde/src/serializerMiddleware.ts @@ -1,7 +1,9 @@ import { - EndpointBearer, + Endpoint, HandlerExecutionContext, + Provider, RequestSerializer, + SerdeContext, SerdeFunctions, SerializeHandler, SerializeHandlerArguments, @@ -13,19 +15,21 @@ import type { V1OrV2Endpoint } from "./serdePlugin"; /** * @internal - * - * Note: 3rd type parameter is deprecated and unused. */ -export const serializerMiddleware = ( +export const serializerMiddleware = < + Input extends object = any, + Output extends object = any, + CommandSerdeContext extends SerdeContext = any +>( options: V1OrV2Endpoint & SerdeFunctions, - serializer: RequestSerializer + serializer: RequestSerializer ): SerializeMiddleware => ( next: SerializeHandler, context: HandlerExecutionContext ): SerializeHandler => async ( args: SerializeHandlerArguments ): Promise> => { - const endpoint = + const endpoint: Provider = context.endpointV2?.url && options.urlParser ? async () => options.urlParser!(context.endpointV2!.url as URL) : options.endpoint!; @@ -34,7 +38,14 @@ export const serializerMiddleware =