-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapiRoutes.ts
214 lines (185 loc) · 6.14 KB
/
apiRoutes.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Credentials } from '../utils/userSession'
const API_HOST = 'localhost:8080'
type NavigationLink = {
href?: string,
hrefTemplate?: string
}
type ApiRoutes = {
home: HomeRoutes,
project: ProjectRoutes,
label: LabelRoutes,
state: StateRoutes,
issue: IssueRoutes,
comment: CommentRoutes,
user: UserRoutes
}
export type HomeRoutes = {
getHomeRoute: NavigationLink
}
type ProjectRoutes = {
getProjectsRoute: NavigationLink
getProjectRoute: NavigationLink
}
type LabelRoutes = {
getLabelsRoute: NavigationLink,
getLabelRoute: NavigationLink,
getIssueLabelsRoute: NavigationLink
}
type StateRoutes = {
getStatesRoute: NavigationLink,
getStateRoute: NavigationLink,
getNextStatesRoute: NavigationLink
}
type IssueRoutes = {
getIssuesRoute: NavigationLink,
getIssueRoute: NavigationLink,
}
type CommentRoutes = {
getCommentsRoute: NavigationLink,
getCommentRoute: NavigationLink
}
type UserRoutes = {
getUsersRoute: NavigationLink,
getUserRoute: NavigationLink,
getAuthenticatedUserRoute: NavigationLink
}
const apiRoutes = {
home: {
getHomeRoute: { href: '/api'} as NavigationLink
} as HomeRoutes
} as ApiRoutes
function getRequestOptions(method: string, credentials: Credentials, body: any = null): RequestInit {
return {
method: method,
headers: {
'Host': API_HOST,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `${credentials?.scheme} ${credentials?.content}`
},
body: body ? JSON.stringify(body) : null
}
}
function getSirenLink(links: SirenLink[], rel: string): SirenLink {
return links
.find(link => {
const relArr = Array.from(link.rel)
return relArr.includes(rel)
})
}
function getSirenAction(actions: SirenAction[], name: string): SirenAction {
return actions?.find(action => action.name == name)
}
async function throwErrorFromResponse(res: Response, genericMessage: string): Promise<void> {
let message: string
try {
const json = await res.json()
switch (json.type) {
case '/problems/resource-not-found':
message = 'Page does not exist'
break
case '/problems/resource-already-exists':
message = 'Item already exists'
break
case '/problems/resource-referenced':
message = 'Cannot modify an item that is referenced by other items'
break
case '/problems/invalid-string-size':
message = 'Input too long'
break
case '/problems/invalid-input':
case '/problems/forbidden-operation':
case '/problems/no-start-state':
case '/problems/invalid-state-transition':
case '/problems/archived-issue':
case '/problems/forbidden-state-modification':
message = json.detail
break
default:
message = genericMessage
}
} catch {
throw {
status: res.status,
message: genericMessage
} as ApiError
}
throw {
status: res.status,
message: message
} as ApiError
}
function fetchRoutes(): Promise<ApiRoutes> {
return fetch(apiRoutes.home.getHomeRoute.href, getRequestOptions('GET', null))
.then(res => res.status == 200 ? res.json() : null)
.then(entity => {
if (!entity) return null
const links: SirenLink[] = Array.from(entity.links)
apiRoutes.project = {
getProjectsRoute: {
href: getSirenLink(links, 'projects').href
},
getProjectRoute: {
hrefTemplate: getSirenLink(links, 'project').hrefTemplate
}
}
apiRoutes.label = {
getLabelsRoute: {
hrefTemplate: getSirenLink(links, 'labels').hrefTemplate
},
getLabelRoute: {
hrefTemplate: getSirenLink(links, 'label').hrefTemplate
},
getIssueLabelsRoute: {
hrefTemplate: getSirenLink(links, 'issueLabels').hrefTemplate
}
}
apiRoutes.state = {
getStatesRoute: {
hrefTemplate: getSirenLink(links, 'states').hrefTemplate
},
getStateRoute: {
hrefTemplate: getSirenLink(links, 'state').hrefTemplate
},
getNextStatesRoute: {
hrefTemplate: getSirenLink(links, 'nextStates').hrefTemplate
}
}
apiRoutes.issue = {
getIssuesRoute: {
hrefTemplate: getSirenLink(links, 'issues').hrefTemplate
},
getIssueRoute: {
hrefTemplate: getSirenLink(links, 'issue').hrefTemplate
}
}
apiRoutes.comment = {
getCommentsRoute: {
hrefTemplate: getSirenLink(links, 'comments').hrefTemplate
},
getCommentRoute: {
hrefTemplate: getSirenLink(links, 'comment').hrefTemplate
}
}
apiRoutes.user = {
getUsersRoute: {
href: getSirenLink(links, 'users').href
},
getUserRoute: {
hrefTemplate: getSirenLink(links, 'user').hrefTemplate
},
getAuthenticatedUserRoute: {
href: getSirenLink(links, 'authUser').href
},
}
return apiRoutes
})
}
export {
fetchRoutes,
apiRoutes,
ApiRoutes,
getRequestOptions,
getSirenAction,
throwErrorFromResponse
}