Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(documents): throw on empty lists of documents #262

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion src/Typesense/Documents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ReadStream } from "fs";
import ApiCall from "./ApiCall";
import Configuration from "./Configuration";
import { ImportError } from "./Errors";
import { ImportError, RequestMalformed } from "./Errors";
import { SearchOnlyDocuments } from "./SearchOnlyDocuments";

// Todo: use generic to extract filter_by values
Expand Down Expand Up @@ -388,6 +388,9 @@ export default class Documents<T extends DocumentSchema = object>
): Promise<string | ImportResponse[]> {
let documentsInJSONLFormat;
if (Array.isArray(documents)) {
if (documents.length === 0) {
throw new RequestMalformed("No documents provided");
}
try {
documentsInJSONLFormat = documents
.map((document) => JSON.stringify(document))
Expand All @@ -410,6 +413,9 @@ export default class Documents<T extends DocumentSchema = object>
}
} else {
documentsInJSONLFormat = documents;
if (isEmptyString(documentsInJSONLFormat)) {
throw new RequestMalformed("No documents provided");
}
}

const resultsInJSONLFormat = await this.apiCall.performRequest<string>(
Expand Down Expand Up @@ -514,3 +520,7 @@ export default class Documents<T extends DocumentSchema = object>
});
}
}

function isEmptyString(str: string | null | undefined): boolean {
return str == null || str === "" || str.length === 0;
}