|
| 1 | +import type { Endpoint, EndpointParameters, EndpointV2, Logger, Provider } from "@smithy/types"; |
| 2 | +import { parseUrl } from "@smithy/url-parser"; |
| 3 | + |
| 4 | +/** |
| 5 | + * This is an additional config resolver layer for clients using the default |
| 6 | + * AWS regional endpoints ruleset. It makes the *resolved* config guarantee the presence of an |
| 7 | + * endpoint provider function. This differs from the base behavior of the Endpoint |
| 8 | + * config resolver, which only normalizes config.endpoint IFF one is provided by the caller. |
| 9 | + * |
| 10 | + * This is not used by AWS SDK clients, but rather |
| 11 | + * generated clients that have the aws.api#service trait. This includes protocol tests |
| 12 | + * and other customers. |
| 13 | + * |
| 14 | + * This resolver is MUTUALLY EXCLUSIVE with the EndpointRequired config resolver from |
| 15 | + * |@smithy/middleware-endpoint. |
| 16 | + * |
| 17 | + * It must be placed after the `resolveEndpointConfig` |
| 18 | + * resolver. This replaces the endpoints.json-based default endpoint provider. |
| 19 | + * |
| 20 | + * @public |
| 21 | + */ |
| 22 | +export type DefaultAwsRegionalEndpointsInputConfig = { |
| 23 | + endpoint?: unknown; |
| 24 | + endpointProvider: ( |
| 25 | + endpointParams: EndpointParameters | DefaultRegionalEndpointParameters, |
| 26 | + context?: { logger?: Logger } |
| 27 | + ) => EndpointV2; |
| 28 | +}; |
| 29 | + |
| 30 | +type PreviouslyResolved = { |
| 31 | + logger?: Logger; |
| 32 | + region?: undefined | string | Provider<string | undefined>; |
| 33 | + useFipsEndpoint?: undefined | boolean | Provider<string | boolean>; |
| 34 | + useDualstackEndpoint?: undefined | boolean | Provider<string | boolean>; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * @internal |
| 39 | + */ |
| 40 | +type DefaultRegionalEndpointParameters = { |
| 41 | + Region?: string | undefined; |
| 42 | + UseDualStack?: boolean | undefined; |
| 43 | + UseFIPS?: boolean | undefined; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * @internal |
| 48 | + */ |
| 49 | +export interface DefaultAwsRegionalEndpointsResolvedConfig { |
| 50 | + endpoint: Provider<Endpoint>; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * MUST resolve after `\@smithy/middleware-endpoint`::`resolveEndpointConfig`. |
| 55 | + * |
| 56 | + * @internal |
| 57 | + */ |
| 58 | +export const resolveDefaultAwsRegionalEndpointsConfig = <T>( |
| 59 | + input: T & DefaultAwsRegionalEndpointsInputConfig & PreviouslyResolved |
| 60 | +): T & DefaultAwsRegionalEndpointsResolvedConfig => { |
| 61 | + if (typeof input.endpointProvider !== "function") { |
| 62 | + throw new Error("@aws-sdk/util-endpoint - endpointProvider and endpoint missing in config for this client."); |
| 63 | + } |
| 64 | + |
| 65 | + const { endpoint } = input; |
| 66 | + if (endpoint === undefined) { |
| 67 | + input.endpoint = async () => { |
| 68 | + return toEndpointV1( |
| 69 | + input.endpointProvider( |
| 70 | + { |
| 71 | + Region: typeof input.region === "function" ? await input.region() : input.region, |
| 72 | + UseDualStack: |
| 73 | + typeof input.useDualstackEndpoint === "function" |
| 74 | + ? await input.useDualstackEndpoint() |
| 75 | + : input.useDualstackEndpoint, |
| 76 | + UseFIPS: |
| 77 | + typeof input.useFipsEndpoint === "function" ? await input.useFipsEndpoint() : input.useFipsEndpoint, |
| 78 | + }, |
| 79 | + { logger: input.logger } |
| 80 | + ) |
| 81 | + ); |
| 82 | + }; |
| 83 | + } |
| 84 | + return input as T & DefaultEndpointResolvedConfig; |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * @internal |
| 89 | + */ |
| 90 | +export const toEndpointV1 = (endpoint: EndpointV2): Endpoint => parseUrl(endpoint.url); |
0 commit comments