Skip to content

Commit 65dc588

Browse files
committed
fix: correct crawl SDK bugs and add missing CRUD wrappers
The crawl module had several copy-paste bugs from the datasets module: - JSDoc on getCrawlsForDataset said "create a dataset" - Return type was Promise<Dataset> instead of Promise<Array<CrawlRequest>> - Error message referenced "create a crawl" on a GET operation The SDK was also missing wrappers for the createCrawl, updateCrawl, and deleteCrawl endpoints that already exist in the OpenAPI spec. Changes: - Fix JSDoc, return type, and error message on getCrawlsForDataset - Make the props argument optional (limit/page are both optional) - Add createCrawl, updateCrawl, and deleteCrawl wrappers - Add a basic crawl.test.ts that exercises getCrawlsForDataset
1 parent a99b21e commit 65dc588

2 files changed

Lines changed: 127 additions & 8 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { beforeAll, describe, expectTypeOf } from "vitest";
2+
import { TrieveSDK } from "../../sdk";
3+
import { CrawlRequest } from "../../types.gen";
4+
import { TRIEVE } from "../../__tests__/constants";
5+
import { test } from "../../__tests__/utils";
6+
7+
describe("Crawl Tests", async () => {
8+
let trieve: TrieveSDK;
9+
beforeAll(() => {
10+
trieve = TRIEVE;
11+
});
12+
13+
test("getCrawlsForDataset", async () => {
14+
const data = await trieve.getCrawlsForDataset({
15+
page: 1,
16+
limit: 10,
17+
});
18+
19+
expectTypeOf(data).toEqualTypeOf<Array<CrawlRequest>>();
20+
});
21+
});

clients/ts-sdk/src/functions/crawl/index.ts

Lines changed: 106 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,137 @@
77
import { TrieveSDK } from "../../sdk";
88
import {
99
$OpenApiTs,
10-
Dataset,
10+
CreateCrawlReqPayload,
11+
CrawlRequest,
1112
GetCrawlRequestsForDatasetData,
13+
UpdateCrawlReqPayload,
1214
} from "../../types.gen";
1315

1416
/**
15-
* Function that provides the ability to create a dataset. This function is used to create a new dataset in the organization.
17+
* Function that retrieves all crawl requests for the current dataset, with optional pagination.
1618
*
1719
* Example:
1820
* ```js
19-
* const dataset = await trieve.createDataset({
20-
* dataset_name: "My Dataset",
21+
* const crawls = await trieve.getCrawlsForDataset({
22+
* page: 1,
23+
* limit: 10,
2124
* });
2225
* ```
2326
*/
2427
export async function getCrawlsForDataset(
2528
/** @hidden */
2629
this: TrieveSDK,
27-
props: GetCrawlRequestsForDatasetData,
30+
props: Omit<GetCrawlRequestsForDatasetData, "trDataset"> = {},
2831
signal?: AbortSignal,
29-
): Promise<Dataset> {
32+
): Promise<Array<CrawlRequest>> {
3033
if (!this.datasetId) {
31-
throw new Error("Dataset ID is required to create a crawl");
34+
throw new Error("Dataset ID is required to get crawls");
3235
}
3336

3437
return this.trieve.fetch<"eject">(
3538
`/api/crawl?limit=${props.limit ?? 10}&page=${
3639
props.page ?? 1
3740
}` as keyof $OpenApiTs,
3841
"get",
42+
{
43+
datasetId: this.datasetId,
44+
},
45+
signal,
46+
) as Promise<Array<CrawlRequest>>;
47+
}
48+
49+
/**
50+
* Function that creates a new crawl request for the current dataset.
51+
*
52+
* Example:
53+
* ```js
54+
* const crawl = await trieve.createCrawl({
55+
* crawl_options: {
56+
* site_url: "https://example.com",
57+
* },
58+
* });
59+
* ```
60+
*/
61+
export async function createCrawl(
62+
/** @hidden */
63+
this: TrieveSDK,
64+
props: CreateCrawlReqPayload,
65+
signal?: AbortSignal,
66+
): Promise<CrawlRequest> {
67+
if (!this.datasetId) {
68+
throw new Error("Dataset ID is required to create a crawl");
69+
}
70+
71+
return this.trieve.fetch(
72+
"/api/crawl",
73+
"post",
3974
{
4075
data: props,
4176
datasetId: this.datasetId,
4277
},
4378
signal,
44-
) as Promise<Dataset>;
79+
) as Promise<CrawlRequest>;
80+
}
81+
82+
/**
83+
* Function that updates an existing crawl request for the current dataset.
84+
*
85+
* Example:
86+
* ```js
87+
* const crawl = await trieve.updateCrawl({
88+
* crawl_id: "3c90c3cc-0d44-4b50-8888-8dd25736052a",
89+
* crawl_options: {
90+
* site_url: "https://example.com",
91+
* },
92+
* });
93+
* ```
94+
*/
95+
export async function updateCrawl(
96+
/** @hidden */
97+
this: TrieveSDK,
98+
props: UpdateCrawlReqPayload,
99+
signal?: AbortSignal,
100+
): Promise<CrawlRequest> {
101+
if (!this.datasetId) {
102+
throw new Error("Dataset ID is required to update a crawl");
103+
}
104+
105+
return this.trieve.fetch(
106+
"/api/crawl",
107+
"put",
108+
{
109+
data: props,
110+
datasetId: this.datasetId,
111+
},
112+
signal,
113+
) as Promise<CrawlRequest>;
114+
}
115+
116+
/**
117+
* Function that deletes a crawl request for the current dataset by its ID.
118+
*
119+
* Example:
120+
* ```js
121+
* await trieve.deleteCrawl("3c90c3cc-0d44-4b50-8888-8dd25736052a");
122+
* ```
123+
*/
124+
export async function deleteCrawl(
125+
/** @hidden */
126+
this: TrieveSDK,
127+
crawlId: string,
128+
signal?: AbortSignal,
129+
): Promise<void> {
130+
if (!this.datasetId) {
131+
throw new Error("Dataset ID is required to delete a crawl");
132+
}
133+
134+
return this.trieve.fetch(
135+
"/api/crawl/{crawl_id}",
136+
"delete",
137+
{
138+
crawlId,
139+
datasetId: this.datasetId,
140+
},
141+
signal,
142+
) as Promise<void>;
45143
}

0 commit comments

Comments
 (0)