@event-inc/connections
Version:
Event Inc is a fully managed event bus lets you send and receive data across mission-critical cloud apps, databases and warehouses.
101 lines • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OauthConnection = void 0;
const tslib_1 = require("tslib");
const types_1 = require("@event-inc/types");
const utils_1 = require("@event-inc/utils");
const ramda_1 = require("ramda");
const jwt_decode_1 = tslib_1.__importDefault(require("jwt-decode"));
[];
const endpoints = {
xero: 'https://identity.xero.com/connect/token',
};
const deleteEndpoints = {
xero: 'https://api.xero.com/connections/{connectionId}',
};
class OauthConnection {
constructor({ OAUTH_APP_CLIENT_ID, OAUTH_APP_CLIENT_SECRET, type, }) {
this.getOauthDetails = () => {
switch (this.type) {
case types_1.OauthConnectionTypes.Xero:
return {
endpoint: endpoints[this.type],
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${Buffer.from(`${this.OAUTH_APP_CLIENT_ID}:${this.OAUTH_APP_CLIENT_SECRET}`).toString('base64')}`,
},
sourceIntegrationDefinitionId: 'conn_def_src_MTY4ODc2MzM3ODY2OQ::OWE2NDdmYjAtZjFlMy00MWEyLTgwMTctMzVhODMxYTBkOWNi',
destinationIntegrationDefinitionId: 'conn_def_dst_MTY4NjA3MDUyOTk1OA::MTgwM2E1NGItMTYxMC00NjE4LTk1NTItNjBjY2I1NmY1ZmQx',
};
}
};
this.getOauthFormData = ({ accessToken, refreshToken, }) => {
switch (this.type) {
case types_1.OauthConnectionTypes.Xero:
return {
XERO_REFRESH_TOKEN: refreshToken,
XERO_ACCESS_TOKEN: accessToken,
XERO_CLIENT_ID: this.OAUTH_APP_CLIENT_ID,
XERO_CLIENT_SECRET: this.OAUTH_APP_CLIENT_SECRET,
};
}
};
this.deleteOauthConnection = ({ connectionId, accessToken, }) => tslib_1.__awaiter(this, void 0, void 0, function* () {
const deleteEndpoint = deleteEndpoints[this.type];
const response = yield (0, utils_1.makeHttpNetworkCall)({
method: 'DELETE',
url: deleteEndpoint.replace('{connectionId}', connectionId),
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return (0, utils_1.matchResultAndHandleHttpError)(response, ramda_1.identity);
});
this.getTenantId = (accessToken) => tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a, _b;
switch (this.type) {
case types_1.OauthConnectionTypes.Xero:
const decodedToken = (0, jwt_decode_1.default)(accessToken);
const response = yield (0, utils_1.makeHttpNetworkCall)({
method: 'GET',
url: 'https://api.xero.com/connections',
headers: {
Authorization: `Bearer ${accessToken}`,
},
params: {
authEventId: decodedToken === null || decodedToken === void 0 ? void 0 : decodedToken.authentication_event_id,
},
});
let { data } = (0, utils_1.matchResultAndHandleHttpError)(response, ramda_1.identity);
if ((data === null || data === void 0 ? void 0 : data.length) === 0 || (data === null || data === void 0 ? void 0 : data.length) > 1) {
const existingTenants = yield this.getAuthorizedTenants(accessToken);
const tenantNames = (_a = existingTenants === null || existingTenants === void 0 ? void 0 : existingTenants.map((item) => item.tenantName)) === null || _a === void 0 ? void 0 : _a.join(', ');
const existingTenantsMessage = (tenantNames === null || tenantNames === void 0 ? void 0 : tenantNames.length) > 0
? ` The following tenants are currently connected: ${tenantNames}.`
: '';
throw new Error(`No tenant was explicitly selected on connect.${existingTenantsMessage}Ensure you disconnect and connect again with a single tenant selected.`);
}
return (_b = data === null || data === void 0 ? void 0 : data[0]) === null || _b === void 0 ? void 0 : _b.id;
}
});
this.getAuthorizedTenants = (accessToken) => tslib_1.__awaiter(this, void 0, void 0, function* () {
switch (this.type) {
case types_1.OauthConnectionTypes.Xero:
const response = yield (0, utils_1.makeHttpNetworkCall)({
method: 'GET',
url: 'https://api.xero.com/connections',
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const { data } = (0, utils_1.matchResultAndHandleHttpError)(response, ramda_1.identity);
return data;
}
});
this.OAUTH_APP_CLIENT_ID = OAUTH_APP_CLIENT_ID;
this.OAUTH_APP_CLIENT_SECRET = OAUTH_APP_CLIENT_SECRET;
this.type = type;
}
}
exports.OauthConnection = OauthConnection;
//# sourceMappingURL=index.js.map