@coko/server
Version:
Reusable server for use by Coko's projects
50 lines • 2.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccessToken = exports.callMicroservice = void 0;
const makeCall_1 = __importDefault(require("./makeCall"));
const tokens_1 = require("./tokens");
Object.defineProperty(exports, "getAccessToken", { enumerable: true, get: function () { return tokens_1.getAccessToken; } });
/**
* Calls given microservice, while taking care of authentication for you.
* Services need to be defined in the config.
*
* First, on the service container, you need to generate a client id & secret,
* then add these variables as credentials in the environment file of your app.
*
* This function will:
* - Grab those variables from the environment / config
* - Communicate with the service to get authenticated and get an access token
* - Store the access token on the ServiceCredential table
* - Make a call to the service with the parameters you gave it
*
* If the access token exists already, it will be used without calling the
* service for a new one.
*
* If the access token exists, but has expired, this is also handled
* automatically by getting a new token from the service.
*
* Other errors will be thrown and should be handled by your app logic.
*/
const callMicroservice = async (serviceName, callParameters) => {
if (!callParameters)
throw new Error(`communication parameters needed for calling ${serviceName} microservice`);
const accessToken = await (0, tokens_1.getAccessToken)(serviceName);
return (0, makeCall_1.default)(callParameters, accessToken).catch(async (err) => {
const { response } = err;
if (!response) {
throw new Error(`Request failed with message: ${err.code}`);
}
const { status, data } = response;
const { msg } = data;
if (status === 401 && msg === 'expired token') {
const freshToken = await (0, tokens_1.getAccessToken)(serviceName, true);
return (0, makeCall_1.default)(callParameters, freshToken);
}
throw new Error(err);
});
};
exports.callMicroservice = callMicroservice;
//# sourceMappingURL=microservices.js.map