(url, callback_or_options)
| 353 | * @param {function|object} callback_or_options either a callback function or an object with a 'callback' property and other configuration properties |
| 354 | */ |
| 355 | export async function makeConfig(url, callback_or_options) { |
| 356 | |
| 357 | let callback = null, |
| 358 | options = {}; |
| 359 | if (typeof(callback_or_options) == 'function') { |
| 360 | callback = callback_or_options |
| 361 | } else if (typeof(callback_or_options) == 'object') { |
| 362 | options = callback_or_options |
| 363 | callback = callback_or_options['callback'] |
| 364 | if (typeof(options['callback']) == 'function') callback = options['callback'] |
| 365 | } |
| 366 | |
| 367 | if (!callback) { |
| 368 | throw new TLError("Second argument to makeConfig must be either a function or an object which includes a 'callback' property with a 'function' type value") |
| 369 | } |
| 370 | |
| 371 | // Determine the source type and fetch the JSON configuration |
| 372 | const key = parseGoogleSpreadsheetURL(url); |
| 373 | |
| 374 | if (key) { |
| 375 | // Google Sheets: convert to CSV, then to JSON config format |
| 376 | try { |
| 377 | console.log(`reading url ${url}`); |
| 378 | const json = await jsonFromGoogleURL(url, options); |
| 379 | finalizeConfig(json, callback); |
| 380 | } catch (e) { |
| 381 | handleConfigError(e, callback); |
| 382 | } |
| 383 | } else if (isCSVURL(url)) { |
| 384 | // CSV file: parse and convert to JSON config format |
| 385 | try { |
| 386 | console.log(`reading CSV from url ${url}`); |
| 387 | const json = await readCSVFromURL(url); |
| 388 | finalizeConfig(json, callback); |
| 389 | } catch (e) { |
| 390 | handleConfigError(e, callback); |
| 391 | } |
| 392 | } else { |
| 393 | // Direct JSON: fetch and use as-is |
| 394 | ajax({ |
| 395 | url: url, |
| 396 | dataType: 'json', |
| 397 | success: function(data) { |
| 398 | try { |
| 399 | finalizeConfig(data, callback); |
| 400 | } catch (e) { |
| 401 | handleConfigError(e, callback); |
| 402 | } |
| 403 | }, |
| 404 | error: function(xhr, errorType) { |
| 405 | // Convert ajax error types to appropriate TLError |
| 406 | const tlError = errorType == 'parsererror' |
| 407 | ? new TLError("invalid_url_err") |
| 408 | : new TLError("unknown_read_err", errorType); |
| 409 | handleConfigError(tlError, callback); |
| 410 | } |
| 411 | }); |
| 412 | } |
no test coverage detected