/
api-gateway-v2-wrapper.ts
222 lines (208 loc) · 6.77 KB
/
api-gateway-v2-wrapper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Wrapper class for AWS APIGatewayV2 provider
*/
import DomainConfig = require("../models/domain-config");
import DomainInfo = require("../models/domain-info");
import Globals from "../globals";
import ApiGatewayMap = require("../models/api-gateway-map");
import APIGatewayBase = require("../models/apigateway-base");
import {
ApiGatewayV2Client,
ApiMapping,
CreateApiMappingCommand,
CreateDomainNameCommand,
CreateDomainNameCommandOutput,
DeleteApiMappingCommand,
DeleteDomainNameCommand,
GetApiMappingsCommand,
GetApiMappingsCommandInput,
GetApiMappingsCommandOutput,
GetDomainNameCommand,
GetDomainNameCommandOutput,
UpdateApiMappingCommand
} from "@aws-sdk/client-apigatewayv2";
import Logging from "../logging";
import { getAWSPagedResults } from "../utils";
class APIGatewayV2Wrapper extends APIGatewayBase {
public readonly apiGateway: ApiGatewayV2Client;
constructor (credentials?: any) {
super();
this.apiGateway = new ApiGatewayV2Client({
credentials,
region: Globals.getRegion(),
retryStrategy: Globals.getRetryStrategy(),
requestHandler: Globals.getRequestHandler()
});
}
/**
* Creates Custom Domain Name
* @param domain: DomainConfig
*/
public async createCustomDomain (domain: DomainConfig): Promise<DomainInfo> {
const providerTags = {
...Globals.serverless.service.provider.stackTags,
...Globals.serverless.service.provider.tags
};
const params: any = {
DomainName: domain.givenDomainName,
DomainNameConfigurations: [{
CertificateArn: domain.certificateArn,
EndpointType: domain.endpointType,
SecurityPolicy: domain.securityPolicy
}],
Tags: providerTags
};
const isEdgeType = domain.endpointType === Globals.endpointTypes.edge;
if (!isEdgeType && domain.tlsTruststoreUri) {
params.MutualTlsAuthentication = {
TruststoreUri: domain.tlsTruststoreUri
};
if (domain.tlsTruststoreVersion) {
params.MutualTlsAuthentication.TruststoreVersion = domain.tlsTruststoreVersion;
}
}
try {
const domainInfo: CreateDomainNameCommandOutput = await this.apiGateway.send(
new CreateDomainNameCommand(params)
);
return new DomainInfo(domainInfo);
} catch (err) {
throw new Error(
`V2 - Failed to create custom domain '${domain.givenDomainName}':\n${err.message}`
);
}
}
/**
* Get Custom Domain Info
* @param domain: DomainConfig
* @param silent: To issue an error or not. Not by default.
*/
public async getCustomDomain (domain: DomainConfig, silent: boolean = true): Promise<DomainInfo> {
// Make API call
try {
const domainInfo: GetDomainNameCommandOutput = await this.apiGateway.send(
new GetDomainNameCommand({
DomainName: domain.givenDomainName
})
);
return new DomainInfo(domainInfo);
} catch (err) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) {
throw new Error(
`V2 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}`
);
}
Logging.logInfo(`V2 - '${domain.givenDomainName}' does not exist.`);
}
}
/**
* Delete Custom Domain Name
* @param domain: DomainConfig
*/
public async deleteCustomDomain (domain: DomainConfig): Promise<void> {
// Make API call
try {
await this.apiGateway.send(
new DeleteDomainNameCommand({
DomainName: domain.givenDomainName
})
);
} catch (err) {
throw new Error(
`V2 - Failed to delete custom domain '${domain.givenDomainName}':\n${err.message}`
);
}
}
/**
* Create Base Path Mapping
* @param domain: DomainConfig
*/
public async createBasePathMapping (domain: DomainConfig): Promise<void> {
if (domain.apiType === Globals.apiTypes.http && domain.stage !== Globals.defaultStage) {
Logging.logWarning(
`Using a HTTP API with a stage name other than '${Globals.defaultStage}'. ` +
`HTTP APIs require a stage named '${Globals.defaultStage}'. ` +
"Please make sure that stage exists in the API Gateway. " +
"See https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html"
);
}
try {
await this.apiGateway.send(
new CreateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: domain.stage
})
);
Logging.logInfo(`V2 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`);
} catch (err) {
throw new Error(
`V2 - Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}`
);
}
}
/**
* Get APi Mapping
* @param domain: DomainConfig
*/
public async getBasePathMappings (domain: DomainConfig): Promise<ApiGatewayMap[]> {
try {
const items = await getAWSPagedResults<ApiMapping, GetApiMappingsCommandInput, GetApiMappingsCommandOutput>(
this.apiGateway,
"Items",
"NextToken",
"NextToken",
new GetApiMappingsCommand({
DomainName: domain.givenDomainName
})
);
return items.map(
(item) => new ApiGatewayMap(item.ApiId, item.ApiMappingKey, item.Stage, item.ApiMappingId)
);
} catch (err) {
throw new Error(
`V2 - Make sure the '${domain.givenDomainName}' exists. Unable to get API Mappings:\n${err.message}`
);
}
}
/**
* Update APi Mapping
* @param domain: DomainConfig
*/
public async updateBasePathMapping (domain: DomainConfig): Promise<void> {
try {
await this.apiGateway.send(
new UpdateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingId: domain.apiMapping.apiMappingId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: domain.stage
})
);
Logging.logInfo(`V2 - Updated API mapping to '${domain.basePath}' for '${domain.givenDomainName}'`);
} catch (err) {
throw new Error(
`V2 - Unable to update base path mapping for '${domain.givenDomainName}':\n${err.message}`
);
}
}
/**
* Delete Api Mapping
*/
public async deleteBasePathMapping (domain: DomainConfig): Promise<void> {
try {
await this.apiGateway.send(new DeleteApiMappingCommand({
ApiMappingId: domain.apiMapping.apiMappingId,
DomainName: domain.givenDomainName
}));
Logging.logInfo(`V2 - Removed API Mapping with id: '${domain.apiMapping.apiMappingId}'`);
} catch (err) {
throw new Error(
`V2 - Unable to remove base path mapping for '${domain.givenDomainName}':\n${err.message}`
);
}
}
}
export = APIGatewayV2Wrapper;