Skip to content

Commit

Permalink
fix: type transforms account for no-args operation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed May 8, 2024
1 parent fc3af97 commit 08b6228
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-ligers-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/types": minor
---

fix type transforms for method signatures with no arguments
13 changes: 13 additions & 0 deletions packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export interface InvokeMethod<InputType extends object, OutputType extends Metad
(input: InputType, options?: any, cb?: (err: any, data?: OutputType) => void): Promise<OutputType> | void;
}

/**
* @public
*
* Signature that appears on aggregated clients' methods when argument is optional.
*/
export interface InvokeMethodOptionalArgs<InputType extends object, OutputType extends MetadataBearer> {
(): Promise<OutputType>;
(input: InputType, options?: any): Promise<OutputType>;
(input: InputType, cb: (err: any, data?: OutputType) => void): void;
(input: InputType, options: any, cb: (err: any, data?: OutputType) => void): void;
(input: InputType, options?: any, cb?: (err: any, data?: OutputType) => void): Promise<OutputType> | void;
}

/**
* A general interface for service clients, idempotent to browser or node clients
* This type corresponds to SmithyClient(https://github.com/aws/aws-sdk-js-v3/blob/main/packages/smithy-client/src/client.ts).
Expand Down
21 changes: 21 additions & 0 deletions packages/types/src/transform/no-undefined.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type A = {
putObject(args: MyInput, options?: HttpHandlerOptions): Promise<MyOutput>;
putObject(args: MyInput, cb: (err: any, data?: MyOutput) => void): void;
putObject(args: MyInput, options: HttpHandlerOptions, cb: (err: any, data?: MyOutput) => void): void;

listObjects(): Promise<MyOutput>;
listObjects(args: MyInput, options?: HttpHandlerOptions): Promise<MyOutput>;
listObjects(args: MyInput, cb: (err: any, data?: MyOutput) => void): void;
listObjects(args: MyInput, options: HttpHandlerOptions, cb: (err: any, data?: MyOutput) => void): void;
}

{
Expand Down Expand Up @@ -92,4 +97,20 @@ type A = {
const assert5: Exact<typeof output.r.b, number> = true as const;
const assert6: Exact<typeof output.r.c, string | number> = true as const;
}

{
// Handles methods with optionally zero args.
const c = (null as unknown) as AssertiveClient<MyClient>;
const list = c.listObjects();
const output = (null as unknown) as Awaited<typeof list>;

const assert1: Exact<typeof output.a, string | undefined> = true as const;
const assert2: Exact<typeof output.b, number | undefined> = true as const;
const assert3: Exact<typeof output.c, string | number | undefined> = true as const;
if (output.r) {
const assert4: Exact<typeof output.r.a, string | undefined> = true as const;
const assert5: Exact<typeof output.r.b, number | undefined> = true as const;
const assert6: Exact<typeof output.r.c, string | number | undefined> = true as const;
}
}
}
10 changes: 7 additions & 3 deletions packages/types/src/transform/no-undefined.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { InvokeFunction, InvokeMethod } from "../client";
import type { InvokeFunction, InvokeMethod, InvokeMethodOptionalArgs } from "../client";

/**
* @public
Expand Down Expand Up @@ -59,8 +59,10 @@ export type RecursiveRequired<T> = T extends Function
*/
type NarrowClientIOTypes<ClientType extends object> = {
[key in keyof ClientType]: [ClientType[key]] extends [
InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>
InvokeMethodOptionalArgs<infer FunctionInputTypes, infer FunctionOutputTypes>
]
? InvokeMethodOptionalArgs<NoUndefined<FunctionInputTypes>, NoUndefined<FunctionOutputTypes>>
: [ClientType[key]] extends [InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>]
? InvokeFunction<NoUndefined<InputTypes>, NoUndefined<OutputTypes>, ConfigType>
: [ClientType[key]] extends [InvokeMethod<infer FunctionInputTypes, infer FunctionOutputTypes>]
? InvokeMethod<NoUndefined<FunctionInputTypes>, NoUndefined<FunctionOutputTypes>>
Expand All @@ -74,8 +76,10 @@ type NarrowClientIOTypes<ClientType extends object> = {
*/
type UncheckedClientOutputTypes<ClientType extends object> = {
[key in keyof ClientType]: [ClientType[key]] extends [
InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>
InvokeMethodOptionalArgs<infer FunctionInputTypes, infer FunctionOutputTypes>
]
? InvokeMethodOptionalArgs<NoUndefined<FunctionInputTypes>, RecursiveRequired<FunctionOutputTypes>>
: [ClientType[key]] extends [InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>]
? InvokeFunction<NoUndefined<InputTypes>, RecursiveRequired<OutputTypes>, ConfigType>
: [ClientType[key]] extends [InvokeMethod<infer FunctionInputTypes, infer FunctionOutputTypes>]
? InvokeMethod<NoUndefined<FunctionInputTypes>, RecursiveRequired<FunctionOutputTypes>>
Expand Down

0 comments on commit 08b6228

Please sign in to comment.