gohl
Version:
Go Highlevel Node Js ease of use library implementation to their API
206 lines • 10.8 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Gohighlevel = void 0;
const blogs_1 = require("./classes/blogs");
const business_1 = require("./classes/business");
const calendars_1 = require("./classes/calendars");
const campaigns_1 = require("./classes/campaigns");
const company_1 = require("./classes/company");
const contacts_1 = require("./classes/contacts");
const contacts_workflows_1 = require("./classes/contacts.workflows");
const conversations_1 = require("./classes/conversations");
const courses_1 = require("./classes/courses");
const customfields_1 = require("./classes/customfields");
const custommenus_1 = require("./classes/custommenus");
const email_1 = require("./classes/email");
const forms_1 = require("./classes/forms");
const location_1 = require("./classes/location");
const medialibrary_1 = require("./classes/medialibrary");
const oauth_1 = require("./classes/oauth");
const opportunities_1 = require("./classes/opportunities");
const products_1 = require("./classes/products");
const saas_1 = require("./classes/saas");
const snapshots_1 = require("./classes/snapshots");
const subaccounts_1 = require("./classes/subaccounts");
const surveys_1 = require("./classes/surveys");
const triggerlinks_1 = require("./classes/triggerlinks");
const users_1 = require("./classes/users");
const axios_1 = require("axios");
const BASE_API_URL = 'https://rest.gohighlevel.com/v1';
const PROD = "https://services.leadconnectorhq.com";
const MOCK = "https://stoplight.io/mocks/highlevel/integrations/39582850";
class Gohighlevel {
constructor(credientials) {
this.credientials = credientials;
this.oauth = new oauth_1.OAuth(credientials);
if (credientials.apiKey) {
this.authData = {
useAPIKey: true,
baseurl: BASE_API_URL,
access_token: credientials.apiKey,
refresh_token: credientials.apiKey,
headers: {
"Version": "2021-04-15",
"Authorization": "Bearer " + credientials.apiKey,
"Accept": 'application/json'
}
};
this.contacts = new contacts_1.Contacts(this.authData);
this.calendar = new calendars_1.Calendar(this.authData);
this.campaigns = new campaigns_1.Campaign(this.authData);
this.company = new company_1.Company(this.authData);
this.links = new triggerlinks_1.TriggerLink(this.authData);
this.conversations = new conversations_1.Conversations(this.authData);
this.courses = new courses_1.Course(this.authData);
this.businesses = new business_1.Business(this.authData);
this.workflows = new contacts_workflows_1.Workflow(this.authData);
this.surveys = new surveys_1.Survey(this.authData);
this.blogs = new blogs_1.Blog(this.authData);
this.medialibrary = new medialibrary_1.MediaLibrary(this.authData);
this.forms = new forms_1.Form(this.authData);
this.subaccounts = new subaccounts_1.SubAccount(this.authData);
this.agency = {
locations: new location_1.Location(this.authData),
users: new users_1.User(this.authData)
};
this.customFields = new customfields_1.CustomFields(this.authData);
this.customMenus = new custommenus_1.CustomMenus(this.authData);
this.opportunities = new opportunities_1.Opportunities(this.authData);
this.products = new products_1.Products(this.authData);
this.saas = new saas_1.SaaS(this.authData);
this.snapshots = new snapshots_1.Snapshots(this.authData);
this.email = new email_1.Email(this.authData);
}
else {
this.contacts = new contacts_1.Contacts();
this.calendar = new calendars_1.Calendar();
this.campaigns = new campaigns_1.Campaign();
this.conversations = new conversations_1.Conversations();
this.company = new company_1.Company();
this.links = new triggerlinks_1.TriggerLink();
this.courses = new courses_1.Course();
this.businesses = new business_1.Business();
this.workflows = new contacts_workflows_1.Workflow();
this.surveys = new surveys_1.Survey();
this.blogs = new blogs_1.Blog();
this.forms = new forms_1.Form();
this.medialibrary = new medialibrary_1.MediaLibrary();
this.subaccounts = new subaccounts_1.SubAccount();
this.agency = {
locations: new location_1.Location(),
users: new users_1.User()
};
this.customFields = new customfields_1.CustomFields();
this.customMenus = new custommenus_1.CustomMenus();
this.opportunities = new opportunities_1.Opportunities();
this.products = new products_1.Products();
this.saas = new saas_1.SaaS();
this.snapshots = new snapshots_1.Snapshots();
this.email = new email_1.Email();
this.authData = { access_token: "", locationId: "" };
}
}
/**
* Use mock or live server
* @param test
*/
setTestMode(test) {
if (test)
this.authData.baseurl = MOCK;
else
this.authData.baseurl = PROD;
}
/**
* Set the Authorization Data for the GHL Account.
* Set the auth after getting it from the GHL.oauth.getOAuthURL()
* @param authData
*/
setAuth(authData) {
var _a, _b, _c;
this.authData = authData;
this.authData.baseurl = PROD;
this.authData.headers = {
"Version": ((_a = authData === null || authData === void 0 ? void 0 : authData.headers) === null || _a === void 0 ? void 0 : _a.Version) || "2021-04-15",
"Authorization": ((_b = authData === null || authData === void 0 ? void 0 : authData.headers) === null || _b === void 0 ? void 0 : _b.Authorization) || "Bearer " + authData.access_token,
"Accept": ((_c = authData === null || authData === void 0 ? void 0 : authData.headers) === null || _c === void 0 ? void 0 : _c.Accept) || 'application/json'
};
this.contacts = new contacts_1.Contacts(authData);
this.calendar = new calendars_1.Calendar(authData);
this.campaigns = new campaigns_1.Campaign(authData);
this.conversations = new conversations_1.Conversations(authData);
this.company = new company_1.Company(authData);
this.links = new triggerlinks_1.TriggerLink(authData);
this.courses = new courses_1.Course(authData);
this.businesses = new business_1.Business(authData);
this.workflows = new contacts_workflows_1.Workflow(authData);
this.surveys = new surveys_1.Survey(authData);
this.blogs = new blogs_1.Blog(authData);
this.forms = new forms_1.Form(authData);
this.medialibrary = new medialibrary_1.MediaLibrary(authData);
this.subaccounts = new subaccounts_1.SubAccount(authData);
this.agency = {
locations: new location_1.Location(authData),
users: new users_1.User(authData)
};
this.customFields = new customfields_1.CustomFields(authData);
this.customMenus = new custommenus_1.CustomMenus(authData);
this.opportunities = new opportunities_1.Opportunities(authData);
this.products = new products_1.Products(authData);
this.saas = new saas_1.SaaS(authData);
this.snapshots = new snapshots_1.Snapshots(authData);
this.email = new email_1.Email(authData);
}
/**
* Retrieves the authentication tokens based on the refresh token.
* If the current token hasn't expired, the current token is returned.
* Otherwise, the token is refreshed and the new token is returned.
* If an error occurs when refreshing the token, the current token is returned.
* @returns The authentication tokens including the access token, refresh token, expires in time, scope, location ID, and user type.
*/
getAuth() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
// Token hasn't expired
if (this.authData.expires_in && !isNaN(parseInt(this.authData.expires_in.toString())) && parseInt(this.authData.expires_in.toString()) > Date.now())
return this.authData;
try {
const body = new URLSearchParams({
grant_type: "refresh_token",
client_id: ((_a = this.credientials) === null || _a === void 0 ? void 0 : _a.clientId) || "",
client_secret: ((_b = this.credientials) === null || _b === void 0 ? void 0 : _b.clientSecret) || "",
refresh_token: (_c = this.authData.refresh_token) !== null && _c !== void 0 ? _c : ""
});
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const response = yield axios_1.default.post(`https://services.leadconnectorhq.com/oauth/token/`, body, { headers });
const { access_token, expires_in, scope, locationId, userType } = response.data;
// Todo save this new auth info
this.authData = { access_token: access_token, locationId: locationId };
return { access_token, expires_in, scope, locationId, userType };
}
catch (e) {
return this.authData;
}
});
}
/**
* Determines if the given auth data has expired.
*
* @returns True if the token has expired, false otherwise.
*/
isAuthExpired() {
// Token hasn't expired
const stillGood = (this.authData.expires_in && !isNaN(parseInt(this.authData.expires_in.toString())) && parseInt(this.authData.expires_in.toString()) > Date.now());
return !stillGood;
}
}
exports.Gohighlevel = Gohighlevel;
//# sourceMappingURL=index.js.map