Skip to content

Commit 45cd1cd

Browse files
committed
fix(types): decouple V1orV2Endpoint type from EndpointBearer type
1 parent fe3e30a commit 45cd1cd

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

packages/middleware-serde/src/serdePlugin.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MetadataBearer,
66
MiddlewareStack,
77
Pluggable,
8+
Provider,
89
RequestSerializer,
910
ResponseDeserializer,
1011
SerializeHandlerOptions,
@@ -30,22 +31,22 @@ export const serializerMiddlewareOption: SerializeHandlerOptions = {
3031

3132
// Type the modifies the EndpointBearer to make it compatible with Endpoints 2.0 change.
3233
// Must be removed after all clients has been onboard the Endpoints 2.0
33-
export type V1OrV2Endpoint<T extends EndpointBearer> = T & {
34+
export type V1OrV2Endpoint = {
35+
// for v2
3436
urlParser?: UrlParser;
37+
38+
// for v1
39+
endpoint?: Provider<Endpoint>;
3540
};
3641

37-
export function getSerdePlugin<
38-
InputType extends object,
39-
SerDeContext extends EndpointBearer,
40-
OutputType extends MetadataBearer
41-
>(
42-
config: V1OrV2Endpoint<SerDeContext>,
43-
serializer: RequestSerializer<any, SerDeContext>,
42+
export function getSerdePlugin<InputType extends object, SerDeContext, OutputType extends MetadataBearer>(
43+
config: V1OrV2Endpoint,
44+
serializer: RequestSerializer<any, SerDeContext & EndpointBearer>,
4445
deserializer: ResponseDeserializer<OutputType, any, SerDeContext>
4546
): Pluggable<InputType, OutputType> {
4647
return {
4748
applyToStack: (commandStack: MiddlewareStack<InputType, OutputType>) => {
48-
commandStack.add(deserializerMiddleware(config, deserializer), deserializerMiddlewareOption);
49+
commandStack.add(deserializerMiddleware(config as SerDeContext, deserializer), deserializerMiddlewareOption);
4950
commandStack.add(serializerMiddleware(config, serializer), serializerMiddlewareOption);
5051
},
5152
};

packages/middleware-serde/src/serializerMiddleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import type { V1OrV2Endpoint } from "./serdePlugin";
1212

1313
export const serializerMiddleware =
1414
<Input extends object, Output extends object, RuntimeUtils extends EndpointBearer>(
15-
options: V1OrV2Endpoint<RuntimeUtils>,
15+
options: V1OrV2Endpoint,
1616
serializer: RequestSerializer<any, RuntimeUtils>
1717
): SerializeMiddleware<Input, Output> =>
1818
(next: SerializeHandler<Input, Output>, context: HandlerExecutionContext): SerializeHandler<Input, Output> =>
1919
async (args: SerializeHandlerArguments<Input>): Promise<SerializeHandlerOutput<Output>> => {
2020
const endpoint =
2121
context.endpointV2?.url && options.urlParser
2222
? async () => options.urlParser!(context.endpointV2!.url as URL)
23-
: options.endpoint;
23+
: options.endpoint!;
2424

2525
if (!endpoint) {
2626
throw new Error("No valid endpoint provider available.");
2727
}
2828

29-
const request = await serializer(args.input, { ...options, endpoint });
29+
const request = await serializer(args.input, { ...options, endpoint } as RuntimeUtils);
3030

3131
return next({
3232
...args,

0 commit comments

Comments
 (0)