Skip to content

Commit

Permalink
feat: add pluggable runtime specific config (#404)
Browse files Browse the repository at this point in the history
* feat: support pluggable runtime config

* export runtimeConfig.runtime.ts to manually set the client to be compatible with specific runtime
* get rid of rollup, instead using browser property to swap runtime config
* add endpoint to the serializer utilities and insert it when building a request

* chore: set prettier-vscode as default formatter
  • Loading branch information
AllanZhengYP authored and trivikr committed Jan 3, 2020
1 parent 1493cc3 commit 8be08fc
Show file tree
Hide file tree
Showing 32 changed files with 455 additions and 485 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"editor.tabSize": 2,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
2 changes: 0 additions & 2 deletions clients/node/client-rds-data-node/.npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/coverage/
/docs/
*.ts
!*.d.ts
tsconfig.test.json
*.tsbuildinfo
32 changes: 22 additions & 10 deletions clients/node/client-rds-data-node/RDSDataClient.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { contentLengthPlugin } from "@aws-sdk/middleware-content-length";
import { userAgentPlugin, UserAgentConfig } from "@aws-sdk/middleware-user-agent";
import {
userAgentPlugin,
UserAgentConfig
} from "@aws-sdk/middleware-user-agent";
import { retryPlugin, RetryConfig } from "@aws-sdk/retry-middleware";
import { awsAuthPlugin, AwsAuthConfiguration } from "@aws-sdk/signing-middleware";
import {
awsAuthPlugin,
AwsAuthConfiguration
} from "@aws-sdk/signing-middleware";
import {
RDSDataConfiguration,
RDSDataResolvedConfiguration,
RDSRuntimeConfiguration
RDSDataResolvedConfiguration
} from "./RDSDataConfiguration";
import { RegionConfiguration, EndpointsConfig, ProtocolConfig } from '@aws-sdk/config-resolver';
import { HttpOptions, MetadataBearer } from '@aws-sdk/types';
import { RDSRuntimeConfiguration } from "./runtimeConfig";
import {
RegionConfiguration,
EndpointsConfig,
ProtocolConfig
} from "@aws-sdk/config-resolver";
import { HttpOptions, MetadataBearer } from "@aws-sdk/types";
import { Client as SmithyClient } from "@aws-sdk/smithy-client";

type InputTypesUnion = any;
type OutputTypesUnion = MetadataBearer;

export class RDSDataClient extends SmithyClient<HttpOptions, InputTypesUnion, OutputTypesUnion> {
export class RDSDataClient extends SmithyClient<
HttpOptions,
InputTypesUnion,
OutputTypesUnion
> {
readonly config: RDSDataResolvedConfiguration;

constructor(configuration: RDSDataConfiguration) {
Expand All @@ -36,9 +50,7 @@ export class RDSDataClient extends SmithyClient<HttpOptions, InputTypesUnion, Ou
}

destroy(): void {
if (
typeof this.config.httpHandler.destroy === 'function'
) {
if (typeof this.config.httpHandler.destroy === "function") {
this.config.httpHandler.destroy();
}
}
Expand Down
147 changes: 106 additions & 41 deletions clients/node/client-rds-data-node/RDSDataConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,125 @@
import { defaultProvider as credentialDefaultProvider } from "@aws-sdk/credential-provider-node";
import { Hash } from "@aws-sdk/hash-node";
import { NodeHttpHandler } from "@aws-sdk/node-http-handler";
import { defaultProvider as regionDefaultProvider } from "@aws-sdk/region-provider";
import { parseUrl } from "@aws-sdk/url-parser-node";
import { calculateBodyLength } from "@aws-sdk/util-body-length-node";
import { streamCollector } from '@aws-sdk/stream-collector-node';
import { RestJsonProtocol } from "@aws-sdk/protocol-rest-json";
import { fromUtf8, toUtf8 } from '@aws-sdk/util-utf8-node';
import { fromBase64, toBase64 } from '@aws-sdk/util-base64-node';
import { defaultUserAgent } from '@aws-sdk/util-user-agent-node';
import { AwsAuthConfiguration, AwsAuthConfigurationInput } from '@aws-sdk/signing-middleware';
import { UserAgentConfig, UserAgentConfigInput } from '@aws-sdk/middleware-user-agent';
import { RetryConfig, RetryConfigInput } from '@aws-sdk/retry-middleware';
import { name, version } from './package.json';
import {
AwsAuthConfiguration,
AwsAuthConfigurationInput
} from "@aws-sdk/signing-middleware";
import {
UserAgentConfig,
UserAgentConfigInput
} from "@aws-sdk/middleware-user-agent";
import { RetryConfig, RetryConfigInput } from "@aws-sdk/retry-middleware";
import {
RegionConfiguration,
RegionConfigurationInput,
EndpointsConfig,
EndpointsConfigInput,
ProtocolConfig,
ProtocolConfigInput,
AWSClientRuntimeConfiguration
} from '@aws-sdk/config-resolver';

export type AWSClientRuntimeResolvedConfiguration = Required<AWSClientRuntimeConfiguration>;

export const RDSRuntimeConfiguration: AWSClientRuntimeResolvedConfiguration = {
protocolDefaultProvider: (handler) => new RestJsonProtocol(handler),
signingName: "rds-data",
service: "rds-data",
httpHandler: new NodeHttpHandler(),
sha256: Hash.bind(null, "sha256"),
credentialDefaultProvider,
regionDefaultProvider,
urlParser: parseUrl,
bodyLengthChecker: calculateBodyLength,
streamCollector,
base64Decoder: fromBase64,
base64Encoder: toBase64,
utf8Decoder: fromUtf8,
utf8Encoder: toUtf8,
defaultUserAgent: defaultUserAgent(name, version)
ProtocolConfigInput
} from "@aws-sdk/config-resolver";
import {
Credentials,
Provider,
HashConstructor,
UrlParser,
Protocol,
StreamCollector,
Decoder,
Encoder
} from "@aws-sdk/types";
import { HttpHandler, HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";

export interface RDSDataRuntimeDependencies {
/**
* The HTTP handler to use. Fetch in browser and Https in Nodejs
*/
httpHandler?: HttpHandler;

/**
* A constructor for a class implementing the @aws-sdk/types.Hash interface that computes the SHA-256 HMAC or checksum of a string or binary buffer
*/
sha256?: HashConstructor;

/**
* Default credentials provider; Not available in browser runtime
*/
credentialDefaultProvider?: (input: any) => Provider<Credentials>;

/**
* Provider function that return promise of a region string
*/
regionDefaultProvider?: (input: any) => Provider<string>;

/**
* The function that will be used to convert strings into HTTP endpoints
*/
urlParser?: UrlParser;

/**
* A function that can calculate the length of a request body.
*/
bodyLengthChecker?: (body: any) => number | undefined;

/**
* A function that converts a stream into an array of bytes.
*/
streamCollector?: StreamCollector;

/**
* The function that will be used to convert a base64-encoded string to a byte array
*/
base64Decoder?: Decoder;

/**
* The function that will be used to convert binary data to a base64-encoded string
*/
base64Encoder?: Encoder;

/**
* The function that will be used to convert a UTF8-encoded string to a byte array
*/
utf8Decoder?: Decoder;

/**
* The function that will be used to convert binary data to a UTF-8 encoded string
*/
utf8Encoder?: Encoder;

/**
* The function that will be used to populate default value in 'User-Agent' header
*/
defaultUserAgent?: string;

/**
* The function that will be used to populate serializing protocol
*/
protocolDefaultProvider?: (
handler: HttpHandler
) => Protocol<HttpRequest, HttpResponse>;

/**
* The service name with which to sign requests.
*/
signingName?: string;

/**
* The service name with which to construct endpoints.
*/
service?: string;
}

export type RDSDataConfiguration = AWSClientRuntimeConfiguration &
export type RDSDataConfiguration = RDSDataRuntimeDependencies &
AwsAuthConfigurationInput &
RegionConfigurationInput &
RetryConfigInput &
EndpointsConfigInput &
ProtocolConfigInput &
UserAgentConfigInput
UserAgentConfigInput;

export type RDSDataResolvedConfiguration = AWSClientRuntimeResolvedConfiguration &
export type RDSDataResolvedConfiguration = Required<
RDSDataRuntimeDependencies
> &
AwsAuthConfiguration.Resolved &
RegionConfiguration.Resolved &
RetryConfig.Resolved &
EndpointsConfig.Resolved &
ProtocolConfig.Resolved &
UserAgentConfig.Resolved
UserAgentConfig.Resolved;
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type OutputTypesUnion = any;
export class ExecuteStatementCommand extends Command<
ExecuteStatementRequest,
ExecuteStatementResponse
> {
> {
constructor(readonly input: ExecuteStatementRequest) {
super();
}
Expand All @@ -28,13 +28,18 @@ export class ExecuteStatementCommand extends Command<
clientStack: MiddlewareStack<InputTypesUnion, OutputTypesUnion>,
configuration: RDSDataResolvedConfiguration,
options?: HttpOptions
): Handler<
ExecuteStatementRequest,
ExecuteStatementResponse
> {
const { protocol: { handler } } = configuration;
): Handler<ExecuteStatementRequest, ExecuteStatementResponse> {
const {
protocol: { handler }
} = configuration;

this.use(serdePlugin(configuration, executeStatementSerializer, executeStatementDeserializer));
this.use(
serdePlugin(
configuration,
executeStatementSerializer,
executeStatementDeserializer
)
);

const stack = clientStack.concat(this.middlewareStack);

Expand Down
4 changes: 0 additions & 4 deletions clients/node/client-rds-data-node/index.browser.ts

This file was deleted.

Loading

0 comments on commit 8be08fc

Please sign in to comment.