|
1 | 1 | import axios, { AxiosInstance, AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
|
2 |
| -import { User, Note, Team, CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, DeleteUserNote, DeleteTeamNote, UpdateUserNote, SingleNote, UpdateTeamNote } from './type' |
| 2 | +import { User, Note, Team, CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type' |
3 | 3 | import * as HackMDErrors from './error'
|
4 | 4 |
|
5 |
| -export default class API { |
| 5 | +export type RequestOptions = { |
| 6 | + unwrapData?: boolean |
| 7 | +} |
| 8 | + |
| 9 | +const defaultOption: RequestOptions = { |
| 10 | + unwrapData: true, |
| 11 | +} |
| 12 | + |
| 13 | +export class API { |
6 | 14 | private axios: AxiosInstance
|
7 | 15 |
|
8 | 16 | constructor (readonly accessToken: string, public hackmdAPIEndpointURL: string = "https://api.hackmd.io/v1") {
|
@@ -53,67 +61,105 @@ export default class API {
|
53 | 61 | )
|
54 | 62 | }
|
55 | 63 |
|
56 |
| - async getMe (): Promise<GetMe> { |
57 |
| - const { data } = await this.axios.get<User>("me") |
58 |
| - return data |
| 64 | + async getMe (option = defaultOption) { |
| 65 | + if (option.unwrapData) { |
| 66 | + return this.axios.get<User>("me").then(response => response.data) as Promise<GetMe> |
| 67 | + } else { |
| 68 | + return this.axios.get<GetMe>("me") |
| 69 | + } |
59 | 70 | }
|
60 | 71 |
|
61 |
| - async getHistory (): Promise<GetUserHistory> { |
62 |
| - const { data } = await this.axios.get<Note[]>("history") |
63 |
| - return data |
| 72 | + async getHistory (options = defaultOption) { |
| 73 | + if (options.unwrapData) { |
| 74 | + return this.axios.get<Note[]>("history").then(response => response.data) as Promise<GetUserHistory> |
| 75 | + } else { |
| 76 | + return this.axios.get<GetUserHistory>("history") |
| 77 | + } |
64 | 78 | }
|
65 | 79 |
|
66 |
| - async getNoteList (): Promise<GetUserNotes> { |
67 |
| - const { data } = await this.axios.get<Note[]>("notes") |
68 |
| - return data |
| 80 | + async getNoteList (options = defaultOption) { |
| 81 | + if (options.unwrapData) { |
| 82 | + return this.axios.get<Note[]>("notes").then(response => response.data) as Promise<GetUserNotes> |
| 83 | + } else { |
| 84 | + return this.axios.get<GetUserNotes>("notes") |
| 85 | + } |
69 | 86 | }
|
70 | 87 |
|
71 |
| - async getNote (noteId: string): Promise<GetUserNote> { |
72 |
| - const { data } = await this.axios.get<SingleNote>(`notes/${noteId}`) |
73 |
| - return data |
| 88 | + async getNote (noteId: string, options = defaultOption) { |
| 89 | + if (options.unwrapData) { |
| 90 | + return this.axios.get<SingleNote>(`notes/${noteId}`).then(response => response.data) as Promise<GetUserNote> |
| 91 | + } else { |
| 92 | + return this.axios.get<GetUserNote>(`notes/${noteId}`) |
| 93 | + } |
74 | 94 | }
|
75 | 95 |
|
76 |
| - async createNote (options: CreateNoteOptions): Promise<CreateUserNote> { |
77 |
| - const { data } = await this.axios.post<SingleNote>("notes", options) |
78 |
| - return data |
| 96 | + async createNote (payload: CreateNoteOptions, options = defaultOption) { |
| 97 | + if (options.unwrapData) { |
| 98 | + return this.axios.post<SingleNote>("notes", payload).then(response => response.data) as Promise<CreateUserNote> |
| 99 | + } else { |
| 100 | + return this.axios.post<CreateUserNote>("notes", payload) |
| 101 | + } |
79 | 102 | }
|
80 | 103 |
|
81 |
| - async updateNoteContent (noteId: string, content?: string): Promise<UpdateUserNote> { |
82 |
| - await this.axios.patch<AxiosResponse>(`notes/${noteId}`, { content }) |
| 104 | + async updateNoteContent (noteId: string, content?: string, options = defaultOption) { |
| 105 | + if (options.unwrapData) { |
| 106 | + return this.axios.patch<SingleNote>(`notes/${noteId}`, { content }).then(response => response.data) |
| 107 | + } else { |
| 108 | + return this.axios.patch<SingleNote>(`notes/${noteId}`, { content }) |
| 109 | + } |
83 | 110 | }
|
84 | 111 |
|
85 |
| - async updateNote (noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse> { |
86 |
| - return await this.axios.patch<AxiosResponse>(`notes/${noteId}`, options) |
| 112 | + async updateNote (noteId: string, payload: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>, options = defaultOption) { |
| 113 | + if (options.unwrapData) { |
| 114 | + return this.axios.patch<SingleNote>(`notes/${noteId}`, payload).then(response => response.data) |
| 115 | + } else { |
| 116 | + return this.axios.patch<SingleNote>(`notes/${noteId}`, payload) |
| 117 | + } |
87 | 118 | }
|
88 | 119 |
|
89 |
| - async deleteNote (noteId: string): Promise<DeleteUserNote> { |
90 |
| - await this.axios.delete<AxiosResponse>(`notes/${noteId}`) |
| 120 | + async deleteNote (noteId: string, options = defaultOption) { |
| 121 | + if (options.unwrapData) { |
| 122 | + return this.axios.delete<SingleNote>(`notes/${noteId}`).then(response => response.data) |
| 123 | + } else { |
| 124 | + return this.axios.delete<SingleNote>(`notes/${noteId}`) |
| 125 | + } |
91 | 126 | }
|
92 | 127 |
|
93 |
| - async getTeams (): Promise<GetUserTeams> { |
94 |
| - const { data } = await this.axios.get<Team[]>("teams") |
95 |
| - return data |
| 128 | + async getTeams (options = defaultOption) { |
| 129 | + if (options.unwrapData) { |
| 130 | + return this.axios.get<Team[]>("teams").then(response => response.data) as Promise<GetUserTeams> |
| 131 | + } else { |
| 132 | + return this.axios.get<GetUserTeams>("teams") |
| 133 | + } |
96 | 134 | }
|
97 | 135 |
|
98 |
| - async getTeamNotes (teamPath: string): Promise<GetTeamNotes> { |
99 |
| - const { data } = await this.axios.get<Note[]>(`teams/${teamPath}/notes`) |
100 |
| - return data |
| 136 | + async getTeamNotes (teamPath: string, options = defaultOption) { |
| 137 | + if (options.unwrapData) { |
| 138 | + return this.axios.get<Note[]>(`teams/${teamPath}/notes`).then(response => response.data) as Promise<GetTeamNotes> |
| 139 | + } else { |
| 140 | + return this.axios.get<GetTeamNotes>(`teams/${teamPath}/notes`) |
| 141 | + } |
101 | 142 | }
|
102 | 143 |
|
103 |
| - async createTeamNote (teamPath: string, options: CreateNoteOptions): Promise<CreateTeamNote> { |
104 |
| - const { data } = await this.axios.post<SingleNote>(`teams/${teamPath}/notes`, options) |
105 |
| - return data |
| 144 | + async createTeamNote (teamPath: string, payload: CreateNoteOptions, options = defaultOption) { |
| 145 | + if (options.unwrapData) { |
| 146 | + return this.axios.post<SingleNote>(`teams/${teamPath}/notes`, payload).then(response => response.data) as Promise<CreateTeamNote> |
| 147 | + } else { |
| 148 | + return this.axios.post<CreateTeamNote>(`teams/${teamPath}/notes`, payload) |
| 149 | + } |
106 | 150 | }
|
107 | 151 |
|
108 |
| - async updateTeamNoteContent (teamPath: string, noteId: string, content?: string): Promise<UpdateTeamNote> { |
109 |
| - await this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, { content }) |
| 152 | + async updateTeamNoteContent (teamPath: string, noteId: string, content?: string): Promise<AxiosResponse> { |
| 153 | + return this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, { content }) |
110 | 154 | }
|
111 | 155 |
|
112 | 156 | async updateTeamNote (teamPath: string, noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse> {
|
113 |
| - return await this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, options) |
| 157 | + return this.axios.patch<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`, options) |
114 | 158 | }
|
115 | 159 |
|
116 |
| - async deleteTeamNote (teamPath: string, noteId: string): Promise<DeleteTeamNote> { |
117 |
| - await this.axios.delete<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`) |
| 160 | + async deleteTeamNote (teamPath: string, noteId: string): Promise<AxiosResponse> { |
| 161 | + return this.axios.delete<AxiosResponse>(`teams/${teamPath}/notes/${noteId}`) |
118 | 162 | }
|
119 | 163 | }
|
| 164 | + |
| 165 | +export default API |
0 commit comments