Skip to content

Commit 066349d

Browse files
authored
Merge pull request #262 from tharropoulos/throw-on-empty-docs
fix(documents): throw on empty lists of documents
2 parents 1c86e3a + 3a55318 commit 066349d

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-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
@@ -394,6 +394,9 @@ export default class Documents<T extends DocumentSchema = object>
394394
): Promise<string | ImportResponse[]> {
395395
let documentsInJSONLFormat;
396396
if (Array.isArray(documents)) {
397+
if (documents.length === 0) {
398+
throw new RequestMalformed("No documents provided");
399+
}
397400
try {
398401
documentsInJSONLFormat = documents
399402
.map((document) => JSON.stringify(document))
@@ -416,6 +419,9 @@ export default class Documents<T extends DocumentSchema = object>
416419
}
417420
} else {
418421
documentsInJSONLFormat = documents;
422+
if (isEmptyString(documentsInJSONLFormat)) {
423+
throw new RequestMalformed("No documents provided");
424+
}
419425
}
420426

421427
const resultsInJSONLFormat = await this.apiCall.performRequest<string>(
@@ -520,3 +526,7 @@ export default class Documents<T extends DocumentSchema = object>
520526
});
521527
}
522528
}
529+
530+
function isEmptyString(str: string | null | undefined): boolean {
531+
return str == null || str === "" || str.length === 0;
532+
}

test/Typesense/Documents.spec.js

+59
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,65 @@ describe("Documents", function () {
436436
});
437437

438438
describe(".import", function () {
439+
context("when an empty array of documents is passed", function () {
440+
it("throws RequestMalformed error", function (done) {
441+
documents
442+
.import([])
443+
.then(() => {
444+
done(new Error("Expected import to throw RequestMalformed error"));
445+
})
446+
.catch((error) => {
447+
expect(error.constructor.name).to.eq("RequestMalformed");
448+
expect(error.message).to.eq("No documents provided");
449+
done();
450+
});
451+
});
452+
});
453+
454+
context("when an empty string is passed", function () {
455+
it("throws RequestMalformed error", function (done) {
456+
documents
457+
.import("")
458+
.then(() => {
459+
done(new Error("Expected import to throw RequestMalformed error"));
460+
})
461+
.catch((error) => {
462+
expect(error.constructor.name).to.eq("RequestMalformed");
463+
expect(error.message).to.eq("No documents provided");
464+
done();
465+
});
466+
});
467+
});
468+
469+
context("when null is passed as JSONL string", function () {
470+
it("throws RequestMalformed error", function (done) {
471+
documents
472+
.import(null)
473+
.then(() => {
474+
done(new Error("Expected import to throw RequestMalformed error"));
475+
})
476+
.catch((error) => {
477+
expect(error.constructor.name).to.eq("RequestMalformed");
478+
expect(error.message).to.eq("No documents provided");
479+
done();
480+
});
481+
});
482+
});
483+
484+
context("when undefined is passed as JSONL string", function () {
485+
it("throws RequestMalformed error", function (done) {
486+
documents
487+
.import(undefined)
488+
.then(() => {
489+
done(new Error("Expected import to throw RequestMalformed error"));
490+
})
491+
.catch((error) => {
492+
expect(error.constructor.name).to.eq("RequestMalformed");
493+
expect(error.message).to.eq("No documents provided");
494+
done();
495+
});
496+
});
497+
});
439498
context("when a query paramater is passed", function () {
440499
it("passes the query parameter to the API", function (done) {
441500
mockAxios

0 commit comments

Comments
 (0)