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
Show file tree
Hide file tree
Changes from all commits
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;
}
59 changes: 59 additions & 0 deletions test/Typesense/Documents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,65 @@ describe("Documents", function () {
});

describe(".import", function () {
context("when an empty array of documents is passed", function () {
it("throws RequestMalformed error", function (done) {
documents
.import([])
.then(() => {
done(new Error("Expected import to throw RequestMalformed error"));
})
.catch((error) => {
expect(error.constructor.name).to.eq("RequestMalformed");
expect(error.message).to.eq("No documents provided");
done();
});
});
});

context("when an empty string is passed", function () {
it("throws RequestMalformed error", function (done) {
documents
.import("")
.then(() => {
done(new Error("Expected import to throw RequestMalformed error"));
})
.catch((error) => {
expect(error.constructor.name).to.eq("RequestMalformed");
expect(error.message).to.eq("No documents provided");
done();
});
});
});

context("when null is passed as JSONL string", function () {
it("throws RequestMalformed error", function (done) {
documents
.import(null)
.then(() => {
done(new Error("Expected import to throw RequestMalformed error"));
})
.catch((error) => {
expect(error.constructor.name).to.eq("RequestMalformed");
expect(error.message).to.eq("No documents provided");
done();
});
});
});

context("when undefined is passed as JSONL string", function () {
it("throws RequestMalformed error", function (done) {
documents
.import(undefined)
.then(() => {
done(new Error("Expected import to throw RequestMalformed error"));
})
.catch((error) => {
expect(error.constructor.name).to.eq("RequestMalformed");
expect(error.message).to.eq("No documents provided");
done();
});
});
});
context("when a query paramater is passed", function () {
it("passes the query parameter to the API", function (done) {
mockAxios
Expand Down