1+ import promise from "es6-promise"
2+ import "whatwg-fetch"
3+
4+ export let getList = async ( page , tag ) => {
5+ let response = await fetch ( `https://cnodejs.org/api/v1/topics?page=${ page } &limit=20&tab=${ tag } ` , {
6+ mode : "cors"
7+ } ) . catch ( ( error ) => {
8+ console . log ( error )
9+ } )
10+
11+ return await response . json ( ) . catch ( ( error ) => {
12+ console . log ( error )
13+ } )
14+ }
15+
16+ export let getTopic = async ( topicId ) => {
17+ let response = await fetch ( `https://cnodejs.org/api/v1/topic/${ topicId } ` , {
18+ mode : "cors"
19+ } ) . catch ( ( error ) => {
20+ console . log ( error )
21+ } )
22+
23+ return await response . json ( ) . catch ( ( error ) => {
24+ console . log ( error )
25+ } )
26+ }
27+
28+ export let login = async ( token ) => {
29+ let response = await fetch ( `https://cnodejs.org/api/v1/accesstoken ` , {
30+ method : "POST" ,
31+ mode : "cors" ,
32+ headers : {
33+ "Content-Type" : "application/x-www-form-urlencoded"
34+ } ,
35+ body : `accesstoken=${ token } `
36+ } ) . catch ( ( error ) => {
37+ console . log ( error )
38+ } )
39+
40+ return await response . json ( ) . catch ( ( error ) => {
41+ console . log ( error )
42+ } )
43+ }
44+
45+ export let like = async ( id , token ) => {
46+ let response = await fetch ( `https://cnodejs.org/api/v1/reply/${ id } /ups` , {
47+ method : "POST" ,
48+ mode : "cors" ,
49+ headers : {
50+ "Content-Type" : "application/x-www-form-urlencoded"
51+ } ,
52+ body : `accesstoken=${ token } `
53+ } ) . catch ( ( error ) => {
54+ console . log ( error )
55+ } )
56+
57+ return await response . json ( ) . catch ( ( error ) => {
58+ console . log ( error )
59+ } )
60+ }
61+
62+ export let reply = async ( token , topicId , content , replyId ) => {
63+ let body = replyId ? `accesstoken=${ token } &content=${ content } &reply_id=${ replyId } ` : `accesstoken=${ token } &content=${ content } `
64+
65+ let response = await fetch ( `https://cnodejs.org/api/v1/topic/${ topicId } /replies` , {
66+ method : "POST" ,
67+ mode : "cors" ,
68+ headers : {
69+ "Content-Type" : "application/x-www-form-urlencoded"
70+ } ,
71+ body : body
72+ } ) . catch ( ( error ) => {
73+ console . log ( error )
74+ } )
75+
76+ return await response . json ( ) . catch ( ( error ) => {
77+ console . log ( error )
78+ } )
79+ }
80+
81+ export let getProfile = async ( nickname ) => {
82+ let response = await fetch ( `https://cnodejs.org/api/v1/user/${ nickname } ` , {
83+ mode : "cors"
84+ } ) . catch ( ( error ) => {
85+ console . log ( error )
86+ } )
87+
88+ return await response . json ( ) . catch ( ( error ) => {
89+ console . log ( error )
90+ } )
91+ }
92+
93+ export let getMessages = async ( token ) => {
94+ let response = await fetch ( `https://cnodejs.org/api/v1/messages?accesstoken=${ token } ` , {
95+ mode : "cors"
96+ } ) . catch ( ( error ) => {
97+ console . log ( error )
98+ } )
99+
100+ return await response . json ( ) . catch ( ( error ) => {
101+ console . log ( error )
102+ } )
103+ }
104+
105+ export let getMessageCount = async ( token ) => {
106+ let response = await fetch ( `https://cnodejs.org/api/v1/message/count?accesstoken=${ token } ` , {
107+ mode : "cors"
108+ } ) . catch ( ( error ) => {
109+ console . log ( error )
110+ } )
111+
112+ return await response . json ( ) . catch ( ( error ) => {
113+ console . log ( error )
114+ } )
115+ }
116+
117+ export let post = async ( { token, title, tab, content} ) => {
118+ let response = await fetch ( "https://cnodejs.org/api/v1/topics" , {
119+ method : "POST" ,
120+ headers : {
121+ "Content-Type" : "application/x-www-form-urlencoded"
122+ } ,
123+ mode : "cors" ,
124+ body : `accesstoken=${ token } &title=${ title } &tab=${ tab } &content=${ content } `
125+ } ) . catch ( ( error ) => {
126+ console . log ( error )
127+ } )
128+
129+ return await response . json ( ) . catch ( ( error ) => {
130+ console . log ( error )
131+ } )
132+ }
0 commit comments