@bytescale/sdk 3.0.0-alpha.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,7 +42,7 @@ __lib_require__.d(__lib_exports__, {
42
42
  "AuthManager": function() { return /* reexport */ AuthManager; },
43
43
  "BaseAPI": function() { return /* reexport */ BaseAPI; },
44
44
  "BinaryResult": function() { return /* reexport */ BinaryResult; },
45
- "BytescaleApiClientConfigResolver": function() { return /* reexport */ BytescaleApiClientConfigResolver; },
45
+ "BytescaleApiClientConfigUtils": function() { return /* reexport */ BytescaleApiClientConfigUtils; },
46
46
  "BytescaleApiError": function() { return /* reexport */ BytescaleApiError; },
47
47
  "CancelledError": function() { return /* reexport */ CancelledError; },
48
48
  "CommonTypesNoOp": function() { return /* reexport */ CommonTypesNoOp; },
@@ -118,21 +118,21 @@ function runtime_createClass(Constructor, protoProps, staticProps) { if (protoPr
118
118
  function runtime_toPropertyKey(arg) { var key = runtime_toPrimitive(arg, "string"); return runtime_typeof(key) === "symbol" ? key : String(key); }
119
119
  function runtime_toPrimitive(input, hint) { if (runtime_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (runtime_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
120
120
 
121
- var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
122
- function BytescaleApiClientConfigResolver() {
123
- runtime_classCallCheck(this, BytescaleApiClientConfigResolver);
121
+ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
122
+ function BytescaleApiClientConfigUtils() {
123
+ runtime_classCallCheck(this, BytescaleApiClientConfigUtils);
124
124
  }
125
- runtime_createClass(BytescaleApiClientConfigResolver, null, [{
125
+ runtime_createClass(BytescaleApiClientConfigUtils, null, [{
126
126
  key: "getApiUrl",
127
127
  value: function getApiUrl(config) {
128
128
  var _a;
129
- return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultApiUrl;
129
+ return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultApiUrl;
130
130
  }
131
131
  }, {
132
132
  key: "getCdnUrl",
133
133
  value: function getCdnUrl(config) {
134
134
  var _a;
135
- return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
135
+ return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
136
136
  }
137
137
  }, {
138
138
  key: "getFetchApi",
@@ -140,11 +140,48 @@ var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
140
140
  var _a;
141
141
  return (_a = config.fetchApi) !== null && _a !== void 0 ? _a : fetch;
142
142
  }
143
+ }, {
144
+ key: "getAccountId",
145
+ value: function getAccountId(config) {
146
+ var _a, _b;
147
+ var accountId;
148
+ if (BytescaleApiClientConfigUtils.specialApiKeys.includes(config.apiKey)) {
149
+ accountId = BytescaleApiClientConfigUtils.specialApiKeyAccountId;
150
+ } else {
151
+ accountId = (_b = (_a = config.apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BytescaleApiClientConfigUtils.accountIdLength)) !== null && _b !== void 0 ? _b : "";
152
+ if (accountId.length !== BytescaleApiClientConfigUtils.accountIdLength) {
153
+ throw new Error("Invalid Bytescale API key.");
154
+ }
155
+ }
156
+ return accountId;
157
+ }
158
+ }, {
159
+ key: "validate",
160
+ value: function validate(config) {
161
+ var _a;
162
+ // Defensive programming, for users not using TypeScript. Mainly because this is used by UploadWidget users.
163
+ if ((config !== null && config !== void 0 ? config : undefined) === undefined) {
164
+ throw new Error("Config parameter required.");
165
+ }
166
+ if (((_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined) === undefined) {
167
+ throw new Error("Please provide an API key via the 'apiKey' config parameter.");
168
+ }
169
+ if (config.apiKey.trim() !== config.apiKey) {
170
+ // We do not support API keys with whitespace (by trimming ourselves) because otherwise we'd need to support this
171
+ // everywhere in perpetuity (since removing the trimming would be a breaking change).
172
+ throw new Error("API key needs trimming (whitespace detected).");
173
+ }
174
+ // This performs futher validation on the API key...
175
+ BytescaleApiClientConfigUtils.getAccountId(config);
176
+ }
143
177
  }]);
144
- return BytescaleApiClientConfigResolver;
178
+ return BytescaleApiClientConfigUtils;
145
179
  }();
146
- BytescaleApiClientConfigResolver.defaultApiUrl = "https://api.bytescale.com";
147
- BytescaleApiClientConfigResolver.defaultCdnUrl = "https://upcdn.io";
180
+ BytescaleApiClientConfigUtils.defaultApiUrl = "https://api.bytescale.com";
181
+ BytescaleApiClientConfigUtils.defaultCdnUrl = "https://upcdn.io";
182
+ BytescaleApiClientConfigUtils.specialApiKeys = ["free", "demo"];
183
+ BytescaleApiClientConfigUtils.specialApiKeyAccountId = "W142hJk";
184
+ BytescaleApiClientConfigUtils.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
148
185
  /**
149
186
  * This is the base class for all generated API classes.
150
187
  */
@@ -152,6 +189,7 @@ var BaseAPI = /*#__PURE__*/function () {
152
189
  function BaseAPI(config) {
153
190
  runtime_classCallCheck(this, BaseAPI);
154
191
  this.config = config;
192
+ BytescaleApiClientConfigUtils.validate(config);
155
193
  }
156
194
  runtime_createClass(BaseAPI, [{
157
195
  key: "request",
@@ -166,7 +204,7 @@ var BaseAPI = /*#__PURE__*/function () {
166
204
  }
167
205
  // Key: any possible value for 'baseUrlOverride'
168
206
  // Value: user-overridden value for that base URL from the config.
169
- var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigResolver.defaultCdnUrl, BytescaleApiClientConfigResolver.getCdnUrl(_this2.config));
207
+ var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this2.config));
170
208
  return _await(_this2.createFetchParams(context, initOverrides, baseUrlOverride === undefined ? undefined : (_b = nonDefaultBasePaths[baseUrlOverride]) !== null && _b !== void 0 ? _b : baseUrlOverride), function (_ref) {
171
209
  var url = _ref.url,
172
210
  init = _ref.init;
@@ -188,7 +226,7 @@ var BaseAPI = /*#__PURE__*/function () {
188
226
  var _a, _b;
189
227
  var response;
190
228
  return _continue(_catch(function () {
191
- return _await(BytescaleApiClientConfigResolver.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
229
+ return _await(BytescaleApiClientConfigUtils.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
192
230
  response = _BytescaleApiClientCo;
193
231
  });
194
232
  }, function (e) {
@@ -242,7 +280,7 @@ var BaseAPI = /*#__PURE__*/function () {
242
280
  value: function createFetchParams(context, initOverrides, baseUrlOverride) {
243
281
  try {
244
282
  var _this6 = this;
245
- var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigResolver.getApiUrl(_this6.config)) + context.path;
283
+ var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigUtils.getApiUrl(_this6.config)) + context.path;
246
284
  if (context.query !== undefined && Object.keys(context.query).length !== 0) {
247
285
  // only add the querystring to the URL if there are query parameters.
248
286
  // this is done to avoid urls ending with a "?" character which buggy webservers
@@ -286,9 +324,6 @@ var BaseAPI = /*#__PURE__*/function () {
286
324
  }]);
287
325
  return BaseAPI;
288
326
  }();
289
- BaseAPI.specialApiKeys = ["free", "demo"];
290
- BaseAPI.specialApiKeyAccountId = "W142hJk";
291
- BaseAPI.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
292
327
  var CancelledError = /*#__PURE__*/function (_Error) {
293
328
  _inherits(CancelledError, _Error);
294
329
  var _super = _createSuper(CancelledError);
@@ -1340,7 +1375,7 @@ var UrlBuilder = /*#__PURE__*/function () {
1340
1375
  function UrlBuilder(config) {
1341
1376
  UrlBuilder_classCallCheck(this, UrlBuilder);
1342
1377
  var _a;
1343
- this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
1378
+ this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
1344
1379
  }
1345
1380
  /**
1346
1381
  * Builds a URL to either a raw file or a transformed file.
@@ -2446,7 +2481,6 @@ var AuthManager = /*#__PURE__*/function (_BaseAPI) {
2446
2481
  function AuthManager(config) {
2447
2482
  var _this;
2448
2483
  AuthManagerBrowser_classCallCheck(this, AuthManager);
2449
- var _a, _b;
2450
2484
  _this = _super.call(this, config);
2451
2485
  _this.authSessionMutex = new Mutex();
2452
2486
  _this.contentType = "content-type";
@@ -2455,16 +2489,9 @@ var AuthManager = /*#__PURE__*/function (_BaseAPI) {
2455
2489
  _this.minJwtTtlSeconds = 10;
2456
2490
  _this.retryAuthAfterErrorSeconds = 5;
2457
2491
  _this.refreshBeforeExpirySeconds = 20;
2458
- var accountId;
2459
- if (BaseAPI.specialApiKeys.includes(config.apiKey)) {
2460
- accountId = BaseAPI.specialApiKeyAccountId;
2461
- } else {
2462
- accountId = (_b = (_a = config.apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BaseAPI.accountIdLength)) !== null && _b !== void 0 ? _b : "";
2463
- if (accountId.length !== BaseAPI.accountIdLength) {
2464
- throw new Error("Invalid Bytescale API key.");
2465
- }
2466
- }
2467
- _this.accessTokenUrl = "".concat(BytescaleApiClientConfigResolver.getCdnUrl(config), "/api/v1/access_tokens/").concat(accountId);
2492
+ var cdnUrl = BytescaleApiClientConfigUtils.getCdnUrl(config);
2493
+ var accountId = BytescaleApiClientConfigUtils.getAccountId(config);
2494
+ _this.accessTokenUrl = "".concat(cdnUrl, "/api/v1/access_tokens/").concat(accountId);
2468
2495
  return _this;
2469
2496
  }
2470
2497
  AuthManagerBrowser_createClass(AuthManager, [{
@@ -64,7 +64,7 @@ __lib_require__.d(__lib_exports__, {
64
64
  "AuthManager": function() { return /* reexport */ AuthManager; },
65
65
  "BaseAPI": function() { return /* reexport */ BaseAPI; },
66
66
  "BinaryResult": function() { return /* reexport */ BinaryResult; },
67
- "BytescaleApiClientConfigResolver": function() { return /* reexport */ BytescaleApiClientConfigResolver; },
67
+ "BytescaleApiClientConfigUtils": function() { return /* reexport */ BytescaleApiClientConfigUtils; },
68
68
  "BytescaleApiError": function() { return /* reexport */ BytescaleApiError; },
69
69
  "CancelledError": function() { return /* reexport */ CancelledError; },
70
70
  "CommonTypesNoOp": function() { return /* reexport */ CommonTypesNoOp; },
@@ -140,21 +140,21 @@ function runtime_createClass(Constructor, protoProps, staticProps) { if (protoPr
140
140
  function runtime_toPropertyKey(arg) { var key = runtime_toPrimitive(arg, "string"); return runtime_typeof(key) === "symbol" ? key : String(key); }
141
141
  function runtime_toPrimitive(input, hint) { if (runtime_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (runtime_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
142
142
 
143
- var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
144
- function BytescaleApiClientConfigResolver() {
145
- runtime_classCallCheck(this, BytescaleApiClientConfigResolver);
143
+ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
144
+ function BytescaleApiClientConfigUtils() {
145
+ runtime_classCallCheck(this, BytescaleApiClientConfigUtils);
146
146
  }
147
- runtime_createClass(BytescaleApiClientConfigResolver, null, [{
147
+ runtime_createClass(BytescaleApiClientConfigUtils, null, [{
148
148
  key: "getApiUrl",
149
149
  value: function getApiUrl(config) {
150
150
  var _a;
151
- return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultApiUrl;
151
+ return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultApiUrl;
152
152
  }
153
153
  }, {
154
154
  key: "getCdnUrl",
155
155
  value: function getCdnUrl(config) {
156
156
  var _a;
157
- return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
157
+ return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
158
158
  }
159
159
  }, {
160
160
  key: "getFetchApi",
@@ -162,11 +162,48 @@ var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
162
162
  var _a;
163
163
  return (_a = config.fetchApi) !== null && _a !== void 0 ? _a : fetch;
164
164
  }
165
+ }, {
166
+ key: "getAccountId",
167
+ value: function getAccountId(config) {
168
+ var _a, _b;
169
+ var accountId;
170
+ if (BytescaleApiClientConfigUtils.specialApiKeys.includes(config.apiKey)) {
171
+ accountId = BytescaleApiClientConfigUtils.specialApiKeyAccountId;
172
+ } else {
173
+ accountId = (_b = (_a = config.apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BytescaleApiClientConfigUtils.accountIdLength)) !== null && _b !== void 0 ? _b : "";
174
+ if (accountId.length !== BytescaleApiClientConfigUtils.accountIdLength) {
175
+ throw new Error("Invalid Bytescale API key.");
176
+ }
177
+ }
178
+ return accountId;
179
+ }
180
+ }, {
181
+ key: "validate",
182
+ value: function validate(config) {
183
+ var _a;
184
+ // Defensive programming, for users not using TypeScript. Mainly because this is used by UploadWidget users.
185
+ if ((config !== null && config !== void 0 ? config : undefined) === undefined) {
186
+ throw new Error("Config parameter required.");
187
+ }
188
+ if (((_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined) === undefined) {
189
+ throw new Error("Please provide an API key via the 'apiKey' config parameter.");
190
+ }
191
+ if (config.apiKey.trim() !== config.apiKey) {
192
+ // We do not support API keys with whitespace (by trimming ourselves) because otherwise we'd need to support this
193
+ // everywhere in perpetuity (since removing the trimming would be a breaking change).
194
+ throw new Error("API key needs trimming (whitespace detected).");
195
+ }
196
+ // This performs futher validation on the API key...
197
+ BytescaleApiClientConfigUtils.getAccountId(config);
198
+ }
165
199
  }]);
166
- return BytescaleApiClientConfigResolver;
200
+ return BytescaleApiClientConfigUtils;
167
201
  }();
168
- BytescaleApiClientConfigResolver.defaultApiUrl = "https://api.bytescale.com";
169
- BytescaleApiClientConfigResolver.defaultCdnUrl = "https://upcdn.io";
202
+ BytescaleApiClientConfigUtils.defaultApiUrl = "https://api.bytescale.com";
203
+ BytescaleApiClientConfigUtils.defaultCdnUrl = "https://upcdn.io";
204
+ BytescaleApiClientConfigUtils.specialApiKeys = ["free", "demo"];
205
+ BytescaleApiClientConfigUtils.specialApiKeyAccountId = "W142hJk";
206
+ BytescaleApiClientConfigUtils.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
170
207
  /**
171
208
  * This is the base class for all generated API classes.
172
209
  */
@@ -174,6 +211,7 @@ var BaseAPI = /*#__PURE__*/function () {
174
211
  function BaseAPI(config) {
175
212
  runtime_classCallCheck(this, BaseAPI);
176
213
  this.config = config;
214
+ BytescaleApiClientConfigUtils.validate(config);
177
215
  }
178
216
  runtime_createClass(BaseAPI, [{
179
217
  key: "request",
@@ -188,7 +226,7 @@ var BaseAPI = /*#__PURE__*/function () {
188
226
  }
189
227
  // Key: any possible value for 'baseUrlOverride'
190
228
  // Value: user-overridden value for that base URL from the config.
191
- var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigResolver.defaultCdnUrl, BytescaleApiClientConfigResolver.getCdnUrl(_this2.config));
229
+ var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this2.config));
192
230
  return _await(_this2.createFetchParams(context, initOverrides, baseUrlOverride === undefined ? undefined : (_b = nonDefaultBasePaths[baseUrlOverride]) !== null && _b !== void 0 ? _b : baseUrlOverride), function (_ref) {
193
231
  var url = _ref.url,
194
232
  init = _ref.init;
@@ -210,7 +248,7 @@ var BaseAPI = /*#__PURE__*/function () {
210
248
  var _a, _b;
211
249
  var response;
212
250
  return _continue(_catch(function () {
213
- return _await(BytescaleApiClientConfigResolver.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
251
+ return _await(BytescaleApiClientConfigUtils.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
214
252
  response = _BytescaleApiClientCo;
215
253
  });
216
254
  }, function (e) {
@@ -264,7 +302,7 @@ var BaseAPI = /*#__PURE__*/function () {
264
302
  value: function createFetchParams(context, initOverrides, baseUrlOverride) {
265
303
  try {
266
304
  var _this6 = this;
267
- var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigResolver.getApiUrl(_this6.config)) + context.path;
305
+ var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigUtils.getApiUrl(_this6.config)) + context.path;
268
306
  if (context.query !== undefined && Object.keys(context.query).length !== 0) {
269
307
  // only add the querystring to the URL if there are query parameters.
270
308
  // this is done to avoid urls ending with a "?" character which buggy webservers
@@ -308,9 +346,6 @@ var BaseAPI = /*#__PURE__*/function () {
308
346
  }]);
309
347
  return BaseAPI;
310
348
  }();
311
- BaseAPI.specialApiKeys = ["free", "demo"];
312
- BaseAPI.specialApiKeyAccountId = "W142hJk";
313
- BaseAPI.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
314
349
  var CancelledError = /*#__PURE__*/function (_Error) {
315
350
  _inherits(CancelledError, _Error);
316
351
  var _super = _createSuper(CancelledError);
@@ -1362,7 +1397,7 @@ var UrlBuilder = /*#__PURE__*/function () {
1362
1397
  function UrlBuilder(config) {
1363
1398
  UrlBuilder_classCallCheck(this, UrlBuilder);
1364
1399
  var _a;
1365
- this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
1400
+ this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
1366
1401
  }
1367
1402
  /**
1368
1403
  * Builds a URL to either a raw file or a transformed file.
@@ -51,7 +51,7 @@ __lib_require__.d(__lib_exports__, {
51
51
  "AuthManager": function() { return /* reexport */ AuthManager; },
52
52
  "BaseAPI": function() { return /* reexport */ BaseAPI; },
53
53
  "BinaryResult": function() { return /* reexport */ BinaryResult; },
54
- "BytescaleApiClientConfigResolver": function() { return /* reexport */ BytescaleApiClientConfigResolver; },
54
+ "BytescaleApiClientConfigUtils": function() { return /* reexport */ BytescaleApiClientConfigUtils; },
55
55
  "BytescaleApiError": function() { return /* reexport */ BytescaleApiError; },
56
56
  "CancelledError": function() { return /* reexport */ CancelledError; },
57
57
  "CommonTypesNoOp": function() { return /* reexport */ CommonTypesNoOp; },
@@ -127,21 +127,21 @@ function runtime_createClass(Constructor, protoProps, staticProps) { if (protoPr
127
127
  function runtime_toPropertyKey(arg) { var key = runtime_toPrimitive(arg, "string"); return runtime_typeof(key) === "symbol" ? key : String(key); }
128
128
  function runtime_toPrimitive(input, hint) { if (runtime_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (runtime_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
129
129
 
130
- var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
131
- function BytescaleApiClientConfigResolver() {
132
- runtime_classCallCheck(this, BytescaleApiClientConfigResolver);
130
+ var BytescaleApiClientConfigUtils = /*#__PURE__*/function () {
131
+ function BytescaleApiClientConfigUtils() {
132
+ runtime_classCallCheck(this, BytescaleApiClientConfigUtils);
133
133
  }
134
- runtime_createClass(BytescaleApiClientConfigResolver, null, [{
134
+ runtime_createClass(BytescaleApiClientConfigUtils, null, [{
135
135
  key: "getApiUrl",
136
136
  value: function getApiUrl(config) {
137
137
  var _a;
138
- return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultApiUrl;
138
+ return (_a = config.apiUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultApiUrl;
139
139
  }
140
140
  }, {
141
141
  key: "getCdnUrl",
142
142
  value: function getCdnUrl(config) {
143
143
  var _a;
144
- return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
144
+ return (_a = config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
145
145
  }
146
146
  }, {
147
147
  key: "getFetchApi",
@@ -149,11 +149,48 @@ var BytescaleApiClientConfigResolver = /*#__PURE__*/function () {
149
149
  var _a;
150
150
  return (_a = config.fetchApi) !== null && _a !== void 0 ? _a : fetch;
151
151
  }
152
+ }, {
153
+ key: "getAccountId",
154
+ value: function getAccountId(config) {
155
+ var _a, _b;
156
+ var accountId;
157
+ if (BytescaleApiClientConfigUtils.specialApiKeys.includes(config.apiKey)) {
158
+ accountId = BytescaleApiClientConfigUtils.specialApiKeyAccountId;
159
+ } else {
160
+ accountId = (_b = (_a = config.apiKey.split("_")[1]) === null || _a === void 0 ? void 0 : _a.substr(0, BytescaleApiClientConfigUtils.accountIdLength)) !== null && _b !== void 0 ? _b : "";
161
+ if (accountId.length !== BytescaleApiClientConfigUtils.accountIdLength) {
162
+ throw new Error("Invalid Bytescale API key.");
163
+ }
164
+ }
165
+ return accountId;
166
+ }
167
+ }, {
168
+ key: "validate",
169
+ value: function validate(config) {
170
+ var _a;
171
+ // Defensive programming, for users not using TypeScript. Mainly because this is used by UploadWidget users.
172
+ if ((config !== null && config !== void 0 ? config : undefined) === undefined) {
173
+ throw new Error("Config parameter required.");
174
+ }
175
+ if (((_a = config.apiKey) !== null && _a !== void 0 ? _a : undefined) === undefined) {
176
+ throw new Error("Please provide an API key via the 'apiKey' config parameter.");
177
+ }
178
+ if (config.apiKey.trim() !== config.apiKey) {
179
+ // We do not support API keys with whitespace (by trimming ourselves) because otherwise we'd need to support this
180
+ // everywhere in perpetuity (since removing the trimming would be a breaking change).
181
+ throw new Error("API key needs trimming (whitespace detected).");
182
+ }
183
+ // This performs futher validation on the API key...
184
+ BytescaleApiClientConfigUtils.getAccountId(config);
185
+ }
152
186
  }]);
153
- return BytescaleApiClientConfigResolver;
187
+ return BytescaleApiClientConfigUtils;
154
188
  }();
155
- BytescaleApiClientConfigResolver.defaultApiUrl = "https://api.bytescale.com";
156
- BytescaleApiClientConfigResolver.defaultCdnUrl = "https://upcdn.io";
189
+ BytescaleApiClientConfigUtils.defaultApiUrl = "https://api.bytescale.com";
190
+ BytescaleApiClientConfigUtils.defaultCdnUrl = "https://upcdn.io";
191
+ BytescaleApiClientConfigUtils.specialApiKeys = ["free", "demo"];
192
+ BytescaleApiClientConfigUtils.specialApiKeyAccountId = "W142hJk";
193
+ BytescaleApiClientConfigUtils.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
157
194
  /**
158
195
  * This is the base class for all generated API classes.
159
196
  */
@@ -161,6 +198,7 @@ var BaseAPI = /*#__PURE__*/function () {
161
198
  function BaseAPI(config) {
162
199
  runtime_classCallCheck(this, BaseAPI);
163
200
  this.config = config;
201
+ BytescaleApiClientConfigUtils.validate(config);
164
202
  }
165
203
  runtime_createClass(BaseAPI, [{
166
204
  key: "request",
@@ -175,7 +213,7 @@ var BaseAPI = /*#__PURE__*/function () {
175
213
  }
176
214
  // Key: any possible value for 'baseUrlOverride'
177
215
  // Value: user-overridden value for that base URL from the config.
178
- var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigResolver.defaultCdnUrl, BytescaleApiClientConfigResolver.getCdnUrl(_this2.config));
216
+ var nonDefaultBasePaths = _defineProperty({}, BytescaleApiClientConfigUtils.defaultCdnUrl, BytescaleApiClientConfigUtils.getCdnUrl(_this2.config));
179
217
  return _await(_this2.createFetchParams(context, initOverrides, baseUrlOverride === undefined ? undefined : (_b = nonDefaultBasePaths[baseUrlOverride]) !== null && _b !== void 0 ? _b : baseUrlOverride), function (_ref) {
180
218
  var url = _ref.url,
181
219
  init = _ref.init;
@@ -197,7 +235,7 @@ var BaseAPI = /*#__PURE__*/function () {
197
235
  var _a, _b;
198
236
  var response;
199
237
  return _continue(_catch(function () {
200
- return _await(BytescaleApiClientConfigResolver.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
238
+ return _await(BytescaleApiClientConfigUtils.getFetchApi(_this4.config)(url, init), function (_BytescaleApiClientCo) {
201
239
  response = _BytescaleApiClientCo;
202
240
  });
203
241
  }, function (e) {
@@ -251,7 +289,7 @@ var BaseAPI = /*#__PURE__*/function () {
251
289
  value: function createFetchParams(context, initOverrides, baseUrlOverride) {
252
290
  try {
253
291
  var _this6 = this;
254
- var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigResolver.getApiUrl(_this6.config)) + context.path;
292
+ var url = (baseUrlOverride !== null && baseUrlOverride !== void 0 ? baseUrlOverride : BytescaleApiClientConfigUtils.getApiUrl(_this6.config)) + context.path;
255
293
  if (context.query !== undefined && Object.keys(context.query).length !== 0) {
256
294
  // only add the querystring to the URL if there are query parameters.
257
295
  // this is done to avoid urls ending with a "?" character which buggy webservers
@@ -295,9 +333,6 @@ var BaseAPI = /*#__PURE__*/function () {
295
333
  }]);
296
334
  return BaseAPI;
297
335
  }();
298
- BaseAPI.specialApiKeys = ["free", "demo"];
299
- BaseAPI.specialApiKeyAccountId = "W142hJk";
300
- BaseAPI.accountIdLength = 7; // Sync with: upload/shared/**/AccountIdUtils
301
336
  var CancelledError = /*#__PURE__*/function (_Error) {
302
337
  _inherits(CancelledError, _Error);
303
338
  var _super = _createSuper(CancelledError);
@@ -1349,7 +1384,7 @@ var UrlBuilder = /*#__PURE__*/function () {
1349
1384
  function UrlBuilder(config) {
1350
1385
  UrlBuilder_classCallCheck(this, UrlBuilder);
1351
1386
  var _a;
1352
- this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigResolver.defaultCdnUrl;
1387
+ this.cdnUrl = (_a = config === null || config === void 0 ? void 0 : config.cdnUrl) !== null && _a !== void 0 ? _a : BytescaleApiClientConfigUtils.defaultCdnUrl;
1353
1388
  }
1354
1389
  /**
1355
1390
  * Builds a URL to either a raw file or a transformed file.
@@ -2624,7 +2659,7 @@ var AuthManager = /*#__PURE__*/function () {
2624
2659
  var __lib_exports__AuthManager = __lib_exports__.AuthManager;
2625
2660
  var __lib_exports__BaseAPI = __lib_exports__.BaseAPI;
2626
2661
  var __lib_exports__BinaryResult = __lib_exports__.BinaryResult;
2627
- var __lib_exports__BytescaleApiClientConfigResolver = __lib_exports__.BytescaleApiClientConfigResolver;
2662
+ var __lib_exports__BytescaleApiClientConfigUtils = __lib_exports__.BytescaleApiClientConfigUtils;
2628
2663
  var __lib_exports__BytescaleApiError = __lib_exports__.BytescaleApiError;
2629
2664
  var __lib_exports__CancelledError = __lib_exports__.CancelledError;
2630
2665
  var __lib_exports__CommonTypesNoOp = __lib_exports__.CommonTypesNoOp;
@@ -2638,4 +2673,4 @@ var __lib_exports__UrlBuilder = __lib_exports__.UrlBuilder;
2638
2673
  var __lib_exports__UrlBuilderTypesNoOp = __lib_exports__.UrlBuilderTypesNoOp;
2639
2674
  var __lib_exports__VoidApiResponse = __lib_exports__.VoidApiResponse;
2640
2675
  var __lib_exports__querystring = __lib_exports__.querystring;
2641
- export { __lib_exports__AuthManager as AuthManager, __lib_exports__BaseAPI as BaseAPI, __lib_exports__BinaryResult as BinaryResult, __lib_exports__BytescaleApiClientConfigResolver as BytescaleApiClientConfigResolver, __lib_exports__BytescaleApiError as BytescaleApiError, __lib_exports__CancelledError as CancelledError, __lib_exports__CommonTypesNoOp as CommonTypesNoOp, __lib_exports__FileApi as FileApi, __lib_exports__FolderApi as FolderApi, __lib_exports__JSONApiResponse as JSONApiResponse, __lib_exports__JobApi as JobApi, __lib_exports__UploadApi as UploadApi, __lib_exports__UploadManager as UploadManager, __lib_exports__UrlBuilder as UrlBuilder, __lib_exports__UrlBuilderTypesNoOp as UrlBuilderTypesNoOp, __lib_exports__VoidApiResponse as VoidApiResponse, __lib_exports__querystring as querystring };
2676
+ export { __lib_exports__AuthManager as AuthManager, __lib_exports__BaseAPI as BaseAPI, __lib_exports__BinaryResult as BinaryResult, __lib_exports__BytescaleApiClientConfigUtils as BytescaleApiClientConfigUtils, __lib_exports__BytescaleApiError as BytescaleApiError, __lib_exports__CancelledError as CancelledError, __lib_exports__CommonTypesNoOp as CommonTypesNoOp, __lib_exports__FileApi as FileApi, __lib_exports__FolderApi as FolderApi, __lib_exports__JSONApiResponse as JSONApiResponse, __lib_exports__JobApi as JobApi, __lib_exports__UploadApi as UploadApi, __lib_exports__UploadManager as UploadManager, __lib_exports__UrlBuilder as UrlBuilder, __lib_exports__UrlBuilderTypesNoOp as UrlBuilderTypesNoOp, __lib_exports__VoidApiResponse as VoidApiResponse, __lib_exports__querystring as querystring };
@@ -12,13 +12,13 @@ import { ErrorResponse } from "./models";
12
12
  */
13
13
  export interface BytescaleApiClientConfig {
14
14
  /**
15
- * Required for Node.js: you must provide an instance of requires("node-fetch")
15
+ * Only required for Node.js. Must be an instance of requires("node-fetch").
16
16
  *
17
17
  * Not required for the browser.
18
18
  */
19
19
  fetchApi?: FetchAPI;
20
20
  /**
21
- * Required for all environments. Must begin with "public_" or "secret_".
21
+ * Must begin with "public_" or "secret_".
22
22
  *
23
23
  * Please note: if you require JWT-based auth, you must provide an API key to this field, and then call 'AuthManager.beginAuthSession' to start a JWT-based auth session. The JWT's permissions will be merged with the API key's permissions, with precedence given to the JWT.
24
24
  */
@@ -38,21 +38,23 @@ export interface BytescaleApiClientConfig {
38
38
  */
39
39
  headers?: () => Promise<HTTPHeaders>;
40
40
  }
41
- export declare class BytescaleApiClientConfigResolver {
41
+ export declare class BytescaleApiClientConfigUtils {
42
42
  static defaultApiUrl: string;
43
43
  static defaultCdnUrl: string;
44
+ private static readonly specialApiKeys;
45
+ private static readonly specialApiKeyAccountId;
46
+ private static readonly accountIdLength;
44
47
  static getApiUrl(config: BytescaleApiClientConfig): string;
45
48
  static getCdnUrl(config: BytescaleApiClientConfig): string;
46
49
  static getFetchApi(config: BytescaleApiClientConfig): FetchAPI;
50
+ static getAccountId(config: BytescaleApiClientConfig): string;
51
+ static validate(config: BytescaleApiClientConfig): void;
47
52
  }
48
53
  /**
49
54
  * This is the base class for all generated API classes.
50
55
  */
51
56
  export declare class BaseAPI {
52
57
  protected readonly config: BytescaleApiClientConfig;
53
- protected static readonly specialApiKeys: string[];
54
- protected static readonly specialApiKeyAccountId = "W142hJk";
55
- protected static readonly accountIdLength = 7;
56
58
  constructor(config: BytescaleApiClientConfig);
57
59
  protected request(context: RequestOpts, initOverrides: RequestInit | InitOverrideFunction | undefined, baseUrlOverride: string | undefined): Promise<Response>;
58
60
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytescale/sdk",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.2",
4
4
  "description": "Bytescale JavaScript SDK",
5
5
  "author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
6
6
  "license": "MIT",