Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions src/__tests__/linkup-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import axios, { type AxiosResponse } from 'axios';
import { z } from 'zod';
import {
FetchUrlIsFileError,
LinkupAuthenticationError,
LinkupFetchError,
LinkupFetchResponseTooLargeError,
LinkupFetchUnsupportedContentTypeError,
LinkupInsufficientCreditError,
LinkupInvalidRequestError,
LinkupNoResultError,
Expand All @@ -13,7 +13,7 @@ import {
LinkupUnknownError,
} from '../errors';
import { LinkupClient } from '../linkup-client';
import type { ImageSearchResult, SearchParams, Source, TextSearchResult } from '../types';
import type { SearchParams, Source } from '../types';
import { refineError } from '../utils/refine-error.utils';
import type { X402Signer } from '../x402/types';

Expand Down Expand Up @@ -151,16 +151,16 @@ describe('LinkupClient', () => {
url: 'http://foo.bar/baz',
},
{
content: 'foo bar baz',
favicon: 'http://foo.bar/favicon.ico',
favicon: 'http://bar.baz/favicon.ico',
name: 'bar',
type: 'text',
url: 'http://foo.bar/baz',
snippet: 'bar baz qux',
url: 'http://bar.baz/qux',
},
{
favicon: 'http://foo.bar/favicon.ico',
name: 'baz',
type: 'image',
url: 'http://foo.bar/baz',
snippet: '',
url: 'http://baz.qux/foo',
},
],
},
Expand All @@ -177,16 +177,14 @@ describe('LinkupClient', () => {
expect((result.sources.at(0) as Source)?.url).toEqual('http://foo.bar/baz');
expect((result.sources.at(0) as Source)?.snippet).toEqual('foo bar baz');
expect((result.sources.at(0) as Source)?.favicon).toEqual('http://foo.bar/favicon.ico');
expect((result.sources.at(1) as TextSearchResult)?.type).toEqual('text');
expect((result.sources.at(1) as TextSearchResult)?.name).toEqual('bar');
expect((result.sources.at(1) as TextSearchResult)?.url).toEqual('http://foo.bar/baz');
expect((result.sources.at(1) as TextSearchResult)?.content).toEqual('foo bar baz');
expect((result.sources.at(1) as TextSearchResult)?.favicon).toEqual(
'http://foo.bar/favicon.ico',
);
expect((result.sources.at(2) as ImageSearchResult)?.type).toEqual('image');
expect((result.sources.at(2) as ImageSearchResult)?.name).toEqual('baz');
expect((result.sources.at(2) as ImageSearchResult)?.url).toEqual('http://foo.bar/baz');
expect((result.sources.at(1) as Source)?.name).toEqual('bar');
expect((result.sources.at(1) as Source)?.url).toEqual('http://bar.baz/qux');
expect((result.sources.at(1) as Source)?.snippet).toEqual('bar baz qux');
expect((result.sources.at(1) as Source)?.favicon).toEqual('http://bar.baz/favicon.ico');
expect((result.sources.at(2) as Source)?.name).toEqual('baz');
expect((result.sources.at(2) as Source)?.url).toEqual('http://baz.qux/foo');
expect((result.sources.at(2) as Source)?.snippet).toEqual('');
expect((result.sources.at(2) as Source)?.favicon).toEqual('http://foo.bar/favicon.ico');
});

it('should handle searchResults output type', async () => {
Expand Down Expand Up @@ -695,11 +693,15 @@ describe('LinkupClient', () => {
},
},
{
description: '400 FETCH_URL_IS_FILE',
ErrorClass: FetchUrlIsFileError,
expectedMessage: 'The URL points to a file',
description: '400 FETCH_UNSUPPORTED_CONTENT_TYPE',
ErrorClass: LinkupFetchUnsupportedContentTypeError,
expectedMessage: 'The URL returned an unsupported content type',
input: {
error: { code: 'FETCH_URL_IS_FILE', details: [], message: 'The URL points to a file' },
error: {
code: 'FETCH_UNSUPPORTED_CONTENT_TYPE',
details: [],
message: 'The URL returned an unsupported content type',
},
statusCode: 400,
},
},
Expand Down
14 changes: 13 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export class LinkupFetchResponseTooLargeError extends LinkupError {
}
}

export class LinkupFetchUnsupportedContentTypeError extends LinkupError {
constructor(message?: string) {
super(message);
this.name = LinkupFetchUnsupportedContentTypeError.name;

if ('captureStackTrace' in Error) {
Error.captureStackTrace(this, LinkupFetchUnsupportedContentTypeError);
}
}
}

// Payment required error, raised when x402 payment signing fails or the 402 persists after retry.
export class LinkupPaymentRequiredError extends LinkupError {
constructor(message?: string) {
Expand All @@ -120,7 +131,8 @@ export class LinkupPaymentRequiredError extends LinkupError {
}
}

export class FetchUrlIsFileError extends LinkupError {
// Backward-compatible alias for older platform deployments.
export class FetchUrlIsFileError extends LinkupFetchUnsupportedContentTypeError {
constructor(message?: string) {
super(message);
this.name = FetchUrlIsFileError.name;
Expand Down
10 changes: 2 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ export type StructuredWithSources = {
sources: StructuredSource[];
};

export type StructuredSource = {
url: string;
content: string;
name: string;
type: string;
favicon: string;
};
export type StructuredSource = TextSearchResult | ImageSearchResult;

type BaseSearchRequestParams = {
query: string;
Expand Down Expand Up @@ -93,7 +87,7 @@ export type ImageSearchResult = {

export type SourcedAnswer = {
answer: string;
sources: (Source | TextSearchResult | ImageSearchResult)[];
sources: Source[];
};

export type Source = {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/refine-error.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LinkupError,
LinkupFetchError,
LinkupFetchResponseTooLargeError,
LinkupFetchUnsupportedContentTypeError,
LinkupInsufficientCreditError,
LinkupInvalidRequestError,
LinkupNoResultError,
Expand Down Expand Up @@ -41,6 +42,8 @@ export const refineError = (e: LinkupApiError): LinkupError => {
return new LinkupFetchError(message);
case 'FETCH_RESPONSE_TOO_LARGE':
return new LinkupFetchResponseTooLargeError(message);
case 'FETCH_UNSUPPORTED_CONTENT_TYPE':
return new LinkupFetchUnsupportedContentTypeError(message);
case 'FETCH_URL_IS_FILE':
return new FetchUrlIsFileError(message);
default:
Expand Down