From 807cbb48f541c892194ed2a9a7a7ebcc1180d777 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Mon, 10 Jun 2024 17:39:38 -0500 Subject: [PATCH] feat: fix issue with possibly sending undefined as searchparams to some providers --- legacy/src/services/clearbit-client.ts | 4 +-- legacy/src/services/social-data-client.ts | 35 ++++++++++++--------- legacy/src/services/weather-client.ts | 6 ++-- legacy/src/services/wolfram-alpha-client.ts | 6 ++-- legacy/src/utils.ts | 10 +++--- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/legacy/src/services/clearbit-client.ts b/legacy/src/services/clearbit-client.ts index bb6ac476..ee32adb8 100644 --- a/legacy/src/services/clearbit-client.ts +++ b/legacy/src/services/clearbit-client.ts @@ -556,7 +556,7 @@ export class ClearbitClient { async companyEnrichment(options: clearbit.CompanyEnrichmentOptions) { return this.ky .get('https://company-stream.clearbit.com/v2/companies/find', { - searchParams: { ...options } + searchParams: sanitizeSearchParams(options) }) .json() } @@ -564,7 +564,7 @@ export class ClearbitClient { async companySearch(options: clearbit.CompanySearchOptions) { return this.ky .get('https://discovery.clearbit.com/v1/companies/search', { - searchParams: { ...options } + searchParams: sanitizeSearchParams(options) }) .json() } diff --git a/legacy/src/services/social-data-client.ts b/legacy/src/services/social-data-client.ts index 61574e33..a30b2191 100644 --- a/legacy/src/services/social-data-client.ts +++ b/legacy/src/services/social-data-client.ts @@ -2,7 +2,7 @@ import defaultKy, { type KyInstance } from 'ky' import pThrottle from 'p-throttle' import { AIFunctionsProvider } from '../fns.js' -import { assert, getEnv, throttleKy } from '../utils.js' +import { assert, getEnv, sanitizeSearchParams, throttleKy } from '../utils.js' // TODO: need to add `aiFunction` wrappers for each method @@ -207,7 +207,7 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get('twitter/statuses/show', { - searchParams: options + searchParams: sanitizeSearchParams(options) }) .json() } @@ -223,7 +223,7 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get(`twitter/tweets/${tweetId}/liking_users`, { - searchParams: params + searchParams: sanitizeSearchParams(params) }) .json() } @@ -239,7 +239,7 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get(`twitter/tweets/${tweetId}/retweeted_by`, { - searchParams: params + searchParams: sanitizeSearchParams(params) }) .json() } @@ -257,10 +257,10 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get('twitter/search', { - searchParams: { + searchParams: sanitizeSearchParams({ type: 'top', ...options - } + }) }) .json() } @@ -310,7 +310,7 @@ export class SocialDataClient extends AIFunctionsProvider { .get( `twitter/user/${userId}/${replies ? 'tweets-and-replies' : 'tweets'}`, { - searchParams: params + searchParams: sanitizeSearchParams(params) } ) .json() @@ -329,7 +329,7 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get(`twitter/user/${userId}/likes`, { - searchParams: params + searchParams: sanitizeSearchParams(params) }) .json() } @@ -345,10 +345,10 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get('twitter/followers/list', { - searchParams: { + searchParams: sanitizeSearchParams({ user_id, ...params - } + }) }) .json() } @@ -364,10 +364,10 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get('twitter/friends/list', { - searchParams: { + searchParams: sanitizeSearchParams({ user_id, ...params - } + }) }) .json() } @@ -383,7 +383,7 @@ export class SocialDataClient extends AIFunctionsProvider { return this.ky .get(`twitter/user/${sourceUserId}/following/${targetUserId}`, { - searchParams: params + searchParams: sanitizeSearchParams(params) }) .json() } @@ -391,10 +391,15 @@ export class SocialDataClient extends AIFunctionsProvider { /** * Returns a list of users with screenname or full name matching the search query. */ - async searchUsersByUsername(queryOrOpts: socialdata.SearchUsersOptions) { + async searchUsersByUsername( + queryOrOpts: string | socialdata.SearchUsersOptions + ) { + const params = + typeof queryOrOpts === 'string' ? { query: queryOrOpts } : queryOrOpts + return this.ky .get('twitter/search-users', { - searchParams: queryOrOpts + searchParams: sanitizeSearchParams(params) }) .json() } diff --git a/legacy/src/services/weather-client.ts b/legacy/src/services/weather-client.ts index cde8dbd5..1a8ca8a4 100644 --- a/legacy/src/services/weather-client.ts +++ b/legacy/src/services/weather-client.ts @@ -2,7 +2,7 @@ import defaultKy, { type KyInstance } from 'ky' import { z } from 'zod' import { aiFunction, AIFunctionsProvider } from '../fns.js' -import { assert, getEnv } from '../utils.js' +import { assert, getEnv, sanitizeSearchParams } from '../utils.js' export namespace weatherapi { export const BASE_URL = 'https://api.weatherapi.com/v1' @@ -124,10 +124,10 @@ export class WeatherClient extends AIFunctionsProvider { return this.ky .get('current.json', { - searchParams: { + searchParams: sanitizeSearchParams({ key: this.apiKey, ...options - } + }) }) .json() } diff --git a/legacy/src/services/wolfram-alpha-client.ts b/legacy/src/services/wolfram-alpha-client.ts index 980d5110..a5cdb854 100644 --- a/legacy/src/services/wolfram-alpha-client.ts +++ b/legacy/src/services/wolfram-alpha-client.ts @@ -2,7 +2,7 @@ import defaultKy, { type KyInstance } from 'ky' import { z } from 'zod' import { aiFunction, AIFunctionsProvider } from '../fns.js' -import { assert, getEnv } from '../utils.js' +import { assert, getEnv, sanitizeSearchParams } from '../utils.js' export namespace wolframalpha { export const API_BASE_URL = 'https://www.wolframalpha.com/api/' @@ -85,6 +85,8 @@ export class WolframAlphaClient extends AIFunctionsProvider { ? { input: queryOrOptions } : queryOrOptions - return this.ky.get('v1/llm-api', { searchParams: { ...options } }).text() + return this.ky + .get('v1/llm-api', { searchParams: sanitizeSearchParams(options) }) + .text() } } diff --git a/legacy/src/utils.ts b/legacy/src/utils.ts index 3cd4b82b..7da30cbf 100644 --- a/legacy/src/utils.ts +++ b/legacy/src/utils.ts @@ -106,10 +106,12 @@ export function throttleKy( * that correctly handles arrays of values as repeated keys. */ export function sanitizeSearchParams( - searchParams: Record< - string, - string | number | boolean | string[] | number[] | boolean[] | undefined - > + searchParams: + | Record< + string, + string | number | boolean | string[] | number[] | boolean[] | undefined + > + | object ): URLSearchParams { return new URLSearchParams( Object.entries(searchParams).flatMap(([key, value]) => {