@@ -842,7 +842,7 @@ function () {
842
842
this . apiToken = apiToken ;
843
843
this . apiUrl = options . apiUrl || API_URL ;
844
844
this . transport = options . transport || new HttpTransport ( {
845
- authorization : apiToken ,
845
+ authorization : typeof apiToken === 'function' ? undefined : apiToken ,
846
846
apiUrl : this . apiUrl
847
847
} ) ;
848
848
this . pollInterval = options . pollInterval || 5 ;
@@ -872,7 +872,9 @@ function () {
872
872
options . mutexObj [ mutexKey ] = mutexValue ;
873
873
}
874
874
875
- var requestInstance = request ( ) ;
875
+ var requestPromise = this . updateTransportAuthorization ( ) . then ( function ( ) {
876
+ return request ( ) ;
877
+ } ) ;
876
878
var unsubscribed = false ;
877
879
878
880
var checkMutex =
@@ -881,29 +883,36 @@ function () {
881
883
var _ref = _asyncToGenerator (
882
884
/*#__PURE__*/
883
885
_regeneratorRuntime . mark ( function _callee ( ) {
886
+ var requestInstance ;
884
887
return _regeneratorRuntime . wrap ( function _callee$ ( _context ) {
885
888
while ( 1 ) {
886
889
switch ( _context . prev = _context . next ) {
887
890
case 0 :
891
+ _context . next = 2 ;
892
+ return requestPromise ;
893
+
894
+ case 2 :
895
+ requestInstance = _context . sent ;
896
+
888
897
if ( ! ( options . mutexObj && options . mutexObj [ mutexKey ] !== mutexValue ) ) {
889
- _context . next = 6 ;
898
+ _context . next = 9 ;
890
899
break ;
891
900
}
892
901
893
902
unsubscribed = true ;
894
903
895
904
if ( ! requestInstance . unsubscribe ) {
896
- _context . next = 5 ;
905
+ _context . next = 8 ;
897
906
break ;
898
907
}
899
908
900
- _context . next = 5 ;
909
+ _context . next = 8 ;
901
910
return requestInstance . unsubscribe ( ) ;
902
911
903
- case 5 :
912
+ case 8 :
904
913
throw MUTEX_ERROR ;
905
914
906
- case 6 :
915
+ case 9 :
907
916
case "end" :
908
917
return _context . stop ( ) ;
909
918
}
@@ -922,11 +931,17 @@ function () {
922
931
var _ref2 = _asyncToGenerator (
923
932
/*#__PURE__*/
924
933
_regeneratorRuntime . mark ( function _callee4 ( response , next ) {
925
- var subscribeNext , continueWait , token , body , error , result ;
934
+ var requestInstance , subscribeNext , continueWait , body , error , result ;
926
935
return _regeneratorRuntime . wrap ( function _callee4$ ( _context4 ) {
927
936
while ( 1 ) {
928
937
switch ( _context4 . prev = _context4 . next ) {
929
938
case 0 :
939
+ _context4 . next = 2 ;
940
+ return requestPromise ;
941
+
942
+ case 2 :
943
+ requestInstance = _context4 . sent ;
944
+
930
945
subscribeNext =
931
946
/*#__PURE__*/
932
947
function ( ) {
@@ -1022,20 +1037,8 @@ function () {
1022
1037
} ;
1023
1038
} ( ) ;
1024
1039
1025
- if ( ! ( typeof _this . apiToken === 'function' ) ) {
1026
- _context4 . next = 7 ;
1027
- break ;
1028
- }
1029
-
1030
- _context4 . next = 5 ;
1031
- return _this . apiToken ( ) ;
1032
-
1033
- case 5 :
1034
- token = _context4 . sent ;
1035
-
1036
- if ( _this . transport . authorization !== token ) {
1037
- _this . transport . authorization = token ;
1038
- }
1040
+ _context4 . next = 7 ;
1041
+ return _this . updateTransportAuthorization ( ) ;
1039
1042
1040
1043
case 7 :
1041
1044
if ( ! ( response . status === 502 ) ) {
@@ -1151,31 +1154,39 @@ function () {
1151
1154
} ;
1152
1155
} ( ) ;
1153
1156
1154
- var promise = mutexPromise ( requestInstance . subscribe ( loadImpl ) ) ;
1157
+ var promise = requestPromise . then ( function ( requestInstance ) {
1158
+ return mutexPromise ( requestInstance . subscribe ( loadImpl ) ) ;
1159
+ } ) ;
1155
1160
1156
1161
if ( callback ) {
1157
1162
return {
1158
1163
unsubscribe : function ( ) {
1159
1164
var _unsubscribe = _asyncToGenerator (
1160
1165
/*#__PURE__*/
1161
1166
_regeneratorRuntime . mark ( function _callee5 ( ) {
1167
+ var requestInstance ;
1162
1168
return _regeneratorRuntime . wrap ( function _callee5$ ( _context5 ) {
1163
1169
while ( 1 ) {
1164
1170
switch ( _context5 . prev = _context5 . next ) {
1165
1171
case 0 :
1172
+ _context5 . next = 2 ;
1173
+ return requestPromise ;
1174
+
1175
+ case 2 :
1176
+ requestInstance = _context5 . sent ;
1166
1177
unsubscribed = true ;
1167
1178
1168
1179
if ( ! requestInstance . unsubscribe ) {
1169
- _context5 . next = 3 ;
1180
+ _context5 . next = 6 ;
1170
1181
break ;
1171
1182
}
1172
1183
1173
1184
return _context5 . abrupt ( "return" , requestInstance . unsubscribe ( ) ) ;
1174
1185
1175
- case 3 :
1186
+ case 6 :
1176
1187
return _context5 . abrupt ( "return" , null ) ;
1177
1188
1178
- case 4 :
1189
+ case 7 :
1179
1190
case "end" :
1180
1191
return _context5 . stop ( ) ;
1181
1192
}
@@ -1192,6 +1203,44 @@ function () {
1192
1203
return promise ;
1193
1204
}
1194
1205
}
1206
+ } , {
1207
+ key : "updateTransportAuthorization" ,
1208
+ value : function ( ) {
1209
+ var _updateTransportAuthorization = _asyncToGenerator (
1210
+ /*#__PURE__*/
1211
+ _regeneratorRuntime . mark ( function _callee6 ( ) {
1212
+ var token ;
1213
+ return _regeneratorRuntime . wrap ( function _callee6$ ( _context6 ) {
1214
+ while ( 1 ) {
1215
+ switch ( _context6 . prev = _context6 . next ) {
1216
+ case 0 :
1217
+ if ( ! ( typeof this . apiToken === 'function' ) ) {
1218
+ _context6 . next = 5 ;
1219
+ break ;
1220
+ }
1221
+
1222
+ _context6 . next = 3 ;
1223
+ return this . apiToken ( ) ;
1224
+
1225
+ case 3 :
1226
+ token = _context6 . sent ;
1227
+
1228
+ if ( this . transport . authorization !== token ) {
1229
+ this . transport . authorization = token ;
1230
+ }
1231
+
1232
+ case 5 :
1233
+ case "end" :
1234
+ return _context6 . stop ( ) ;
1235
+ }
1236
+ }
1237
+ } , _callee6 , this ) ;
1238
+ } ) ) ;
1239
+
1240
+ return function updateTransportAuthorization ( ) {
1241
+ return _updateTransportAuthorization . apply ( this , arguments ) ;
1242
+ } ;
1243
+ } ( )
1195
1244
/**
1196
1245
* Fetch data for passed `query`.
1197
1246
*
@@ -1306,6 +1355,7 @@ function () {
1306
1355
* @name cubejs
1307
1356
* @param apiToken - [API token](security) is used to authorize requests and determine SQL database you're accessing.
1308
1357
* In the development mode, Cube.js Backend will print the API token to the console on on startup.
1358
+ * Can be an async function without arguments that returns API token.
1309
1359
* @param options - options object.
1310
1360
* @param options.apiUrl - URL of your Cube.js Backend.
1311
1361
* By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`.
0 commit comments