Skip to content

Commit 1735e89

Browse files
committed
Use parsed serverURL to prevent inconsistency
1 parent 80197bb commit 1735e89

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

nodejs/src/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ class API {
6969
const fetch: nodeFetchType = require('fetch-cookie')(nodeFetch, new tough.CookieJar(jar as any))
7070

7171
this._fetch = fetch
72-
this.serverUrl = serverUrl
72+
this.serverUrl = url.parse(serverUrl).href
7373
this.enterprise = enterprise
7474
}
7575

7676
async login(email: string, password: string) {
77-
await this.fetch(`${this.serverUrl}/login`, {
77+
await this.fetch(url.resolve(this.serverUrl, 'login'), {
7878
method: 'post',
7979
body: encodeFormComponent({email, password}),
8080
headers: await this.wrapHeaders({
@@ -84,7 +84,7 @@ class API {
8484
}
8585

8686
async loginLdap(username: string, password: string) {
87-
await this.fetch(`${this.serverUrl}/auth/ldap`, {
87+
await this.fetch(url.resolve(this.serverUrl, 'auth/ldap'), {
8888
method: 'post',
8989
body: encodeFormComponent({username, password}),
9090
headers: await this.wrapHeaders({
@@ -94,7 +94,7 @@ class API {
9494
}
9595

9696
async logout() {
97-
const response = await this.fetch(`${this.serverUrl}/logout`, {
97+
const response = await this.fetch(url.resolve(this.serverUrl, 'logout'), {
9898
method: this.enterprise ? 'POST' : 'GET',
9999
headers: await this.wrapHeaders({
100100
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
@@ -113,12 +113,12 @@ class API {
113113
}
114114

115115
async getMe() {
116-
const response = await this.fetch(`${this.serverUrl}/me`, this.defaultFetchOptions)
116+
const response = await this.fetch(url.resolve(this.serverUrl, 'me'), this.defaultFetchOptions)
117117
return response.json()
118118
}
119119

120120
async getHistory(): Promise<{ history: HistoryItem[] }> {
121-
const response = await this.fetch(`${this.serverUrl}/history`, this.defaultFetchOptions)
121+
const response = await this.fetch(url.resolve(this.serverUrl, 'history'), this.defaultFetchOptions)
122122
return response.json()
123123
}
124124

@@ -127,9 +127,9 @@ class API {
127127
if (this.enterprise) {
128128
let newNoteUrl
129129
if (options?.team) {
130-
newNoteUrl = `${this.serverUrl}/team/${options.team}/new`
130+
newNoteUrl = url.resolve(this.serverUrl, `team/${options.team}/new`)
131131
} else {
132-
newNoteUrl = `${this.serverUrl}/new`
132+
newNoteUrl = url.resolve(this.serverUrl, 'new')
133133
}
134134

135135
response = await this.fetch(newNoteUrl, {
@@ -141,7 +141,7 @@ class API {
141141
})
142142
} else {
143143
const contentType = 'text/markdown;charset=UTF-8'
144-
response = await this.fetch(`${this.serverUrl}/new`, {
144+
response = await this.fetch(url.resolve(this.serverUrl, 'new'), {
145145
method: 'POST',
146146
body,
147147
headers: await this.wrapHeaders({
@@ -161,17 +161,17 @@ class API {
161161
let res: Response
162162
switch (type) {
163163
case ExportType.PDF:
164-
res = await this.fetch(`${this.serverUrl}/${noteId}/pdf`, this.defaultFetchOptions)
164+
res = await this.fetch(url.resolve(this.serverUrl, `${noteId}/pdf`), this.defaultFetchOptions)
165165
break
166166
case ExportType.HTML:
167-
res = await this.fetch(`${this.serverUrl}/s/${noteId}`, this.defaultFetchOptions)
167+
res = await this.fetch(url.resolve(this.serverUrl, `s/${noteId}`), this.defaultFetchOptions)
168168
break
169169
case ExportType.SLIDE:
170-
res = await this.fetch(`${this.serverUrl}/${noteId}/slide`, this.defaultFetchOptions)
170+
res = await this.fetch(url.resolve(this.serverUrl, `${noteId}/slide`), this.defaultFetchOptions)
171171
break
172172
case ExportType.MD:
173173
default:
174-
res = await this.fetch(`${this.serverUrl}/${noteId}/download`, this.defaultFetchOptions)
174+
res = await this.fetch(url.resolve(this.serverUrl, `${noteId}/download`), this.defaultFetchOptions)
175175
}
176176

177177
return res
@@ -233,7 +233,7 @@ class API {
233233
}
234234

235235
private async loadCSRFToken() {
236-
const html = await this.fetch(`${this.serverUrl}`).then(r => r.text())
236+
const html = await this.fetch(this.serverUrl).then(r => r.text())
237237
const $ = cheerio.load(html)
238238

239239
return $('meta[name="csrf-token"]').attr('content') || ''

0 commit comments

Comments
 (0)