-
Notifications
You must be signed in to change notification settings - Fork 42
/
cors.ts
69 lines (62 loc) · 2.3 KB
/
cors.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
68
69
"use strict";
import "es6-promise";
import {Path, GET, POST, DELETE, PUT, PathParam, Errors, Return} from "typescript-rest";
import {ApiCorsConfig, validateApiCorsConfig} from "../../config/cors";
import {CorsService} from "../../service/api";
import {AutoWired, Inject} from "typescript-ioc";
@Path('apis/:apiId/cors')
@AutoWired
export class CorsRest {
@Inject private service: CorsService;
@GET
list(@PathParam("apiId") apiId: string): Promise<Array<ApiCorsConfig>>{
return this.service.list(apiId);
}
@POST
addCors(@PathParam("apiId") apiId: string, cors: ApiCorsConfig): Promise<Return.NewResource> {
return new Promise<Return.NewResource>((resolve, reject) => {
validateApiCorsConfig(cors)
.catch(err => {
throw new Errors.ForbidenError(JSON.stringify(err));
})
.then(() => this.service.create(apiId, cors))
.then(corsId => resolve(new Return.NewResource(`apis/${apiId}/cors/${corsId}`)))
.catch(reject);
});
}
@PUT
@Path("/:corsId")
updateCors(@PathParam("apiId") apiId: string,
@PathParam("corsId") corsId: string,
cors: ApiCorsConfig): Promise<void> {
return new Promise<void>((resolve, reject) => {
validateApiCorsConfig(cors)
.catch(err => {
throw new Errors.ForbidenError(JSON.stringify(err));
})
.then(() => this.service.update(apiId, corsId, cors))
.then(() => resolve())
.catch(reject);
});
}
@DELETE
@Path("/:corsId")
deleteCors(@PathParam("apiId") apiId: string,
@PathParam("corsId") corsId: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.service.remove(apiId, corsId)
.then(() => resolve())
.catch(reject);
});
}
@GET
@Path("/:corsId")
getCors(@PathParam("apiId") apiId: string,
@PathParam("corsId") corsId: string) : Promise<ApiCorsConfig> {
return new Promise((resolve, reject) => {
this.service.get(apiId, corsId)
.then(resolve)
.catch(reject);
});
}
}