serverless-domain-manager
Version:
Serverless plugin for managing custom domains with API Gateways.
188 lines (187 loc) • 8.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const DomainInfo = require("../models/domain-info");
const globals_1 = __importDefault(require("../globals"));
const ApiGatewayMap = require("../models/api-gateway-map");
const APIGatewayBase = require("../models/apigateway-base");
const client_apigatewayv2_1 = require("@aws-sdk/client-apigatewayv2");
const logging_1 = __importDefault(require("../logging"));
const utils_1 = require("../utils");
class APIGatewayV2Wrapper extends APIGatewayBase {
constructor(credentials) {
super();
this.apiGateway = new client_apigatewayv2_1.ApiGatewayV2Client({
credentials,
region: globals_1.default.getRegion(),
retryStrategy: globals_1.default.getRetryStrategy(),
requestHandler: globals_1.default.getRequestHandler(),
endpoint: globals_1.default.getServiceEndpoint("apigatewayv2")
});
}
/**
* Creates Custom Domain Name
* @param domain: DomainConfig
*/
createCustomDomain(domain) {
return __awaiter(this, void 0, void 0, function* () {
const providerTags = Object.assign(Object.assign({}, globals_1.default.serverless.service.provider.stackTags), globals_1.default.serverless.service.provider.tags);
const params = {
DomainName: domain.givenDomainName,
DomainNameConfigurations: [{
CertificateArn: domain.certificateArn,
EndpointType: domain.endpointType,
SecurityPolicy: domain.securityPolicy
}],
Tags: providerTags
};
const isEdgeType = domain.endpointType === globals_1.default.endpointTypes.edge;
if (!isEdgeType && domain.tlsTruststoreUri) {
params.MutualTlsAuthentication = {
TruststoreUri: domain.tlsTruststoreUri
};
if (domain.tlsTruststoreVersion) {
params.MutualTlsAuthentication.TruststoreVersion = domain.tlsTruststoreVersion;
}
}
try {
const domainInfo = yield this.apiGateway.send(new client_apigatewayv2_1.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.
*/
getCustomDomain(domain_1) {
return __awaiter(this, arguments, void 0, function* (domain, silent = true) {
// Make API call
try {
const domainInfo = yield this.apiGateway.send(new client_apigatewayv2_1.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_1.default.logInfo(`V2 - '${domain.givenDomainName}' does not exist.`);
}
});
}
/**
* Delete Custom Domain Name
* @param domain: DomainConfig
*/
deleteCustomDomain(domain) {
return __awaiter(this, void 0, void 0, function* () {
// Make API call
try {
yield this.apiGateway.send(new client_apigatewayv2_1.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
*/
createBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
if (domain.apiType === globals_1.default.apiTypes.http && domain.stage !== globals_1.default.defaultStage) {
logging_1.default.logWarning(`Using a HTTP API with a stage name other than '${globals_1.default.defaultStage}'. ` +
`HTTP APIs require a stage named '${globals_1.default.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 {
yield this.apiGateway.send(new client_apigatewayv2_1.CreateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: domain.stage
}));
logging_1.default.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
*/
getBasePathMappings(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
const items = yield (0, utils_1.getAWSPagedResults)(this.apiGateway, "Items", "NextToken", "NextToken", new client_apigatewayv2_1.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
*/
updateBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.apiGateway.send(new client_apigatewayv2_1.UpdateApiMappingCommand({
ApiId: domain.apiId,
ApiMappingId: domain.apiMapping.apiMappingId,
ApiMappingKey: domain.basePath,
DomainName: domain.givenDomainName,
Stage: domain.stage
}));
logging_1.default.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
*/
deleteBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.apiGateway.send(new client_apigatewayv2_1.DeleteApiMappingCommand({
ApiMappingId: domain.apiMapping.apiMappingId,
DomainName: domain.givenDomainName
}));
logging_1.default.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}`);
}
});
}
}
module.exports = APIGatewayV2Wrapper;
;