( path: string, body?: Record<string, unknown>, )
| 57 | } |
| 58 | |
| 59 | const oauthRequest = async <T>( |
| 60 | path: string, |
| 61 | body?: Record<string, unknown>, |
| 62 | ): Promise<T> => { |
| 63 | const res = await fetch(`${apiUrl}/auth/oauth2/${path}`, { |
| 64 | method: body ? 'POST' : 'GET', |
| 65 | credentials: 'include', |
| 66 | headers: { |
| 67 | Accept: 'application/json', |
| 68 | ...(body && { 'Content-Type': 'application/json' }), |
| 69 | }, |
| 70 | ...(body && { body: JSON.stringify(body) }), |
| 71 | }); |
| 72 | const data = await res.json().catch(() => null); |
| 73 | |
| 74 | if (!res.ok) { |
| 75 | throw new OAuthRequestError( |
| 76 | getBetterAuthErrorMessage(data?.error_description ?? data), |
| 77 | res.status, |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | return data as T; |
| 82 | }; |
| 83 | |
| 84 | export const getOAuthClients = (): Promise<OAuthClient[]> => |
| 85 | oauthRequest<OAuthClient[]>('get-clients'); |
no test coverage detected