forked from bogeeee/restfuncs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainframeService.ts
More file actions
41 lines (34 loc) · 1.24 KB
/
Copy pathMainframeService.ts
File metadata and controls
41 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {CommunicationError, remote, ServerSession} from "restfuncs-server";
import _ from "underscore";
import {TestServiceSessionBase} from "./TestsService.js";
class NotLoggedInError extends CommunicationError {
name= "NotLoggedInError"; // Properly flag it for the client to recognize
constructor() {
super("Not logged in",{log: false, httpStatusCode: 401})
}
}
export class MainframeService extends TestServiceSessionBase {
// Interceptor that checks for login on every function call
protected async doCall(funcName: string, args: any[]) {
if(!_(["login", "myUnrestrictedFunction"]).contains(funcName) && !this.user) {
throw new NotLoggedInError();
}
return await super.doCall(funcName, args);
}
@remote()
async login(userName: string) {
const shallPass = _(["admin", "alice", "bob"]).contains(userName.toLowerCase());
if(shallPass) {
this.user = userName
}
return shallPass
}
@remote()
async multiplyBy10(value: number) {
return `Logged in as ${this.user}. Result is: ${value * 10}`;
}
@remote()
async getConcat(a: string, b: number) {
return `Logged in as ${this.user}. Result is: ${a + b}`;
}
}