Skip to content

Commit a0aa01e

Browse files
committed
Fix double slash error and refactor api routes
1 parent ae4807d commit a0aa01e

File tree

3 files changed

+239
-58
lines changed

3 files changed

+239
-58
lines changed

src/http-requests.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ class HttpRequests {
1717
}
1818
this.url = config.host
1919
}
20+
static constructCorrectPath(pathname: string, url: string): string {
21+
let slash = '/'
22+
if (pathname.endsWith('/') || url.startsWith('/')) {
23+
slash = ''
24+
}
25+
if (pathname.endsWith('//')) {
26+
pathname = pathname.substring(1)
27+
}
28+
if (url.endsWith('/')) {
29+
url = url.slice(0, -1)
30+
}
31+
if (pathname.endsWith('/') && url.startsWith('/')) {
32+
url = url.substring(1)
33+
}
34+
return pathname + slash + url
35+
}
2036

2137
async request({
2238
method,
@@ -33,16 +49,17 @@ class HttpRequests {
3349
}) {
3450
try {
3551
const constructURL = new URL(this.url)
36-
constructURL.pathname = constructURL.pathname + url
37-
52+
constructURL.pathname = HttpRequests.constructCorrectPath(
53+
constructURL.pathname,
54+
url
55+
)
3856
if (params) {
3957
const queryParams = new URLSearchParams()
4058
Object.keys(params)
4159
.filter((x: string) => params[x] !== null)
4260
.map((x: string) => queryParams.set(x, params[x]))
4361
constructURL.search = queryParams.toString()
4462
}
45-
4663
const response: Response = await fetch(constructURL.toString(), {
4764
...config,
4865
method,

0 commit comments

Comments
 (0)