Skip to content

Commit

Permalink
feat: add support for qs options
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-parakey committed Jun 27, 2023
1 parent f375dbb commit 3e55810
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare module "moleculer-web" {
ServiceSchema,
} from "moleculer";
import { Errors } from "moleculer";
import { IParseOptions } from 'qs';

// RateLimit
export type generateRateLimitKey = (req: IncomingMessage) => string;
Expand Down Expand Up @@ -581,6 +582,7 @@ declare module "moleculer-web" {
* Path prefix to this route
*/
path: string;
qsOptions: IParseOptions;
/**
* If you don’t want to publish all actions, you can filter them with whitelist option.<br>
* Use match strings or regexp in list. To enable all actions, use "**" item.<br>
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ module.exports = {
let query = {};
const questionIdx = req.url.indexOf("?", 1);
if (questionIdx !== -1) {
query = queryString.parse(req.url.substring(questionIdx + 1));
query = queryString.parse(req.url.substring(questionIdx + 1), this.settings.qsOptions);
url = req.url.substring(0, questionIdx);
}
return { query, url };
Expand Down
46 changes: 46 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5262,3 +5262,49 @@ describe("Test named routes with same path", () => {
});

});

describe("Test no qs options forwarding", () => {
let broker;
let server;

beforeAll(() => {
[broker, , server] = setup();
broker.loadService("./test/services/test.service");
return broker.start();
});

afterAll(() => broker.stop());

it("doesn't split query string", () => {
return request(server)
.get("/test/greeter?name=Alice,Bob")
.then(res => {
expect(res.body).toBe("Hello Alice,Bob");
});
});
});

describe("Test qs options forwarding", () => {
let broker;
let server;

beforeAll(() => {
[broker, , server] = setup({
qsOptions: {
comma: true
},
});
broker.loadService("./test/services/test.service");
return broker.start();
});

afterAll(() => broker.stop());

it("splits comma separated query param", () => {
return request(server)
.get("/test/queryParamsArray?names=Alice,Bob")
.then(r => {
expect(r.body).toStrictEqual(["Alice", "Bob"]);
});
});
});
13 changes: 13 additions & 0 deletions test/services/test.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,18 @@ module.exports = {
return `Hello ${ctx.params.name}`;
}
},

queryParamsArray: {
rest: {
method: "GET",
path: "/queryParamsArray"
},
params: {
names: "array"
},
handler(ctx) {
return ctx.params.names;
}
},
}
};

0 comments on commit 3e55810

Please sign in to comment.