UNPKG

xero-node

Version:

Xero NodeJS OAuth 2.0 client for xero-node

259 lines 9.94 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.VoidAuth = exports.OAuth = exports.ApiKeyAuth = exports.HttpBasicAuth = exports.ObjectSerializer = void 0; __exportStar(require("././countryCode"), exports); __exportStar(require("././creditDebitIndicator"), exports); __exportStar(require("././currencyCode"), exports); __exportStar(require("././endBalance"), exports); __exportStar(require("././feedConnection"), exports); __exportStar(require("././feedConnections"), exports); __exportStar(require("././modelError"), exports); __exportStar(require("././pagination"), exports); __exportStar(require("././startBalance"), exports); __exportStar(require("././statement"), exports); __exportStar(require("././statementLine"), exports); __exportStar(require("././statements"), exports); const countryCode_1 = require("././countryCode"); const creditDebitIndicator_1 = require("././creditDebitIndicator"); const currencyCode_1 = require("././currencyCode"); const endBalance_1 = require("././endBalance"); const feedConnection_1 = require("././feedConnection"); const feedConnections_1 = require("././feedConnections"); const modelError_1 = require("././modelError"); const pagination_1 = require("././pagination"); const startBalance_1 = require("././startBalance"); const statement_1 = require("././statement"); const statementLine_1 = require("././statementLine"); const statements_1 = require("././statements"); /* tslint:disable:no-unused-variable */ let primitives = [ "string", "boolean", "double", "integer", "long", "float", "number", "any" ]; let enumsMap = { "CountryCode": countryCode_1.CountryCode, "CreditDebitIndicator": creditDebitIndicator_1.CreditDebitIndicator, "CurrencyCode": currencyCode_1.CurrencyCode, "FeedConnection.AccountTypeEnum": feedConnection_1.FeedConnection.AccountTypeEnum, "FeedConnection.StatusEnum": feedConnection_1.FeedConnection.StatusEnum, "ModelError.TypeEnum": modelError_1.ModelError.TypeEnum, "Statement.StatusEnum": statement_1.Statement.StatusEnum, }; let typeMap = { "EndBalance": endBalance_1.EndBalance, "FeedConnection": feedConnection_1.FeedConnection, "FeedConnections": feedConnections_1.FeedConnections, "ModelError": modelError_1.ModelError, "Pagination": pagination_1.Pagination, "StartBalance": startBalance_1.StartBalance, "Statement": statement_1.Statement, "StatementLine": statementLine_1.StatementLine, "Statements": statements_1.Statements, }; class ObjectSerializer { static findCorrectType(data, expectedType) { if (data == undefined) { return expectedType; } else if (primitives.indexOf(expectedType.toLowerCase()) !== -1) { return expectedType; } else if (expectedType === "Date") { return expectedType; } else { if (enumsMap[expectedType]) { return expectedType; } if (!typeMap[expectedType]) { return expectedType; // w/e we don't know the type } // Check the discriminator let discriminatorProperty = typeMap[expectedType].discriminator; if (discriminatorProperty == null) { return expectedType; // the type does not have a discriminator. use it. } else { if (data[discriminatorProperty]) { var discriminatorType = data[discriminatorProperty]; if (typeMap[discriminatorType]) { return discriminatorType; // use the type given in the discriminator } else { return expectedType; // discriminator did not map to a type } } else { return expectedType; // discriminator was not present (or an empty string) } } } } static serialize(data, type) { if (data == undefined) { return data; } else if (primitives.indexOf(type.toLowerCase()) !== -1) { return data; } else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6 let subType = type.replace("Array<", ""); // Array<Type> => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData = []; for (let [index, date] of Object.entries(data)) { transformedData.push(ObjectSerializer.serialize(date, subType)); } if (subType === 'string') { return transformedData.join(','); } else { return transformedData; } } else if (type === "Date") { return data.toISOString(); } else { if (enumsMap[type]) { return data; } if (!typeMap[type]) { // in case we dont know the type return data; } // Get the actual type of this object type = this.findCorrectType(data, type); // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); let instance = {}; for (let [index, attributeType] of Object.entries(attributeTypes)) { instance[attributeType['baseName']] = ObjectSerializer.serialize(data[attributeType['name']], attributeType['type']); } return instance; } } static deserializeDateFormats(type, data) { const isDate = new Date(data); if (isNaN(isDate.getTime())) { const re = /-?\d+/; const m = re.exec(data); return new Date(parseInt(m[0], 10)); } else { return isDate; } } static deserialize(data, type) { // polymorphism may change the actual type. type = ObjectSerializer.findCorrectType(data, type); if (data == undefined) { return data; } else if (primitives.indexOf(type.toLowerCase()) !== -1) { if (type === "string" && data.toString().substring(0, 6) === "/Date(") { return this.deserializeDateFormats(type, data); // For MS dates that are of type 'string' } else { return data; } } else if (type.lastIndexOf("Array<", 0) === 0) { // string.startsWith pre es6 let subType = type.replace("Array<", ""); // Array<Type> => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData = []; // Asset API returns string even for Array<Model> const dataFormatted = typeof data == 'string' ? JSON.parse(data) : data; for (let [index, currentData] of Object.entries(dataFormatted)) { transformedData.push(ObjectSerializer.deserialize(currentData, subType)); } return transformedData; } else if (type === "Date") { return this.deserializeDateFormats(type, data); } else { if (enumsMap[type]) { // is Enum return data; } if (!typeMap[type]) { // dont know the type return data; } let instance = new typeMap[type](); let attributeTypes = typeMap[type].getAttributeTypeMap(); for (let [index, attributeType] of Object.entries(attributeTypes)) { instance[attributeType['name']] = ObjectSerializer.deserialize(data[attributeType['baseName']], attributeType['type']); } return instance; } } } exports.ObjectSerializer = ObjectSerializer; class HttpBasicAuth { constructor() { this.username = ''; this.password = ''; } applyToRequest(requestOptions) { requestOptions.auth = { username: this.username, password: this.password }; } } exports.HttpBasicAuth = HttpBasicAuth; class ApiKeyAuth { constructor(location, paramName) { this.location = location; this.paramName = paramName; this.apiKey = ''; } applyToRequest(requestOptions) { if (this.location == "query") { requestOptions.params[this.paramName] = this.apiKey; } else if (this.location == "header" && requestOptions && requestOptions.headers) { requestOptions.headers[this.paramName] = this.apiKey; } } } exports.ApiKeyAuth = ApiKeyAuth; class OAuth { constructor() { this.accessToken = ''; } applyToRequest(requestOptions) { if (requestOptions && requestOptions.headers) { requestOptions.headers["Authorization"] = "Bearer " + this.accessToken; } } } exports.OAuth = OAuth; class VoidAuth { constructor() { this.username = ''; this.password = ''; } applyToRequest(_) { // Do nothing } } exports.VoidAuth = VoidAuth; //# sourceMappingURL=models.js.map