Skip to content

Commit f6ed726

Browse files
James TsaiJames Tsai
authored andcommitted
refactor: change interface to type style and add typing on api function
1 parent dccab72 commit f6ed726

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

nodejs/src/index.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
2-
import { User, Note, Team, CreateNoteOptions } from './type'
2+
import { User, Note, Team, CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, DeleteUserNote, DeleteTeamNote, UpdateUserNote, SingleNote, UpdateTeamNote } from './type'
33
import * as HackMDErrors from './error'
44

55
export default class API {
@@ -53,63 +53,59 @@ export default class API {
5353
)
5454
}
5555

56-
async getMe () {
56+
async getMe (): Promise<GetMe> {
5757
const { data } = await this.axios.get<User>("me")
5858
return data
5959
}
6060

61-
async getHistory () {
61+
async getHistory (): Promise<GetUserHistory> {
6262
const { data } = await this.axios.get<Note[]>("history")
6363
return data
6464
}
6565

66-
async getNoteList () {
66+
async getNoteList (): Promise<GetUserNotes> {
6767
const { data } = await this.axios.get<Note[]>("notes")
6868
return data
6969
}
7070

71-
async getNote (noteId: string) {
72-
const { data } = await this.axios.get<Note>(`notes/${noteId}`)
71+
async getNote (noteId: string): Promise<GetUserNote> {
72+
const { data } = await this.axios.get<SingleNote>(`notes/${noteId}`)
7373
return data
7474
}
7575

76-
async createNote (options: CreateNoteOptions) {
77-
const { data } = await this.axios.post<Note>("notes", options)
76+
async createNote (options: CreateNoteOptions): Promise<CreateUserNote> {
77+
const { data } = await this.axios.post<SingleNote>("notes", options)
7878
return data
7979
}
8080

81-
async updateNoteContent (noteId: string, content?: string) {
82-
const { data } = await this.axios.patch<string>(`notes/${noteId}`, { content })
83-
return data
81+
async updateNoteContent (noteId: string, content?: string): Promise<UpdateUserNote> {
82+
await this.axios.patch<AxiosResponse>(`notes/${noteId}`, { content })
8483
}
8584

86-
async deleteNote (noteId: string) {
87-
const { data } = await this.axios.delete<void>(`notes/${noteId}`)
88-
return data
85+
async deleteNote (noteId: string): Promise<DeleteUserNote> {
86+
await this.axios.delete<AxiosResponse>(`notes/${noteId}`)
8987
}
9088

91-
async getTeams () {
89+
async getTeams (): Promise<GetUserTeams> {
9290
const { data } = await this.axios.get<Team[]>("teams")
9391
return data
9492
}
9593

96-
async getTeamNotes (teamPath: string) {
97-
const {data} = await this.axios.get<Note[]>(`teams/${teamPath}/notes`)
94+
async getTeamNotes (teamPath: string): Promise<GetTeamNotes> {
95+
const { data } = await this.axios.get<Note[]>(`teams/${teamPath}/notes`)
9896
return data
9997
}
10098

101-
async createTeamNote (teamPath: string, options: CreateNoteOptions) {
102-
const { data } = await this.axios.post<Note>(`teams/${teamPath}/notes`, options)
99+
async createTeamNote (teamPath: string, options: CreateNoteOptions): Promise<CreateTeamNote> {
100+
const { data } = await this.axios.post<SingleNote>(`teams/${teamPath}/notes`, options)
103101
return data
104102
}
105103

106-
async updateTeamNoteContent (teamPath: string, noteId: string, content?: string) {
107-
const { data } = await this.axios.patch<string>(`teams/${teamPath}/notes/${noteId}`, { content })
108-
return data
104+
async updateTeamNoteContent (teamPath: string, noteId: string, content?: string): Promise<UpdateTeamNote> {
105+
await this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, { content })
109106
}
110107

111-
async deleteTeamNote (teamPath: string, noteId: string) {
112-
const { data } = await this.axios.delete<void>(`teams/${teamPath}/notes/${noteId}`)
113-
return data
108+
async deleteTeamNote (teamPath: string, noteId: string): Promise<DeleteTeamNote> {
109+
await this.axios.delete<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`)
114110
}
115111
}

nodejs/src/type.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export type CreateNoteOptions = {
2626
commentPermission?: CommentPermissionType
2727
}
2828

29-
export interface Team {
29+
export type Team = {
3030
id: string
3131
ownerId: string
3232
name: string
@@ -38,7 +38,7 @@ export interface Team {
3838
createdAt: Date
3939
}
4040

41-
export interface User {
41+
export type User = {
4242
id: string
4343
email: string | null
4444
name: string
@@ -47,7 +47,7 @@ export interface User {
4747
teams: Team[]
4848
}
4949

50-
export interface SimpleUserProfile {
50+
export type SimpleUserProfile = {
5151
name: string,
5252
userPath: string
5353
photo: string
@@ -61,10 +61,9 @@ export enum NotePermissionRole {
6161
GUEST = 'guest'
6262
}
6363

64-
export interface Note {
64+
export type Note = {
6565
id: string
6666
title: string
67-
content: string
6867
tags: string[]
6968
lastChangedAt: string
7069
createdAt: string
@@ -78,30 +77,31 @@ export interface Note {
7877

7978
readPermission: NotePermissionRole
8079
writePermission: NotePermissionRole
80+
8181
}
8282

83-
export interface SingleNote extends Note {
83+
export type SingleNote = Note & {
8484
content: string
8585
}
8686

87+
// User
8788
export type GetMe = User
8889

90+
// User notes
8991
export type GetUserNotes = Note[]
9092
export type GetUserNote = SingleNote
91-
export type CreateUserNote = SingleNote
92-
export type UpdateUserNote = SingleNote
93-
94-
// !Use status code to indicate wether the request is successful or not
95-
// export type DeleteUserNote = Note
96-
9793
export type GetUserHistory = Note[]
94+
export type CreateUserNote = SingleNote
95+
export type UpdateUserNote = void
96+
export type DeleteUserNote = void
9897

98+
// Teams
9999
export type GetUserTeams = Team[]
100100

101+
// Team notes
101102
export type GetTeamNotes = Note[]
102103
export type CreateTeamNote = SingleNote
103-
export type UpdateTeamNote = SingleNote
104+
export type UpdateTeamNote = void
105+
export type DeleteTeamNote = void
104106

105107

106-
// !Use status code to indicate wether the request is successful or not
107-
// export type DeleteTeamNote = Note

0 commit comments

Comments
 (0)