@tableau/taco-toolkit
Version:
Tableau Connector Toolkit
485 lines (469 loc) • 18.7 kB
JavaScript
;
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
/**
* @deprecated Will be removed in the later release.
*/
var Auth = /** @class */ (function () {
function Auth() {
}
Auth.prototype.run = function (options) {
return __awaiter(this, void 0, void 0, function () {
var authResult;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
log('>authenticate.run');
return [4 /*yield*/, this.authenticate(options)];
case 1:
authResult = _a.sent();
return [2 /*return*/, authResult];
}
});
});
};
return Auth;
}());
/**
* An abstract class for implementing fetcher functionality that asynchronously retrieves data.
*
* @example
* ```ts
* class MyFetcher extends Fetcher {
* async *fetch(options: FetchOptions): AsyncGenerator {
* // Implementation to asynchronously retrieve data using the provided options.
* }
* }
* ```
*/
var Fetcher = /** @class */ (function () {
function Fetcher() {
}
return Fetcher;
}());
var BaseParser = /** @class */ (function () {
function BaseParser() {
}
/**
* A static method to create a DataContainerBuilder instance based on the provided dataContainer object.
*/
BaseParser.createContainerBuilder = function (dataContainer) {
if (!isValidDataContainer(dataContainer)) {
throw new Error('Found invalid DataContainer: ' + JSON.stringify(dataContainer));
}
return new DataContainerBuilder(dataContainer);
};
return BaseParser;
}());
/**
* An abstract class for the implementation of connector custom parsing that synchronously parses the results yielded from the Fetcher.
*
* For TypeScript, the fetcherResult type can be provided via the type parameter. By default, fetcherResult is typed as `any`.
*
* @example
* ```ts
* export default class MyParser extends Parser<Uint8Array> {
* parse(fetcherResult: Uint8Array, options: ParseOptions): DataContainer {
* // ...
* }
* }
* ```
*/
var Parser = /** @class */ (function (_super) {
__extends(Parser, _super);
function Parser() {
return _super !== null && _super.apply(this, arguments) || this;
}
return Parser;
}(BaseParser));
/**
* An abstract class for the implementation of connector custom parsing that asynchronously parses the results yielded from the Fetcher.
*
* For TypeScript, the fetcherResult type can be provided via the type parameter. By default, fetcherResult is typed as `any`.
*
* @example
* ```ts
* export default class MyParser extends AsyncParser<Uint8Array> {
* async parse(fetcherResult: Uint8Array, options: ParseOptions): Promise<DataContainer> {
* // ...
* }
* }
* ```
*/
var AsyncParser = /** @class */ (function (_super) {
__extends(AsyncParser, _super);
function AsyncParser() {
return _super !== null && _super.apply(this, arguments) || this;
}
return AsyncParser;
}(BaseParser));
/**
* A builder class for constructing and modifying a DataContainer instance.
*
* This class provides methods to retrieve, and append tables within a DataContainer.
*
* @example
* ```ts
* const dataContainerBuilder = Parser.createContainerBuilder(initialDataContainer)
*
* // Create or get a table and add columns or rows
* const { tableBuilder, isNew } = dataContainerBuilder.getTable('myTable')
* tableBuilder.addColumnHeader({ name: 'column1', dataType: 'string' });
* tableBuilder.addColumnHeader({ name: 'column2', dataType: 'string' });
* tableBuilder.addRow({ column1: 'value1', column2: 'value2' });
*
* // Get the final DataContainer
* const finalDataContainer = dataContainerBuilder.getDataContainer()
* ```
*/
var DataContainerBuilder = /** @class */ (function () {
// Hide the constructor from the doc. Users should Parser.createContainerBuilder instead.
/** @ignore */
function DataContainerBuilder(dataContainer) {
this.dataContainer = dataContainer;
}
/**
* Retrieves a table builder for the specified table name. If the table does not exist,
* a new table is created within the DataContainer.
*
* @param {string} name - The name of the table.
* @returns {{ tableBuilder: DataTableBuilder; isNew: boolean }} An object containing the table builder
* and a boolean indicating whether the table is newly created.
*/
DataContainerBuilder.prototype.getTable = function (name) {
var table = this.dataContainer.tables.find(function (t) { return t.name === name; });
if (table === undefined) {
var newTable = {
columns: [],
id: '',
name: name,
properties: {},
rows: [],
};
this.dataContainer.tables.push(newTable);
return {
isNew: true,
tableBuilder: new DataTableBuilder(newTable),
};
}
return { isNew: false, tableBuilder: new DataTableBuilder(table) };
};
/**
* Appends an array of DataTable instances to the DataContainer.
*
* @param {DataTable[]} tables - An array of DataTable instances to append.
*/
/** @ignore */
DataContainerBuilder.prototype.appendTables = function (tables) {
for (var i = 0; i < tables.length; i++) {
this.appendTable(tables[i]);
}
};
/**
* Appends a DataTable instance to the DataContainer.
*
* @param {DataTable} table - The DataTable instance to append.
* @throws {Error} Throws an error if a table with the same name already exists in the DataContainer.
*/
/** @ignore */
DataContainerBuilder.prototype.appendTable = function (table) {
if (this.dataContainer.tables.find(function (t) { return t.name === table.name; })) {
throw new Error("Failed to append table. The DataContainer already has a table named '".concat(table.name, "'"));
}
this.dataContainer.tables.push(table);
};
/**
* Gets the final DataContainer instance after building and modifying it.
*
* @returns {DataContainer} The constructed DataContainer instance.
*/
DataContainerBuilder.prototype.getDataContainer = function () {
return this.dataContainer;
};
return DataContainerBuilder;
}());
/**
* A builder class for constructing and modifying a DataTable instance within a DataContainer.
*
* This class provides methods to add row/s and add column header/s to a DataTable.
*
* @example
* ```ts
* const dataContainerBuilder = Parser.createContainerBuilder(initialDataContainer);
* const { tableBuilder } = dataContainerBuilder.getTable('myTable');
*
* // Add row and column header for a specific table
* tableBuilder.addColumnHeader({ name: 'column1', dataType: 'string' });
* tableBuilder.addColumnHeader({ name: 'column2', dataType: 'string' });
* tableBuilder.addRow({ column1: 'value1', column2: 'value2' });
*
* // Get the final DataContainer
* const finalDataContainer = dataContainerBuilder.getDataContainer();
* ```
*/
var DataTableBuilder = /** @class */ (function () {
// Hide the constructor from the doc. Users should get this from DataContainerBuilder.
/** @ignore */
function DataTableBuilder(dataTable) {
this.dataTable = dataTable;
}
/** @ignore */
DataTableBuilder.prototype.setId = function (id) {
this.dataTable.id = id;
};
/** @ignore */
DataTableBuilder.prototype.setProperties = function (properties) {
this.dataTable.properties = properties;
};
/** @ignore */
DataTableBuilder.prototype.setDeferHandler = function (handlerInput) {
this.dataTable.properties = __assign(__assign({}, this.dataTable.properties), { handlerInput: handlerInput, isDeferred: true });
};
/**
* Adds a single row to the DataTable.
*
* @param {DataRow} row - The row to add to the DataTable.
*/
DataTableBuilder.prototype.addRow = function (row) {
this.dataTable.rows.push(row);
};
/**
* Adds multiple rows to the DataTable.
*
* @param {DataRow[]} rows - The array of rows to add to the DataTable.
*/
DataTableBuilder.prototype.addRows = function (rows) {
var e_1, _a;
try {
// using for...of to handle large data.
// avoid using spread operator which causes maximum stack error.
for (var rows_1 = __values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) {
var row = rows_1_1.value;
if (row) {
this.addRow(row);
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (rows_1_1 && !rows_1_1.done && (_a = rows_1.return)) _a.call(rows_1);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* Adds a single column header to the DataTable.
*
* @param {ColumnHeader} column - The column header to add to the DataTable.
*/
DataTableBuilder.prototype.addColumnHeader = function (column) {
this.dataTable.columns.push(column);
};
/**
* Adds multiple column headers to the DataTable.
*
* @param {ColumnHeader[]} colHeaders - The array of column headers to add to the DataTable.
*/
DataTableBuilder.prototype.addColumnHeaders = function (colHeaders) {
var e_2, _a;
try {
// using for...of to handle large data.
// avoid using spread operator which causes maximum stack error.
for (var colHeaders_1 = __values(colHeaders), colHeaders_1_1 = colHeaders_1.next(); !colHeaders_1_1.done; colHeaders_1_1 = colHeaders_1.next()) {
var header = colHeaders_1_1.value;
if (header) {
this.addColumnHeader(header);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (colHeaders_1_1 && !colHeaders_1_1.done && (_a = colHeaders_1.return)) _a.call(colHeaders_1);
}
finally { if (e_2) throw e_2.error; }
}
};
return DataTableBuilder;
}());
function isValidDataContainer(value) {
if (typeof value !== 'object' || value === null) {
return false;
}
return 'metadata' in value && Array.isArray(value.tables);
}
var Ingestor = /** @class */ (function () {
function Ingestor() {
}
return Ingestor;
}());
/**
* The data type for a table column.
*/
exports.DataType = void 0;
(function (DataType) {
DataType["Bool"] = "bool";
DataType["Date"] = "date";
DataType["Datetime"] = "datetime";
DataType["Float"] = "float";
DataType["Int"] = "int";
DataType["String"] = "string";
DataType["Geometry"] = "geometry";
DataType["Unknown"] = "unknown";
})(exports.DataType || (exports.DataType = {}));
/**
* The aggregation type for a table column. It can be assigned as metadata
* for columns of the int and float dataType.
* */
exports.AggType = void 0;
(function (AggType) {
AggType["Avg"] = "avg";
AggType["Count"] = "count";
AggType["CountDist"] = "count_dist";
AggType["Median"] = "median";
AggType["Sum"] = "sum";
})(exports.AggType || (exports.AggType = {}));
/**
* The role for a table column.
*/
exports.ColumnRole = void 0;
(function (ColumnRole) {
ColumnRole["Dimension"] = "dimension";
ColumnRole["Measure"] = "measure";
})(exports.ColumnRole || (exports.ColumnRole = {}));
/**
* The type for a table column.
*/
exports.ColumnType = void 0;
(function (ColumnType) {
ColumnType["Continuous"] = "continuous";
ColumnType["Discrete"] = "discrete";
})(exports.ColumnType || (exports.ColumnType = {}));
/**
* The geographic role for a table column.
*/
exports.GeographicRole = void 0;
(function (GeographicRole) {
GeographicRole["AreaCode"] = "area_code";
/** core-based statistical area / metropolitan statistical area */
GeographicRole["CbsaMsa"] = "cbsa_msa";
GeographicRole["City"] = "city";
GeographicRole["CongressionalDistrict"] = "congressional_district";
GeographicRole["CountryRegion"] = "country_region";
GeographicRole["County"] = "county";
GeographicRole["StateProvince"] = "state_province";
GeographicRole["ZipCodePostcode"] = "zip_code_postcode";
GeographicRole["Latitude"] = "latitude";
GeographicRole["Longitude"] = "longitude";
})(exports.GeographicRole || (exports.GeographicRole = {}));
/**
* The number format for a table column. It can be assigned as metadata
* for columns of the int and float dataType.
* */
exports.NumberFormat = void 0;
(function (NumberFormat) {
NumberFormat["Number"] = "number";
NumberFormat["Currency"] = "currency";
NumberFormat["Scientific"] = "scientific";
NumberFormat["Percentage"] = "percentage";
})(exports.NumberFormat || (exports.NumberFormat = {}));
/**
* The unit format for a table column. It can be assigned as metadata
* for columns of the int and float dataType.
* */
exports.UnitsFormat = void 0;
(function (UnitsFormat) {
UnitsFormat["Thousands"] = "thousands";
UnitsFormat["Millions"] = "millions";
UnitsFormat["BillionsEnglish"] = "billions_english";
UnitsFormat["BillionsStandard"] = "billions_standard";
})(exports.UnitsFormat || (exports.UnitsFormat = {}));
exports.AsyncParser = AsyncParser;
exports.Auth = Auth;
exports.DataContainerBuilder = DataContainerBuilder;
exports.DataTableBuilder = DataTableBuilder;
exports.Fetcher = Fetcher;
exports.Ingestor = Ingestor;
exports.Parser = Parser;