@wepublish/api
Version:
API core for we.publish.
57 lines • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRemotePeerProfile = exports.getAdminPeerProfile = void 0;
const tslib_1 = require("tslib");
const delegate_1 = require("@graphql-tools/delegate");
const wrap_1 = require("@graphql-tools/wrap");
const api_1 = require("../../../../permissions-api/src");
const api_2 = require("../../../../settings-api/src");
const context_1 = require("../../context");
const error_1 = require("../../error");
const utility_1 = require("../../utility");
const permissions_1 = require("../permissions");
const peer_profile_queries_1 = require("./peer-profile.queries");
const create_safe_host_url_1 = require("../peer/create-safe-host-url");
const getAdminPeerProfile = (hostURL, websiteURL, authenticate, peerProfile) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanGetPeerProfile, roles);
return (0, peer_profile_queries_1.getPeerProfile)(hostURL, websiteURL, peerProfile);
});
exports.getAdminPeerProfile = getAdminPeerProfile;
const getRemotePeerProfile = (hostURL, token, authenticate, info, setting) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
const { roles } = authenticate();
(0, permissions_1.authorise)(api_1.CanCreatePeer, roles);
const link = (0, create_safe_host_url_1.createSafeHostUrl)(hostURL, 'v1/admin');
const peerTimeoutSetting = yield setting.findUnique({
where: {
name: api_2.SettingName.PEERING_TIMEOUT_MS
}
});
const peerTimeout = (_a = peerTimeoutSetting === null || peerTimeoutSetting === void 0 ? void 0 : peerTimeoutSetting.value) !== null && _a !== void 0 ? _a : parseInt((_b = process.env.PEERING_TIMEOUT_IN_MS) !== null && _b !== void 0 ? _b : '');
if (!peerTimeout) {
throw new Error('No value set for PEERING_TIMEOUT_IN_MS');
}
const fetcher = yield (0, context_1.createFetcher)(link, token, peerTimeout);
const remoteExecutableSchema = {
schema: yield (0, wrap_1.schemaFromExecutor)(fetcher),
executor: fetcher
};
const remoteAnswer = yield (0, delegate_1.delegateToSchema)({
info,
fieldName: 'peerProfile',
args: {},
schema: remoteExecutableSchema,
transforms: []
});
if (((_c = remoteAnswer === null || remoteAnswer === void 0 ? void 0 : remoteAnswer.extensions) === null || _c === void 0 ? void 0 : _c.code) === 'UNAUTHENTICATED') {
// check for unauthenticated error and throw more specific error.
// otherwise client doesn't know who (own or remote api) threw the error
throw new error_1.PeerTokenInvalidError(link);
}
else {
return yield (0, utility_1.markResultAsProxied)(remoteAnswer);
}
});
exports.getRemotePeerProfile = getRemotePeerProfile;
//# sourceMappingURL=peer-profile.private-queries.js.map