cobalt-int-sdk
Version:
Wrapper around Cobalt Intelligence services, such as the Secretary of State API.
249 lines • 9.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SosApi = void 0;
const axios_1 = __importDefault(require("axios"));
class SosApi {
constructor(apiKey, targetedEnvironment = null) {
this.apiKey = apiKey;
this.targetedEnvironment = targetedEnvironment;
}
/**
* This function will handle any long polling and return the business details of any business
* if found.
* @param businessName
* @param state
* @returns
*/
async getBusinessDetails(businessName, state, liveData, screenshot, uccData, street, city, zip, callbackUrl, nameAvailabilityCheck) {
var _a;
let url = `https://apigateway.cobaltintelligence.com/v1/search?searchQuery=${encodeURIComponent(businessName)}&state=${state}`;
if (this.targetedEnvironment) {
url = `https://apigateway.cobaltintelligence.com/${this.targetedEnvironment}/search?searchQuery=${encodeURIComponent(businessName)}&state=${state}`;
}
if (liveData === false) {
url += `&liveData=${liveData}`;
}
if (screenshot) {
url += '&screenshot=true';
}
if (uccData) {
url += '&uccData=true';
}
if (street) {
url += `&street=${encodeURIComponent(street)}`;
}
if (city) {
url += `&city=${encodeURIComponent(city)}`;
}
if (zip) {
url += `&zip=${encodeURIComponent(zip)}`;
}
if (callbackUrl) {
url += `&callbackUrl=${callbackUrl}`;
}
if (nameAvailabilityCheck) {
url += `&nameAvailabilityCheck=${nameAvailabilityCheck}`;
}
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
// This will take longer
if ((_a = axiosResponse.data) === null || _a === void 0 ? void 0 : _a.retryId) {
return await this.retryBusinessDetails(axiosResponse.data.retryId, 0, screenshot);
}
return axiosResponse.data;
}
/**
* This function will handle any long polling and return the business details of any business
* if found.
* @param sosId
* @param state
* @returns
*/
async getBusinessDetailsBySosId(sosId, state, liveData, screenshot, uccData, street, city, zip, searchQuery) {
var _a;
let url = `https://apigateway.cobaltintelligence.com/v1/search?sosId=${encodeURIComponent(sosId)}&state=${state}`;
if (this.targetedEnvironment) {
url = `https://apigateway.cobaltintelligence.com/${this.targetedEnvironment}/search?sosId=${encodeURIComponent(sosId)}&state=${state}`;
}
if (searchQuery) {
url += `&searchQuery=${encodeURIComponent(searchQuery)}`;
}
if (liveData === false) {
url += `&liveData=${liveData}`;
}
if (screenshot) {
url += '&screenshot=true';
}
if (uccData) {
url += '&uccData=true';
}
if (street) {
url += `&street=${street}`;
}
if (city) {
url += `&city=${city}`;
}
if (zip) {
url += `&zip=${zip}`;
}
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
// This will take longer
if ((_a = axiosResponse.data) === null || _a === void 0 ? void 0 : _a.retryId) {
return await this.retryBusinessDetails(axiosResponse.data.retryId, 0, screenshot);
}
return axiosResponse.data;
}
/**
* This function will handle any long polling and return the business details of any business
* if found.
* @param sosId
* @param state
* @returns
*/
async getListBySearchQuery(businessName, state, liveData) {
var _a;
let url = `https://apigateway.cobaltintelligence.com/v1/search/list?searchQuery=${encodeURIComponent(businessName)}&state=${state}`;
if (this.targetedEnvironment) {
url = `https://apigateway.cobaltintelligence.com/${this.targetedEnvironment}/search/list?searchQuery=${encodeURIComponent(businessName)}&state=${state}`;
}
if (liveData === false) {
url += `&liveData=${liveData}`;
}
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
// This will take longer
if ((_a = axiosResponse.data) === null || _a === void 0 ? void 0 : _a.retryId) {
return await this.retryBusinessDetails(axiosResponse.data.retryId, 0);
}
return axiosResponse.data;
}
/**
* This function allows you to send a business name and search all available states for instances
* of this business.
* @param businessName
* @returns
*/
async searchAllStatesByBusinessName(businessName) {
// Get all available states from sos-search-index
const indexUrl = 'https://apigateway.cobaltintelligence.com/v1/search/index';
const indexAxiosResponse = await axios_1.default.get(indexUrl, {
headers: {
'x-api-key': this.apiKey
}
});
const states = indexAxiosResponse.data;
const results = [];
const promises = [];
for (let i = 0; i < states.length; i++) {
const state = states[i].functionName.split('-')[0];
promises.push(this.getBusinessDetails(businessName, state).then((result) => {
var _a;
console.log('Results from', state, result);
results.push({
state: state,
result: ((_a = result.results) === null || _a === void 0 ? void 0 : _a.length) > 0 ? result.results[0] : result.status
});
}));
}
await Promise.all(promises);
return results;
}
/**
* This function will handle any long polling and return the business details of any business
* if found.
* @param firstName
* @param lastName
* @param state
* @returns
*/
async getDetailsByPersonName(firstName, lastName, state, liveData, screenshot, uccData, street, city, zip) {
var _a;
let url = `https://apigateway.cobaltintelligence.com/v1/search?searchByPersonFirstName=${encodeURIComponent(firstName)}&searchByPersonLastName=${encodeURIComponent(lastName)}&state=${state}`;
if (this.targetedEnvironment) {
url = `https://apigateway.cobaltintelligence.com/${this.targetedEnvironment}/search?searchByPersonFirstName=${encodeURIComponent(firstName)}&searchByPersonLastName=${encodeURIComponent(lastName)}&state=${state}`;
}
if (liveData === false) {
url += `&liveData=${liveData}`;
}
if (screenshot) {
url += '&screenshot=true';
}
if (uccData) {
url += '&uccData=true';
}
if (street) {
url += `&street=${street}`;
}
if (city) {
url += `&city=${city}`;
}
if (zip) {
url += `&zip=${zip}`;
}
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
// This will take longer
if ((_a = axiosResponse.data) === null || _a === void 0 ? void 0 : _a.retryId) {
return await this.retryBusinessDetails(axiosResponse.data.retryId, 0, screenshot);
}
return axiosResponse.data;
}
async getAPIKeyUsage() {
const url = 'https://apigateway.cobaltintelligence.com/v1/usage';
// get the usage
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
return axiosResponse.data;
}
async retryBusinessDetails(retryId, retryCount = 0, screenshot, uccData, street, city, zip) {
var _a;
let url = `https://apigateway.cobaltintelligence.com/v1/search?retryId=${retryId}`;
if (this.targetedEnvironment) {
url = `https://apigateway.cobaltintelligence.com/${this.targetedEnvironment}/search?retryId=${retryId}`;
}
if (screenshot) {
url += '&screenshot=true';
}
const axiosResponse = await axios_1.default.get(url, {
headers: {
'x-api-key': this.apiKey
}
});
// Functions timeout after 70 attempts
if (retryCount > 70) {
return { message: 'Passed 70 attempts of retries. Something must have gone wrong. Sorry.' };
}
if (((_a = axiosResponse.data) === null || _a === void 0 ? void 0 : _a.status) === 'Incomplete') {
retryCount++;
// Item not ready yet
// We wait 10 seconds and then try again
await this.timeout(10000);
return await this.retryBusinessDetails(retryId, retryCount, screenshot);
}
return axiosResponse.data;
}
timeout(ms) {
return new Promise(res => setTimeout(res, ms));
}
}
exports.SosApi = SosApi;
//# sourceMappingURL=sosApi.js.map