UNPKG

@gohighlevel/api-client

Version:
256 lines 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Oauth = void 0; const constants_1 = require("../../constants"); /** * Oauth Service * Documentation for OAuth 2.0 API */ class Oauth { constructor(httpClient, config = {}) { this.MARKETPLACE_URL = 'https://marketplace.gohighlevel.com'; this.client = httpClient; this.config = config; } /** * Generate OAuth authorization URL for the authorization code flow */ getAuthorizationUrl(clientId, redirectUri, scope) { const params = { client_id: clientId, redirect_uri: redirectUri, scope, response_type: 'code' }; return `${this.MARKETPLACE_URL}/oauth/chooselocation?${new URLSearchParams(params).toString()}`; } /** * Refresh access token using refresh token * @param refreshToken The refresh token * @param clientId OAuth client ID * @param clientSecret OAuth client secret * @param grantType Grant type (must be 'refresh_token') * @param userType User type (UserType.Location or UserType.Company) */ async refreshToken(refreshToken, clientId, clientSecret, grantType, userType) { if (grantType !== 'refresh_token') { throw new Error('grantType must be "refresh_token"'); } if (!Object.values(constants_1.UserType).includes(userType)) { throw new Error(`userType must be "${constants_1.UserType.Location}" or "${constants_1.UserType.Company}"`); } return this.getAccessToken({ refresh_token: refreshToken, client_id: clientId, client_secret: clientSecret, grant_type: grantType, user_type: userType }); } /** * Get Access Token * Use Access Tokens to access GoHighLevel resources on behalf of an authenticated location/company. */ async getAccessToken(requestBody, options) { let url = '/oauth/token'; const queryParams = {}; const headerParams = {}; // Extract security requirements for this endpoint const securityRequirements = []; // Collect all parameters for token resolution (including path params) const allParams = {}; const config = { method: 'POST', url, params: queryParams, headers: { 'Content-Type': 'application/x-www-form-urlencoded', ...headerParams, ...options?.headers }, data: new URLSearchParams(requestBody).toString(), ...options }; // If security requirements exist, override Authorization header with appropriate token if (securityRequirements.length > 0) { // Access the HighLevel instance through the parent to get the token const ghlInstance = this.client.__ghlInstance; if (ghlInstance && typeof ghlInstance.getTokenForSecurity === 'function') { try { // Combine queryParams with allParams for token resolution const combinedQuery = { ...queryParams, ...allParams }; const authToken = await ghlInstance.getTokenForSecurity(securityRequirements, { ...headerParams, ...options?.headers }, combinedQuery, requestBody); config.headers = { ...config.headers, 'Authorization': authToken }; } catch (error) { throw error; // Re-throw GHLError with appropriate message } } } const response = await this.client.request(config); return response.data; } /** * Get Location Access Token from Agency Token * This API allows you to generate locationAccessToken from AgencyAccessToken */ async getLocationAccessToken(requestBody, options) { let url = '/oauth/locationToken'; const queryParams = {}; const headerParams = {}; // Extract security requirements for this endpoint const securityRequirements = ["Agency-Access-Only"]; // Collect all parameters for token resolution (including path params) const allParams = {}; const config = { method: 'POST', url, params: queryParams, headers: { 'Content-Type': 'application/x-www-form-urlencoded', ...headerParams, ...options?.headers }, data: new URLSearchParams(requestBody).toString(), ...options }; // If security requirements exist, override Authorization header with appropriate token if (securityRequirements.length > 0) { // Access the HighLevel instance through the parent to get the token const ghlInstance = this.client.__ghlInstance; if (ghlInstance && typeof ghlInstance.getTokenForSecurity === 'function') { try { // Combine queryParams with allParams for token resolution const combinedQuery = { ...queryParams, ...allParams }; const authToken = await ghlInstance.getTokenForSecurity(securityRequirements, { ...headerParams, ...options?.headers }, combinedQuery, requestBody); config.headers = { ...config.headers, 'Authorization': authToken }; } catch (error) { throw error; // Re-throw GHLError with appropriate message } } } const response = await this.client.request(config); return response.data; } /** * Get Location where app is installed * This API allows you fetch location where app is installed upon */ async getInstalledLocation(params, options) { let url = '/oauth/installedLocations'; const queryParams = {}; const headerParams = {}; // Extract security requirements for this endpoint const securityRequirements = ["Agency-Access"]; if (params) { if (params.skip !== undefined) { queryParams['skip'] = params.skip; } if (params.limit !== undefined) { queryParams['limit'] = params.limit; } if (params.query !== undefined) { queryParams['query'] = params.query; } if (params.isInstalled !== undefined) { queryParams['isInstalled'] = params.isInstalled; } if (params.companyId !== undefined) { queryParams['companyId'] = params.companyId; } if (params.appId !== undefined) { queryParams['appId'] = params.appId; } if (params.versionId !== undefined) { queryParams['versionId'] = params.versionId; } if (params.onTrial !== undefined) { queryParams['onTrial'] = params.onTrial; } if (params.planId !== undefined) { queryParams['planId'] = params.planId; } } // Collect all parameters for token resolution (including path params) const allParams = {}; if (params) { if (params.skip !== undefined) { allParams['skip'] = params.skip; } if (params.limit !== undefined) { allParams['limit'] = params.limit; } if (params.query !== undefined) { allParams['query'] = params.query; } if (params.isInstalled !== undefined) { allParams['isInstalled'] = params.isInstalled; } if (params.companyId !== undefined) { allParams['companyId'] = params.companyId; } if (params.appId !== undefined) { allParams['appId'] = params.appId; } if (params.versionId !== undefined) { allParams['versionId'] = params.versionId; } if (params.onTrial !== undefined) { allParams['onTrial'] = params.onTrial; } if (params.planId !== undefined) { allParams['planId'] = params.planId; } } const config = { method: 'GET', url, params: queryParams, headers: { ...headerParams, ...options?.headers }, ...options }; // If security requirements exist, override Authorization header with appropriate token if (securityRequirements.length > 0) { // Access the HighLevel instance through the parent to get the token const ghlInstance = this.client.__ghlInstance; if (ghlInstance && typeof ghlInstance.getTokenForSecurity === 'function') { try { // Combine queryParams with allParams for token resolution const combinedQuery = { ...queryParams, ...allParams }; const authToken = await ghlInstance.getTokenForSecurity(securityRequirements, { ...headerParams, ...options?.headers }, combinedQuery, {}); config.headers = { ...config.headers, 'Authorization': authToken }; } catch (error) { throw error; // Re-throw GHLError with appropriate message } } } const response = await this.client.request(config); return response.data; } } exports.Oauth = Oauth; exports.default = Oauth; //# sourceMappingURL=oauth.js.map