@@ -9,6 +9,11 @@ import * as url from 'url'
9
9
10
10
import { defaults } from './utils'
11
11
12
+ let version = ''
13
+ try {
14
+ version = require ( '../package.json' ) . version
15
+ } catch ( err ) { }
16
+
12
17
const defaultCookiePath = path . join ( homedir ( ) , '.hackmd' , 'cookies.json' )
13
18
14
19
const defaultConfig = {
@@ -107,12 +112,12 @@ class API {
107
112
}
108
113
109
114
async getMe ( ) {
110
- const response = await this . fetch ( `${ this . serverUrl } /me` )
115
+ const response = await this . fetch ( `${ this . serverUrl } /me` , this . defaultFetchOptions )
111
116
return response . json ( )
112
117
}
113
118
114
119
async getHistory ( ) : Promise < { history : HistoryItem [ ] } > {
115
- const response = await this . fetch ( `${ this . serverUrl } /history` )
120
+ const response = await this . fetch ( `${ this . serverUrl } /history` , this . defaultFetchOptions )
116
121
return response . json ( )
117
122
}
118
123
@@ -131,9 +136,9 @@ class API {
131
136
response = await this . fetch ( `${ this . serverUrl } /new` , {
132
137
method : 'POST' ,
133
138
body,
134
- headers : {
139
+ headers : await this . wrapHeaders ( {
135
140
'Content-Type' : contentType
136
- }
141
+ } )
137
142
} )
138
143
}
139
144
@@ -148,17 +153,17 @@ class API {
148
153
let res : Response
149
154
switch ( type ) {
150
155
case ExportType . PDF :
151
- res = await this . fetch ( `${ this . serverUrl } /${ noteId } /pdf` )
156
+ res = await this . fetch ( `${ this . serverUrl } /${ noteId } /pdf` , this . defaultFetchOptions )
152
157
break
153
158
case ExportType . HTML :
154
- res = await this . fetch ( `${ this . serverUrl } /s/${ noteId } ` )
159
+ res = await this . fetch ( `${ this . serverUrl } /s/${ noteId } ` , this . defaultFetchOptions )
155
160
break
156
161
case ExportType . SLIDE :
157
- res = await this . fetch ( `${ this . serverUrl } /${ noteId } /slide` )
162
+ res = await this . fetch ( `${ this . serverUrl } /${ noteId } /slide` , this . defaultFetchOptions )
158
163
break
159
164
case ExportType . MD :
160
165
default :
161
- res = await this . fetch ( `${ this . serverUrl } /${ noteId } /download` )
166
+ res = await this . fetch ( `${ this . serverUrl } /${ noteId } /download` , this . defaultFetchOptions )
162
167
}
163
168
164
169
return res
@@ -184,15 +189,27 @@ class API {
184
189
return url . parse ( this . serverUrl ) . host
185
190
}
186
191
192
+ get defaultFetchOptions ( ) {
193
+ return {
194
+ headers : {
195
+ 'User-Agent' : `HackMD API Client ${ version } Node.js`
196
+ }
197
+ }
198
+ }
199
+
187
200
private async wrapHeaders ( headers : any ) {
188
201
if ( this . enterprise ) {
189
202
const csrf = await this . loadCSRFToken ( )
190
203
return {
191
204
...headers ,
205
+ 'User-Agent' : `HackMD API Client ${ version } Node.js` ,
192
206
'X-XSRF-Token' : csrf
193
207
}
194
208
} else {
195
- return headers
209
+ return {
210
+ ...headers ,
211
+ 'User-Agent' : `HackMD API Client ${ version } Node.js`
212
+ }
196
213
}
197
214
}
198
215
0 commit comments