@@ -9,7 +9,10 @@ const appInfo = require('../appInfo')
99const ExternalOAuth2 = require ( '../oauth/ExternalOAuth2' ) ;
1010const OAuthRequestManager = require ( '../oauth/OAuthRequestManager' ) ;
1111
12- const errorHandler = ( res ) => {
12+ const BASE_URL = 'https://www.tistory.com/apis'
13+ const PROVIDER_ID = 'tistory'
14+
15+ function _errorHandler ( res ) {
1316 if ( ! res . ok ) {
1417 console . error ( "fetch failed" , res )
1518 res . text ( ) . then ( text => console . error ( "fetch body" , text ) )
@@ -19,10 +22,44 @@ const errorHandler = (res) => {
1922 return res . json ( )
2023}
2124
22- const BASE_URL = 'https://www.tistory.com/apis'
23- const PROVIDER_ID = 'tistory'
25+ function _uploadFile ( accessToken , blogName , fileBlob , fileOption ) {
26+ let formdata = new FormData ( ) ;
27+ formdata . append ( "access_token" , accessToken )
28+ formdata . append ( "output" , "json" )
29+ formdata . append ( "blogName" , blogName )
30+ formdata . append ( "uploadedfile" , fileBlob , fileOption )
31+
32+ return fetch ( BASE_URL + "/post/attach" , {
33+ method : 'post' ,
34+ body : formdata
35+ } )
36+ . then ( _errorHandler )
37+ . then ( res => res . tistory . url )
38+ }
2439
25- const requestAuth = ( successHandler , failureHandler ) => {
40+
41+ function _tistoryPostToEditorPost ( post ) {
42+ return {
43+ id : post . id ,
44+ url : post . postUrl ,
45+ title : post . title ,
46+ date : post . date ,
47+ categoryId : post . categoryId ,
48+ state : post . visibility > 0 ? 'published' : 'draft' ,
49+ tags : post . tags && post . tags . tag ? [ ] . concat ( post . tags . tag ) : [ ] ,
50+ content : post . content ? post . content : ''
51+ }
52+ }
53+
54+ function _tistoryPostsToEditorPosts ( tistoryPosts ) {
55+ let posts = tistoryPosts ? [ ] . concat ( tistoryPosts ) : [ ]
56+ return posts . map ( _tistoryPostToEditorPost )
57+ }
58+
59+
60+
61+
62+ function requestAuth ( successHandler , failureHandler ) {
2663 const oauthInfoReader = new OauthInfoReader ( )
2764 const oauth2 = new ExternalOAuth2 ( oauthInfoReader . getTistory ( ) )
2865 OAuthRequestManager . saveRequestInfo ( "oauth" , ( searchParams ) => {
@@ -46,7 +83,7 @@ const requestAuth = (successHandler, failureHandler) => {
4683 oauth2 . requestAuth ( { } )
4784}
4885
49- const fetchBlogInfo = ( auth ) => {
86+ function fetchBlogInfo ( auth ) {
5087 return fetch ( BASE_URL + "/blog/info?" + querystring . stringify ( {
5188 access_token : auth . access_token ,
5289 output : "json"
@@ -55,10 +92,10 @@ const fetchBlogInfo = (auth) => {
5592 'User-Agent' : appInfo . userAgent
5693 }
5794 } )
58- . then ( errorHandler )
95+ . then ( _errorHandler )
5996}
6097
61- const fetchUser = ( auth ) => {
98+ function fetchUser ( auth ) {
6299 return fetch ( BASE_URL + "/user?" + querystring . stringify ( {
63100 access_token : auth . access_token ,
64101 output : "json"
@@ -67,26 +104,9 @@ const fetchUser = (auth) => {
67104 'User-Agent' : appInfo . userAgent
68105 }
69106 } )
70- . then ( errorHandler )
107+ . then ( _errorHandler )
71108}
72109
73- function _tistoryPostToEditorPost ( post ) {
74- return {
75- id : post . id ,
76- url : post . postUrl ,
77- title : post . title ,
78- date : post . date ,
79- categoryId : post . categoryId ,
80- state : post . visibility > 0 ? 'published' : 'draft' ,
81- tags : post . tags && post . tags . tag ? [ ] . concat ( post . tags . tag ) : [ ] ,
82- content : post . content ? post . content : ''
83- }
84- }
85-
86- function _tistoryPostsToEditorPosts ( tistoryPosts ) {
87- let posts = tistoryPosts ? [ ] . concat ( tistoryPosts ) : [ ]
88- return posts . map ( _tistoryPostToEditorPost )
89- }
90110
91111const fetchPosts = ( auth , blogName , options ) => {
92112 return fetch ( BASE_URL + "/post/list?" + querystring . stringify ( {
@@ -100,15 +120,15 @@ const fetchPosts = (auth, blogName, options) => {
100120 'User-Agent' : appInfo . userAgent
101121 }
102122 } )
103- . then ( errorHandler )
123+ . then ( _errorHandler )
104124 . then ( res => ( {
105125 page : res . tistory . item . page ,
106126 posts : _tistoryPostsToEditorPosts ( res . tistory . item . posts ) ,
107127 hasNext : res . tistory . item . totalCount > res . tistory . item . page * res . tistory . item . count
108128 } ) )
109129}
110130
111- const fetchPost = ( auth , blogName , postId ) => {
131+ function fetchPost ( auth , blogName , postId ) {
112132 return fetch ( BASE_URL + "/post/read?" + querystring . stringify ( {
113133 access_token : auth . access_token ,
114134 output : "json" ,
@@ -119,13 +139,13 @@ const fetchPost = (auth, blogName, postId) => {
119139 'User-Agent' : appInfo . userAgent
120140 }
121141 } )
122- . then ( errorHandler )
142+ . then ( _errorHandler )
123143 . then ( res => ( {
124144 post : _tistoryPostToEditorPost ( res . tistory . item )
125145 } ) )
126146}
127147
128- const fetchCategories = ( auth , blogName ) => {
148+ function fetchCategories ( auth , blogName ) {
129149 return fetch ( BASE_URL + "/category/list?" + querystring . stringify ( {
130150 access_token : auth . access_token ,
131151 output : "json" ,
@@ -135,11 +155,11 @@ const fetchCategories = (auth, blogName) => {
135155 'User-Agent' : appInfo . userAgent
136156 }
137157 } )
138- . then ( errorHandler )
158+ . then ( _errorHandler )
139159 . then ( res => res . tistory . item . categories )
140160}
141161
142- const savePost = ( auth , blogName , post ) => {
162+ function savePost ( auth , blogName , post ) {
143163 let formdata = makePostFormData ( auth , blogName , post )
144164 formdata . append ( "postId" , post . id )
145165
@@ -151,11 +171,11 @@ const savePost = (auth, blogName, post) => {
151171 'User-Agent' : appInfo . userAgent
152172 }
153173 } )
154- . then ( errorHandler )
174+ . then ( _errorHandler )
155175 . then ( res => fetchPost ( auth , blogName , res . tistory . postId ) )
156176}
157177
158- const addPost = ( auth , blogName , post ) => {
178+ function addPost ( auth , blogName , post ) {
159179 let formdata = makePostFormData ( auth , blogName , post )
160180
161181 return fetch ( BASE_URL + "/post/write" , {
@@ -166,11 +186,11 @@ const addPost = (auth, blogName, post) => {
166186 'User-Agent' : appInfo . userAgent
167187 }
168188 } )
169- . then ( errorHandler )
189+ . then ( _errorHandler )
170190 . then ( res => fetchPost ( auth , blogName , res . tistory . postId ) )
171191}
172192
173- const makePostFormData = ( auth , blogName , post ) => {
193+ function makePostFormData ( auth , blogName , post ) {
174194 let formdata = new FormData ( )
175195 formdata . append ( "access_token" , auth . access_token )
176196 formdata . append ( "output" , "json" )
@@ -189,19 +209,19 @@ const makePostFormData = (auth, blogName, post) => {
189209 return formdata
190210}
191211
192- const uploadFile = ( auth , blogName , filepath ) => {
212+ function uploadFile ( auth , blogName , filepath ) {
193213 console . log ( "uploadFile" , blogName , filepath )
194214 return _uploadFile ( auth . access_token , blogName , fs . createReadStream ( filepath ) )
195215}
196216
197- const uploadFileWithBuffer = ( auth , blogName , buffer , options ) => {
217+ function uploadFileWithBuffer ( auth , blogName , buffer , options ) {
198218 console . log ( "uploadFileWithClipboard" , blogName )
199219 var imageStream = new stream . PassThrough ( )
200220 imageStream . end ( buffer )
201221 return _uploadFile ( auth . access_token , blogName , imageStream , options )
202222}
203223
204- const uploadFileWithBlob = ( auth , blogName , blob , options ) => {
224+ function uploadFileWithBlob ( auth , blogName , blob , options ) {
205225 console . log ( "uploadFileWithBlob" , blogName )
206226 var imageStream = new stream . PassThrough ( )
207227 imageStream . end ( blob . buffer )
@@ -212,24 +232,12 @@ const uploadFileWithBlob = (auth, blogName, blob, options) => {
212232 } )
213233}
214234
215- const _uploadFile = ( accessToken , blogName , fileBlob , fileOption ) => {
216- let formdata = new FormData ( ) ;
217- formdata . append ( "access_token" , accessToken )
218- formdata . append ( "output" , "json" )
219- formdata . append ( "blogName" , blogName )
220- formdata . append ( "uploadedfile" , fileBlob , fileOption )
221235
222- return fetch ( BASE_URL + "/post/attach" , {
223- method : 'post' ,
224- body : formdata
225- } )
226- . then ( errorHandler )
227- . then ( res => res . tistory . url )
236+ function validateAuthInfo ( auth ) {
237+ return auth && auth . access_token
228238}
229239
230- const validateAuthInfo = ( auth ) => auth && auth . access_token
231-
232- const fetchAccount = async ( auth ) => {
240+ async function fetchAccount ( auth ) {
233241 let user = {
234242 name : "tistory" ,
235243 image : null
0 commit comments