From 2f6deac215e5b42cb63ad53af67f31b2d16a3741 Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 12 May 2022 23:02:02 +0900 Subject: [PATCH 1/4] Install camel-keys --- ui/package-lock.json | 94 +++++++++++++++++++++++++++++++++++++++++--- ui/package.json | 1 + 2 files changed, 89 insertions(+), 6 deletions(-) diff --git a/ui/package-lock.json b/ui/package-lock.json index 177b63dd..9c52f386 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -14,6 +14,7 @@ "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^12.8.3", "antd": "^4.16.1", + "camelcase-keys": "^7.0.2", "craco-less": "^1.17.1", "follow-redirects": "^1.14.7", "http-status-codes": "^2.1.4", @@ -4913,11 +4914,42 @@ "integrity": "sha512-gS9GVHRU+RGn5KQM2rllAlR3dU6m7AcpJKdtH8gFvQiC4Otgk98XnmMU+nZenHt/+VhnBPWwgrJsyrdcw6i23w==" }, "node_modules/camelcase": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", - "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==", + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", "engines": { "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/camelcase-keys": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/camelcase-keys/-/camelcase-keys-7.0.2.tgz", + "integrity": "sha512-Rjs1H+A9R+Ig+4E/9oyB66UC5Mj9Xq3N//vcLf2WzgdTi/3gUu3Z9KoqmlrEG4VuuLK8wJHofxzdQXz/knhiYg==", + "dependencies": { + "camelcase": "^6.3.0", + "map-obj": "^4.1.0", + "quick-lru": "^5.1.1", + "type-fest": "^1.2.1" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/camelcase-keys/node_modules/type-fest": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-1.4.0.tgz", + "integrity": "sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/caniuse-api": { @@ -12383,6 +12415,17 @@ "node": ">=0.10.0" } }, + "node_modules/map-obj": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz", + "integrity": "sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ==", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/map-visit": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/map-visit/-/map-visit-1.0.0.tgz", @@ -15303,6 +15346,17 @@ "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==" }, + "node_modules/quick-lru": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz", + "integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/raf": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/raf/-/raf-3.4.1.tgz", @@ -25221,9 +25275,27 @@ } }, "camelcase": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.2.0.tgz", - "integrity": "sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==" + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==" + }, + "camelcase-keys": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/camelcase-keys/-/camelcase-keys-7.0.2.tgz", + "integrity": "sha512-Rjs1H+A9R+Ig+4E/9oyB66UC5Mj9Xq3N//vcLf2WzgdTi/3gUu3Z9KoqmlrEG4VuuLK8wJHofxzdQXz/knhiYg==", + "requires": { + "camelcase": "^6.3.0", + "map-obj": "^4.1.0", + "quick-lru": "^5.1.1", + "type-fest": "^1.2.1" + }, + "dependencies": { + "type-fest": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-1.4.0.tgz", + "integrity": "sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA==" + } + } }, "caniuse-api": { "version": "3.0.0", @@ -31275,6 +31347,11 @@ "resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz", "integrity": "sha1-wyq9C9ZSXZsFFkW7TyasXcmKDb8=" }, + "map-obj": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/map-obj/-/map-obj-4.3.0.tgz", + "integrity": "sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ==" + }, "map-visit": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/map-visit/-/map-visit-1.0.0.tgz", @@ -33687,6 +33764,11 @@ "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==" }, + "quick-lru": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz", + "integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==" + }, "raf": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/raf/-/raf-3.4.1.tgz", diff --git a/ui/package.json b/ui/package.json index 839964f5..24dee514 100644 --- a/ui/package.json +++ b/ui/package.json @@ -9,6 +9,7 @@ "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^12.8.3", "antd": "^4.16.1", + "camelcase-keys": "^7.0.2", "craco-less": "^1.17.1", "follow-redirects": "^1.14.7", "http-status-codes": "^2.1.4", From 5e58746c881022c9217ed21a10e6938efda5aa09 Mon Sep 17 00:00:00 2001 From: noah Date: Thu, 12 May 2022 23:12:30 +0900 Subject: [PATCH 2/4] Mapping data by camelcase-keys --- ui/.eslintrc.js | 1 + ui/src/apis/branch.ts | 18 +++---- ui/src/apis/commit.ts | 69 ++++++-------------------- ui/src/apis/config.ts | 42 ++-------------- ui/src/apis/deployment.ts | 100 +++++++++----------------------------- ui/src/apis/license.ts | 19 +++----- ui/src/apis/lock.ts | 35 +++++-------- ui/src/apis/perm.ts | 35 ++++++------- ui/src/apis/repo.ts | 27 ++++------ ui/src/apis/review.ts | 61 +++++------------------ ui/src/apis/user.ts | 36 ++++++-------- 11 files changed, 118 insertions(+), 325 deletions(-) diff --git a/ui/.eslintrc.js b/ui/.eslintrc.js index 153c3fe3..5761868b 100644 --- a/ui/.eslintrc.js +++ b/ui/.eslintrc.js @@ -6,5 +6,6 @@ module.exports = { rules: { '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-empty-interface': 'off', + '@typescript-eslint/explicit-module-boundary-types': 'off', }, }; diff --git a/ui/src/apis/branch.ts b/ui/src/apis/branch.ts index a0218cbd..785f08d4 100644 --- a/ui/src/apis/branch.ts +++ b/ui/src/apis/branch.ts @@ -1,19 +1,13 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; import { Branch, HttpNotFoundError } from '../models'; -interface BranchData { - name: string; - commit_sha: string; -} - -const mapDataToBranch = (data: BranchData): Branch => { - return { - name: data.name, - commitSha: data.commit_sha, - }; +const mapDataToBranch = (data: any): Branch => { + const branch = camelcaseKeys(data, { deep: true }); + return branch; }; export const listBranches = async ( @@ -30,7 +24,7 @@ export const listBranches = async ( } ) .then((response) => response.json()) - .then((branches) => branches.map((b: BranchData) => mapDataToBranch(b))); + .then((branches) => branches.map((b: any) => mapDataToBranch(b))); return branches; }; @@ -54,7 +48,7 @@ export const getBranch = async ( const ret: Branch = await response .json() - .then((b: BranchData) => mapDataToBranch(b)); + .then((b: any) => mapDataToBranch(b)); return ret; }; diff --git a/ui/src/apis/commit.ts b/ui/src/apis/commit.ts index 3ab1a251..b24cd882 100644 --- a/ui/src/apis/commit.ts +++ b/ui/src/apis/commit.ts @@ -1,61 +1,24 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { - Commit, - Author, - Status, - HttpNotFoundError, - StatusState, -} from '../models'; - -interface CommitData { - sha: string; - message: string; - is_pull_request: boolean; - html_url: string; - author?: { - login: string; - avatar_url: string; - date: string; - }; -} - -export const mapDataToCommit = (data: CommitData): Commit => { - let author: Author | undefined; - - if (data.author) { - author = { - login: data.author.login, - avatarUrl: data.author.avatar_url, - date: new Date(data.author.date), - }; +import { Commit, Status, HttpNotFoundError, StatusState } from '../models'; + +export const mapDataToCommit = (data: any): Commit => { + const commit: Commit = camelcaseKeys(data, { deep: true }); + + if (commit.author) { + // Convert the type of date field. + commit.author.date = new Date(data.author.date); } - return { - sha: data.sha, - message: data.message, - isPullRequest: data.is_pull_request, - htmlUrl: data.html_url, - author, - }; + return commit; }; -interface StatusData { - context: string; - avatar_url: string; - target_url: string; - state: string; -} - -const mapDataToStatus = (data: StatusData): Status => { - return { - context: data.context, - avatarUrl: data.avatar_url, - targetUrl: data.target_url, - state: mapStatusState(data.state), - }; +const mapDataToStatus = (data: any): Status => { + const status = camelcaseKeys(data, { deep: true }); + return status; }; const mapStatusState = (state: string): StatusState => { @@ -90,7 +53,7 @@ export const listCommits = async ( } ) .then((response) => response.json()) - .then((commits) => commits.map((c: CommitData) => mapDataToCommit(c))); + .then((commits) => commits.map((c: any) => mapDataToCommit(c))); return commits; }; @@ -114,7 +77,7 @@ export const getCommit = async ( const commit: Commit = await response .json() - .then((c: CommitData) => mapDataToCommit(c)); + .then((c: any) => mapDataToCommit(c)); return commit; }; @@ -138,7 +101,7 @@ export const listStatuses = async ( const result = await response.json().then((d) => { let state: StatusState; - const statuses: Status[] = d.statuses.map((status: StatusData) => + const statuses: Status[] = d.statuses.map((status: any) => mapDataToStatus(status) ); diff --git a/ui/src/apis/config.ts b/ui/src/apis/config.ts index 490ccaf1..f1607fd0 100644 --- a/ui/src/apis/config.ts +++ b/ui/src/apis/config.ts @@ -1,46 +1,12 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { Config, Env, HttpNotFoundError } from '../models'; +import { Config, HttpNotFoundError } from '../models'; -interface ConfigData { - envs: EnvData[]; -} - -interface EnvData { - name: string; - required_contexts?: string[]; - dynamic_payload?: { - enabled: boolean; - inputs: any; - }; - review?: { - enabled: boolean; - reviewers: string[]; - }; -} - -const mapDataToConfig = (data: ConfigData): Config => { - const envs: Env[] = data.envs.map((e: EnvData) => { - const { dynamic_payload, review } = e; - - return { - name: e.name, - requiredContexts: e.required_contexts, - dynamicPayload: dynamic_payload - ? { - enabled: dynamic_payload?.enabled, - inputs: dynamic_payload?.inputs, - } - : undefined, - review, - }; - }); - - return { - envs, - }; +const mapDataToConfig = (data: any): Config => { + return camelcaseKeys(data, { deep: true }); }; export const getConfig = async ( diff --git a/ui/src/apis/deployment.ts b/ui/src/apis/deployment.ts index 967eb62e..b5f08849 100644 --- a/ui/src/apis/deployment.ts +++ b/ui/src/apis/deployment.ts @@ -1,10 +1,9 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; import { - User, - Repo, Deployment, DeploymentType, DeploymentStatus, @@ -47,95 +46,44 @@ interface DeploymentStatusData { updated_at: string; } -export const mapDataToDeployment = (data: DeploymentData): Deployment => { - let deployer: User | undefined; - let repo: Repo | undefined; - let statuses: DeploymentStatus[] = []; +export const mapDataToDeployment = (data: any): Deployment => { + const deployment: Deployment = camelcaseKeys(data); - if (typeof data.edges.user !== 'undefined') { - deployer = mapDataToUser(data.edges.user); + // Convert the time string into Date. + deployment.createdAt = new Date(data.created_at); + deployment.updatedAt = new Date(data.updated_at); + + if ('user' in data.edges) { + deployment.deployer = mapDataToUser(data.edges.user); } - if (typeof data.edges.repo !== 'undefined') { - repo = mapDataToRepo(data.edges.repo); + if ('repo' in data.edges) { + deployment.repo = mapDataToRepo(data.edges.repo); } - if (typeof data.edges.deployment_statuses !== 'undefined') { - statuses = data.edges.deployment_statuses.map((data: any) => + if ('deployment_statuses' in data.edges) { + deployment.statuses = data.edges.deployment_statuses.map((data: any) => mapDataToDeploymentStatus(data) ); } - return { - id: data.id, - number: data.number, - type: mapDeploymentType(data.type), - ref: data.ref, - sha: data.sha, - env: data.env, - status: mapDeploymentStatusEnum(data.status), - uid: data.uid, - isRollback: data.is_rollback, - createdAt: new Date(data.created_at), - updatedAt: new Date(data.updated_at), - deployer, - repo, - statuses, - }; + return deployment; }; -function mapDeploymentType(t: string) { - switch (t) { - case 'commit': - return DeploymentType.Commit; - case 'branch': - return DeploymentType.Branch; - case 'tag': - return DeploymentType.Tag; - default: - return DeploymentType.Commit; +export function mapDataToDeploymentStatus(data: any): DeploymentStatus { + const deploymentStatus = camelcaseKeys(data, { deep: true }); + + if ('deployment' in data.edges) { + deploymentStatus.edges.deployment = mapDataToDeployment( + data.edges.deployment + ); } -} -function mapDeploymentStatusEnum(s: string) { - switch (s) { - case 'waiting': - return DeploymentStatusEnum.Waiting; - case 'created': - return DeploymentStatusEnum.Created; - case 'queued': - return DeploymentStatusEnum.Queued; - case 'running': - return DeploymentStatusEnum.Running; - case 'success': - return DeploymentStatusEnum.Success; - case 'failure': - return DeploymentStatusEnum.Failure; - case 'canceled': - return DeploymentStatusEnum.Canceled; - default: - return DeploymentStatusEnum.Waiting; + if ('repo' in data.edges) { + deploymentStatus.edges.repo = mapDataToRepo(data.edges.repo); } -} -// eslint-disable-next-line -export function mapDataToDeploymentStatus(data: any): DeploymentStatus { - return { - id: data.id, - status: data.status, - description: data.description, - logUrl: data.log_url, - createdAt: data.created_at, - updatedAt: data.updated_at, - deploymentId: data.deployment_id, - repoId: data.repo_id, - edges: { - deployment: data.edges.deployment - ? mapDataToDeployment(data.edges.deployment) - : undefined, - repo: data.edges.repo ? mapDataToRepo(data.edges.repo) : undefined, - }, - }; + return deploymentStatus; } function mapDeploymentStatusToString(status: DeploymentStatusEnum): string { diff --git a/ui/src/apis/license.ts b/ui/src/apis/license.ts index 44bdda94..14d93ffe 100644 --- a/ui/src/apis/license.ts +++ b/ui/src/apis/license.ts @@ -1,22 +1,15 @@ +import camelcaseKeys from 'camelcase-keys'; import { instance, headers } from './setting'; import { _fetch } from './_base'; import { License } from '../models'; -interface LicenseData { - kind: string; - member_count: number; - member_limit: number; - expired_at: string; -} +function mapDataToLicense(data: any): License { + const license: License = camelcaseKeys(data); + + license.expiredAt = new Date(data.expired_at); -function mapDataToLicense(data: LicenseData): License { - return { - kind: data.kind, - memberCount: data.member_count, - memberLimit: data.member_limit, - expiredAt: new Date(data.expired_at), - }; + return license; } export const getLicense = async (): Promise => { diff --git a/ui/src/apis/lock.ts b/ui/src/apis/lock.ts index 33139ce2..ccd46077 100644 --- a/ui/src/apis/lock.ts +++ b/ui/src/apis/lock.ts @@ -1,40 +1,27 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { UserData, mapDataToUser } from './user'; +import { mapDataToUser } from './user'; import { Lock, - User, HttpForbiddenError, HttpNotFoundError, HttpUnprocessableEntityError, } from '../models'; -interface LockData { - id: number; - env: string; - expired_at?: string; - created_at: string; - edges: { - user?: UserData; - }; -} - -const mapDataToLock = (data: LockData): Lock => { - let user: User | undefined; - - if (data.edges.user) { - user = mapDataToUser(data.edges.user); +const mapDataToLock = (data: any): Lock => { + const lock: Lock = camelcaseKeys(data); + + lock.expiredAt = data.expired_at ? new Date(data.expired_at) : undefined; + lock.createdAt = new Date(data.created_at); + + if ('user' in data.edges) { + lock.user = mapDataToUser(data.edges.user); } - return { - id: data.id, - env: data.env, - expiredAt: data.expired_at ? new Date(data.expired_at) : undefined, - createdAt: new Date(data.created_at), - user, - }; + return lock; }; export const listLocks = async ( diff --git a/ui/src/apis/perm.ts b/ui/src/apis/perm.ts index 8269ab02..0ee79b40 100644 --- a/ui/src/apis/perm.ts +++ b/ui/src/apis/perm.ts @@ -1,29 +1,22 @@ +import camelcaseKeys from 'camelcase-keys'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { mapDataToUser, UserData } from './user'; -import { mapDataToRepo, RepoData } from './repo'; +import { mapDataToUser } from './user'; +import { mapDataToRepo } from './repo'; import { Perm } from '../models'; -interface PermData { - repo_perm: string; - synced_at: string; - created_at: string; - updated_at: string; - edges: { - user: UserData; - repo: RepoData; - }; -} +const mapDataToPerm = (data: any): Perm => { + const perm: Perm = camelcaseKeys(data); -const mapDataToPerm = (data: PermData): Perm => { - return { - repoPerm: data.repo_perm, - syncedAt: new Date(data.synced_at), - createdAt: new Date(data.created_at), - updatedAt: new Date(data.updated_at), - user: mapDataToUser(data.edges.user), - repo: mapDataToRepo(data.edges.repo), - }; + perm.syncedAt = new Date(data.synced_at); + perm.createdAt = new Date(data.created_at); + perm.updatedAt = new Date(data.updated_at); + + // Edges + perm.user = mapDataToUser(data.edges.user); + perm.repo = mapDataToRepo(data.edges.repo); + + return perm; }; export const listPerms = async ( diff --git a/ui/src/apis/repo.ts b/ui/src/apis/repo.ts index 73a23609..9eb577e9 100644 --- a/ui/src/apis/repo.ts +++ b/ui/src/apis/repo.ts @@ -1,3 +1,4 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; @@ -7,7 +8,6 @@ import { DeploymentData, mapDataToDeployment } from './deployment'; import { Repo, HttpForbiddenError, - Deployment, HttpUnprocessableEntityError, } from '../models'; @@ -27,28 +27,19 @@ export interface RepoData { }; } -// eslint-disable-next-line -export const mapDataToRepo = (data: RepoData): Repo => { - let deployments: Deployment[] | undefined; +export const mapDataToRepo = (data: any): Repo => { + const repo: Repo = camelcaseKeys(data); - if (typeof data.edges.deployments !== 'undefined') { - deployments = data.edges.deployments.map((data) => + repo.createdAt = new Date(data.created_at); + repo.updatedAt = new Date(data.updated_at); + + if ('deployments' in data.edges) { + repo.deployments = data.edges.deployments.map((data) => mapDataToDeployment(data) ); } - return { - id: data.id, - namespace: data.namespace, - name: data.name, - description: data.description, - configPath: data.config_path, - active: data.active, - webhookId: data.webhook_id, - createdAt: new Date(data.created_at), - updatedAt: new Date(data.updated_at), - deployments, - }; + return repo; }; export const listRepos = async ( diff --git a/ui/src/apis/review.ts b/ui/src/apis/review.ts index af852b08..a9467426 100644 --- a/ui/src/apis/review.ts +++ b/ui/src/apis/review.ts @@ -1,64 +1,27 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { UserData, mapDataToUser } from './user'; -import { DeploymentData, mapDataToDeployment } from './deployment'; -import { - User, - Deployment, - Review, - ReviewStatusEnum, - HttpNotFoundError, -} from '../models'; - -export interface ReviewData { - id: number; - status: string; - comment: string; - created_at: string; - updated_at: string; - edges: { - user: UserData; - deployment: DeploymentData; - }; -} +import { mapDataToUser } from './user'; +import { mapDataToDeployment } from './deployment'; +import { Review, HttpNotFoundError } from '../models'; + +export const mapDataToReview = (data: any): Review => { + const review: Review = camelcaseKeys(data); -// eslint-disable-next-line -export const mapDataToReview = (data: ReviewData): Review => { - let user: User | undefined; - let deployment: Deployment | undefined; + review.createdAt = new Date(data.created_at); + review.updatedAt = new Date(data.updated_at); if ('user' in data.edges) { - user = mapDataToUser(data.edges.user); + review.user = mapDataToUser(data.edges.user); } if ('deployment' in data.edges) { - deployment = mapDataToDeployment(data.edges.deployment); + review.deployment = mapDataToDeployment(data.edges.deployment); } - return { - id: data.id, - status: mapDataToReviewStatus(data.status), - comment: data.comment, - createdAt: new Date(data.created_at), - updatedAt: new Date(data.updated_at), - user, - deployment, - }; -}; - -const mapDataToReviewStatus = (status: string): ReviewStatusEnum => { - switch (status) { - case 'pending': - return ReviewStatusEnum.Pending; - case 'approved': - return ReviewStatusEnum.Approved; - case 'rejected': - return ReviewStatusEnum.Rejected; - default: - return ReviewStatusEnum.Pending; - } + return review; }; export const searchReviews = async (): Promise => { diff --git a/ui/src/apis/user.ts b/ui/src/apis/user.ts index c382afd2..2e036794 100644 --- a/ui/src/apis/user.ts +++ b/ui/src/apis/user.ts @@ -1,8 +1,9 @@ +import camelcaseKeys from 'camelcase-keys'; import { StatusCodes } from 'http-status-codes'; import { _fetch } from './_base'; import { instance, headers } from './setting'; -import { User, ChatUser, RateLimit, HttpForbiddenError } from '../models'; +import { User, RateLimit, HttpForbiddenError } from '../models'; export interface UserData { id: number; @@ -29,30 +30,23 @@ interface RateLimitData { reset: string; } -export const mapDataToUser = (data: UserData): User => { - let cu: ChatUser | null; - if (data.edges.chat_user) { - const chat_user = data.edges.chat_user; +export const mapDataToUser = (data: any): User => { + const user: User = camelcaseKeys(data); - cu = { - id: chat_user.id, - createdAt: new Date(chat_user.created_at), - updatedAt: new Date(chat_user.updated_at), + user.createdAt = new Date(data.created_at); + user.updatedAt = new Date(data.updated_at); + + if ('chat_user' in data.edges) { + const { chatUserData } = data.edges; + + user.chatUser = { + id: chatUserData.id, + createdAt: new Date(chatUserData.created_at), + updatedAt: new Date(chatUserData.updated_at), }; - } else { - cu = null; } - return { - id: data.id, - login: data.login, - avatar: data.avatar, - admin: data.admin, - hash: data.hash, - createdAt: new Date(data.created_at), - updatedAt: new Date(data.updated_at), - chatUser: cu, - }; + return user; }; export const mapDataToRateLimit = (data: RateLimitData): RateLimit => { From 67d650ddab43ad963b3090cee00fa946057a8a2b Mon Sep 17 00:00:00 2001 From: noah Date: Mon, 16 May 2022 23:27:40 +0900 Subject: [PATCH 3/4] WIP --- ui/src/apis/deployment.ts | 33 ++------------------------------- ui/src/apis/repo.ts | 18 +----------------- ui/src/apis/user.ts | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 58 deletions(-) diff --git a/ui/src/apis/deployment.ts b/ui/src/apis/deployment.ts index b5f08849..954a3320 100644 --- a/ui/src/apis/deployment.ts +++ b/ui/src/apis/deployment.ts @@ -13,39 +13,10 @@ import { HttpForbiddenError, HttpConflictError, } from '../models'; -import { UserData, mapDataToUser } from './user'; -import { RepoData, mapDataToRepo } from './repo'; +import { mapDataToUser } from './user'; +import { mapDataToRepo } from './repo'; import { mapDataToCommit } from './commit'; -export interface DeploymentData { - id: number; - number: number; - type: string; - ref: string; - sha: string; - env: string; - status: string; - uid: number; - is_rollback: boolean; - auto_deploy: boolean; - created_at: string; - updated_at: string; - edges: { - user?: UserData; - repo?: RepoData; - deployment_statuses?: DeploymentStatusData[]; - }; -} - -interface DeploymentStatusData { - id: number; - status: string; - description: string; - log_url: string; - created_at: string; - updated_at: string; -} - export const mapDataToDeployment = (data: any): Deployment => { const deployment: Deployment = camelcaseKeys(data); diff --git a/ui/src/apis/repo.ts b/ui/src/apis/repo.ts index 9eb577e9..83624941 100644 --- a/ui/src/apis/repo.ts +++ b/ui/src/apis/repo.ts @@ -3,7 +3,7 @@ import { StatusCodes } from 'http-status-codes'; import { instance, headers } from './setting'; import { _fetch } from './_base'; -import { DeploymentData, mapDataToDeployment } from './deployment'; +import { mapDataToDeployment } from './deployment'; import { Repo, @@ -11,22 +11,6 @@ import { HttpUnprocessableEntityError, } from '../models'; -export interface RepoData { - id: number; - namespace: string; - name: string; - description: string; - config_path: string; - active: boolean; - webhook_id: number; - locked: boolean; - created_at: string; - updated_at: string; - edges: { - deployments?: DeploymentData[]; - }; -} - export const mapDataToRepo = (data: any): Repo => { const repo: Repo = camelcaseKeys(data); diff --git a/ui/src/apis/user.ts b/ui/src/apis/user.ts index 2e036794..c79b0463 100644 --- a/ui/src/apis/user.ts +++ b/ui/src/apis/user.ts @@ -37,24 +37,24 @@ export const mapDataToUser = (data: any): User => { user.updatedAt = new Date(data.updated_at); if ('chat_user' in data.edges) { - const { chatUserData } = data.edges; + const { chat_user } = data.edges; user.chatUser = { - id: chatUserData.id, - createdAt: new Date(chatUserData.created_at), - updatedAt: new Date(chatUserData.updated_at), + id: chat_user.id, + createdAt: new Date(chat_user.created_at), + updatedAt: new Date(chat_user.updated_at), }; } return user; }; -export const mapDataToRateLimit = (data: RateLimitData): RateLimit => { - return { - limit: data.limit, - remaining: data.remaining, - reset: new Date(data.reset), - }; +export const mapDataToRateLimit = (data: any): RateLimit => { + const rateLimit = camelcaseKeys(data); + + rateLimit.reset = new Date(data.reset); + + return rateLimit; }; /** From 16a91206fd9c513d7522ff6fe0511a1e304916e8 Mon Sep 17 00:00:00 2001 From: noah Date: Mon, 16 May 2022 23:28:49 +0900 Subject: [PATCH 4/4] WIP --- ui/src/apis/user.ts | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/ui/src/apis/user.ts b/ui/src/apis/user.ts index c79b0463..9c9f79fe 100644 --- a/ui/src/apis/user.ts +++ b/ui/src/apis/user.ts @@ -5,31 +5,6 @@ import { _fetch } from './_base'; import { instance, headers } from './setting'; import { User, RateLimit, HttpForbiddenError } from '../models'; -export interface UserData { - id: number; - login: string; - avatar: string; - admin: boolean; - hash?: string; - created_at: string; - updated_at: string; - edges: { - chat_user: ChatUserData; - }; -} - -interface ChatUserData { - id: string; - created_at: string; - updated_at: string; -} - -interface RateLimitData { - limit: number; - remaining: number; - reset: string; -} - export const mapDataToUser = (data: any): User => { const user: User = camelcaseKeys(data); @@ -79,7 +54,7 @@ export const listUsers = async ( const users = await res .json() - .then((data: UserData[]) => data.map((d) => mapDataToUser(d))); + .then((data: any[]) => data.map((d) => mapDataToUser(d))); return users; }; @@ -99,9 +74,7 @@ export const updateUser = async ( throw new HttpForbiddenError(message); } - const user: User = await res - .json() - .then((data: UserData) => mapDataToUser(data)); + const user: User = await res.json().then((data: any) => mapDataToUser(data)); return user; };