Skip to content

Commit a2d0c77

Browse files
committed
fix: Support apiToken to be an async function: first request sends incorrect token
1 parent e712ac6 commit a2d0c77

File tree

7 files changed

+256
-91
lines changed

7 files changed

+256
-91
lines changed

docs/Cube.js-Frontend/@cubejs-client-core.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ API entry point.
2828

2929
- `apiToken` - [API token](security) is used to authorize requests and determine SQL database you're accessing.
3030
In the development mode, Cube.js Backend will print the API token to the console on on startup.
31+
Can be an async function without arguments that returns API token.
3132
- `options` - options object.
3233
- `options.apiUrl` - URL of your Cube.js Backend.
3334
By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`.

packages/cubejs-client-core/dist/cubejs-client-core.esm.js

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ function () {
842842
this.apiToken = apiToken;
843843
this.apiUrl = options.apiUrl || API_URL;
844844
this.transport = options.transport || new HttpTransport({
845-
authorization: apiToken,
845+
authorization: typeof apiToken === 'function' ? undefined : apiToken,
846846
apiUrl: this.apiUrl
847847
});
848848
this.pollInterval = options.pollInterval || 5;
@@ -872,7 +872,9 @@ function () {
872872
options.mutexObj[mutexKey] = mutexValue;
873873
}
874874

875-
var requestInstance = request();
875+
var requestPromise = this.updateTransportAuthorization().then(function () {
876+
return request();
877+
});
876878
var unsubscribed = false;
877879

878880
var checkMutex =
@@ -881,29 +883,36 @@ function () {
881883
var _ref = _asyncToGenerator(
882884
/*#__PURE__*/
883885
_regeneratorRuntime.mark(function _callee() {
886+
var requestInstance;
884887
return _regeneratorRuntime.wrap(function _callee$(_context) {
885888
while (1) {
886889
switch (_context.prev = _context.next) {
887890
case 0:
891+
_context.next = 2;
892+
return requestPromise;
893+
894+
case 2:
895+
requestInstance = _context.sent;
896+
888897
if (!(options.mutexObj && options.mutexObj[mutexKey] !== mutexValue)) {
889-
_context.next = 6;
898+
_context.next = 9;
890899
break;
891900
}
892901

893902
unsubscribed = true;
894903

895904
if (!requestInstance.unsubscribe) {
896-
_context.next = 5;
905+
_context.next = 8;
897906
break;
898907
}
899908

900-
_context.next = 5;
909+
_context.next = 8;
901910
return requestInstance.unsubscribe();
902911

903-
case 5:
912+
case 8:
904913
throw MUTEX_ERROR;
905914

906-
case 6:
915+
case 9:
907916
case "end":
908917
return _context.stop();
909918
}
@@ -922,11 +931,17 @@ function () {
922931
var _ref2 = _asyncToGenerator(
923932
/*#__PURE__*/
924933
_regeneratorRuntime.mark(function _callee4(response, next) {
925-
var subscribeNext, continueWait, token, body, error, result;
934+
var requestInstance, subscribeNext, continueWait, body, error, result;
926935
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
927936
while (1) {
928937
switch (_context4.prev = _context4.next) {
929938
case 0:
939+
_context4.next = 2;
940+
return requestPromise;
941+
942+
case 2:
943+
requestInstance = _context4.sent;
944+
930945
subscribeNext =
931946
/*#__PURE__*/
932947
function () {
@@ -1022,20 +1037,8 @@ function () {
10221037
};
10231038
}();
10241039

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();
10391042

10401043
case 7:
10411044
if (!(response.status === 502)) {
@@ -1151,31 +1154,39 @@ function () {
11511154
};
11521155
}();
11531156

1154-
var promise = mutexPromise(requestInstance.subscribe(loadImpl));
1157+
var promise = requestPromise.then(function (requestInstance) {
1158+
return mutexPromise(requestInstance.subscribe(loadImpl));
1159+
});
11551160

11561161
if (callback) {
11571162
return {
11581163
unsubscribe: function () {
11591164
var _unsubscribe = _asyncToGenerator(
11601165
/*#__PURE__*/
11611166
_regeneratorRuntime.mark(function _callee5() {
1167+
var requestInstance;
11621168
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
11631169
while (1) {
11641170
switch (_context5.prev = _context5.next) {
11651171
case 0:
1172+
_context5.next = 2;
1173+
return requestPromise;
1174+
1175+
case 2:
1176+
requestInstance = _context5.sent;
11661177
unsubscribed = true;
11671178

11681179
if (!requestInstance.unsubscribe) {
1169-
_context5.next = 3;
1180+
_context5.next = 6;
11701181
break;
11711182
}
11721183

11731184
return _context5.abrupt("return", requestInstance.unsubscribe());
11741185

1175-
case 3:
1186+
case 6:
11761187
return _context5.abrupt("return", null);
11771188

1178-
case 4:
1189+
case 7:
11791190
case "end":
11801191
return _context5.stop();
11811192
}
@@ -1192,6 +1203,44 @@ function () {
11921203
return promise;
11931204
}
11941205
}
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+
}()
11951244
/**
11961245
* Fetch data for passed `query`.
11971246
*
@@ -1306,6 +1355,7 @@ function () {
13061355
* @name cubejs
13071356
* @param apiToken - [API token](security) is used to authorize requests and determine SQL database you're accessing.
13081357
* 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.
13091359
* @param options - options object.
13101360
* @param options.apiUrl - URL of your Cube.js Backend.
13111361
* By default, in the development environment it is `http://localhost:4000/cubejs-api/v1`.

0 commit comments

Comments
 (0)