Skip to content

Commit

Permalink
feat(front-end): add project response type
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 14, 2023
1 parent 3a6713a commit 64a7614
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
5 changes: 3 additions & 2 deletions apps/frontend/src/api/projects.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { http } from './http'
import type { ProjectResponse } from './types'

export function fetchAllProjects() {
return http.get('/projects')
return http.get<ProjectResponse[], ProjectResponse[]>('/projects')
}

export function fetchCreateProject(name: string) {
return http.post('/projects', {
return http.post<ProjectResponse, ProjectResponse>('/projects', {
name,
})
}
7 changes: 7 additions & 0 deletions apps/frontend/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ export interface TaskResponse {
created_at: string
updated_at: string
}

export interface ProjectResponse {
created_at: string
name: string
updated_at: string
_id: string
}
10 changes: 5 additions & 5 deletions apps/frontend/src/store/listProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TaskStatus } from './tasks'
import { TasksSelectorType } from './tasksSelector'
import { fetchAllProjects, fetchAllTasks, fetchCreateProject } from '@/api'
import { useTasksSelectorStore } from '@/store'
import type { ProjectResponse } from '@/api/types'

export interface ListProject {
id: string
Expand All @@ -16,8 +17,8 @@ export const useListProjectsStore = defineStore('newProjects', () => {
const projects = ref<ListProject[]>([])

async function init() {
const rawProjects: any = await fetchAllProjects()
projects.value = rawProjects.map(normalizeProject)
const rawProjects = await fetchAllProjects()
projects.value = rawProjects.map(mapProjectResponseToProject)

if (projects.value.length > 0)
tasksSelectorStore.setCurrentSelector(projects.value[0])
Expand Down Expand Up @@ -48,7 +49,7 @@ export const useListProjectsStore = defineStore('newProjects', () => {
return

const rawProject = await fetchCreateProject(name)
const newProject = normalizeProject(rawProject)
const newProject = mapProjectResponseToProject(rawProject)
projects.value.push(newProject)

selectProject(newProject)
Expand All @@ -70,8 +71,7 @@ export const useListProjectsStore = defineStore('newProjects', () => {
}
})

// TODO 需要提供后端返回的 project 的 type shape
function normalizeProject(rawProject: any): ListProject {
function mapProjectResponseToProject(rawProject: ProjectResponse): ListProject {
return {
id: `${rawProject._id}`,
name: rawProject.name,
Expand Down

0 comments on commit 64a7614

Please sign in to comment.