riof
Version:
Rio framework
199 lines • 7.87 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RioMethodError = exports.Rio = exports.RioReservedUserIdentities = exports.RioClassReservedMethods = exports.RioClass = exports.RioMethod = void 0;
const rdk_1 = __importDefault(require("@retter/rdk"));
function RioMethod(options) {
return function (target, propertyKey, descriptor) {
let method = descriptor.value;
descriptor.value = function () {
var _a;
if (options === null || options === void 0 ? void 0 : options.inputModel) {
(_a = options === null || options === void 0 ? void 0 : options.inputModel) === null || _a === void 0 ? void 0 : _a.parse(arguments[0].body);
}
return method.apply(this, arguments);
};
};
}
exports.RioMethod = RioMethod;
function RioClass(options) {
return function (target) {
};
}
exports.RioClass = RioClass;
var RioClassReservedMethods;
(function (RioClassReservedMethods) {
RioClassReservedMethods["INIT"] = "INIT";
RioClassReservedMethods["STATE"] = "STATE";
RioClassReservedMethods["GET"] = "GET";
RioClassReservedMethods["DESTROY"] = "DESTROY";
})(RioClassReservedMethods = exports.RioClassReservedMethods || (exports.RioClassReservedMethods = {}));
var RioReservedUserIdentities;
(function (RioReservedUserIdentities) {
RioReservedUserIdentities["DEVELOPER"] = "developer";
RioReservedUserIdentities["ENDUSER"] = "enduser";
RioReservedUserIdentities["NONE"] = "none";
RioReservedUserIdentities["ANONYMOUSUSER"] = "anonymous_user";
})(RioReservedUserIdentities = exports.RioReservedUserIdentities || (exports.RioReservedUserIdentities = {}));
class Rio {
constructor(constructorData) {
this.newInstance = true;
this.state = {};
this.schedule = [];
this.tasks = [];
this.events = [];
if (constructorData && typeof constructorData === 'string') {
this.instanceId = constructorData;
this.newInstance = false;
}
else if (constructorData && typeof constructorData === 'object') {
if (constructorData.lookup) {
this.lookup = constructorData.lookup;
this.newInstance = false;
}
else if (constructorData.body) {
this.body = constructorData;
}
}
}
init(data) {
return __awaiter(this, void 0, void 0, function* () {
});
}
_get(data) {
return __awaiter(this, void 0, void 0, function* () {
data.response = { statusCode: 200 };
});
}
destroy(data) {
return __awaiter(this, void 0, void 0, function* () {
});
}
authorizer(data) {
return __awaiter(this, void 0, void 0, function* () {
if (RioReservedUserIdentities.DEVELOPER)
return true;
switch (data.context.methodName) {
case RioClassReservedMethods.INIT:
case RioClassReservedMethods.GET:
case RioClassReservedMethods.DESTROY:
case RioClassReservedMethods.STATE:
if (RioReservedUserIdentities.ENDUSER)
return true;
break;
default:
break;
}
throw new RioMethodError({
code: 'PERMISSION_DENIED',
statusCode: 403,
message: 'Permission denied',
title: 'Permission Denied'
});
});
}
getInstanceId(data) {
return __awaiter(this, void 0, void 0, function* () {
return this.generateInstanceId();
});
}
getState(data) {
return __awaiter(this, void 0, void 0, function* () {
return data.state;
});
}
generateInstanceId() {
return BigInt(process.hrtime.bigint()).toString(32);
}
/** This method is magical and should not be used directly. **/
setDataProperties(data) {
this.state = data.state;
}
/** This method is magical and should not be used directly. **/
getRioClassInstance() {
return __awaiter(this, void 0, void 0, function* () {
if (this.instanceId || this.lookup)
return this;
const rdk = new rdk_1.default();
const response = yield rdk.getInstance({
classId: this.constructor.name,
body: this.body,
instanceId: this.instanceId,
lookupKey: this.lookup
});
if (!response || response.statusCode >= 400 && !response.body && !response.body.instanceId) {
throw new Error('Failed to get instance');
}
this.instanceId = response.body.instanceId;
this.newInstance = !!response.body.newInstance;
return this;
});
}
}
__decorate([
RioMethod()
], Rio.prototype, "init", null);
__decorate([
RioMethod()
], Rio.prototype, "_get", null);
__decorate([
RioMethod()
], Rio.prototype, "destroy", null);
__decorate([
RioMethod()
], Rio.prototype, "authorizer", null);
__decorate([
RioMethod()
], Rio.prototype, "getInstanceId", null);
__decorate([
RioMethod()
], Rio.prototype, "getState", null);
exports.Rio = Rio;
class RioMethodError extends Error {
constructor(props) {
super();
this.message = 'An error occurred!';
this.title = 'Error';
this.statusCode = 400;
if (props === null || props === void 0 ? void 0 : props.statusCode)
this.statusCode = props.statusCode;
if (props === null || props === void 0 ? void 0 : props.code)
this.code = props.code;
if (props === null || props === void 0 ? void 0 : props.message)
this.message = props.message;
if (props === null || props === void 0 ? void 0 : props.title)
this.title = props.title;
if (props === null || props === void 0 ? void 0 : props.addons)
this.addons = props.addons;
}
getRioResponse() {
return {
statusCode: this.statusCode,
body: {
title: this.title,
message: this.message,
code: this.code,
addons: this.addons
}
};
}
}
exports.RioMethodError = RioMethodError;
//# sourceMappingURL=Rio.js.map