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.

65 lines (64 loc) 2.36 kB
import { Environment, Query } from '../../../types/types.js'; import { Endpoints } from '../../../types/enums/endpoints.js'; import { CompanyInfoQueryBuilder } from './company-info-query-builder.js'; // Import the Services import { getCompanyInfo } from './services/get-company-info.js'; import { rawCompanyInfoQuery } from './services/raw-company-info-query.js'; /** * Company Info API */ export class CompanyInfoAPI { apiClient; // The List of Company Info Services getCompanyInfo = getCompanyInfo.bind(this); rawCompanyInfoQuery = rawCompanyInfoQuery.bind(this); /** * Constructor * @param apiClient - The API Client */ constructor(apiClient) { this.apiClient = apiClient; } /** * Get the Company Endpoint * @returns The Company Endpoint with the attached token realmId */ async getCompanyEndpoint() { // Get the Base Endpoint const baseEndpoint = this.apiClient.environment === Environment.Production ? Endpoints.ProductionCompanyApi : Endpoints.SandboxCompanyApi; // Get the Token const token = await this.apiClient.authProvider.getToken(); // Return the Company Endpoint return `${baseEndpoint}/${token.realmId}`; } /** * Format the Response * @param response - The Response * @returns The Company Info */ formatResponse(response) { // Check if the Response is Invalid if (!response?.QueryResponse) throw new Error('Invalid Response'); // Check if the CompanyInfo is Not set if (!response.QueryResponse.CompanyInfo || response.QueryResponse.CompanyInfo.length === 0) { throw new Error('No Company Info found'); } // Get the Company Info const queryResponse = response.QueryResponse; // Return the first Company Info (there should only be one) return queryResponse.CompanyInfo[0]; } /** * Get the Query Builder * @returns The Query Builder */ async getQueryBuilder() { // Get the Company Endpoint const companyEndpoint = await this.getCompanyEndpoint(); // Setup the New Query Builder const queryBuilder = new CompanyInfoQueryBuilder(companyEndpoint, Query.CompanyInfo); // Return the Query Builder return queryBuilder; } }