Skip to content

Commit 7a7c84d

Browse files
authored
fix: type transforms account for no-args operation methods (#1262)
1 parent fc3af97 commit 7a7c84d

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

.changeset/cold-ligers-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/types": minor
3+
---
4+
5+
fix type transforms for method signatures with no arguments

packages/types/src/client.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ export interface InvokeMethod<InputType extends object, OutputType extends Metad
5353
(input: InputType, options?: any, cb?: (err: any, data?: OutputType) => void): Promise<OutputType> | void;
5454
}
5555

56+
/**
57+
* @public
58+
*
59+
* Signature that appears on aggregated clients' methods when argument is optional.
60+
*/
61+
export interface InvokeMethodOptionalArgs<InputType extends object, OutputType extends MetadataBearer> {
62+
(): Promise<OutputType>;
63+
(input: InputType, options?: any): Promise<OutputType>;
64+
(input: InputType, cb: (err: any, data?: OutputType) => void): void;
65+
(input: InputType, options: any, cb: (err: any, data?: OutputType) => void): void;
66+
(input: InputType, options?: any, cb?: (err: any, data?: OutputType) => void): Promise<OutputType> | void;
67+
}
68+
5669
/**
5770
* A general interface for service clients, idempotent to browser or node clients
5871
* This type corresponds to SmithyClient(https://github.com/aws/aws-sdk-js-v3/blob/main/packages/smithy-client/src/client.ts).

packages/types/src/transform/no-undefined.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type A = {
4949
putObject(args: MyInput, options?: HttpHandlerOptions): Promise<MyOutput>;
5050
putObject(args: MyInput, cb: (err: any, data?: MyOutput) => void): void;
5151
putObject(args: MyInput, options: HttpHandlerOptions, cb: (err: any, data?: MyOutput) => void): void;
52+
53+
listObjects(): Promise<MyOutput>;
54+
listObjects(args: MyInput, options?: HttpHandlerOptions): Promise<MyOutput>;
55+
listObjects(args: MyInput, cb: (err: any, data?: MyOutput) => void): void;
56+
listObjects(args: MyInput, options: HttpHandlerOptions, cb: (err: any, data?: MyOutput) => void): void;
5257
}
5358

5459
{
@@ -92,4 +97,20 @@ type A = {
9297
const assert5: Exact<typeof output.r.b, number> = true as const;
9398
const assert6: Exact<typeof output.r.c, string | number> = true as const;
9499
}
100+
101+
{
102+
// Handles methods with optionally zero args.
103+
const c = (null as unknown) as AssertiveClient<MyClient>;
104+
const list = c.listObjects();
105+
const output = (null as unknown) as Awaited<typeof list>;
106+
107+
const assert1: Exact<typeof output.a, string | undefined> = true as const;
108+
const assert2: Exact<typeof output.b, number | undefined> = true as const;
109+
const assert3: Exact<typeof output.c, string | number | undefined> = true as const;
110+
if (output.r) {
111+
const assert4: Exact<typeof output.r.a, string | undefined> = true as const;
112+
const assert5: Exact<typeof output.r.b, number | undefined> = true as const;
113+
const assert6: Exact<typeof output.r.c, string | number | undefined> = true as const;
114+
}
115+
}
95116
}

packages/types/src/transform/no-undefined.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InvokeFunction, InvokeMethod } from "../client";
1+
import type { InvokeFunction, InvokeMethod, InvokeMethodOptionalArgs } from "../client";
22

33
/**
44
* @public
@@ -59,8 +59,10 @@ export type RecursiveRequired<T> = T extends Function
5959
*/
6060
type NarrowClientIOTypes<ClientType extends object> = {
6161
[key in keyof ClientType]: [ClientType[key]] extends [
62-
InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>
62+
InvokeMethodOptionalArgs<infer FunctionInputTypes, infer FunctionOutputTypes>
6363
]
64+
? InvokeMethodOptionalArgs<NoUndefined<FunctionInputTypes>, NoUndefined<FunctionOutputTypes>>
65+
: [ClientType[key]] extends [InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>]
6466
? InvokeFunction<NoUndefined<InputTypes>, NoUndefined<OutputTypes>, ConfigType>
6567
: [ClientType[key]] extends [InvokeMethod<infer FunctionInputTypes, infer FunctionOutputTypes>]
6668
? InvokeMethod<NoUndefined<FunctionInputTypes>, NoUndefined<FunctionOutputTypes>>
@@ -74,8 +76,10 @@ type NarrowClientIOTypes<ClientType extends object> = {
7476
*/
7577
type UncheckedClientOutputTypes<ClientType extends object> = {
7678
[key in keyof ClientType]: [ClientType[key]] extends [
77-
InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>
79+
InvokeMethodOptionalArgs<infer FunctionInputTypes, infer FunctionOutputTypes>
7880
]
81+
? InvokeMethodOptionalArgs<NoUndefined<FunctionInputTypes>, RecursiveRequired<FunctionOutputTypes>>
82+
: [ClientType[key]] extends [InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>]
7983
? InvokeFunction<NoUndefined<InputTypes>, RecursiveRequired<OutputTypes>, ConfigType>
8084
: [ClientType[key]] extends [InvokeMethod<infer FunctionInputTypes, infer FunctionOutputTypes>]
8185
? InvokeMethod<NoUndefined<FunctionInputTypes>, RecursiveRequired<FunctionOutputTypes>>

0 commit comments

Comments
 (0)