Skip to content

Commit

Permalink
transform inputs for platform specific type helpers (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Oct 19, 2023
1 parent 901cb6c commit 5e9fd6c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-flies-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/types": minor
---

transform inputs for env specific type helpers
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import type { Client } from "../client";
import type { HttpHandlerOptions } from "../http";
import type { MetadataBearer } from "../response";
import type { SdkStream } from "../serde";
import type {
NodeJsRuntimeStreamingBlobPayloadInputTypes,
StreamingBlobPayloadInputTypes,
} from "../streaming-payload/streaming-blob-payload-input-types";
import type { StreamingBlobPayloadOutputTypes } from "../streaming-payload/streaming-blob-payload-output-types";
import type { BrowserClient, NodeJsClient } from "./client-payload-blob-type-narrow";
import type { Exact } from "./exact";
import type { Transform } from "./type-transform";

// it should narrow operational methods and the generic send method

type MyInput = Partial<{
a: boolean;
b: boolean | number;
c: boolean | number | string;
body?: StreamingBlobPayloadInputTypes;
}>;

type MyOutput = {
Expand All @@ -37,6 +43,19 @@ interface MyClient extends Client<MyInput, MyOutput, MyConfig> {
putObject(args: MyInput, options: HttpHandlerOptions, cb: (err: any, data?: MyOutput) => void): void;
}

{
interface NodeJsMyClient extends NodeJsClient<MyClient> {}
const mockClient = (null as unknown) as NodeJsMyClient;
type Input = Parameters<typeof mockClient.getObject>[0];

const assert1: Exact<
Input,
Transform<MyInput, StreamingBlobPayloadInputTypes | undefined, NodeJsRuntimeStreamingBlobPayloadInputTypes>
> = true as const;

const assert2: Exact<Input, MyInput> = false as const;
}

{
interface NodeJsMyClient extends NodeJsClient<MyClient> {}
const mockClient = (null as unknown) as NodeJsMyClient;
Expand Down
46 changes: 42 additions & 4 deletions packages/types/src/transform/client-payload-blob-type-narrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import type { ClientHttp2Stream } from "http2";
import type { InvokeFunction, InvokeMethod } from "../client";
import type { HttpHandlerOptions } from "../http";
import type { SdkStream } from "../serde";
import type {
BrowserRuntimeStreamingBlobPayloadInputTypes,
NodeJsRuntimeStreamingBlobPayloadInputTypes,
StreamingBlobPayloadInputTypes,
} from "../streaming-payload/streaming-blob-payload-input-types";
import type { NarrowedInvokeFunction, NarrowedInvokeMethod } from "./client-method-transforms";
import type { Transform } from "./type-transform";

/**
* @public
Expand All @@ -20,15 +26,17 @@ import type { NarrowedInvokeFunction, NarrowedInvokeMethod } from "./client-meth
* const client = new YourClient({}) as NodeJsClient<YourClient>;
* ```
*/
export type NodeJsClient<ClientType extends object> = NarrowPayloadBlobOutputType<
export type NodeJsClient<ClientType extends object> = NarrowPayloadBlobTypes<
NodeJsRuntimeStreamingBlobPayloadInputTypes,
SdkStream<IncomingMessage>,
ClientType
>;
/**
* @public
* Variant of NodeJsClient for node:http2.
*/
export type NodeJsHttp2Client<ClientType extends object> = NarrowPayloadBlobOutputType<
export type NodeJsHttp2Client<ClientType extends object> = NarrowPayloadBlobTypes<
NodeJsRuntimeStreamingBlobPayloadInputTypes,
SdkStream<ClientHttp2Stream>,
ClientType
>;
Expand All @@ -47,7 +55,8 @@ export type NodeJsHttp2Client<ClientType extends object> = NarrowPayloadBlobOutp
* const client = new YourClient({}) as BrowserClient<YourClient>;
* ```
*/
export type BrowserClient<ClientType extends object> = NarrowPayloadBlobOutputType<
export type BrowserClient<ClientType extends object> = NarrowPayloadBlobTypes<
BrowserRuntimeStreamingBlobPayloadInputTypes,
SdkStream<ReadableStream>,
ClientType
>;
Expand All @@ -56,14 +65,17 @@ export type BrowserClient<ClientType extends object> = NarrowPayloadBlobOutputTy
*
* Variant of BrowserClient for XMLHttpRequest.
*/
export type BrowserXhrClient<ClientType extends object> = NarrowPayloadBlobOutputType<
export type BrowserXhrClient<ClientType extends object> = NarrowPayloadBlobTypes<
BrowserRuntimeStreamingBlobPayloadInputTypes,
SdkStream<ReadableStream | Blob>,
ClientType
>;

/**
* @public
*
* @deprecated use NarrowPayloadBlobTypes<I, O, ClientType>.
*
* Narrow a given Client's blob payload outputs to the given type T.
*/
export type NarrowPayloadBlobOutputType<T, ClientType extends object> = {
Expand All @@ -75,3 +87,29 @@ export type NarrowPayloadBlobOutputType<T, ClientType extends object> = {
? NarrowedInvokeMethod<T, HttpHandlerOptions, FunctionInputTypes, FunctionOutputTypes>
: ClientType[key];
};

/**
* @public
*
* Narrow a Client's blob payload input and output types to I and O.
*/
export type NarrowPayloadBlobTypes<I, O, ClientType extends object> = {
[key in keyof ClientType]: [ClientType[key]] extends [
InvokeFunction<infer InputTypes, infer OutputTypes, infer ConfigType>
]
? NarrowedInvokeFunction<
O,
HttpHandlerOptions,
Transform<InputTypes, StreamingBlobPayloadInputTypes | undefined, I>,
OutputTypes,
ConfigType
>
: [ClientType[key]] extends [InvokeMethod<infer FunctionInputTypes, infer FunctionOutputTypes>]
? NarrowedInvokeMethod<
O,
HttpHandlerOptions,
Transform<FunctionInputTypes, StreamingBlobPayloadInputTypes | undefined, I>,
FunctionOutputTypes
>
: ClientType[key];
};

0 comments on commit 5e9fd6c

Please sign in to comment.