import axios, { AxiosInstance } from 'axios'; import axiosCookieJarSupport from 'axios-cookiejar-support'; import MockAdapter from 'axios-mock-adapter'; import tough from 'tough-cookie'; import { Address } from './address'; import { Bank } from './bank'; import { Credit } from './credit'; import { Form } from './form'; import { User } from './user'; import { Kyc } from './kyc'; import { LoanPart } from './loan-part'; import { Person } from './person'; import { TinkLoan } from './tink-loan'; import { Tink } from './tink'; import { Raw } from './raw'; import { ApiKeys } from '../types'; import { Markets, Countries, Currencies, EmploymentTypes, Genders, CreditTypes, KycSources, KycPurposes, } from './enums'; import { Session } from './session'; import { primeMock } from './mock'; export type RequestApiOptions = { baseURL: string; timeout?: number; clientId: string; customHeaders?: { [headerName: string]: string; }; }; axiosCookieJarSupport(axios); export class RequestApi { public options: RequestApiOptions; private _axiosInstance: AxiosInstance; private _mock: any; public [ApiKeys.ADDRESSES]: Address; public [ApiKeys.BANKS]: Bank; public [ApiKeys.CREDITS]: Credit; public [ApiKeys.FORMS]: Form; public [ApiKeys.KYCS]: Kyc; public [ApiKeys.LOANPARTS]: LoanPart; public [ApiKeys.PERSONS]: Person; public [ApiKeys.RAW]: Raw; public [ApiKeys.SESSIONS]: Session; public [ApiKeys.TINK]: Tink; public [ApiKeys.TINKLOANS]: TinkLoan; public [ApiKeys.USERS]: User; // enums public [ApiKeys.COUNTRIES]: Countries; public [ApiKeys.CURRENCIES]: Currencies; public [ApiKeys.EMPLOYMENT_TYPES]: EmploymentTypes; public [ApiKeys.CREDIT_TYPES]: CreditTypes; public [ApiKeys.GENDERS]: Genders; public [ApiKeys.MARKETS]: Markets; public [ApiKeys.KYC_PURPOSES]: KycPurposes; public [ApiKeys.KYC_SOURCES]: KycSources; constructor(options: RequestApiOptions) { this.options = options; const cookieJar = new tough.CookieJar(); this._axiosInstance = axios.create({ baseURL: options.baseURL, timeout: options.timeout, jar: cookieJar, withCredentials: true, headers: { ...options.customHeaders, 'X-Client-Id': options.clientId, 'Content-Type': 'application/json', }, }); this._mock = new MockAdapter(this._axiosInstance); primeMock(this._mock); this[ApiKeys.ADDRESSES] = new Address(this._axiosInstance); this[ApiKeys.BANKS] = new Bank(this._axiosInstance); this[ApiKeys.CREDITS] = new Credit(this._axiosInstance); this[ApiKeys.FORMS] = new Form(this._axiosInstance); this[ApiKeys.KYCS] = new Kyc(this._axiosInstance); this[ApiKeys.LOANPARTS] = new LoanPart(this._axiosInstance); this[ApiKeys.PERSONS] = new Person(this._axiosInstance); this[ApiKeys.RAW] = new Raw(this._axiosInstance); this[ApiKeys.SESSIONS] = new Session(this._axiosInstance); this[ApiKeys.TINK] = new Tink(this._axiosInstance); this[ApiKeys.TINKLOANS] = new TinkLoan(this._axiosInstance); this[ApiKeys.USERS] = new User(this._axiosInstance); //enums this[ApiKeys.COUNTRIES] = new Countries(this._axiosInstance); this[ApiKeys.CURRENCIES] = new Currencies(this._axiosInstance); this[ApiKeys.CREDIT_TYPES] = new CreditTypes(this._axiosInstance); this[ApiKeys.EMPLOYMENT_TYPES] = new EmploymentTypes(this._axiosInstance); this[ApiKeys.GENDERS] = new Genders(this._axiosInstance); this[ApiKeys.MARKETS] = new Markets(this._axiosInstance); this[ApiKeys.KYC_PURPOSES] = new KycPurposes(this._axiosInstance); this[ApiKeys.KYC_SOURCES] = new KycSources(this._axiosInstance); } } export default RequestApi;