diff --git a/lib/getOptions.js b/lib/getOptions.js index 7bac4ca..24bcae4 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -4,9 +4,13 @@ const parseQuery = require("./parseQuery"); function getOptions(loaderContext) { const query = loaderContext.query; - if(typeof query === "string") { + if(typeof query === "string" && query !== "") { return parseQuery(loaderContext.query); } + if(!query || typeof query !== "object") { + // Not object-like queries are not supported. + return null; + } return query; } diff --git a/lib/parseQuery.js b/lib/parseQuery.js index b586739..5748cde 100644 --- a/lib/parseQuery.js +++ b/lib/parseQuery.js @@ -13,6 +13,9 @@ function parseQuery(query) { throw new Error("A valid query string passed to parseQuery should begin with '?'"); } query = query.substr(1); + if(!query) { + return {}; + } if(query.substr(0, 1) === "{" && query.substr(-1) === "}") { return JSON5.parse(query); } diff --git a/test/getOptions.test.js b/test/getOptions.test.js index 120f269..8d244e3 100644 --- a/test/getOptions.test.js +++ b/test/getOptions.test.js @@ -4,8 +4,13 @@ const assert = require("assert"); const loaderUtils = require("../lib"); describe("getOptions()", () => { - describe("when loaderContext.query is a string", () => { + describe("when loaderContext.query is a query string starting with ?", () => { [{ + it: "should return an empty object by default", + query: "?", + expected: {} + }, + { it: "should parse query params", query: "?name=cheesecake&slices=8&delicious&warm=false", expected: { @@ -66,17 +71,27 @@ describe("getOptions()", () => { ); }); }); - describe("and the query string does not start with ?", () => { - it("should throw an error", () => { - assert.throws( - () => loaderUtils.getOptions({ query: "a" }), - "A valid query string passed to parseQuery should begin with '?'" - ); - }); + }); + describe("when loaderContext.query is an empty string", () => { + it("should return null", () => { + assert.strictEqual( + loaderUtils.getOptions({ + query: "" + }), + null + ); + }); + }); + describe("when loaderContext.query is any other string not starting with ?", () => { + it("should throw an error", () => { + assert.throws( + () => loaderUtils.getOptions({ query: "a" }), + "A valid query string passed to parseQuery should begin with '?'" + ); }); }); describe("when loaderContext.query is an object", () => { - it("should just return the object", () => { + it("should just return it", () => { const query = {}; assert.strictEqual( loaderUtils.getOptions({ @@ -86,7 +101,7 @@ describe("getOptions()", () => { ); }); }); - describe("when loaderContext.query is anything else", () => { + describe("when loaderContext.query is an array", () => { it("should just return it", () => { const query = []; assert.strictEqual( @@ -95,11 +110,15 @@ describe("getOptions()", () => { }), query ); + }); + }); + describe("when loaderContext.query is anything else", () => { + it("should return null", () => { assert.strictEqual( loaderUtils.getOptions({ query: undefined }), - undefined + null ); assert.strictEqual( loaderUtils.getOptions({ @@ -107,6 +126,18 @@ describe("getOptions()", () => { }), null ); + assert.strictEqual( + loaderUtils.getOptions({ + query: 1 + }), + null + ); + assert.strictEqual( + loaderUtils.getOptions({ + query: 0 + }), + null + ); }); }); });