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

Commit ce7d294

Browse files
author
Noah Lee
authored
Mapping a response to a model dynamically. (#436)
* Install camel-keys * Mapping data by camelcase-keys * WIP * WIP
1 parent dee4264 commit ce7d294

13 files changed

+215
-411
lines changed

ui/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
rules: {
77
'@typescript-eslint/no-explicit-any': 'off',
88
'@typescript-eslint/no-empty-interface': 'off',
9+
'@typescript-eslint/explicit-module-boundary-types': 'off',
910
},
1011
};

ui/package-lock.json

+88-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@testing-library/react": "^11.2.7",
1010
"@testing-library/user-event": "^12.8.3",
1111
"antd": "^4.16.1",
12+
"camelcase-keys": "^7.0.2",
1213
"craco-less": "^1.17.1",
1314
"follow-redirects": "^1.14.7",
1415
"http-status-codes": "^2.1.4",

ui/src/apis/branch.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
import camelcaseKeys from 'camelcase-keys';
12
import { StatusCodes } from 'http-status-codes';
23

34
import { instance, headers } from './setting';
45
import { _fetch } from './_base';
56
import { Branch, HttpNotFoundError } from '../models';
67

7-
interface BranchData {
8-
name: string;
9-
commit_sha: string;
10-
}
11-
12-
const mapDataToBranch = (data: BranchData): Branch => {
13-
return {
14-
name: data.name,
15-
commitSha: data.commit_sha,
16-
};
8+
const mapDataToBranch = (data: any): Branch => {
9+
const branch = camelcaseKeys(data, { deep: true });
10+
return branch;
1711
};
1812

1913
export const listBranches = async (
@@ -30,7 +24,7 @@ export const listBranches = async (
3024
}
3125
)
3226
.then((response) => response.json())
33-
.then((branches) => branches.map((b: BranchData) => mapDataToBranch(b)));
27+
.then((branches) => branches.map((b: any) => mapDataToBranch(b)));
3428

3529
return branches;
3630
};
@@ -54,7 +48,7 @@ export const getBranch = async (
5448

5549
const ret: Branch = await response
5650
.json()
57-
.then((b: BranchData) => mapDataToBranch(b));
51+
.then((b: any) => mapDataToBranch(b));
5852

5953
return ret;
6054
};

ui/src/apis/commit.ts

+16-53
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,24 @@
1+
import camelcaseKeys from 'camelcase-keys';
12
import { StatusCodes } from 'http-status-codes';
23

34
import { instance, headers } from './setting';
45
import { _fetch } from './_base';
5-
import {
6-
Commit,
7-
Author,
8-
Status,
9-
HttpNotFoundError,
10-
StatusState,
11-
} from '../models';
12-
13-
interface CommitData {
14-
sha: string;
15-
message: string;
16-
is_pull_request: boolean;
17-
html_url: string;
18-
author?: {
19-
login: string;
20-
avatar_url: string;
21-
date: string;
22-
};
23-
}
24-
25-
export const mapDataToCommit = (data: CommitData): Commit => {
26-
let author: Author | undefined;
27-
28-
if (data.author) {
29-
author = {
30-
login: data.author.login,
31-
avatarUrl: data.author.avatar_url,
32-
date: new Date(data.author.date),
33-
};
6+
import { Commit, Status, HttpNotFoundError, StatusState } from '../models';
7+
8+
export const mapDataToCommit = (data: any): Commit => {
9+
const commit: Commit = camelcaseKeys(data, { deep: true });
10+
11+
if (commit.author) {
12+
// Convert the type of date field.
13+
commit.author.date = new Date(data.author.date);
3414
}
3515

36-
return {
37-
sha: data.sha,
38-
message: data.message,
39-
isPullRequest: data.is_pull_request,
40-
htmlUrl: data.html_url,
41-
author,
42-
};
16+
return commit;
4317
};
4418

45-
interface StatusData {
46-
context: string;
47-
avatar_url: string;
48-
target_url: string;
49-
state: string;
50-
}
51-
52-
const mapDataToStatus = (data: StatusData): Status => {
53-
return {
54-
context: data.context,
55-
avatarUrl: data.avatar_url,
56-
targetUrl: data.target_url,
57-
state: mapStatusState(data.state),
58-
};
19+
const mapDataToStatus = (data: any): Status => {
20+
const status = camelcaseKeys(data, { deep: true });
21+
return status;
5922
};
6023

6124
const mapStatusState = (state: string): StatusState => {
@@ -90,7 +53,7 @@ export const listCommits = async (
9053
}
9154
)
9255
.then((response) => response.json())
93-
.then((commits) => commits.map((c: CommitData) => mapDataToCommit(c)));
56+
.then((commits) => commits.map((c: any) => mapDataToCommit(c)));
9457

9558
return commits;
9659
};
@@ -114,7 +77,7 @@ export const getCommit = async (
11477

11578
const commit: Commit = await response
11679
.json()
117-
.then((c: CommitData) => mapDataToCommit(c));
80+
.then((c: any) => mapDataToCommit(c));
11881

11982
return commit;
12083
};
@@ -138,7 +101,7 @@ export const listStatuses = async (
138101

139102
const result = await response.json().then((d) => {
140103
let state: StatusState;
141-
const statuses: Status[] = d.statuses.map((status: StatusData) =>
104+
const statuses: Status[] = d.statuses.map((status: any) =>
142105
mapDataToStatus(status)
143106
);
144107

ui/src/apis/config.ts

+4-38
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,12 @@
1+
import camelcaseKeys from 'camelcase-keys';
12
import { StatusCodes } from 'http-status-codes';
23

34
import { instance, headers } from './setting';
45
import { _fetch } from './_base';
5-
import { Config, Env, HttpNotFoundError } from '../models';
6+
import { Config, HttpNotFoundError } from '../models';
67

7-
interface ConfigData {
8-
envs: EnvData[];
9-
}
10-
11-
interface EnvData {
12-
name: string;
13-
required_contexts?: string[];
14-
dynamic_payload?: {
15-
enabled: boolean;
16-
inputs: any;
17-
};
18-
review?: {
19-
enabled: boolean;
20-
reviewers: string[];
21-
};
22-
}
23-
24-
const mapDataToConfig = (data: ConfigData): Config => {
25-
const envs: Env[] = data.envs.map((e: EnvData) => {
26-
const { dynamic_payload, review } = e;
27-
28-
return {
29-
name: e.name,
30-
requiredContexts: e.required_contexts,
31-
dynamicPayload: dynamic_payload
32-
? {
33-
enabled: dynamic_payload?.enabled,
34-
inputs: dynamic_payload?.inputs,
35-
}
36-
: undefined,
37-
review,
38-
};
39-
});
40-
41-
return {
42-
envs,
43-
};
8+
const mapDataToConfig = (data: any): Config => {
9+
return camelcaseKeys(data, { deep: true });
4410
};
4511

4612
export const getConfig = async (

0 commit comments

Comments
 (0)