-
Notifications
You must be signed in to change notification settings - Fork 1
/
action-result.ts
67 lines (61 loc) · 1.46 KB
/
action-result.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
61
62
63
64
65
66
67
/**
* Base class that represents the result of a controller action
*/
export abstract class ActionResult {}
/**
* Represents a result of a controller action with a status code
*/
export class StatusCodeResult extends ActionResult {
readonly #statusCode: number;
constructor(statusCode: number) {
super();
this.#statusCode = statusCode;
}
get statusCode() {
return this.#statusCode;
}
}
/**
* Represents a result of a controller with content
*/
export class ContentResult extends StatusCodeResult {
readonly #content: string;
constructor(content: string, statusCode = 200) {
super(statusCode);
this.#content = content;
}
get content() {
return this.#content;
}
}
/**
* Represents a controller action result with a redirect
*/
export class RedirectResult extends StatusCodeResult {
readonly #location: string;
constructor(location: string, statusCode: 301 | 302 = 302) {
super(statusCode);
this.#location = location;
}
get location() {
return this.#location;
}
}
/**
* Create a @see StatusCodeResult
*/
export function statusCode(statusCode: number) {
return new StatusCodeResult(statusCode);
}
/**
* Create a @see RedirectResult
*/
export function redirect(location: string, statusCode: 301 | 302 = 302) {
return new RedirectResult(location, statusCode);
}
/**
* Create a @see ContentResult
*/
export function content(content: string, statusCode = 200) {
return new ContentResult(content, statusCode);
}