Skip to content

Commit ad42bbd

Browse files
committed
feat: new fetch endpoint
1 parent cd71fcb commit ad42bbd

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ askLinkup()
7373

7474
### ⬇️ Fetch Endpoint
7575

76-
You can use the fetch endpoint to retrieve the content of a given URL either in `html` or `markdown` format.
76+
You can use the fetch endpoint to retrieve the content of a given URL in clean `markdown` format.
7777

78-
You can also use the `renderJS` flag to execute the JavaScript code of the page before returning the content.
78+
You can also use the `renderJs` flag to execute the JavaScript code of the page before returning the content.
79+
80+
You can also ask to `includeRawHtml` if you feel like it.
7981

8082
#### 📝 Example
8183

@@ -87,9 +89,8 @@ const client = new LinkupClient({
8789
});
8890

8991
const fetchLinkup = async () => client.fetch({
90-
url: 'https://www.linkup.so',
91-
outputFormat: 'html',
92-
renderJS: true,
92+
url: 'https://docs.linkup.so',
93+
renderJs: true,
9394
});
9495

9596
fetchLinkup()

src/__tests__/linkup-client.test.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,51 +192,49 @@ describe('LinkupClient', () => {
192192
});
193193

194194
describe('fetch method', () => {
195-
it('should make a successful fetch API call with HTML output', async () => {
196-
const mockResponse = { data: '<html><body>Content</body></html>' };
195+
it('should make a successful fetch API call', async () => {
196+
const mockResponse = { data: { content: 'Content' } };
197197
mockAxiosInstance.post.mockResolvedValueOnce(mockResponse as AxiosResponse);
198198

199199
const result = await underTest.fetch({
200-
outputFormat: 'html',
201200
url: 'https://example.com',
202201
});
203202

204203
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/fetch', {
205-
outputFormat: 'html',
206204
url: 'https://example.com',
207205
});
208-
expect(result).toEqual('<html><body>Content</body></html>');
206+
expect(result).toEqual({ content: 'Content' });
209207
});
210208

211-
it('should make a successful fetch API call with Markdown output', async () => {
212-
const mockResponse = { data: '# Title\n\nContent' };
209+
it('should make a successful fetch API call including raw HTML', async () => {
210+
const mockResponse = {
211+
data: { content: 'Content', rawHtml: '<h1>Title</h1><p>Content</p>' },
212+
};
213213
mockAxiosInstance.post.mockResolvedValueOnce(mockResponse as AxiosResponse);
214214

215215
const result = await underTest.fetch({
216-
outputFormat: 'markdown',
216+
includeRawHtml: true,
217217
url: 'https://example.com',
218218
});
219219

220220
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/fetch', {
221-
outputFormat: 'markdown',
221+
includeRawHtml: true,
222222
url: 'https://example.com',
223223
});
224-
expect(result).toEqual('# Title\n\nContent');
224+
expect(result).toEqual({ content: 'Content', rawHtml: '<h1>Title</h1><p>Content</p>' });
225225
});
226226

227227
it('should handle fetch with renderJS parameter', async () => {
228-
const mockResponse = { data: 'Fetched content' };
228+
const mockResponse = { data: { content: 'Fetched content' } };
229229
mockAxiosInstance.post.mockResolvedValueOnce(mockResponse as AxiosResponse);
230230

231231
await underTest.fetch({
232-
outputFormat: 'html',
233-
renderJS: true,
232+
renderJs: true,
234233
url: 'https://example.com',
235234
});
236235

237236
expect(mockAxiosInstance.post).toHaveBeenCalledWith('/fetch', {
238-
outputFormat: 'html',
239-
renderJS: true,
237+
renderJs: true,
240238
url: 'https://example.com',
241239
});
242240
});
@@ -254,7 +252,6 @@ describe('LinkupClient', () => {
254252

255253
try {
256254
await underTest.fetch({
257-
outputFormat: 'html',
258255
url: 'https://invalid-url.com',
259256
});
260257
fail('Expected fetch to throw an error');

src/linkup-client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import zodToJsonSchema from 'zod-to-json-schema';
44
import {
55
ApiConfig,
66
FetchParams,
7+
LinkupFetchResponse,
78
LinkupSearchResponse,
89
SearchOutputType,
910
SearchParams,
@@ -56,8 +57,8 @@ export class LinkupClient {
5657
.then(response => this.formatResponse<T>(response.data, params.outputType));
5758
}
5859

59-
async fetch(params: FetchParams): Promise<unknown> {
60-
return this.client.post('/fetch', params).then(response => response.data);
60+
async fetch(params: FetchParams): Promise<LinkupFetchResponse> {
61+
return this.client.post<LinkupFetchResponse>('/fetch', params).then(response => response.data);
6162
}
6263

6364
private sanitizeParams<T extends SearchOutputType>({

src/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export type LinkupApiError = {
7373

7474
export interface FetchParams {
7575
url: string;
76-
outputFormat: 'html' | 'markdown';
77-
renderJS?: boolean;
76+
renderJs?: boolean;
77+
includeRawHtml?: boolean;
78+
}
79+
80+
export interface LinkupFetchResponse {
81+
content: string;
82+
rawHtml?: string;
7883
}

0 commit comments

Comments
 (0)