Skip to content

Commit bd71a54

Browse files
committedFeb 5, 2025··
fix(documents): throw on empty lists of documents
1 parent 7652a07 commit bd71a54

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed
 

‎src/Typesense/Documents.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReadStream } from "fs";
22
import ApiCall from "./ApiCall";
33
import Configuration from "./Configuration";
4-
import { ImportError } from "./Errors";
4+
import { ImportError, RequestMalformed } from "./Errors";
55
import { SearchOnlyDocuments } from "./SearchOnlyDocuments";
66

77
// Todo: use generic to extract filter_by values
@@ -388,6 +388,9 @@ export default class Documents<T extends DocumentSchema = object>
388388
): Promise<string | ImportResponse[]> {
389389
let documentsInJSONLFormat;
390390
if (Array.isArray(documents)) {
391+
if (documents.length === 0) {
392+
throw new RequestMalformed("No documents provided");
393+
}
391394
try {
392395
documentsInJSONLFormat = documents
393396
.map((document) => JSON.stringify(document))
@@ -410,6 +413,9 @@ export default class Documents<T extends DocumentSchema = object>
410413
}
411414
} else {
412415
documentsInJSONLFormat = documents;
416+
if (isEmptyString(documentsInJSONLFormat)) {
417+
throw new RequestMalformed("No documents provided");
418+
}
413419
}
414420

415421
const resultsInJSONLFormat = await this.apiCall.performRequest<string>(
@@ -514,3 +520,7 @@ export default class Documents<T extends DocumentSchema = object>
514520
});
515521
}
516522
}
523+
524+
function isEmptyString(str: string | null | undefined): boolean {
525+
return str == null || str === "" || str.length === 0;
526+
}

0 commit comments

Comments
 (0)