Skip to content

Commit

Permalink
feat: automatic query param decoding (#183)
Browse files Browse the repository at this point in the history
Fixes #184
  • Loading branch information
HomelessDinosaur authored May 16, 2023
2 parents 95e7327 + 314d422 commit 47639ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/faas/v0/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {
const testQuery = new QueryValue();
testQuery.addValue('test');
ctx.getQueryParamsMap().set('test', testQuery);
const testEncodedQuery = new QueryValue();
testEncodedQuery.addValue(encodeURIComponent('/path/here'));
ctx.getQueryParamsMap().set('test-encoded', testEncodedQuery);
const request = new TriggerRequest();
request.setData('Hello World');
request.setHttp(ctx);
Expand Down Expand Up @@ -69,6 +72,7 @@ describe('NitricTrigger.fromGrpcTriggerRequest', () => {

it('should have the provided query params', () => {
expect(trigger.http.req.query['test']).toBe('test');
expect(trigger.http.req.query['test-encoded']).toBe('/path/here');
});

it('should allow json response', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/faas/v0/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ export class HttpContext extends TriggerContext<HttpRequest, HttpResponse> {
).reduce(
(acc, [key, [val]]) => ({
...acc,
[key]: val.length === 1 ? val[0] : val,
[key]:
val.length === 1
? decodeURIComponent(val[0])
: val.map((v) => decodeURIComponent(v)),
}),
{}
);
Expand All @@ -326,7 +329,7 @@ export class HttpContext extends TriggerContext<HttpRequest, HttpResponse> {
.reduce(
(acc, [key, val]) => ({
...acc,
[key]: val,
[key]: decodeURIComponent(val),
}),
{}
);
Expand Down

0 comments on commit 47639ed

Please sign in to comment.