serverless-domain-manager
Version:
Serverless plugin for managing custom domains with API Gateways.
170 lines (169 loc) • 7.9 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 client_api_gateway_1 = require("@aws-sdk/client-api-gateway");
const ApiGatewayMap = require("../models/api-gateway-map");
const APIGatewayBase = require("../models/apigateway-base");
const logging_1 = __importDefault(require("../logging"));
const utils_1 = require("../utils");
class APIGatewayV1Wrapper extends APIGatewayBase {
constructor(credentials) {
super();
this.apiGateway = new client_api_gateway_1.APIGatewayClient({
credentials,
region: globals_1.default.getRegion(),
retryStrategy: globals_1.default.getRetryStrategy(),
requestHandler: globals_1.default.getRequestHandler(),
endpoint: globals_1.default.getServiceEndpoint("apigateway")
});
}
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,
endpointConfiguration: {
types: [domain.endpointType]
},
securityPolicy: domain.securityPolicy,
tags: providerTags
};
const isEdgeType = domain.endpointType === globals_1.default.endpointTypes.edge;
if (isEdgeType) {
params.certificateArn = domain.certificateArn;
}
else {
params.regionalCertificateArn = domain.certificateArn;
if (domain.tlsTruststoreUri) {
params.mutualTlsAuthentication = {
truststoreUri: domain.tlsTruststoreUri
};
if (domain.tlsTruststoreVersion) {
params.mutualTlsAuthentication.truststoreVersion = domain.tlsTruststoreVersion;
}
}
}
try {
const domainInfo = yield this.apiGateway.send(new client_api_gateway_1.CreateDomainNameCommand(params));
return new DomainInfo(domainInfo);
}
catch (err) {
throw new Error(`V1 - 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_api_gateway_1.GetDomainNameCommand({
domainName: domain.givenDomainName
}));
return new DomainInfo(domainInfo);
}
catch (err) {
if (!err.$metadata || err.$metadata.httpStatusCode !== 404 || !silent) {
throw new Error(`V1 - Unable to fetch information about '${domain.givenDomainName}':\n${err.message}`);
}
logging_1.default.logWarning(`V1 - '${domain.givenDomainName}' does not exist.`);
}
});
}
deleteCustomDomain(domain) {
return __awaiter(this, void 0, void 0, function* () {
// Make API call
try {
yield this.apiGateway.send(new client_api_gateway_1.DeleteDomainNameCommand({
domainName: domain.givenDomainName
}));
}
catch (err) {
throw new Error(`V1 - Failed to delete custom domain '${domain.givenDomainName}':\n${err.message}`);
}
});
}
createBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.apiGateway.send(new client_api_gateway_1.CreateBasePathMappingCommand({
basePath: domain.basePath,
domainName: domain.givenDomainName,
restApiId: domain.apiId,
stage: domain.stage
}));
logging_1.default.logInfo(`V1 - Created API mapping '${domain.basePath}' for '${domain.givenDomainName}'`);
}
catch (err) {
throw new Error(`V1 - Unable to create base path mapping for '${domain.givenDomainName}':\n${err.message}`);
}
});
}
getBasePathMappings(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
const items = yield (0, utils_1.getAWSPagedResults)(this.apiGateway, "items", "position", "position", new client_api_gateway_1.GetBasePathMappingsCommand({
domainName: domain.givenDomainName
}));
return items.map((item) => {
return new ApiGatewayMap(item.restApiId, item.basePath, item.stage, null);
});
}
catch (err) {
throw new Error(`V1 - Make sure the '${domain.givenDomainName}' exists.
Unable to get Base Path Mappings:\n${err.message}`);
}
});
}
updateBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
logging_1.default.logInfo(`V1 - Updating API mapping from '${domain.apiMapping.basePath}'
to '${domain.basePath}' for '${domain.givenDomainName}'`);
try {
yield this.apiGateway.send(new client_api_gateway_1.UpdateBasePathMappingCommand({
basePath: domain.apiMapping.basePath,
domainName: domain.givenDomainName,
patchOperations: [{
op: "replace",
path: "/basePath",
value: domain.basePath
}]
}));
}
catch (err) {
throw new Error(`V1 - Unable to update base path mapping for '${domain.givenDomainName}':\n${err.message}`);
}
});
}
deleteBasePathMapping(domain) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.apiGateway.send(new client_api_gateway_1.DeleteBasePathMappingCommand({
basePath: domain.apiMapping.basePath,
domainName: domain.givenDomainName
}));
logging_1.default.logInfo(`V1 - Removed '${domain.apiMapping.basePath}' base path mapping`);
}
catch (err) {
throw new Error(`V1 - Unable to remove base path mapping for '${domain.givenDomainName}':\n${err.message}`);
}
});
}
}
module.exports = APIGatewayV1Wrapper;
;