Skip to content

Commit bc5542a

Browse files
committed
Specify user agent in reqeust
fix #3
1 parent fdfcfb3 commit bc5542a

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

nodejs/src/index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import * as url from 'url'
99

1010
import { defaults } from './utils'
1111

12+
let version = ''
13+
try {
14+
version = require('../package.json').version
15+
} catch (err) {}
16+
1217
const defaultCookiePath = path.join(homedir(), '.hackmd', 'cookies.json')
1318

1419
const defaultConfig = {
@@ -107,12 +112,12 @@ class API {
107112
}
108113

109114
async getMe() {
110-
const response = await this.fetch(`${this.serverUrl}/me`)
115+
const response = await this.fetch(`${this.serverUrl}/me`, this.defaultFetchOptions)
111116
return response.json()
112117
}
113118

114119
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)
116121
return response.json()
117122
}
118123

@@ -131,9 +136,9 @@ class API {
131136
response = await this.fetch(`${this.serverUrl}/new`, {
132137
method: 'POST',
133138
body,
134-
headers: {
139+
headers: await this.wrapHeaders({
135140
'Content-Type': contentType
136-
}
141+
})
137142
})
138143
}
139144

@@ -148,17 +153,17 @@ class API {
148153
let res: Response
149154
switch (type) {
150155
case ExportType.PDF:
151-
res = await this.fetch(`${this.serverUrl}/${noteId}/pdf`)
156+
res = await this.fetch(`${this.serverUrl}/${noteId}/pdf`, this.defaultFetchOptions)
152157
break
153158
case ExportType.HTML:
154-
res = await this.fetch(`${this.serverUrl}/s/${noteId}`)
159+
res = await this.fetch(`${this.serverUrl}/s/${noteId}`, this.defaultFetchOptions)
155160
break
156161
case ExportType.SLIDE:
157-
res = await this.fetch(`${this.serverUrl}/${noteId}/slide`)
162+
res = await this.fetch(`${this.serverUrl}/${noteId}/slide`, this.defaultFetchOptions)
158163
break
159164
case ExportType.MD:
160165
default:
161-
res = await this.fetch(`${this.serverUrl}/${noteId}/download`)
166+
res = await this.fetch(`${this.serverUrl}/${noteId}/download`, this.defaultFetchOptions)
162167
}
163168

164169
return res
@@ -184,15 +189,27 @@ class API {
184189
return url.parse(this.serverUrl).host
185190
}
186191

192+
get defaultFetchOptions () {
193+
return {
194+
headers: {
195+
'User-Agent': `HackMD API Client ${version} Node.js`
196+
}
197+
}
198+
}
199+
187200
private async wrapHeaders(headers: any) {
188201
if (this.enterprise) {
189202
const csrf = await this.loadCSRFToken()
190203
return {
191204
...headers,
205+
'User-Agent': `HackMD API Client ${version} Node.js`,
192206
'X-XSRF-Token': csrf
193207
}
194208
} else {
195-
return headers
209+
return {
210+
...headers,
211+
'User-Agent': `HackMD API Client ${version} Node.js`
212+
}
196213
}
197214
}
198215

nodejs/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"rootDir": "src",
88
"strict": true,
99
"target": "es2017",
10-
"esModuleInterop": true
10+
"esModuleInterop": true,
11+
"resolveJsonModule": true
1112
},
1213
"include": [
1314
"src/**/*"

0 commit comments

Comments
 (0)