@@ -11,28 +11,30 @@ import {
11
11
} from "../interfaces"
12
12
import { apiCore } from "./core"
13
13
14
+ const jsonify = async ( response : Response ) => {
15
+ if ( response . ok ) {
16
+ return await response . json ( )
17
+ }
18
+ throw `Request failed with ${ response . status } : ${ response . statusText } `
19
+ }
20
+
14
21
export const apiAuth = {
15
- // TEST
16
- async getTestText ( ) {
17
- const res = await fetch ( `${ apiCore . url } /users/tester` )
18
- return ( await res . json ( ) ) as IMsg
19
- } ,
20
22
// LOGIN WITH MAGIC LINK OR OAUTH2 (USERNAME/PASSWORD)
21
- async loginWithMagicLink ( email : string ) {
23
+ async loginWithMagicLink ( email : string ) : Promise < IWebToken > {
22
24
const res = await fetch ( `${ apiCore . url } /login/magic/${ email } ` , {
23
25
method : "POST" ,
24
26
} )
25
- return ( await res . json ( ) ) as IWebToken
27
+ return ( await jsonify ( res ) ) as IWebToken
26
28
} ,
27
- async validateMagicLink ( token : string , data : IWebToken ) {
29
+ async validateMagicLink ( token : string , data : IWebToken ) : Promise < ITokenResponse > {
28
30
const res = await fetch ( `${ apiCore . url } /login/claim` , {
29
31
method : "POST" ,
30
32
body : JSON . stringify ( data ) ,
31
33
headers : apiCore . headers ( token ) ,
32
34
} )
33
- return ( await res . json ( ) ) as ITokenResponse
35
+ return ( await jsonify ( res ) ) as ITokenResponse
34
36
} ,
35
- async loginWithOauth ( username : string , password : string ) {
37
+ async loginWithOauth ( username : string , password : string ) : Promise < ITokenResponse > {
36
38
// Version of this: https://github.com/unjs/ofetch/issues/37#issuecomment-1262226065
37
39
// useFetch is borked, so you'll need to ignore errors https://github.com/unjs/ofetch/issues/37
38
40
const params = new URLSearchParams ( )
@@ -44,85 +46,85 @@ export const apiAuth = {
44
46
// @ts -ignore
45
47
headers : { "Content-Disposition" : params } ,
46
48
} )
47
- return ( await res . json ( ) ) as ITokenResponse
49
+ return ( await jsonify ( res ) ) as ITokenResponse
48
50
} ,
49
51
// TOTP SETUP AND AUTHENTICATION
50
- async loginWithTOTP ( token : string , data : IWebToken ) {
52
+ async loginWithTOTP ( token : string , data : IWebToken ) : Promise < ITokenResponse > {
51
53
const res = await fetch ( `${ apiCore . url } /login/totp` , {
52
54
method : "POST" ,
53
55
body : JSON . stringify ( data ) ,
54
56
headers : apiCore . headers ( token ) ,
55
57
} )
56
- return ( await res . json ( ) ) as ITokenResponse
58
+ return ( await jsonify ( res ) ) as ITokenResponse
57
59
} ,
58
- async requestNewTOTP ( token : string ) {
60
+ async requestNewTOTP ( token : string ) : Promise < INewTOTP > {
59
61
const res = await fetch ( `${ apiCore . url } /users/new-totp` , {
60
62
method : "POST" ,
61
63
headers : apiCore . headers ( token ) ,
62
64
} )
63
- return ( await res . json ( ) ) as INewTOTP
65
+ return ( await jsonify ( res ) ) as INewTOTP
64
66
} ,
65
- async enableTOTPAuthentication ( token : string , data : IEnableTOTP ) {
67
+ async enableTOTPAuthentication ( token : string , data : IEnableTOTP ) : Promise < IMsg > {
66
68
const res = await fetch ( `${ apiCore . url } /login/totp` , {
67
69
method : "PUT" ,
68
70
body : JSON . stringify ( data ) ,
69
71
headers : apiCore . headers ( token ) ,
70
72
} )
71
- return ( await res . json ( ) ) as IMsg
73
+ return ( await jsonify ( res ) ) as IMsg
72
74
} ,
73
- async disableTOTPAuthentication ( token : string , data : IUserProfileUpdate ) {
75
+ async disableTOTPAuthentication ( token : string , data : IUserProfileUpdate ) : Promise < IMsg > {
74
76
const res = await fetch ( `${ apiCore . url } /login/totp` , {
75
77
method : "DELETE" ,
76
78
body : JSON . stringify ( data ) ,
77
79
headers : apiCore . headers ( token ) ,
78
80
} )
79
- return ( await res . json ( ) ) as IMsg
81
+ return ( await jsonify ( res ) ) as IMsg
80
82
} ,
81
83
// MANAGE JWT TOKENS (REFRESH / REVOKE)
82
- async getRefreshedToken ( token : string ) {
84
+ async getRefreshedToken ( token : string ) : Promise < ITokenResponse > {
83
85
const res = await fetch ( `${ apiCore . url } /login/refresh` , {
84
86
method : "POST" ,
85
87
headers : apiCore . headers ( token ) ,
86
88
} )
87
- return ( await res . json ( ) ) as ITokenResponse
89
+ return ( await jsonify ( res ) ) as ITokenResponse
88
90
} ,
89
- async revokeRefreshedToken ( token : string ) {
91
+ async revokeRefreshedToken ( token : string ) : Promise < IMsg > {
90
92
const res = await fetch ( `${ apiCore . url } /login/revoke` , {
91
93
method : "POST" ,
92
94
headers : apiCore . headers ( token ) ,
93
95
} )
94
- return ( await res . json ( ) ) as IMsg
96
+ return ( await jsonify ( res ) ) as IMsg
95
97
} ,
96
98
// USER PROFILE MANAGEMENT
97
- async createProfile ( data : IUserOpenProfileCreate ) {
99
+ async createProfile ( data : IUserOpenProfileCreate ) : Promise < IUserProfile > {
98
100
const res = await fetch ( `${ apiCore . url } /users/` , {
99
101
method : "POST" ,
100
102
body : JSON . stringify ( data ) ,
101
103
} )
102
- return ( await res . json ( ) ) as IUserProfile
104
+ return ( await jsonify ( res ) ) as IUserProfile
103
105
} ,
104
- async getProfile ( token : string ) {
106
+ async getProfile ( token : string ) : Promise < IUserProfile > {
105
107
const res = await fetch ( `${ apiCore . url } /users/` , {
106
108
headers : apiCore . headers ( token ) ,
107
109
} )
108
- return ( await res . json ( ) ) as IUserProfile
110
+ return ( await jsonify ( res ) ) as IUserProfile
109
111
} ,
110
- async updateProfile ( token : string , data : IUserProfileUpdate ) {
112
+ async updateProfile ( token : string , data : IUserProfileUpdate ) : Promise < IUserProfile > {
111
113
const res = await fetch ( `${ apiCore . url } /users/` , {
112
114
method : "PUT" ,
113
115
body : JSON . stringify ( data ) ,
114
116
headers : apiCore . headers ( token ) ,
115
117
} )
116
- return ( await res . json ( ) ) as IUserProfile
118
+ return ( await jsonify ( res ) ) as IUserProfile
117
119
} ,
118
120
// ACCOUNT RECOVERY
119
- async recoverPassword ( email : string ) {
121
+ async recoverPassword ( email : string ) : Promise < IMsg | IWebToken > {
120
122
const res = await fetch ( `${ apiCore . url } /login/recover/${ email } ` , {
121
123
method : "POST" ,
122
124
} )
123
- return ( await res . json ( ) ) as IMsg | IWebToken
125
+ return ( await jsonify ( res ) ) as IMsg | IWebToken
124
126
} ,
125
- async resetPassword ( password : string , claim : string , token : string ) {
127
+ async resetPassword ( password : string , claim : string , token : string ) : Promise < IMsg > {
126
128
const res = await fetch ( `${ apiCore . url } /login/reset` , {
127
129
method : "POST" ,
128
130
body : JSON . stringify ( {
@@ -131,44 +133,44 @@ export const apiAuth = {
131
133
} ) ,
132
134
headers : apiCore . headers ( token ) ,
133
135
} )
134
- return ( await res . json ( ) ) as IMsg
136
+ return ( await jsonify ( res ) ) as IMsg
135
137
} ,
136
- async requestValidationEmail ( token : string ) {
138
+ async requestValidationEmail ( token : string ) : Promise < IMsg > {
137
139
const res = await fetch ( `${ apiCore . url } /users/send-validation-email` , {
138
140
method : "POST" ,
139
141
headers : apiCore . headers ( token ) ,
140
142
} )
141
- return ( await res . json ( ) ) as IMsg
143
+ return ( await jsonify ( res ) ) as IMsg
142
144
} ,
143
- async validateEmail ( token : string , validation : string ) {
145
+ async validateEmail ( token : string , validation : string ) : Promise < IMsg > {
144
146
const res = await fetch ( `${ apiCore . url } /users/validate-email` , {
145
147
method : "POST" ,
146
148
body : JSON . stringify ( { validation } ) ,
147
149
headers : apiCore . headers ( token ) ,
148
150
} )
149
- return ( await res . json ( ) ) as IMsg
151
+ return ( await jsonify ( res ) ) as IMsg
150
152
} ,
151
153
// ADMIN USER MANAGEMENT
152
- async getAllUsers ( token : string ) {
154
+ async getAllUsers ( token : string ) : Promise < IUserProfile [ ] > {
153
155
const res = await fetch ( `${ apiCore . url } /users/all` , {
154
156
headers : apiCore . headers ( token ) ,
155
157
} )
156
- return ( await res . json ( ) ) as IUserProfile [ ]
158
+ return ( await jsonify ( res ) ) as IUserProfile [ ]
157
159
} ,
158
- async toggleUserState ( token : string , data : IUserProfileUpdate ) {
160
+ async toggleUserState ( token : string , data : IUserProfileUpdate ) : Promise < IMsg > {
159
161
const res = await fetch ( `${ apiCore . url } /users/toggle-state` , {
160
162
method : "POST" ,
161
163
body : JSON . stringify ( data ) ,
162
164
headers : apiCore . headers ( token ) ,
163
165
} )
164
- return ( await res . json ( ) ) as IMsg
166
+ return ( await jsonify ( res ) ) as IMsg
165
167
} ,
166
- async createUserProfile ( token : string , data : IUserProfileCreate ) {
168
+ async createUserProfile ( token : string , data : IUserProfileCreate ) : Promise < IUserProfile > {
167
169
const res = await fetch ( `${ apiCore . url } /users/create` , {
168
170
method : "POST" ,
169
171
body : JSON . stringify ( data ) ,
170
172
headers : apiCore . headers ( token ) ,
171
173
} )
172
- return ( await res . json ( ) ) as IUserProfile
174
+ return ( await jsonify ( res ) ) as IUserProfile
173
175
} ,
174
176
}
0 commit comments