import {
EndpointBearer,
HandlerExecutionContext,
RequestSerializer,
SerializeHandler,
SerializeHandlerArguments,
SerializeHandlerOutput,
SerializeMiddleware,
} from "@smithy/types";
import type { V1OrV2Endpoint } from "./serdePlugin";
export const serializerMiddleware = (
options: V1OrV2Endpoint,
serializer: RequestSerializer
): SerializeMiddleware => (
next: SerializeHandler,
context: HandlerExecutionContext
): SerializeHandler => async (
args: SerializeHandlerArguments
): Promise> => {
const endpoint =
context.endpointV2?.url && options.urlParser
? async () => options.urlParser!(context.endpointV2!.url as URL)
: options.endpoint!;
if (!endpoint) {
throw new Error("No valid endpoint provider available.");
}
const request = await serializer(args.input, { ...options, endpoint } as RuntimeUtils);
return next({
...args,
request,
});
};