forked from bogeeee/restfuncs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestsService.ts
More file actions
104 lines (77 loc) · 2.5 KB
/
Copy pathTestsService.ts
File metadata and controls
104 lines (77 loc) · 2.5 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {ServerSession, ServerSessionOptions, remote} from "restfuncs-server";
import fs from "node:fs"
import _ from "underscore";
import {couldBeSimpleRequest} from "restfuncs-server/Util";
export class TestServiceSessionBase extends ServerSession {
user?: string;
bankAccounts?: Record<string, number>;
}
export class TestsService extends TestServiceSessionBase {
@remote()
unsafeMethod() {
return "test"
}
@remote()
async logon(user: string) {
this.user = user;
if (!this.bankAccounts) this.bankAccounts = {} // initialize
// give user some money:
this.bankAccounts[this.user] = 5000;
}
@remote()
async spendMoney(myArg?: any) {
if (!this.user) {
throw new Error("Not logged it");
}
if (!this.bankAccounts) this.bankAccounts = {} // initialize. We need a better session concept from restfuncs
this.bankAccounts[this.user] = 0;
}
@remote({isSafe: true})
async spendMoneyAccidentlyMarkedAsSafe() {
if (!this.user) {
throw new Error("Not logged it");
}
if (!this.bankAccounts) this.bankAccounts = {} // initialize. We need a better session concept from restfuncs
this.bankAccounts[this.user] = 0;
}
@remote()
async getBalance(user: string) {
if (!this.user) {
throw new Error("Not logged it");
}
if (!this.bankAccounts) this.bankAccounts = {} // initialize. We need a better session concept from restfuncs
return this.bankAccounts[this.user];
}
@remote()
async test() {
return "ok";
}
static lastCallWasSimpleRequest?: boolean
@remote()
getLastCallWasSimpleRequest() {
return TestsService.lastCallWasSimpleRequest;
}
@remote()
getIsSimpleRequest(body?: string) {
if (!this.call.req) {
throw new Error("getIsSimpleRequest not called via http")
}
return couldBeSimpleRequest(this.call.req)
}
@remote()
getTestImage() {
this.call.res?.contentType("image/x-png")
return fs.createReadStream("teeest.png")
}
@remote()
testBeacon(myArg?: unknown) {
const i = 0;
}
protected async doCall(funcName: string, args: any[]): Promise<any> {
try {
return super.doCall(funcName, args);
} finally {
TestsService.lastCallWasSimpleRequest = this.call.req ? couldBeSimpleRequest(this.call.req) : undefined;
}
}
}