Skip to content

Commit

Permalink
fix(config-resolver): add options for utility functions (aws#2712)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Aug 26, 2021
1 parent 705de4e commit 56ab50e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
32 changes: 0 additions & 32 deletions packages/config-resolver/src/endpointsConfig/configurations.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { EndpointsInputConfig, PreviouslyResolved } from "./configurations";
import { Provider, RegionInfoProvider, UrlParser } from "@aws-sdk/types";

export const getEndpointFromRegion = async (input: EndpointsInputConfig & PreviouslyResolved) => {
interface GetEndpointFromRegionOptions {
region: Provider<string>;
tls?: boolean;
regionInfoProvider: RegionInfoProvider;
urlParser: UrlParser;
}

export const getEndpointFromRegion = async (input: GetEndpointFromRegionOptions) => {
const { tls = true } = input;
const region = await input.region();

Expand Down
1 change: 0 additions & 1 deletion packages/config-resolver/src/endpointsConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./configurations";
export * from "./resolveEndpointsConfig";
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Endpoint, Provider } from "@aws-sdk/types";
import { Endpoint, Provider, UrlParser } from "@aws-sdk/types";

import { EndpointsInputConfig, PreviouslyResolved } from "./configurations";
interface NormalizeEndpointOptions {
endpoint: string | Endpoint | Provider<Endpoint>;
urlParser: UrlParser;
}

export const normalizeEndpoint = ({
endpoint,
urlParser,
}: EndpointsInputConfig & PreviouslyResolved): Provider<Endpoint> => {
export const normalizeEndpoint = ({ endpoint, urlParser }: NormalizeEndpointOptions): Provider<Endpoint> => {
if (typeof endpoint === "string") {
const promisified = Promise.resolve(urlParser(endpoint));
return () => promisified;
} else if (typeof endpoint === "object") {
const promisified = Promise.resolve(endpoint);
return () => promisified;
}
return endpoint!;
return endpoint;
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import { EndpointsInputConfig, EndpointsResolvedConfig, PreviouslyResolved } from "./configurations";
import { Endpoint, Provider, RegionInfoProvider, UrlParser } from "@aws-sdk/types";

import { getEndpointFromRegion } from "./getEndpointFromRegion";
import { normalizeEndpoint } from "./normalizeEndpoint";

export interface EndpointsInputConfig {
/**
* The fully qualified endpoint of the webservice. This is only required when using a custom endpoint (for example, when using a local version of S3).
*/
endpoint?: string | Endpoint | Provider<Endpoint>;

/**
* Whether TLS is enabled for requests.
*/
tls?: boolean;
}

interface PreviouslyResolved {
regionInfoProvider: RegionInfoProvider;
urlParser: UrlParser;
region: Provider<string>;
}

export interface EndpointsResolvedConfig extends Required<EndpointsInputConfig> {
/**
* Resolved value for input {@link EndpointsResolvedConfig.endpoint}
*/
endpoint: Provider<Endpoint>;

/**
* Whether the endpoint is specified by caller.
* @internal
*/
isCustomEndpoint: boolean;
}

export const resolveEndpointsConfig = <T>(
input: T & EndpointsInputConfig & PreviouslyResolved
): T & EndpointsResolvedConfig => ({
...input,
tls: input.tls ?? true,
endpoint: input.endpoint ? normalizeEndpoint(input) : () => getEndpointFromRegion(input),
endpoint: input.endpoint
? normalizeEndpoint({ ...input, endpoint: input.endpoint })
: () => getEndpointFromRegion(input),
isCustomEndpoint: input.endpoint ? true : false,
});

0 comments on commit 56ab50e

Please sign in to comment.