-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.ts
65 lines (53 loc) · 2.43 KB
/
gateway.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ApiGatewayV2Client, GetApiCommand, GetApiCommandInput, GetApiCommandOutput, GetIntegrationCommand, GetIntegrationCommandInput, GetIntegrationCommandOutput, GetRoutesCommand, GetRoutesCommandInput, GetRoutesCommandOutput, Route } from "@aws-sdk/client-apigatewayv2";
import assert = require("assert");
const client = new ApiGatewayV2Client({ region: process.env.REGION ?? "ca-central-1" });
async function getRoute(apiId: string, emrType: string): Promise<Route> {
const routes = await getRoutes(apiId)
const routesItems = routes.Items;
const emrRoute = routesItems.find(route => route.RouteKey.includes(emrType))
assert(emrRoute, `No valid emrRoute found in ${JSON.stringify(routesItems)} for emrType '${emrType}'`)
return emrRoute
}
async function getRoutes(apiId: string): Promise<GetRoutesCommandOutput> {
const input: GetRoutesCommandInput = {
ApiId: apiId,
};
const command = new GetRoutesCommand(input);
const response: GetRoutesCommandOutput = await client.send(command);
return response;
}
async function getIntegration(apiId: string, integrationId: string): Promise<GetIntegrationCommandOutput> {
const input: GetIntegrationCommandInput = {
ApiId: apiId,
IntegrationId: integrationId,
};
const command = new GetIntegrationCommand(input);
const response: GetIntegrationCommandOutput = await client.send(command);
return response;
}
async function getApi(apiId: string): Promise<GetApiCommandOutput> {
const input: GetApiCommandInput = {
ApiId: apiId,
};
const command = new GetApiCommand(input);
const response: GetApiCommandOutput = await client.send(command);
return response
}
export async function getApiData(apiId: string, emrType: string) {
const api = await getApi(apiId)
const apiEndpoint = api.ApiEndpoint;
const route = await getRoute(apiId, emrType)
const routeKey = route.RouteKey
assert(routeKey.includes('/'), `RouteKey must include a '/' as part of the path. Instead: '${routeKey}'`)
const routeEndpoint = routeKey.split('/').pop()
const invokeUrl = `${apiEndpoint}/${routeEndpoint}`;
const routeTarget = route.Target
assert(routeTarget.includes('/'), `Route.Target must include a '/' as part of the path. Instead: '${routeTarget}'`)
const integrationId = route.Target.split('/').pop();
const integration = await getIntegration(apiId, integrationId)
const integrationUri = integration.IntegrationUri;
return {
invokeUrl: invokeUrl,
aud: integrationUri
}
}