4
4
Controller ,
5
5
Delete ,
6
6
Get ,
7
+ HttpStatusCodeLiteral ,
7
8
Post ,
8
9
Put ,
9
10
Query ,
@@ -13,10 +14,13 @@ import {
13
14
Tags ,
14
15
TsoaResponse ,
15
16
} from 'tsoa' ;
17
+ import { TerraformError } from '../interfaces/errors' ;
16
18
import { StateLockRequest } from '../models/interfaces/StateLockRequest' ;
17
19
import { GithubService } from '../services/GithubService' ;
18
20
import { StateService } from '../services/StateService' ;
19
21
22
+ type HeaderType = { [ key : string ] : string | string [ ] } ;
23
+
20
24
@Route ( '/v1' )
21
25
@Tags ( 'v1' )
22
26
export class ControllerV1 extends Controller {
@@ -31,9 +35,21 @@ export class ControllerV1 extends Controller {
31
35
}
32
36
33
37
@Get ( )
34
- public async getState ( @Request ( ) request : HttpRequest ) : Promise < any > {
35
- const identity = await this . githubService . getIdentity ( request ) ;
36
- return this . stateService . getState ( identity ) ;
38
+ public async getState (
39
+ @Request ( ) request : HttpRequest ,
40
+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 , any > ,
41
+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , any , HeaderType > > {
42
+ try {
43
+ const identity = await this . githubService . getIdentity ( request ) ;
44
+ const state = await this . stateService . getState ( identity ) ;
45
+ const response = res ( 200 , state ) ;
46
+ return response ;
47
+ } catch ( e ) {
48
+ if ( e instanceof TerraformError ) {
49
+ return e . respond ( res ) ;
50
+ }
51
+ throw e ;
52
+ }
37
53
}
38
54
39
55
@Post ( )
@@ -42,38 +58,59 @@ export class ControllerV1 extends Controller {
42
58
@Query ( 'ID' ) id : string ,
43
59
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
44
60
@Body ( ) state : any ,
45
- @Res ( ) res : TsoaResponse < 200 , void > ,
46
- ) : Promise < void > {
47
- const stateLockRequest = await this . stateService . getRequest ( id ) ;
48
- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
49
- await this . stateService . saveState ( identity , id , state ) ;
50
- const response = res ( 200 ) ;
51
- return response ;
61
+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , void > ,
62
+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , void , HeaderType > > {
63
+ try {
64
+ const stateLockRequest = await this . stateService . getRequest ( id ) ;
65
+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
66
+ await this . stateService . saveState ( identity , id , state ) ;
67
+ const response = res ( 200 ) ;
68
+ return response ;
69
+ } catch ( e ) {
70
+ if ( e instanceof TerraformError ) {
71
+ return e . respond ( res ) ;
72
+ }
73
+ throw e ;
74
+ }
52
75
}
53
76
54
77
@Put ( 'lock' )
55
78
public async lockState (
56
79
@Request ( ) request : HttpRequest ,
57
80
@Body ( ) lockRequest : StateLockRequest ,
58
- @Res ( ) res : TsoaResponse < 200 , boolean > ,
59
- ) : Promise < boolean > {
60
- const stateLockRequest = await this . stateService . saveRequest ( lockRequest ) ;
61
- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
62
- await this . stateService . lockState ( identity , stateLockRequest ) ;
63
- const response = res ( 200 , true ) ;
64
- return response ;
81
+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , boolean > ,
82
+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , boolean , HeaderType > > {
83
+ try {
84
+ const stateLockRequest = await this . stateService . saveRequest ( lockRequest ) ;
85
+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
86
+ await this . stateService . lockState ( identity , stateLockRequest ) ;
87
+ const response = res ( 200 , true ) ;
88
+ return response ;
89
+ } catch ( e ) {
90
+ if ( e instanceof TerraformError ) {
91
+ return e . respond ( res ) ;
92
+ }
93
+ throw e ;
94
+ }
65
95
}
66
96
67
97
@Delete ( 'lock' )
68
98
public async unlockState (
69
99
@Request ( ) request : HttpRequest ,
70
100
@Body ( ) lockRequest : StateLockRequest ,
71
- @Res ( ) res : TsoaResponse < 200 , boolean > ,
72
- ) : Promise < boolean > {
73
- const stateLockRequest = await this . stateService . getRequest ( lockRequest . ID ) ;
74
- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
75
- await this . stateService . unlockState ( identity , stateLockRequest ) ;
76
- const response = res ( 200 , true ) ;
77
- return response ;
101
+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , boolean > ,
102
+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , boolean , HeaderType > > {
103
+ try {
104
+ const stateLockRequest = await this . stateService . getRequest ( lockRequest . ID ) ;
105
+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
106
+ await this . stateService . unlockState ( identity , stateLockRequest ) ;
107
+ const response = res ( 200 , true ) ;
108
+ return response ;
109
+ } catch ( e ) {
110
+ if ( e instanceof TerraformError ) {
111
+ return e . respond ( res ) ;
112
+ }
113
+ throw e ;
114
+ }
78
115
}
79
116
}
0 commit comments