Skip to content

Commit 9585451

Browse files
committed
fix: better dx for search endpoint
1 parent a3a1d93 commit 9585451

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

src/__tests__/linkup-client.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { LinkupClient } from '../linkup-client';
1313
import type {
1414
ImageSearchResult,
1515
LinkupApiError,
16+
SearchParams,
1617
Source,
17-
SourcedAnswerParams,
1818
TextSearchResult,
1919
} from '../types';
2020
import { refineError } from '../utils/refine-error.utils';
@@ -375,7 +375,7 @@ describe('LinkupClient', () => {
375375
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
376376

377377
try {
378-
await underTest.search({} as SourcedAnswerParams);
378+
await underTest.search({} as SearchParams);
379379
} catch (e) {
380380
expect(e).toBeInstanceOf(LinkupInvalidRequestError);
381381
expect((e as LinkupInvalidRequestError).message).toEqual(
@@ -395,7 +395,7 @@ describe('LinkupClient', () => {
395395
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
396396

397397
try {
398-
await underTest.search({} as SourcedAnswerParams);
398+
await underTest.search({} as SearchParams);
399399
} catch (e) {
400400
expect(e).toBeInstanceOf(LinkupNoResultError);
401401
expect((e as LinkupNoResultError).message).toEqual('The query did not yield any result');
@@ -413,7 +413,7 @@ describe('LinkupClient', () => {
413413
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
414414

415415
try {
416-
await underTest.search({} as SourcedAnswerParams);
416+
await underTest.search({} as SearchParams);
417417
} catch (e) {
418418
expect(e).toBeInstanceOf(LinkupAuthenticationError);
419419
expect((e as LinkupAuthenticationError).message).toEqual('Unauthorized action');
@@ -431,7 +431,7 @@ describe('LinkupClient', () => {
431431
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
432432

433433
try {
434-
await underTest.search({} as SourcedAnswerParams);
434+
await underTest.search({} as SearchParams);
435435
} catch (e) {
436436
expect(e).toBeInstanceOf(LinkupAuthenticationError);
437437
expect((e as LinkupAuthenticationError).message).toEqual('Forbidden action');
@@ -449,7 +449,7 @@ describe('LinkupClient', () => {
449449
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
450450

451451
try {
452-
await underTest.search({} as SourcedAnswerParams);
452+
await underTest.search({} as SearchParams);
453453
} catch (e) {
454454
expect(e).toBeInstanceOf(LinkupInsufficientCreditError);
455455
expect((e as LinkupInsufficientCreditError).message).toEqual(
@@ -469,7 +469,7 @@ describe('LinkupClient', () => {
469469
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
470470

471471
try {
472-
await underTest.search({} as SourcedAnswerParams);
472+
await underTest.search({} as SearchParams);
473473
} catch (e) {
474474
expect(e).toBeInstanceOf(LinkupTooManyRequestsError);
475475
expect((e as LinkupTooManyRequestsError).message).toEqual('Too many requests');
@@ -487,7 +487,7 @@ describe('LinkupClient', () => {
487487
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
488488

489489
try {
490-
await underTest.search({} as SourcedAnswerParams);
490+
await underTest.search({} as SearchParams);
491491
} catch (e) {
492492
expect(e).toBeInstanceOf(LinkupUnknownError);
493493
expect((e as LinkupUnknownError).message).toEqual(
@@ -507,7 +507,7 @@ describe('LinkupClient', () => {
507507
mockAxiosInstance.post.mockRejectedValueOnce(refineError(invalidError));
508508

509509
try {
510-
await underTest.search({} as SourcedAnswerParams);
510+
await underTest.search({} as SearchParams);
511511
} catch (e) {
512512
expect(e).toBeInstanceOf(LinkupUnknownError);
513513
expect((e as LinkupUnknownError).message).toEqual(

src/linkup-client.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ import {
55
ApiConfig,
66
FetchParams,
77
LinkupFetchResponse,
8+
LinkupSearchResponse,
89
SearchParams,
9-
SearchResults,
10-
SearchResultsParams,
11-
SourcedAnswer,
12-
SourcedAnswerParams,
13-
Structured,
14-
StructuredParams,
15-
StructuredWithSources,
1610
} from './types';
1711
import { refineError } from './utils/refine-error.utils';
1812
import { isZodObject } from './utils/schema.utils';
@@ -43,24 +37,17 @@ export class LinkupClient {
4337
);
4438
}
4539

46-
async search(params: SourcedAnswerParams): Promise<SourcedAnswer>;
47-
async search(params: SearchResultsParams): Promise<SearchResults>;
48-
async search(params: StructuredParams & { includeSources: true }): Promise<StructuredWithSources>;
49-
async search(
50-
params: StructuredParams & { includeSources?: false | undefined },
51-
): Promise<Structured>;
52-
53-
async search(
54-
params: SearchParams,
55-
): Promise<SourcedAnswer | SearchResults | Structured | StructuredWithSources> {
40+
async search<T extends SearchParams>(params: T): Promise<LinkupSearchResponse<T>> {
5641
return this.client.post('/search', this.sanitizeParams(params)).then(response => response.data);
5742
}
5843

5944
async fetch<T extends FetchParams>(params: T): Promise<LinkupFetchResponse<T>> {
6045
return this.client.post('/fetch', params).then(response => response.data);
6146
}
6247

63-
private sanitizeParams(params: SearchParams): Record<string, string | boolean | string[]> {
48+
private sanitizeParams<T extends SearchParams>(
49+
params: T,
50+
): Record<string, string | boolean | string[]> {
6451
const {
6552
query,
6653
depth,

src/types.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ export type SearchParams = {
4747
}
4848
);
4949

50-
export type SourcedAnswerParams = Extract<SearchParams, { outputType: 'sourcedAnswer' }>;
51-
export type SearchResultsParams = Extract<SearchParams, { outputType: 'searchResults' }>;
52-
export type StructuredParams = Extract<SearchParams, { outputType: 'structured' }>;
53-
54-
export type LinkupSearchResponse =
55-
| SourcedAnswer
56-
| SearchResults
57-
| Structured
58-
| StructuredWithSources;
50+
export type LinkupSearchResponse<T> = T extends { outputType: 'structured'; includeSources: true }
51+
? StructuredWithSources
52+
: T extends { outputType: 'structured' }
53+
? Structured
54+
: T extends { outputType: 'sourcedAnswer' }
55+
? SourcedAnswer
56+
: SearchResults;
5957

6058
export type SearchResults = {
6159
results: (TextSearchResult | ImageSearchResult)[];

0 commit comments

Comments
 (0)