@questlabs/core
Version:
This is the quest SDK
132 lines (131 loc) • 6.97 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityAuthenticationToken = void 0;
const constants_1 = require("../../utils/constants");
const helperFunctions_1 = require("../../utils/helperFunctions");
class EntityAuthenticationToken {
constructor(questSdk) {
this.questSdk = questSdk;
}
createToken({ entityId } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token });
const url = `/entities/${defaultEntityId}/users/${userId}/authentication-token`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
updateToken({ entityId, flags, expiry } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, flags, expiry, userId, token });
const url = `/entities/${defaultEntityId}/users/${userId}/authentication-token`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ flags, expiry }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.PATCH,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
body,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
refreshToken({ entityId, authenticationToken } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
const savedAuthenticationToken = this.questSdk.getEntityAuthenticationToken();
(0, helperFunctions_1.validateParams)({
entityId: defaultEntityId,
userId,
authenticationToken: authenticationToken || savedAuthenticationToken,
token,
});
const url = `/entities/${defaultEntityId}/users/${userId}/authentication-token/refresh`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const body = Object.assign({ authenticationToken: authenticationToken || savedAuthenticationToken }, options === null || options === void 0 ? void 0 : options._body);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.POST,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
body,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
deleteToken({ entityId } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, userId, token });
const url = `/entities/${defaultEntityId}/users/${userId}/authentication-token`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
return this.questSdk.makeRequest({
url,
method: constants_1.HTTP_METHOD.DELETE,
headers: options === null || options === void 0 ? void 0 : options._headers,
queryParams,
});
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
getToken({ entityId, requestOrigin } = {}, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { userId, token } = this.questSdk.getUser();
const defaultEntityId = entityId || this.questSdk.getEntityId();
(0, helperFunctions_1.validateParams)({ entityId: defaultEntityId, requestOrigin, userId, token });
const url = `/entities/${defaultEntityId}/users/${userId}/authentication-token`;
const queryParams = Object.assign({ userId }, options === null || options === void 0 ? void 0 : options._queryParams);
const headers = { Referer: requestOrigin };
return this.questSdk.makeRequest({ url, method: constants_1.HTTP_METHOD.GET, headers, queryParams });
}
catch (error) {
this.questSdk.captureSentryException(error);
throw error;
}
});
}
}
exports.EntityAuthenticationToken = EntityAuthenticationToken;