16
16
*/
17
17
18
18
import { FirebaseError , querystring } from '@firebase/util' ;
19
- import { AuthErrorCode , AUTH_ERROR_FACTORY } from '../core/errors' ;
19
+
20
+ import { AUTH_ERROR_FACTORY , AuthErrorCode } from '../core/errors' ;
21
+ import { Delay } from '../core/util/delay' ;
20
22
import { Auth } from '../model/auth' ;
21
23
import { IdTokenResponse } from '../model/id_token' ;
22
- import {
23
- JsonError ,
24
- ServerError ,
25
- ServerErrorMap ,
26
- SERVER_ERROR_MAP
27
- } from './errors' ;
28
- import { Delay } from '../core/util/delay' ;
24
+ import { JsonError , SERVER_ERROR_MAP , ServerError , ServerErrorMap } from './errors' ;
29
25
30
26
export enum HttpMethod {
31
27
POST = 'POST' ,
@@ -61,10 +57,9 @@ export async function _performApiRequest<T, V>(
61
57
method : HttpMethod ,
62
58
path : Endpoint ,
63
59
request ?: T ,
64
- customErrorMap : Partial < ServerErrorMap < ServerError > > = { }
60
+ customErrorMap : Partial < ServerErrorMap < ServerError > > = { } ,
65
61
) : Promise < V > {
66
- const errorMap = { ...SERVER_ERROR_MAP , ...customErrorMap } ;
67
- try {
62
+ return performFetchWithErrorHandling ( auth , customErrorMap , ( ) => {
68
63
let body = { } ;
69
64
let params = { } ;
70
65
if ( request ) {
@@ -82,8 +77,7 @@ export async function _performApiRequest<T, V>(
82
77
...params
83
78
} ) . slice ( 1 ) ;
84
79
85
- const response : Response = await Promise . race < Promise < Response > > ( [
86
- fetch (
80
+ return fetch (
87
81
`${ auth . config . apiScheme } ://${ auth . config . apiHost } ${ path } ?${ query } ` ,
88
82
{
89
83
method,
@@ -94,16 +88,20 @@ export async function _performApiRequest<T, V>(
94
88
referrerPolicy : 'no-referrer' ,
95
89
...body
96
90
}
97
- ) ,
98
- new Promise ( ( _ , reject ) =>
99
- setTimeout ( ( ) => {
100
- return reject (
101
- AUTH_ERROR_FACTORY . create ( AuthErrorCode . TIMEOUT , {
102
- appName : auth . name
103
- } )
104
- ) ;
105
- } , DEFAULT_API_TIMEOUT_MS . get ( ) )
106
- )
91
+ ) ;
92
+ } ) ;
93
+ }
94
+
95
+ export async function performFetchWithErrorHandling < V > (
96
+ auth : Auth ,
97
+ customErrorMap : Partial < ServerErrorMap < ServerError > > ,
98
+ fetchFn : ( ) => Promise < Response > ,
99
+ ) : Promise < V > {
100
+ const errorMap = { ...SERVER_ERROR_MAP , ...customErrorMap } ;
101
+ try {
102
+ const response : Response = await Promise . race < Promise < Response > > ( [
103
+ fetchFn ( ) ,
104
+ makeNetworkTimeout ( auth . name ) ,
107
105
] ) ;
108
106
if ( response . ok ) {
109
107
return response . json ( ) ;
@@ -154,3 +152,15 @@ export async function _performSignInRequest<T, V extends IdTokenResponse>(
154
152
155
153
return serverResponse ;
156
154
}
155
+
156
+ function makeNetworkTimeout < T > ( appName : string ) : Promise < T > {
157
+ return new Promise ( ( _ , reject ) =>
158
+ setTimeout ( ( ) => {
159
+ return reject (
160
+ AUTH_ERROR_FACTORY . create ( AuthErrorCode . TIMEOUT , {
161
+ appName,
162
+ } )
163
+ ) ;
164
+ } , DEFAULT_API_TIMEOUT_MS . get ( ) )
165
+ )
166
+ }
0 commit comments