Skip to content

Commit

Permalink
feat(v2.1.1): add typescript compilation to husky; add createQueryStr…
Browse files Browse the repository at this point in the history
…ingParamsObjectFromString()
  • Loading branch information
mikaelvesavuori committed Mar 1, 2023
1 parent d10d550 commit e787a44
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 178 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx tsc
npm run build
npm run test
npm run docs:madge
Expand Down
358 changes: 185 additions & 173 deletions diagrams/code-diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gitmetrix",
"description": "Helps you find your team-level engineering metrics",
"version": "2.1.0",
"version": "2.1.1",
"author": "Mikael Vesavuori",
"license": "MIT",
"keywords": [
Expand All @@ -24,7 +24,7 @@
"scripts": {
"start": "npx sls offline --reloadHandler",
"test": "npm run test:licenses && npm run test:types && npm run test:unit",
"test:types": "npx type-coverage --at-least 98 --strict --ignore-files \"tests/**/*.ts\" --ignore-files \"*.ts\" --ignore-files \"src/application/errors/*.ts\" --ignore-files \"testdata/*.ts\"",
"test:types": "npx type-coverage --at-least 97.5 --strict --ignore-files \"tests/**/*.ts\" --ignore-files \"*.ts\" --ignore-files \"src/application/errors/*.ts\" --ignore-files \"testdata/*.ts\"",
"test:licenses": "npx license-compliance --direct --allow 'MIT;ISC;0BSD;BSD-2-Clause;BSD-3-Clause;Apache-2.0;Unlicense;CC0-1.0'",
"test:unit": "npx c8 -reporter=lcov ava",
"test:createdata": "npx ts-node tests/createTestData.ts",
Expand Down
17 changes: 17 additions & 0 deletions src/application/createQueryStringParamsObjectFromString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @description Create a conventional object of query string parameters
* from an HTTP API's `event` object that only has query string parameters
* in the shape of a "raw" string.
*/
export function createQueryStringParamsObjectFromString(event: Record<string, any>) {
const queryStringParameters: Record<string, any> = {};

const parts = event?.rawQueryString?.split('&');
if (parts && parts.length > 0)
parts.forEach((part: string) => {
const [key, value] = part.split('=');
queryStringParameters[key] = value.replace('%2F', '/');
});

return queryStringParameters;
}
5 changes: 4 additions & 1 deletion src/infrastructure/adapters/web/GetMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { EventInput } from '../../../interfaces/Lambda';

import { getMetrics } from '../../../usecases/getMetrics';

import { createQueryStringParamsObjectFromString } from '../../../application/createQueryStringParamsObjectFromString';
import { getRequestDTO } from '../../../application/getRequestDTO';

import { getRepo } from '../../frameworks/getRepo';
import { end } from '../../frameworks/end';

Expand All @@ -24,7 +26,8 @@ export async function handler(event: EventInput, context: Record<string, any>) {
});

try {
const input = getRequestDTO(event.queryStringParameters || {});
const queryStringParameters = createQueryStringParamsObjectFromString(event);
const input = getRequestDTO(queryStringParameters);
const repository = getRepo(process.env.NODE_ENV === 'test');
const metrics = await getMetrics(repository, input);
return end(200, metrics);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import test from 'ava';

import { createQueryStringParamsObjectFromString } from '../../../src/application/createQueryStringParamsObjectFromString';

/**
* POSITIVE TESTS
*/

test.serial('It should return a clean object from a query parameter string', (t) => {
const expected = { repo: 'SOMEORG/SOMEREPO', from: '20230220', to: '20230225' };

const result = createQueryStringParamsObjectFromString({
rawQueryString: 'repo=SOMEORG/SOMEREPO&from=20230220&to=20230225'
});

t.deepEqual(result, expected);
});

test.serial('It should return an empty object for an unknown input', (t) => {
const expected = {};

const result = createQueryStringParamsObjectFromString({});

t.deepEqual(result, expected);
});

0 comments on commit e787a44

Please sign in to comment.