Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat(types): change api return values to a generic type (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberthou authored Dec 10, 2020
1 parent cbdf106 commit 853a9dc
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": ["@socialgouv/eslint-config-typescript"]
"extends": ["@socialgouv/eslint-config-typescript"],
"rules": {
"@typescript-eslint/consistent-type-definitions": ["error", "type"]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "tsc",
"start": "node dist/src/app.js",
"k8s": "yarn --silent --cwd .k8s",
"lint": "eslint .",
"lint": "eslint . -c .eslintrc",
"test": "jest"
},
"author": "Fabrique des ministère sociaux",
Expand Down
4 changes: 4 additions & 0 deletions src/api-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ArchifiltreCountStatistic = {
label: string;
value: number;
};
3 changes: 2 additions & 1 deletion src/matomo/matomo-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from "axios";

import type { ArchifiltreCountStatistic } from "../api-types";
import { matomoToken, matomoUrl } from "../config";
import type { MatomoEventCategory } from "./matomo-types";
import {
Expand Down Expand Up @@ -32,5 +33,5 @@ const getBulkMatomoData = async (): Promise<MatomoEventCategory[][]> =>
})
.then(({ data }: { data: MatomoEventCategory[][] }) => data);

export const getMatomoData = async (): Promise<MatomoEventCategory[]> =>
export const getMatomoData = async (): Promise<ArchifiltreCountStatistic[]> =>
getBulkMatomoData().then(sanitizeMatomoData);
4 changes: 2 additions & 2 deletions src/matomo/matomo-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface MatomoEventCategory {
export type MatomoEventCategory = {
label: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
nb_events: number;
}
};
4 changes: 2 additions & 2 deletions src/matomo/matomo-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ describe("matomoUtils", () => {

expect(sanitizeMatomoData(matomoData)).toEqual([
{
count: 10,
label: "label",
nb_events: 10,
},
{
count: 20,
label: "label2",
nb_events: 20,
},
]);
});
Expand Down
20 changes: 16 additions & 4 deletions src/matomo/matomo-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { compose, flatten, map, pick } from "lodash/fp";
import { compose, flatten, map, mapKeys, pick } from "lodash/fp";

import type { ArchifiltreCountStatistic } from "../api-types";
import type { MatomoEventCategory } from "./matomo-types";

interface CreateMatomoMethodParams {
type CreateMatomoMethodParams = {
method: string;
label: string;
}
};

const createMatomoMethod = ({ method, label }: CreateMatomoMethodParams) =>
`method=${method}&idSite=9&date=2019-04-17,today&period=range&label=${label}`;
Expand All @@ -25,9 +26,20 @@ export const getBulkRequestParamsFromLabels = (
{}
);

const keysMap: Record<string, string> = {
label: "label",
// eslint-disable-next-line @typescript-eslint/naming-convention
nb_events: "count",
};

const convertMatomoDataToApiData = mapKeys(
(key: string): string => keysMap[key]
);

export const sanitizeMatomoData: <T extends MatomoEventCategory>(
matomoApiResponse: T[][]
) => MatomoEventCategory[] = compose(
) => ArchifiltreCountStatistic[] = compose(
map(convertMatomoDataToApiData),
map(pick(["label", "nb_events"])),
flatten
);

0 comments on commit 853a9dc

Please sign in to comment.