@adastradev/serverless-discovery-sdk
Version:
Serverless Service Discovery API
105 lines (104 loc) • 4.6 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiscoveryServiceApi = void 0;
var apigClientFactory = require('@adastradev/aws-api-gateway-client').default; // tslint:disable-line
var retryOptions = {
retries: 3,
retryDelay: function () { return 500; },
retryCondition: function (error) {
return (error.retryable ||
!error.response ||
error.response.status === 429 ||
error.response.status === 502 ||
error.response.status === 503 ||
error.response.status === 504);
}
};
var DiscoveryServiceApi = /** @class */ (function () {
function DiscoveryServiceApi(serviceEndpointUri, region, credentials) {
if (credentials.type === 'None') {
this.apigClient = apigClientFactory.newClient(__assign({ accessKey: '', invokeUrl: serviceEndpointUri, region: region, secretKey: '' }, retryOptions));
}
else if (credentials.type === 'IAM') {
var iamCreds = credentials;
this.apigClient = apigClientFactory.newClient(__assign({ accessKey: iamCreds.accessKeyId, invokeUrl: serviceEndpointUri, region: region, secretKey: iamCreds.secretAccessKey, sessionToken: iamCreds === null || iamCreds === void 0 ? void 0 : iamCreds.sessionToken }, retryOptions));
}
else if (credentials.type === 'BearerToken') {
var tokenCreds = credentials;
this.additionalParams = {
headers: {
Authorization: 'Bearer ' + tokenCreds.idToken
}
};
this.apigClient = apigClientFactory.newClient(__assign({ accessKey: '', invokeUrl: serviceEndpointUri, region: region, secretKey: '' }, retryOptions));
}
else {
throw Error('Unsupported credential type in DiscoveryServiceApi');
}
}
DiscoveryServiceApi.prototype.getService = function (id) {
var params = { id: id };
var pathTemplate = '/catalog/service/{id}';
var method = 'GET';
var additionalParams = {};
var body = {};
return this.apigClient.invokeApi(params, pathTemplate, method, additionalParams, body);
};
DiscoveryServiceApi.prototype.lookupService = function (serviceName, stageName, version, externalID, shouldInvalidateCache) {
if (stageName === void 0) { stageName = ''; }
if (version === void 0) { version = ''; }
if (externalID === void 0) { externalID = ''; }
if (shouldInvalidateCache === void 0) { shouldInvalidateCache = false; }
var params = {};
var pathTemplate = '/catalog/service';
var method = 'GET';
var additionalParams = {
queryParams: {
ServiceName: serviceName,
StageName: stageName,
Version: version,
ExternalID: externalID
}
};
if (shouldInvalidateCache) {
additionalParams = __assign(__assign({}, additionalParams), { headers: {
'Cache-Control': 'max-age=0'
} });
}
var body = {};
// We need more than stageName only
if (stageName === '' && version === '' && externalID === '') {
throw new Error('Must provide more than service name only');
}
return this.apigClient.invokeApi(params, pathTemplate, method, additionalParams, body);
};
DiscoveryServiceApi.prototype.createService = function (service) {
var params = {};
var pathTemplate = '/catalog/service';
var method = 'POST';
var additionalParams = {};
var body = service;
return this.apigClient.invokeApi(params, pathTemplate, method, additionalParams, body);
};
DiscoveryServiceApi.prototype.deleteService = function (id) {
var params = { id: id };
var pathTemplate = '/catalog/service/{id}';
var method = 'DELETE';
var additionalParams = {};
var body = {};
return this.apigClient.invokeApi(params, pathTemplate, method, additionalParams, body);
};
return DiscoveryServiceApi;
}());
exports.DiscoveryServiceApi = DiscoveryServiceApi;