UNPKG

quickbooks-api

Version:

A modular TypeScript SDK for seamless integration with Intuit QuickBooks APIs. Provides robust authentication handling and future-ready foundation for accounting, payments, and commerce operations.

108 lines (105 loc) 3.18 kB
import { InvoiceAPI } from './invoices/invoice-api.js'; import { EstimateAPI } from './estimates/estimate-api.js'; import { CustomerAPI } from './customer/customer-api.js'; import { PaymentAPI } from './payment/payment-api.js'; import { AccountAPI } from './account/account-api.js'; import { PreferenceAPI } from './preferences/preference-api.js'; import { CreditMemoAPI } from './credit-memo/credit-memo-api.js'; import { CompanyInfoAPI } from './company-info/company-info-api.js'; import { BillAPI } from './bill/bill-api.js'; /** * API Client */ export class ApiClient { authProvider; environment; /** * Customer API */ customers; /** * Invoices API */ invoices; /** * Credit Memo API */ creditMemos; /** * Estimates API */ estimates; /** * Payments API */ payments; /** * Accounts API */ accounts; /** * Preferences API */ preferences; /** * Company Info API */ companyInfo; /** * Bills API */ bills; /** * Automatically check for a next page (This creates an extra query to the API to check if there is a next page) */ autoCheckNextPage = true; /** * Constructor * @param authProvider - The Auth Provider */ constructor(authProvider, environment) { this.authProvider = authProvider; this.environment = environment; // Initialize the API's this.invoices = new InvoiceAPI(this); this.customers = new CustomerAPI(this); this.estimates = new EstimateAPI(this); this.accounts = new AccountAPI(this); this.payments = new PaymentAPI(this); this.preferences = new PreferenceAPI(this); this.creditMemos = new CreditMemoAPI(this); this.companyInfo = new CompanyInfoAPI(this); this.bills = new BillAPI(this); } /** * Runs a Request * @param url - The URL to run the request on * @param headers - The headers to run the request on * @returns {AuthProvider} The Auth Provider */ async runRequest(url, requestInit) { // Get the Token const token = await this.authProvider.getToken(); // Setup the Request Data requestInit.headers = { Accept: 'application/json', 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', Authorization: `Bearer ${token.accessToken}`, ...requestInit.headers, }; // Run the Request const response = await fetch(url, requestInit); // Check if the Response has failed if (!response.ok) { // Get the Error Message const errorMessage = await response.text(); // Throw an Error throw new Error(`Failed to run request: ${errorMessage}`); } // Check if the response is an Object and if it is, parse it as JSON const responseData = response.headers.get('Content-Type')?.includes('application/json') ? await response.json() : null; // Return the Response return responseData; } }