Skip to content

Commit f2bf650

Browse files
authored
Resolve path removal of host url (#628)
Linting Update client test for clarity remove async from non async function Remove header check in two URL tests Remove header from second test Fixes tests with missing headers Remove header check in two URL tests Fixes tests with missing headers
1 parent 5154d4b commit f2bf650

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/http-requests.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import {
77

88
class HttpRequests {
99
headers: {}
10-
baseUrl: string
11-
url: URL
10+
url: string
1211

1312
constructor(config: Types.Config) {
1413
this.headers = {
1514
...(config.headers || {}),
1615
'Content-Type': 'application/json',
1716
...(config.apiKey ? { 'X-Meili-API-Key': config.apiKey } : {}),
1817
}
19-
this.baseUrl = config.host
20-
this.url = new URL(this.baseUrl)
18+
this.url = config.host
2119
}
2220

2321
async request({
@@ -34,14 +32,17 @@ class HttpRequests {
3432
config?: Request
3533
}) {
3634
try {
37-
const constructURL = new URL(url, this.url)
35+
const constructURL = new URL(this.url)
36+
constructURL.pathname = constructURL.pathname + url
37+
3838
if (params) {
3939
const queryParams = new URLSearchParams()
4040
Object.keys(params)
4141
.filter((x: string) => params[x] !== null)
4242
.map((x: string) => queryParams.set(x, params[x]))
4343
constructURL.search = queryParams.toString()
4444
}
45+
4546
const response: Response = await fetch(constructURL.toString(), {
4647
...config,
4748
method,

tests/client_tests.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ describe.each([
3737
const health = await client.isHealthy()
3838
expect(health).toBe(true)
3939
})
40+
41+
test(`${permission} key: Client handles host URL with domain and path`, () => {
42+
const customHost = `${config.host}/api`
43+
const client = new MeiliSearch({
44+
host: customHost,
45+
apiKey: key,
46+
})
47+
expect(client.config.host).toBe(customHost)
48+
expect(client.httpRequest.url).toBe(customHost)
49+
})
50+
51+
test(`${permission} key: Client uses complete URL with domain and additionnal path in MeiliSearch call`, async () => {
52+
try {
53+
const customHost = `${config.host}/api`
54+
const client = new MeiliSearch({
55+
host: customHost,
56+
apiKey: key,
57+
})
58+
const health = await client.isHealthy()
59+
expect(health).toBe(false) // Left here to trigger failed test if error is not thrown
60+
} catch (e) {
61+
expect(e.type).toBe('MeiliSearchCommunicationError')
62+
expect(e.message).toBe('Not Found') // Expect 404 because host does not exist
63+
}
64+
})
4065
})
4166

4267
describe.each([

0 commit comments

Comments
 (0)