UNPKG

pluggy-js

Version:

Client-side JavaScript toolkit for Pluggy's API.

250 lines (249 loc) 8.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseClientError = exports.isClientError = exports.getErrorResponse = exports.isPluggyServerError = exports.isValidationErrorResponse = exports.isConnectorValidationErrorResponse = exports.ParameterValidationErrorCode = exports.HttpStatusCode = exports.EXECUTION_FINISHED_STATUSES = exports.CREDENTIAL_TYPES = exports.PRODUCT_TYPES = exports.CONNECTOR_TYPES = exports.INVESTMENT_STATUSES = exports.INVESTMENT_TYPES = exports.ACCOUNT_SUB_TYPES = exports.ACCOUNT_TYPES = exports.CURRENCY_CODES = exports.COUNTRY_CODES = void 0; const axios_1 = require("axios"); exports.COUNTRY_CODES = ['AR', 'BR', 'CO', 'MX']; exports.CURRENCY_CODES = ['USD', 'ARS', 'BRL']; exports.ACCOUNT_TYPES = ['BANK', 'CREDIT']; exports.ACCOUNT_SUB_TYPES = [ 'SAVINGS_ACCOUNT', 'CHECKINGS_ACCOUNT', 'CREDIT_CARD', ]; exports.INVESTMENT_TYPES = [ 'MUTUAL_FUND', 'EQUITY', 'SECURITY', 'FIXED_INCOME', 'ETF', 'COE', 'OTHER', ]; exports.INVESTMENT_STATUSES = [ 'ACTIVE', 'PENDING', 'TOTAL_WITHDRAWAL', ]; exports.CONNECTOR_TYPES = [ 'PERSONAL_BANK', 'BUSINESS_BANK', 'INVOICE', 'INVESTMENT', 'TELECOMMUNICATION', 'DIGITAL_ECONOMY', 'PAYMENT_ACCOUNT', 'OTHER', ]; exports.PRODUCT_TYPES = [ 'ACCOUNTS', 'CREDIT_CARDS', 'TRANSACTIONS', 'PAYMENT_DATA', 'INVESTMENTS', 'INVESTMENTS_TRANSACTIONS', 'IDENTITY', 'BROKERAGE_NOTE', 'MOVE_SECURITY', 'LOANS', ]; exports.CREDENTIAL_TYPES = [ 'number', 'password', 'text', 'image', 'select', 'ethaddress', 'hcaptcha', ]; const CONNECTOR_EXECUTION_STATUSES = [ 'LOGIN_IN_PROGRESS', 'WAITING_USER_INPUT', 'WAITING_USER_ACTION', 'LOGIN_MFA_IN_PROGRESS', 'ACCOUNTS_IN_PROGRESS', 'TRANSACTIONS_IN_PROGRESS', 'PAYMENT_DATA_IN_PROGRESS', 'CREDITCARDS_IN_PROGRESS', 'INVESTMENTS_IN_PROGRESS', 'INVESTMENTS_TRANSACTIONS_IN_PROGRESS', 'OPPORTUNITIES_IN_PROGRESS', 'IDENTITY_IN_PROGRESS', 'PORTFOLIO_IN_PROGRESS', ]; const EXECUTION_ERROR_CODES = [ 'INVALID_CREDENTIALS', 'ACCOUNT_CREDENTIALS_RESET', 'ALREADY_LOGGED_IN', 'UNEXPECTED_ERROR', 'INVALID_CREDENTIALS_MFA', 'SITE_NOT_AVAILABLE', 'ACCOUNT_LOCKED', 'CONNECTION_ERROR', 'ACCOUNT_NEEDS_ACTION', 'USER_NOT_SUPPORTED', 'USER_AUTHORIZATION_PENDING', 'USER_AUTHORIZATION_NOT_GRANTED', 'USER_INPUT_TIMEOUT', ]; exports.EXECUTION_FINISHED_STATUSES = [ ...EXECUTION_ERROR_CODES, 'MERGE_ERROR', 'ERROR', 'SUCCESS', 'PARTIAL_SUCCESS', ]; const EXECUTION_STATUSES = [ 'CREATING', 'CREATE_ERROR', 'CREATED', ...CONNECTOR_EXECUTION_STATUSES, ...exports.EXECUTION_FINISHED_STATUSES, ]; const ITEM_STATUSES = [ 'UPDATED', 'UPDATING', 'WAITING_USER_INPUT', 'LOGIN_ERROR', 'OUTDATED', ]; var HttpStatusCode; (function (HttpStatusCode) { HttpStatusCode[HttpStatusCode["BAD_REQUEST"] = 400] = "BAD_REQUEST"; HttpStatusCode[HttpStatusCode["FORBIDDEN"] = 403] = "FORBIDDEN"; HttpStatusCode[HttpStatusCode["NOT_FOUND"] = 404] = "NOT_FOUND"; HttpStatusCode[HttpStatusCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR"; })(HttpStatusCode = exports.HttpStatusCode || (exports.HttpStatusCode = {})); var ParameterValidationErrorCode; (function (ParameterValidationErrorCode) { ParameterValidationErrorCode["MISSING_REQUIRED_PARAMETER"] = "001"; ParameterValidationErrorCode["RULE_VALIDATION_ERROR"] = "002"; ParameterValidationErrorCode["DATE_RULE_VALIDATION_ERROR"] = "003"; })(ParameterValidationErrorCode = exports.ParameterValidationErrorCode || (exports.ParameterValidationErrorCode = {})); /** * Helper type-guard to to narrow an Error to an AxiosError containing ConnectorValidationErrorResponse. * This error response can happen on a Bad Request on 'POST /items' and 'PATCH /items' requests. * @param error */ function isConnectorValidationErrorResponse(error) { if (!axios_1.default.isAxiosError(error)) { return false; } const { response } = error; if (!response) { return false; } const { data: { code, message, details }, } = response; return (code === HttpStatusCode.BAD_REQUEST && typeof message === 'string' && Array.isArray(details)); } exports.isConnectorValidationErrorResponse = isConnectorValidationErrorResponse; /** * Helper guard to narrow an Error to an AxiosError containing a * Pluggy API ValidationErrorResponse object (which represents a 400 Bad Request). * * This error response can happen on a bad request on any resource, for example when some * required parameters are missing or have an invalid format or value. * * @param error */ function isValidationErrorResponse(error) { if (!axios_1.default.isAxiosError(error)) { return false; } const { response } = error; if (!response) { return false; } const { data: { code, message }, } = response; return code === HttpStatusCode.BAD_REQUEST && typeof message === 'string'; } exports.isValidationErrorResponse = isValidationErrorResponse; /** * Helper guard to narrow an Error to an AxiosError containing a Pluggy API ErrorResponse object. * @param error */ function isPluggyServerError(error) { if (!axios_1.default.isAxiosError(error)) { return false; } const { response, code } = error; if (!response) { return false; } if (Number(code) >= 200 && Number(code) < 300) { // Response code is OK -> not an error return false; } return true; } exports.isPluggyServerError = isPluggyServerError; /** * Helper to extract the ErrorResponse data object * from the AxiosError<ErrorResponse> error */ function getErrorResponse(error) { if (!error.response) { throw new Error(`Response is undefined in AxiosError object, can't extract data`); } return error.response.data; } exports.getErrorResponse = getErrorResponse; /** * Helper guard to narrow an Error to an AxiosError that is related to the client, not to the server. * @param error */ function isClientError(error) { if (!axios_1.default.isAxiosError(error)) { return false; } const { request, response } = error; if (request === undefined && response === undefined) { // Something happened in setting up the request that triggered an Error return true; } if (response === undefined) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js return true; } return false; } exports.isClientError = isClientError; /** * Helper to create a NoServerResponseError object * from an error that is an AxiosError related to the client itself. */ function parseClientError(error) { if (!axios_1.default.isAxiosError(error)) { throw new Error('Error is not an AxiosError instance'); } if (!isClientError(error)) { throw new Error('AxiosError is a server response error, not a client error'); } const { request, response, message } = error; if (request === undefined && response === undefined) { // Something happened in setting up the request that triggered an Error return { code: '0', message: `Malformed request error: '${message}'`, }; } if (response === undefined) { // The request was made but no response was received // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js console.log('Request was made but no response was received', request); return { code: '0', message: `Request was made but no response from server was received`, }; } // other - this should not happen (should already have covered all previous scenarios) return { code: '0', message: `Unexpected AxiosError client error`, }; } exports.parseClientError = parseClientError;