Rework /my_account/sign_up/index.html.jst to use custom validation style, reduce...
[ndcode_site.git] / js / api_call.js.min
1 Problem = class {
2   constructor(title, detail, status) {
3     this.title = title
4     this.detail = detail
5     this.status = status
6   }
7
8   static from(error) {
9     return (
10       error instanceof Problem ?
11         error :
12         new Problem('Bad request', (error.stack || error.message), 400)
13     )
14   }
15 }
16
17 api_call = async (endpoint, ...args) => {
18   let response = await fetch(
19     endpoint,
20     {method: 'POST', body: JSON.stringify(args)}
21   )
22   let result = await response.json()
23   if (!response.ok)
24     throw new Problem(result.title, result.detail, result.status)
25   return result
26 }