Skip to content

Commit 6570ec3

Browse files
committed
fix(sdk): codecrawl post request utilites being awaited
1 parent d671074 commit 6570ec3

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

packages/sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"scripts": {
1717
"build": "tsup",
18+
"watch": "tsup --watch",
1819
"build-and-publish": "npm run build && npm publish --access public",
1920
"publish-beta": "npm run build && npm publish --access public --tag beta",
2021
"test": "NODE_OPTIONS=--experimental-vm-modules jest --verbose src/__tests__/index.test.ts"

packages/sdk/src/index.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ export interface CrawlOptions {
6969
* Defines the options and configurations available for generating LLMs.txt.
7070
*/
7171
export interface GenerateLLMsTextParams extends CrawlOptions {
72-
url: string;
73-
maxUrls: number;
7472
showFullText: boolean;
7573
}
7674

@@ -164,16 +162,16 @@ export default class CodecrawlApp {
164162
/**
165163
* Sends a POST request to the specified URL.
166164
* @param url - The URL to send the request to.
167-
* @param data - The data to send in the request body.
168-
* @param headers - Optional headers for the request.
169-
* @returns The response from the request.
165+
* @param data - The data to send in the request.
166+
* @param headers - The headers for the request.
167+
* @returns The response from the POST request.
170168
*/
171-
async postRequest(
169+
postRequest(
172170
url: string,
173171
data: any,
174172
headers: AxiosRequestHeaders,
175173
): Promise<AxiosResponse> {
176-
return await axios.post(url, data, {
174+
return axios.post(url, data, {
177175
headers,
178176
timeout: data?.timeout ? data.timeout + 5000 : undefined,
179177
});
@@ -182,8 +180,8 @@ export default class CodecrawlApp {
182180
/**
183181
* Sends a GET request to the specified URL.
184182
* @param url - The URL to send the request to.
185-
* @param headers - Optional headers for the request.
186-
* @returns The response from the request.
183+
* @param headers - The headers for the request.
184+
* @returns The response from the GET request.
187185
*/
188186
async getRequest(
189187
url: string,
@@ -192,7 +190,7 @@ export default class CodecrawlApp {
192190
try {
193191
return await axios.get(url, { headers });
194192
} catch (error) {
195-
if (error instanceof AxiosError) {
193+
if (error instanceof AxiosError && error.response) {
196194
return error.response as AxiosResponse;
197195
} else {
198196
throw error;
@@ -203,8 +201,8 @@ export default class CodecrawlApp {
203201
/**
204202
* Sends a DELETE request to the specified URL.
205203
* @param url - The URL to send the request to.
206-
* @param headers - Optional headers for the request.
207-
* @returns The response from the request.
204+
* @param headers - The headers for the request.
205+
* @returns The response from the DELETE request.
208206
*/
209207
async deleteRequest(
210208
url: string,
@@ -213,7 +211,7 @@ export default class CodecrawlApp {
213211
try {
214212
return await axios.delete(url, { headers });
215213
} catch (error) {
216-
if (error instanceof AxiosError) {
214+
if (error instanceof AxiosError && error.response) {
217215
return error.response as AxiosResponse;
218216
} else {
219217
throw error;
@@ -259,16 +257,16 @@ export default class CodecrawlApp {
259257
try {
260258
const response = await this.asyncGenerateLLMsText(url, params);
261259

262-
if (response.success || 'error' in response) {
260+
if (!response.success || 'error' in response) {
263261
return {
264262
success: false,
265-
error: 'error' in response ? response.error : 'unknown error',
263+
error: 'error' in response ? response.error : 'Unknown error',
266264
};
267265
}
268266

269267
if (!response.id) {
270268
throw new CodecrawlError(
271-
'Failed to start LLMs.txt generation. No job ID returned',
269+
`Failed to start LLMs.txt generation. No job ID returned.`,
272270
500,
273271
);
274272
}
@@ -324,11 +322,11 @@ export default class CodecrawlApp {
324322
params?: GenerateLLMsTextParams,
325323
): Promise<GenerateLLMsTextResponse | ErrorResponse> {
326324
const headers = this.prepareHeaders();
327-
325+
const jsonData: any = { url, ...params };
328326
try {
329-
const response = await this.postRequest(
327+
const response: AxiosResponse = await this.postRequest(
330328
`${this.apiUrl}/v1/llmstxt`,
331-
{ ...params, url },
329+
jsonData,
332330
headers,
333331
);
334332

@@ -340,18 +338,14 @@ export default class CodecrawlApp {
340338
} catch (error: any) {
341339
if (error.response?.data?.error) {
342340
throw new CodecrawlError(
343-
`Failed to start LLMs.txt generation. Status code: ${error.response.status}. Error: ${error.response.data.error}`,
341+
`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`,
344342
error.response.status,
345343
);
346344
} else {
347-
throw new CodecrawlError(
348-
`Unexpected error occurred while starting LLMs.txt generation. Status code: ${error.response.status}.`,
349-
error.response.status,
350-
);
345+
throw new CodecrawlError(error.message, 500);
351346
}
352347
}
353-
354-
return { success: false, error: 'Internal Server Error' };
348+
return { success: false, error: 'Internal server error.' };
355349
}
356350

357351
/**

0 commit comments

Comments
 (0)