UNPKG

@hubspot/api-client

Version:

NodeJS v3 [HubSpot API](https://developers.hubspot.com/docs/api/overview) SDK(Client) files

114 lines 4.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiClientConfigurator = void 0; const lodash_merge_1 = __importDefault(require("lodash.merge")); const version_1 = require("./version"); class ApiClientConfigurator { static getParams(config, serverConfigurationClass, observableRequestContextParam, observableResponseContextParam) { const params = { middleware: this.getMiddleware(config, observableRequestContextParam, observableResponseContextParam), authMethods: this.getAuthMethods(config), }; (0, lodash_merge_1.default)(params, this.getBaseServer(config, serverConfigurationClass)); return params; } static getUserAgent() { return `hubspot-api-client-nodejs; ${version_1.VERSION}`; } static getAuthMethods(config) { let authMethods = {}; if (config.accessToken) { authMethods = Object.assign(authMethods, { oauth2: { accessToken: config.accessToken, }, }); authMethods = Object.assign(authMethods, { oauth2_legacy: { accessToken: config.accessToken, }, }); } if (config.apiKey) { authMethods = Object.assign(authMethods, { hapikey: config.apiKey, }); } if (config.developerApiKey) { authMethods = Object.assign(authMethods, { developer_hapikey: config.developerApiKey, }); } return authMethods; } static getBaseServer(config, serverConfigurationClass) { if (config.basePath) { return { baseServer: new serverConfigurationClass(config.basePath, {}) }; } return {}; } static getMiddleware(config, observableRequestContextParam, observableResponseContextParam) { const middleware = [ this.getHeaderMiddleware(config, observableRequestContextParam, observableResponseContextParam), ]; if (config.httpAgent) { middleware.push(this.getHttpAgentMiddleware(config, observableRequestContextParam, observableResponseContextParam)); } if (config.middleware) { middleware.push(...this.getCustomMiddleware(config, observableRequestContextParam, observableResponseContextParam)); } return middleware; } static getCustomMiddleware(config, observableRequestContextParam, observableResponseContextParam) { var _a, _b; return ((_b = (_a = config.middleware) === null || _a === void 0 ? void 0 : _a.filter((m) => m.pre || m.post).map((m) => ({ pre: (context) => { if (m.pre && typeof m.pre === 'function') { return new observableRequestContextParam(Promise.resolve(m.pre(context))); } return new observableRequestContextParam(Promise.resolve(context)); }, post: (context) => { if (m.post && typeof m.post === 'function') { return new observableResponseContextParam(Promise.resolve(m.post(context))); } return new observableResponseContextParam(Promise.resolve(context)); }, }))) !== null && _b !== void 0 ? _b : []); } static getHeaderMiddleware(config, observableRequestContextParam, observableResponseContextParam) { const headers = (0, lodash_merge_1.default)(config.defaultHeaders, { 'User-agent': this.getUserAgent() }); return { pre(context) { for (const key in headers) { if (Object.prototype.hasOwnProperty.call(headers, key)) { context.setHeaderParam(key, headers[key]); } } return new observableRequestContextParam(Promise.resolve(context)); }, post(context) { return new observableResponseContextParam(Promise.resolve(context)); }, }; } static getHttpAgentMiddleware(config, observableRequestContextParam, observableResponseContextParam) { const httpAgent = config.httpAgent; return { pre(context) { if (httpAgent) { context.setAgent(httpAgent); } return new observableRequestContextParam(Promise.resolve(context)); }, post(context) { return new observableResponseContextParam(Promise.resolve(context)); }, }; } } exports.ApiClientConfigurator = ApiClientConfigurator; //# sourceMappingURL=ApiClientConfigurator.js.map