Skip to content

Commit

Permalink
feat(@whook/aws-lambda): add a better query parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Mar 22, 2020
1 parent e937aaa commit b44dc40
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
1 change: 1 addition & 0 deletions packages/whook-aws-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"memory-fs": "0.5.0",
"openapi-types": "^1.3.5",
"qs": "^6.9.1",
"strict-qs": "^6.0.1",
"yerror": "^5.0.0"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions packages/whook-aws-lambda/src/services/QUERY_PARSER.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { initializer } from 'knifecycle';
import qs from 'qs';
import { WhookQueryStringParser } from '@whook/http-router';
import { castParameterValue } from '../wrappers/awsHTTPLambda';

// A custom query parser that mimic the AWS one behavior
// for local development
// WARNING: AWS API Gateway Lambda Proxy does not support
// query params repetition (ie foo=bar&foo=bar2)
// TODO: this parser should reflect that
export default initializer(
{
name: 'QUERY_PARSER',
type: 'service',
inject: [],
},
async () => {
return ((parameters, search) => {
const queryStringParameters = qs.parse(search.slice(1));

return castParameters(
parameters ? parameters.filter(p => 'query' === (p as any).in) : [],
queryStringParameters,
);
}) as WhookQueryStringParser;
},
);

function castParameters(parameters, values) {
(parameters || []).forEach(parameter => {
values[parameter.name] = castParameterValue(
parameter,
values[parameter.name],
);
});
return values;
}
30 changes: 1 addition & 29 deletions packages/whook-aws-lambda/src/wrappers/awsHTTPLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '@whook/whook';
import { PassThrough } from 'stream';
import qs from 'qs';
import { parseReentrantNumber, parseBoolean } from 'strict-qs';
import { camelCase } from 'camel-case';
import { TimeService, LogService } from 'common-services';
import { OpenAPIV3 } from 'openapi-types';
Expand All @@ -59,7 +60,6 @@ type HTTPWrapperDependencies = {
WRAPPERS: WhookWrapper<any, any>[];
};

const BASE_10 = 10;
const SEARCH_SEPARATOR = '?';
const uuidPattern =
'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';
Expand Down Expand Up @@ -532,34 +532,6 @@ export function castParameterValue(parameter, value) {
return value;
}

// Above functions were borrowed from here
// https://github.com/nfroidure/strict-qs/blob/master/src/index.js#L221-L238
// TODO: Export it on strict-qs
// and import it here
export function parseReentrantNumber(str) {
const value = parseFloat(str);

if (value.toString(BASE_10) !== str) {
throw new HTTPError(
400,
'E_NON_REENTRANT_NUMBER',
str,
value.toString(BASE_10),
);
}

return value;
}

export function parseBoolean(str) {
if ('true' === str) {
return true;
} else if ('false' === str) {
return false;
}
throw new HTTPError(400, 'E_BAD_BOOLEAN', str);
}

function obfuscateEventBody(obfuscator, rawBody) {
if (typeof rawBody === 'string') {
try {
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"ecstatic": "^4.1.2",
"http-auth-utils": "^2.3.0",
"knifecycle": "^9.0.0",
"strict-qs": "^6.0.0",
"strict-qs": "^6.0.1",
"yerror": "^5.0.0",
"yhttperror": "^5.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-http-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"openapi-types": "^1.3.5",
"qs": "^6.9.1",
"siso": "^3.2.0",
"strict-qs": "^6.0.0",
"strict-qs": "^6.0.1",
"swagger-parser": "^8.0.4",
"yerror": "^5.0.0",
"yhttperror": "^5.0.0"
Expand Down

0 comments on commit b44dc40

Please sign in to comment.