/
request.ts
60 lines (45 loc) · 1.18 KB
/
request.ts
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
import supertest from 'supertest';
export class ExtendedTest {
readonly _test: supertest.Test;
constructor(test: supertest.Test) {
this._test = test;
}
withAuth() {
return this._test.set('Authorization', 'Bearer [token]');
}
}
const handler = {
get: (receiver: ExtendedTest, prop: keyof ExtendedTest) => {
if (prop in receiver) {
return receiver[prop];
}
return receiver._test[prop];
},
};
export type Test = ExtendedTest & supertest.Test;
const testFactory = (test: supertest.Test): Test =>
new Proxy(new ExtendedTest(test), handler) as Test;
export class ExtendedRequest {
_request: supertest.SuperTest<supertest.Test>;
constructor(app: any) {
this._request = supertest(app);
}
get(path: string) {
return testFactory(this._request.get(path));
}
post(path: string) {
return testFactory(this._request.post(path));
}
patch(path: string) {
return testFactory(this._request.patch(path));
}
put(path: string) {
return testFactory(this._request.put(path));
}
delete(path: string) {
return testFactory(this._request.delete(path));
}
}
export default function (app: any) {
return new ExtendedRequest(app);
}