@hubspot/api-client
Version:
NodeJS v3 [HubSpot API](https://developers.hubspot.com/docs/api/overview) SDK(Client) files
81 lines • 2.94 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = void 0;
const ApiClientConfigurator_1 = require("../../configuration/ApiClientConfigurator");
const Auth_1 = require("./Auth");
const AuthTypes_1 = require("./AuthTypes");
class Request {
constructor(config = {}, opts = {}) {
this.baseUrl = 'https://api.hubapi.com';
this.headers = {};
this.config = config;
this.opts = opts;
if (config.basePath) {
this.baseUrl = config.basePath;
}
this.url = this.generateUrl();
this.method = this.opts.method || 'GET';
this.initHeaders();
this.applyAuth();
this.setBody();
}
getSendData() {
let sendData = {
method: this.method,
headers: this.headers,
};
if (this.body) {
sendData = Object.assign(sendData, { body: this.body });
}
return sendData;
}
getUrl() {
return this.url;
}
applyAuth() {
var _a;
const authType = Auth_1.Auth.chooseAuth(this.opts, this.config);
if (authType && authType in AuthTypes_1.AuthTypes) {
const type = authType;
const value = (_a = this.config[type]) !== null && _a !== void 0 ? _a : '';
if (AuthTypes_1.AuthTypes[type] <= AuthTypes_1.AuthTypes.developerApiKey) {
this.url.searchParams.set('hapikey', value);
}
if (AuthTypes_1.AuthTypes[type] === AuthTypes_1.AuthTypes.accessToken) {
this.headers = Object.assign(this.headers, { Authorization: `Bearer ${value}` });
}
}
}
initHeaders() {
var _a, _b;
if ((_b = (_a = this.opts) === null || _a === void 0 ? void 0 : _a.defaultJson) !== null && _b !== void 0 ? _b : true) {
this.headers = { 'Content-Type': 'application/json' };
}
this.headers = Object.assign(this.headers, this.config.defaultHeaders, this.getDefaultHeaders(), this.opts.headers);
}
getDefaultHeaders() {
return {
Accept: 'application/json, */*;q=0.8',
'User-agent': ApiClientConfigurator_1.ApiClientConfigurator.getUserAgent(),
};
}
generateUrl() {
let urlStr = this.opts.overlapUrl || this.baseUrl + (this.opts.path || '');
if (this.opts.qs) {
const params = new URLSearchParams(this.opts.qs);
urlStr = `${urlStr}?${params}`;
}
return new URL(urlStr);
}
setBody() {
var _a, _b;
if (this.opts.body) {
this.body = this.opts.body;
if ((_b = (_a = this.opts) === null || _a === void 0 ? void 0 : _a.defaultJson) !== null && _b !== void 0 ? _b : true) {
this.body = JSON.stringify(this.body);
}
}
}
}
exports.Request = Request;
//# sourceMappingURL=Request.js.map