UNPKG

hud-ai

Version:
160 lines 8.34 kB
"use strict"; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const Promise = require("bluebird"); const isAfter = require("date-fns/is_after"); const addMilliseconds = require("date-fns/add_milliseconds"); const ClientConfigFactory_1 = require("./utils/ClientConfigFactory"); const RequestManager_1 = require("./RequestManager"); const HudAiError_1 = require("./utils/HudAiError"); const resources = require("./resources"); __export(require("./entities")); class HudAiClient { static create(clientConfig) { const config = ClientConfigFactory_1.Factory(clientConfig); return new HudAiClient(config); } constructor(config) { this.baseApiUrl = config.baseApiUrl || 'https://api.hud.ai/v1'; this.baseAuthUrl = config.baseAuthUrl || 'https://accounts.hud.ai'; if (config.redirectUri) this.redirectUri = config.redirectUri; this.clientId = config.clientId; if (config.clientSecret) this.clientSecret = config.clientSecret; this.requestManager = new RequestManager_1.RequestManager(this, config); this.actionItems = new resources.ActionItemResource(this.requestManager); this.articleCompanies = new resources.ArticleCompanyResource(this.requestManager); this.articlePeople = new resources.ArticlePersonResource(this.requestManager); this.articleKeyTerms = new resources.ArticleKeyTermResource(this.requestManager); this.articleTags = new resources.ArticleTagResource(this.requestManager); this.articleGeographies = new resources.ArticleGeographyResource(this.requestManager); this.articles = new resources.ArticleResource(this.requestManager); this.cache = new resources.CacheResource(this.requestManager); this.collateral = new resources.CollateralResource(this.requestManager); this.companies = new resources.CompanyResource(this.requestManager); this.companyEvents = new resources.CompanyEventResource(this.requestManager); this.companyIndustries = new resources.CompanyIndustryResource(this.requestManager); this.companyKeyTerms = new resources.CompanyKeyTermResource(this.requestManager); this.companyProfiles = new resources.CompanyProfileResource(this.requestManager); this.conferences = new resources.ConferenceResource(this.requestManager); this.conferenceSpeakers = new resources.ConferenceSpeakerResource(this.requestManager); this.conferenceLocations = new resources.ConferenceLocationResource(this.requestManager); this.domains = new resources.DomainResource(this.requestManager); this.feed = new resources.FeedResource(this.requestManager); this.highlights = new resources.HighlightResource(this.requestManager); this.industries = new resources.IndustryResource(this.requestManager); this.jobFunctions = new resources.JobFunctionResource(this.requestManager); this.keyTerms = new resources.KeyTermResource(this.requestManager); this.organizations = new resources.OrganizationResource(this.requestManager); this.people = new resources.PersonResource(this.requestManager); this.peopleKeyTerms = new resources.PersonKeyTermResource(this.requestManager); this.quotes = new resources.QuoteResource(this.requestManager); this.sources = new resources.SourceResource(this.requestManager); this.stagedSalesforceData = new resources.StagedSalesforceDataResource(this.requestManager); this.stockAlerts = new resources.StockAlertResource(this.requestManager); this.textCorpora = new resources.TextCorpusResource(this.requestManager); this.tweets = new resources.TweetResource(this.requestManager); this.userCompanies = new resources.UserCompanyResource(this.requestManager); this.userCompanyGroups = new resources.UserCompanyGroupResource(this.requestManager); this.userContacts = new resources.UserContactResource(this.requestManager); this.userConferences = new resources.UserConferenceResource(this.requestManager); this.userContentItemReactions = new resources.UserContentItemReactionResource(this.requestManager); this.userDigestSubscriptions = new resources.UserDigestSubscriptionResource(this.requestManager); this.userFeedSettings = new resources.UserFeedSettingResource(this.requestManager); this.userIndustries = new resources.UserIndustryResource(this.requestManager); this.userJobFunctions = new resources.UserJobFunctionResource(this.requestManager); this.userKeyTerms = new resources.UserKeyTermResource(this.requestManager); this.userPeople = new resources.UserPersonResource(this.requestManager); this.userSavedItems = new resources.UserSavedItemResource(this.requestManager); this.userSources = new resources.UserSourceResource(this.requestManager); this.userTemplates = new resources.UserTemplateResource(this.requestManager); this.users = new resources.UserResource(this.requestManager); this.videos = new resources.VideoResource(this.requestManager); this.videoCompanies = new resources.VideoCompanyResource(this.requestManager); this.videoPeople = new resources.VideoPersonResource(this.requestManager); this.addDeprecatedAttributes(); } getAuthorizeUri(responseType = 'code') { if (!this.redirectUri) throw new HudAiError_1.HudAiError('cannot generate authorization URL without redirectUri'); const params = _.map({ response_type: responseType, client_id: this.clientId, redirect_uri: this.redirectUri, }, (value, key) => `${key}=${encodeURIComponent(value)}`).join('&'); return `${this.baseAuthUrl}/oauth2/authorize?${params}`; } getLogoutUri() { if (!this.redirectUri) throw new HudAiError_1.HudAiError('cannot generate logout URL without redirectUri'); return `${this.baseAuthUrl}/logout/?redirectTo=${this.redirectUri}`; } refreshTokens() { if (this.tokenExpiresAt && isAfter(this.tokenExpiresAt, new Date())) return Promise.resolve(); if (this.authorizationCode) return this.exchangeAuthCode(); if (this.refreshToken) return this.handleTokenRefresh(); if (this.clientSecret) return this.exchangeClientCredentials(); return Promise.resolve(); } setAccessToken(accessToken) { this.accessToken = accessToken; } setAuthorizationCode(authorizationCode) { this.authorizationCode = authorizationCode; } addDeprecatedAttributes() { this.article = this.articles; this.company = this.companies; this.domain = this.domains; this.keyTerm = this.keyTerms; this.textCorpus = this.textCorpora; this.user = this.users; } exchangeAuthCode() { return this.getTokens({ grant_type: 'authorization_code', code: this.authorizationCode }) .then(() => { delete this.authorizationCode; }); } exchangeClientCredentials() { return this.getTokens({ grant_type: 'client_credentials' }); } getTokens(data) { const payload = _.merge({ client_id: this.clientId, client_secret: this.clientSecret, }, data); return this.requestManager.makeRequest({ method: 'POST', data: payload, url: `${this.baseAuthUrl}/oauth2/token` }, { refreshTokens: false }) .then((response) => { this.accessToken = response.access_token; if (response.refresh_token) this.refreshToken = response.refresh_token; this.tokenExpiresAt = addMilliseconds(new Date(), response.expires_in); }); } handleTokenRefresh() { return this.getTokens({ grant_type: 'refresh_grant', refresh_token: this.refreshToken }); } } exports.HudAiClient = HudAiClient; //# sourceMappingURL=HudAiClient.js.map