-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(endpoint): update existing packages for endpoints v2
- Loading branch information
Showing
20 changed files
with
209 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/middleware-endpoint/src/adaptors/getEndpointFromConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { EndpointParameters, EndpointV2, HandlerExecutionContext } from "@aws-sdk/types"; | ||
|
||
import { EndpointResolvedConfig } from "../resolveEndpointConfig"; | ||
import { EndpointParameterInstructions } from "../types"; | ||
|
||
/** | ||
* | ||
* This step in the endpoint resolution process is exposed as a function | ||
* to allow certain packages such as s3 signer, lib-upload to get | ||
* the V2 Endpoint associated to an instance of some (operation) command. | ||
* | ||
* @private | ||
* | ||
*/ | ||
export const getEndpointFromInstructions = async ( | ||
commandInput: any, | ||
instructionsSupplier: Partial<{ | ||
instructions(): EndpointParameterInstructions | ||
}>, | ||
clientConfig: Partial<EndpointResolvedConfig>, | ||
context?: HandlerExecutionContext | ||
): Promise<EndpointV2> => { | ||
const endpointParams: EndpointParameters = {}; | ||
const instructions: EndpointParameterInstructions = instructionsSupplier?.instructions() || {}; | ||
|
||
if (typeof clientConfig.endpointProvider !== "function") { | ||
throw new Error("config.endpointProvider is not set."); | ||
} | ||
|
||
for (const [name, instruction] of Object.entries(instructions)) { | ||
switch (instruction.type) { | ||
case "staticContextParams": | ||
endpointParams[name] = instruction.value; | ||
break; | ||
case "contextParams": | ||
endpointParams[name] = commandInput[instruction.name]; | ||
break; | ||
case "clientContextParams": | ||
case "builtInParams": | ||
endpointParams[name] = await createConfigProvider(instruction.name, clientConfig)(); | ||
break; | ||
default: | ||
throw new Error("Unrecognized endpoint parameter instruction: " + JSON.stringify(instruction)); | ||
} | ||
} | ||
|
||
const endpoint: EndpointV2 = clientConfig.endpointProvider!(endpointParams, context); | ||
return endpoint; | ||
}; | ||
|
||
/** | ||
* @private | ||
*/ | ||
const createConfigProvider = (configKey: string, config: EndpointResolvedConfig & any) => { | ||
return async function configProvider() { | ||
if (!(configKey in config)) { | ||
throw new Error(`The config key ${configKey} was not found in the config object.`); | ||
} | ||
const configValue = config[configKey]; | ||
if (typeof configValue === "function") { | ||
return configValue(); | ||
} | ||
return configValue; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Endpoint, EndpointV2 } from "@aws-sdk/types"; | ||
import { parseUrl } from "@aws-sdk/url-parser"; | ||
|
||
export const toEndpointV1 = (endpoint: string | Endpoint | EndpointV2): Endpoint => { | ||
if (typeof endpoint === "object") { | ||
if ("url" in endpoint) { | ||
// v2 | ||
return parseUrl(endpoint.url); | ||
} | ||
// v1 | ||
return endpoint; | ||
} | ||
return parseUrl(endpoint); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
import { HttpRequest } from "@aws-sdk/protocol-http"; | ||
import { parseQueryString } from "@aws-sdk/querystring-parser"; | ||
import { | ||
EndpointV2, | ||
HandlerExecutionContext, | ||
MetadataBearer, | ||
SerializeHandler, | ||
SerializeHandlerArguments, | ||
SerializeHandlerOutput, | ||
SerializeMiddleware, | ||
} from "@aws-sdk/types"; | ||
|
||
import { EndpointParameterInstruction } from "./types"; | ||
import { getEndpointFromInstructions } from "./adaptors/getEndpointFromConfig"; | ||
import { EndpointResolvedConfig } from "./resolveEndpointConfig"; | ||
import { EndpointParameterInstructions } from "./types"; | ||
|
||
export const endpointMiddleware = (options: { | ||
config: any; // TODO(endpointsV2): should be ResolvedEndpointConfig interface | ||
instruction: EndpointParameterInstruction; | ||
/** | ||
* @private | ||
*/ | ||
export const endpointMiddleware = ({ | ||
config, | ||
instructions, | ||
}: { | ||
config: EndpointResolvedConfig; | ||
instructions: EndpointParameterInstructions; | ||
}): SerializeMiddleware<any, any> => { | ||
return <Output extends MetadataBearer>(next: SerializeHandler<any, Output>): SerializeHandler<any, Output> => | ||
return <Output extends MetadataBearer>( | ||
next: SerializeHandler<any, Output>, | ||
context: HandlerExecutionContext | ||
): SerializeHandler<any, Output> => | ||
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => { | ||
// TODO(endpointsV2): resolve async providers from client config and static values according to | ||
// instruction from input to populate endpoint parameters | ||
if (HttpRequest.isInstance(args.request)) { | ||
const { request } = args; | ||
|
||
const endpoint: EndpointV2 = await getEndpointFromInstructions(args.input, instructions, config, context); | ||
|
||
context.endpointV2 = endpoint; | ||
context.authSchemes = endpoint.properties?.authSchemes; | ||
|
||
request.headers = Object.entries(endpoint.headers || {}).reduce((headers, [name, values]) => { | ||
headers[name] = values.join(","); | ||
return headers; | ||
}, {} as Record<string, string>); | ||
request.hostname = endpoint.url.hostname; | ||
request.path = endpoint.url.pathname; | ||
request.port = parseInt(endpoint.url.port); | ||
request.protocol = endpoint.url.protocol; | ||
request.query = parseQueryString(endpoint.url.search); | ||
} | ||
return next({ | ||
...args, | ||
}); | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.