diff --git a/legacy/src/services/clearbit-client.ts b/legacy/src/services/clearbit-client.ts index f948f33e..b213559b 100644 --- a/legacy/src/services/clearbit-client.ts +++ b/legacy/src/services/clearbit-client.ts @@ -4,14 +4,14 @@ import pThrottle from 'p-throttle' import type { DeepNullable, KyInstance } from '../types.js' import { assert, delay, getEnv, throttleKy } from '../utils.js' -// Only allow 20 clearbit API requests per 60s -const clearbitAPIThrottle = pThrottle({ - limit: 20, - interval: 60 * 1000, - strict: true -}) - export namespace clearbit { + // Only allow 20 clearbit API requests per 60s + export const throttle = pThrottle({ + limit: 20, + interval: 60 * 1000, + strict: true + }) + export interface CompanyEnrichmentOptions { domain: string webhook_url?: string @@ -528,7 +528,7 @@ export class ClearbitClient { this.apiKey = apiKey - const throttledKy = throttle ? throttleKy(ky, clearbitAPIThrottle) : ky + const throttledKy = throttle ? throttleKy(ky, clearbit.throttle) : ky this.ky = throttledKy.extend({ timeout: timeoutMs, diff --git a/legacy/src/services/dexa-client.ts b/legacy/src/services/dexa-client.ts index 1a10ad78..6acf3cae 100644 --- a/legacy/src/services/dexa-client.ts +++ b/legacy/src/services/dexa-client.ts @@ -23,6 +23,7 @@ export class DexaClient { this.apiKey = apiKey this.apiBaseUrl = apiBaseUrl + this.ky = ky.extend({ prefixUrl: this.apiBaseUrl, timeout: timeoutMs }) } diff --git a/legacy/src/services/diffbot-client.ts b/legacy/src/services/diffbot-client.ts index 88423a43..8e4b70ee 100644 --- a/legacy/src/services/diffbot-client.ts +++ b/legacy/src/services/diffbot-client.ts @@ -4,16 +4,16 @@ import pThrottle from 'p-throttle' import { assert, getEnv, throttleKy } from '../utils.js' -const diffbotAPIThrottle = pThrottle({ - limit: 5, - interval: 1000, - strict: true -}) - export namespace diffbot { export const API_BASE_URL = 'https://api.diffbot.com' export const KNOWLEDGE_GRAPH_API_BASE_URL = 'https://kg.diffbot.com' + export const throttle = pThrottle({ + limit: 5, + interval: 1000, + strict: true + }) + export interface DiffbotExtractOptions { /** Specify optional fields to be returned from any fully-extracted pages, e.g.: &fields=querystring,links. See available fields within each API's individual documentation pages. * @see https://docs.diffbot.com/reference/extract-optional-fields @@ -357,7 +357,7 @@ export class DiffbotClient { this.apiBaseUrl = apiBaseUrl this.apiKnowledgeGraphBaseUrl = apiKnowledgeGraphBaseUrl - const throttledKy = throttle ? throttleKy(ky, diffbotAPIThrottle) : ky + const throttledKy = throttle ? throttleKy(ky, diffbot.throttle) : ky this.ky = throttledKy.extend({ prefixUrl: apiBaseUrl, diff --git a/legacy/src/services/exa-client.ts b/legacy/src/services/exa-client.ts index 9aedc08d..d4bf6274 100644 --- a/legacy/src/services/exa-client.ts +++ b/legacy/src/services/exa-client.ts @@ -147,6 +147,7 @@ export class ExaClient { this.apiKey = apiKey this.apiBaseUrl = apiBaseUrl + this.ky = ky.extend({ prefixUrl: this.apiBaseUrl, headers: { diff --git a/legacy/src/services/people-data-labs-client.ts b/legacy/src/services/people-data-labs-client.ts index 7385c1a5..e0846ea1 100644 --- a/legacy/src/services/people-data-labs-client.ts +++ b/legacy/src/services/people-data-labs-client.ts @@ -3,15 +3,15 @@ import pThrottle from 'p-throttle' import { assert, getEnv, throttleKy } from '../utils.js' -const peopleDataLabsAPIThrottle = pThrottle({ - limit: 20, - interval: 60 * 1000, - strict: true -}) - export namespace peopledatalabs { export const BASE_URL = 'https://api.peopledatalabs.com/v5/' + export const throttle = pThrottle({ + limit: 20, + interval: 60 * 1000, + strict: true + }) + export const JobTitleLevels = [ 'cxo', 'director', @@ -453,9 +453,7 @@ export class PeopleDataLabsClient { this.apiKey = apiKey this.apiBaseUrl = apiBaseUrl - const throttledKy = throttle - ? throttleKy(ky, peopleDataLabsAPIThrottle) - : ky + const throttledKy = throttle ? throttleKy(ky, peopledatalabs.throttle) : ky this.ky = throttledKy.extend({ prefixUrl: apiBaseUrl, diff --git a/legacy/src/services/perigon-client.ts b/legacy/src/services/perigon-client.ts index 2886504f..a3be8e08 100644 --- a/legacy/src/services/perigon-client.ts +++ b/legacy/src/services/perigon-client.ts @@ -3,13 +3,13 @@ import pThrottle from 'p-throttle' import { assert, getEnv, throttleKy } from '../utils.js' -const perigonAPIThrottle = pThrottle({ - limit: 20, - interval: 60 * 1000, - strict: true -}) - export namespace perigon { + export const throttle = pThrottle({ + limit: 20, + interval: 60 * 1000, + strict: true + }) + export type ArticleLabel = | 'Opinion' | 'Non-news' @@ -268,7 +268,7 @@ export class PerigonClient { this.apiKey = apiKey - const throttledKy = throttle ? throttleKy(ky, perigonAPIThrottle) : ky + const throttledKy = throttle ? throttleKy(ky, perigon.throttle) : ky this.ky = throttledKy.extend({ prefixUrl: 'https://api.goperigon.com/v1/', diff --git a/legacy/src/services/predict-leads-client.ts b/legacy/src/services/predict-leads-client.ts index b69599c2..5cca84aa 100644 --- a/legacy/src/services/predict-leads-client.ts +++ b/legacy/src/services/predict-leads-client.ts @@ -4,13 +4,13 @@ import pThrottle from 'p-throttle' import type { DeepNullable } from '../types.js' import { assert, getEnv, throttleKy } from '../utils.js' -const predictionLeadsAPIThrottle = pThrottle({ - limit: 20, - interval: 60 * 1000, - strict: true -}) - export namespace predictleads { + export const throttle = pThrottle({ + limit: 20, + interval: 60 * 1000, + strict: true + }) + export type Meta = DeepNullable<{ count: number message?: string | null @@ -210,9 +210,7 @@ export class PredictLeadsClient { this.apiKey = apiKey this.apiToken = apiToken - const throttledKy = throttle - ? throttleKy(ky, predictionLeadsAPIThrottle) - : ky + const throttledKy = throttle ? throttleKy(ky, predictleads.throttle) : ky this.ky = throttledKy.extend({ timeout: timeoutMs, diff --git a/legacy/src/services/serpapi-client.ts b/legacy/src/services/serpapi-client.ts index 38a69b4e..af5f1309 100644 --- a/legacy/src/services/serpapi-client.ts +++ b/legacy/src/services/serpapi-client.ts @@ -2,7 +2,7 @@ import defaultKy, { type KyInstance } from 'ky' import { z } from 'zod' import { aiFunction, AIToolsProvider } from '../fns.js' -import { getEnv } from '../utils.js' +import { assert, getEnv } from '../utils.js' /** * All types have been exported from the `serpapi` package, which we're @@ -625,13 +625,7 @@ export namespace serpapi { device?: 'desktop' | 'tablet' | 'mobile' } - export type Params = Omit - - export interface ClientOptions extends Partial { - apiKey?: string - apiBaseUrl?: string - ky?: KyInstance - } + export type ClientParams = Partial> } /** @@ -640,28 +634,29 @@ export namespace serpapi { * @see https://serpapi.com/search-api */ export class SerpAPIClient extends AIToolsProvider { - protected api: KyInstance + protected ky: KyInstance protected apiKey: string protected apiBaseUrl: string - protected params: Partial + protected params: serpapi.ClientParams constructor({ apiKey = getEnv('SERPAPI_API_KEY') ?? getEnv('SERP_API_KEY'), apiBaseUrl = serpapi.BASE_URL, ky = defaultKy, ...params - }: serpapi.ClientOptions = {}) { - if (!apiKey) { - throw new Error(`Error SerpAPIClient missing required "apiKey"`) - } - + }: { + apiKey?: string + apiBaseUrl?: string + ky?: KyInstance + } & serpapi.ClientParams = {}) { + assert(apiKey, `Error SerpAPIClient missing required "apiKey"`) super() this.apiKey = apiKey this.apiBaseUrl = apiBaseUrl this.params = params - this.api = ky.extend({ + this.ky = ky.extend({ prefixUrl: this.apiBaseUrl }) } @@ -684,7 +679,7 @@ export class SerpAPIClient extends AIToolsProvider { const { timeout, ...rest } = this.params // console.log('SerpAPIClient.search', options) - return this.api + return this.ky .get('search', { searchParams: { ...rest, diff --git a/legacy/src/services/serper-client.ts b/legacy/src/services/serper-client.ts index 485b90e2..064f56de 100644 --- a/legacy/src/services/serper-client.ts +++ b/legacy/src/services/serper-client.ts @@ -186,11 +186,7 @@ export namespace serper { position: number } - export interface ClientOptions extends Omit, 'q'> { - apiKey?: string - apiBaseUrl?: string - ky?: KyInstance - } + export type ClientParams = Partial> } /** @@ -202,14 +198,18 @@ export class SerperClient extends AIToolsProvider { readonly ky: KyInstance readonly apiKey: string readonly apiBaseUrl: string - readonly params: Omit, 'q'> + readonly params: serper.ClientParams constructor({ apiKey = getEnv('SERPER_API_KEY'), apiBaseUrl = serper.BASE_URL, ky = defaultKy, ...params - }: serper.ClientOptions = {}) { + }: { + apiKey?: string + apiBaseUrl?: string + ky?: KyInstance + } & serper.ClientParams = {}) { assert( apiKey, `SerperClient missing required "apiKey" (defaults to "SERPER_API_KEY" env var)` diff --git a/legacy/src/services/weather-client.ts b/legacy/src/services/weather-client.ts index 745b6649..91dea80e 100644 --- a/legacy/src/services/weather-client.ts +++ b/legacy/src/services/weather-client.ts @@ -88,7 +88,10 @@ export class WeatherClient extends AIToolsProvider { apiBaseUrl?: string ky?: KyInstance } = {}) { - assert(apiKey, 'WEATHER_API_KEY is required') + assert( + apiKey, + 'WeatherClient missing required "apiKey" (defaults to "WEATHER_API_KEY")' + ) super() this.apiKey = apiKey