Skip to content

Commit ef893c2

Browse files
author
james_tsai
committed
feat: add list, create, update, delete notes and history APIs
1 parent a4fdf54 commit ef893c2

2 files changed

Lines changed: 89 additions & 13 deletions

File tree

nodejs/src/index.ts

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import axios, { AxiosInstance, AxiosRequestConfig, AxiosError } from 'axios'
2-
import { User } from './type'
2+
import { User, Note, Team, CreateNoteOptions, NotePermissionRole, CommentPermissionType } from './type'
33

44
export class API {
55
public axios: AxiosInstance
66
constructor(accessToken: string) {
77
this.axios = axios.create({
8-
baseURL: "http://localhost:3000/v1/api",
8+
baseURL: "http://localhost:3000/v1",
99
headers:{
10-
"Content-Type": "application.json",
10+
"Content-Type": "application/json",
1111
}
1212
})
13+
1314
this.axios.interceptors.request.use(
1415
(config: AxiosRequestConfig) =>{
15-
if (!config.headers) {
16-
config.headers = {};
17-
}
18-
1916
if (accessToken) {
20-
config.headers.Authorization = `Bearer ${accessToken}`;
17+
config.headers!.Authorization = `Bearer ${accessToken}`;
2118
}
22-
2319
return config
2420
},
2521
(err: AxiosError) => {
@@ -29,13 +25,76 @@ export class API {
2925
}
3026

3127
getMe = async () => {
32-
const { data } = await this.axios.get<User>("/me")
28+
const { data } = await this.axios.get<User>("me")
29+
return data
30+
}
31+
32+
getHistory = async () => {
33+
const { data } = await this.axios.get<Note[]>("history")
34+
return data
35+
}
36+
37+
getNoteList = async () => {
38+
const { data } = await this.axios.get<Note[]>("notes")
39+
return data
40+
}
41+
42+
getNote = async (noteId: string) => {
43+
try {
44+
const { data } = await this.axios.get<Note>(`notes/${noteId}`)
45+
return data
46+
} catch (e) {
47+
console.log(e)
48+
}
49+
}
50+
51+
createNote = async (options: CreateNoteOptions) => {
52+
const { data } = await this.axios.post<Note>("notes", options)
53+
return data
54+
}
55+
56+
updateNoteContent =async (noteId: string, content?: string) => {
57+
const { data } = await this.axios.patch<string>(`notes/${noteId}`, { content })
3358
return data
3459
}
35-
}
3660

37-
const api = new API("60WUTLO5DIHIOHCIY9TO0QEDVATU55XBTE7JJJBX9UTG49M7Y1")
38-
api.getMe()
61+
deleteNote = async (noteId: string) => {
62+
try {
63+
const { data } = await this.axios.delete<void>(`notes/${noteId}`)
64+
return data
65+
} catch(e) {
66+
}
67+
}
68+
69+
getTeams = async () => {
70+
const { data } = await this.axios.get<Team[]>("teams")
71+
return data
72+
}
73+
74+
getTeamNotes = async (teamPath: string) => {
75+
const {data} = await this.axios.get<Note[]>(`teams/${teamPath}/notes`)
76+
return data
77+
}
78+
79+
createTeamNote = async (teamPath: string, options: CreateNoteOptions) => {
80+
const { data } = await this.axios.post<Note>(`teams/${teamPath}/notes`, options)
81+
return data
82+
}
83+
84+
updateTeamNoteContent =async (teamPath: string, noteId: string, content?: string) => {
85+
const { data } = await this.axios.patch<string>(`notes/${noteId}`, { content })
86+
return data
87+
}
88+
89+
deleteTeamNote = async (teamPath: string, noteId: string) => {
90+
try {
91+
const { data } = await this.axios.delete<void>(`teams/${teamPath}/notes/${noteId}`)
92+
console.log(data)
93+
return data
94+
} catch(e) {
95+
}
96+
}
97+
}
3998

4099

41100

nodejs/src/type.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ export enum NotePublishType {
1010
BOOK = 'book'
1111
}
1212

13+
export enum CommentPermissionType {
14+
DISABLED = 'disabled',
15+
FORBIDDEN = 'forbidden',
16+
OWNERS = 'owners',
17+
SIGNED_IN_USERS = 'signed_in_users',
18+
EVERYONE = 'everyone'
19+
}
20+
21+
export type CreateNoteOptions = {
22+
title?: string
23+
content?: string
24+
readPermission?: NotePermissionRole,
25+
writePermission?: NotePermissionRole,
26+
commentPermission?: CommentPermissionType
27+
}
28+
29+
1330
export interface Team {
1431
id: string
1532
ownerId: string

0 commit comments

Comments
 (0)