Skip to content

Commit 6f6f320

Browse files
committed
Refactor URL construction
1 parent 47bda49 commit 6f6f320

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed

src/http-requests.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,22 @@ import {
77

88
class HttpRequests {
99
headers: {}
10-
url: string
10+
url: URL
1111

1212
constructor(config: Types.Config) {
1313
this.headers = {
1414
...(config.headers || {}),
1515
'Content-Type': 'application/json',
1616
...(config.apiKey ? { 'X-Meili-API-Key': config.apiKey } : {}),
1717
}
18-
this.url = config.host
18+
this.url = new 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)
20+
21+
static addTrailingSlash(url: string): string {
22+
if (!url.endsWith('/')) {
23+
url = url + '/'
3324
}
34-
return pathname + slash + url
25+
return url
3526
}
3627

3728
async request({
@@ -48,11 +39,7 @@ class HttpRequests {
4839
config?: Partial<Request>
4940
}) {
5041
try {
51-
const constructURL = new URL(this.url)
52-
constructURL.pathname = HttpRequests.constructCorrectPath(
53-
constructURL.pathname,
54-
url
55-
)
42+
const constructURL = new URL(url, this.url)
5643
if (params) {
5744
const queryParams = new URLSearchParams()
5845
Object.keys(params)

src/index.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ class Index<T> implements Types.IndexInterface<T> {
2929
[key: string]: createIndexPath
3030
} = {
3131
indexRoute: (indexUid: string) => {
32-
return `${Index.apiRoutes.indexes}/${indexUid}/`
32+
return `${Index.apiRoutes.indexes}/${indexUid}`
3333
},
3434
getUpdateStatus: (indexUid: string, updateId: objectId) => {
3535
return (
36-
Index.routeConstructors.indexRoute(indexUid) + `updates/${updateId}`
36+
Index.routeConstructors.indexRoute(indexUid) +
37+
'/' +
38+
`updates/${updateId}`
3739
)
3840
},
3941
getAllUpdateStatus: (indexUid: string) => {
40-
return Index.routeConstructors.indexRoute(indexUid) + `updates`
42+
return Index.routeConstructors.indexRoute(indexUid) + '/' + `updates`
4143
},
4244
search: (indexUid: string) => {
43-
return Index.routeConstructors.indexRoute(indexUid) + `search`
45+
return Index.routeConstructors.indexRoute(indexUid) + '/' + `search`
4446
},
4547
getRawInfo: (indexUid: string) => {
4648
return `indexes/${indexUid}`
@@ -52,15 +54,17 @@ class Index<T> implements Types.IndexInterface<T> {
5254
return Index.routeConstructors.indexRoute(indexUid)
5355
},
5456
getStats: (indexUid: string) => {
55-
return Index.routeConstructors.indexRoute(indexUid) + `stats`
57+
return Index.routeConstructors.indexRoute(indexUid) + '/' + `stats`
5658
},
5759
getDocument: (indexUid: string, documentId: objectId) => {
5860
return (
59-
Index.routeConstructors.indexRoute(indexUid) + `documents/${documentId}`
61+
Index.routeConstructors.indexRoute(indexUid) +
62+
'/' +
63+
`documents/${documentId}`
6064
)
6165
},
6266
getDocuments: (indexUid: string) => {
63-
return Index.routeConstructors.indexRoute(indexUid) + `documents`
67+
return Index.routeConstructors.indexRoute(indexUid) + '/' + `documents`
6468
},
6569
addDocuments: (indexUid: string) => {
6670
return Index.routeConstructors.getDocuments(indexUid)
@@ -73,16 +77,20 @@ class Index<T> implements Types.IndexInterface<T> {
7377
},
7478
deleteDocument: (indexUid: string, documentId: objectId) => {
7579
return (
76-
Index.routeConstructors.indexRoute(indexUid) + `documents/${documentId}`
80+
Index.routeConstructors.indexRoute(indexUid) +
81+
'/' +
82+
`documents/${documentId}`
7783
)
7884
},
7985
deleteDocuments: (indexUid: string) => {
8086
return (
81-
Index.routeConstructors.indexRoute(indexUid) + `documents/delete-batch`
87+
Index.routeConstructors.indexRoute(indexUid) +
88+
'/' +
89+
`documents/delete-batch`
8290
)
8391
},
8492
getSettings: (indexUid: string) => {
85-
return Index.routeConstructors.indexRoute(indexUid) + `settings`
93+
return Index.routeConstructors.indexRoute(indexUid) + '/' + `settings`
8694
},
8795
updateSettings: (indexUid: string) => {
8896
return Index.routeConstructors.getSettings(indexUid)
@@ -91,7 +99,9 @@ class Index<T> implements Types.IndexInterface<T> {
9199
return Index.routeConstructors.getSettings(indexUid)
92100
},
93101
getSynonyms: (indexUid: string) => {
94-
return Index.routeConstructors.indexRoute(indexUid) + `settings/synonyms`
102+
return (
103+
Index.routeConstructors.indexRoute(indexUid) + '/' + `settings/synonyms`
104+
)
95105
},
96106
updateSynonyms: (indexUid: string) => {
97107
return Index.routeConstructors.getSynonyms(indexUid)
@@ -101,7 +111,9 @@ class Index<T> implements Types.IndexInterface<T> {
101111
},
102112
getStopWords: (indexUid: string) => {
103113
return (
104-
Index.routeConstructors.indexRoute(indexUid) + `settings/stop-words`
114+
Index.routeConstructors.indexRoute(indexUid) +
115+
'/' +
116+
`settings/stop-words`
105117
)
106118
},
107119
updateStopWords: (indexUid: string) => {
@@ -112,7 +124,9 @@ class Index<T> implements Types.IndexInterface<T> {
112124
},
113125
getRankingRules: (indexUid: string) => {
114126
return (
115-
Index.routeConstructors.indexRoute(indexUid) + `settings/ranking-rules`
127+
Index.routeConstructors.indexRoute(indexUid) +
128+
'/' +
129+
`settings/ranking-rules`
116130
)
117131
},
118132
updateRankingRules: (indexUid: string) => {
@@ -124,6 +138,7 @@ class Index<T> implements Types.IndexInterface<T> {
124138
getDistinctAttribute: (indexUid: string) => {
125139
return (
126140
Index.routeConstructors.indexRoute(indexUid) +
141+
'/' +
127142
`settings/distinct-attribute`
128143
)
129144
},
@@ -136,6 +151,7 @@ class Index<T> implements Types.IndexInterface<T> {
136151
getAttributesForFaceting: (indexUid: string) => {
137152
return (
138153
Index.routeConstructors.indexRoute(indexUid) +
154+
'/' +
139155
`settings/attributes-for-faceting`
140156
)
141157
},
@@ -148,6 +164,7 @@ class Index<T> implements Types.IndexInterface<T> {
148164
getSearchableAttributes: (indexUid: string) => {
149165
return (
150166
Index.routeConstructors.indexRoute(indexUid) +
167+
'/' +
151168
`settings/searchable-attributes`
152169
)
153170
},
@@ -160,6 +177,7 @@ class Index<T> implements Types.IndexInterface<T> {
160177
getDisplayedAttributes: (indexUid: string) => {
161178
return (
162179
Index.routeConstructors.indexRoute(indexUid) +
180+
'/' +
163181
`settings/displayed-attributes`
164182
)
165183
},
@@ -176,6 +194,16 @@ class Index<T> implements Types.IndexInterface<T> {
176194
this.primaryKey = primaryKey
177195
this.httpRequest = new HttpRequests(config)
178196
}
197+
///
198+
/// STATIC
199+
///
200+
201+
static getApiRoutes(): { [key: string]: string } {
202+
return Index.apiRoutes
203+
}
204+
static getRouteConstructors(): { [key: string]: createIndexPath } {
205+
return Index.routeConstructors
206+
}
179207

180208
///
181209
/// UPDATES

src/meilisearch.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type createPath = (x: string | number) => string
1717
class MeiliSearch implements Types.MeiliSearchInterface {
1818
config: Types.Config
1919
httpRequest: HttpRequests
20-
static apiPaths: {
20+
static apiRoutes: {
2121
[key: string]: string
2222
} = {
2323
listIndexes: 'indexes',
@@ -27,7 +27,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
2727
version: 'version',
2828
createDump: 'dumps',
2929
}
30-
static apiPathConstructors: {
30+
static routeConstructors: {
3131
[key: string]: createPath
3232
} = {
3333
getDumpStatus: (dumpUid: string | number) => {
@@ -36,10 +36,18 @@ class MeiliSearch implements Types.MeiliSearchInterface {
3636
}
3737

3838
constructor(config: Types.Config) {
39+
config.host = HttpRequests.addTrailingSlash(config.host)
3940
this.config = config
4041
this.httpRequest = new HttpRequests(config)
4142
}
4243

44+
static getApiRoutes(): { [key: string]: string } {
45+
return MeiliSearch.apiRoutes
46+
}
47+
static getRouteConstructors(): { [key: string]: createPath } {
48+
return MeiliSearch.routeConstructors
49+
}
50+
4351
/**
4452
* Return an Index instance
4553
* @memberof MeiliSearch
@@ -85,7 +93,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
8593
* @method listIndexes
8694
*/
8795
async listIndexes(): Promise<Types.IndexResponse[]> {
88-
const url = MeiliSearch.apiPaths.listIndexes
96+
const url = MeiliSearch.apiRoutes.listIndexes
8997
return await this.httpRequest.get<Types.IndexResponse[]>(url)
9098
}
9199

@@ -132,7 +140,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
132140
* @method getKey
133141
*/
134142
async getKeys(): Promise<Types.Keys> {
135-
const url = MeiliSearch.apiPaths.getKeys
143+
const url = MeiliSearch.apiRoutes.getKeys
136144
return await this.httpRequest.get<Types.Keys>(url)
137145
}
138146

@@ -148,7 +156,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
148156
*/
149157
async isHealthy(): Promise<true> {
150158
return await this.httpRequest
151-
.get(MeiliSearch.apiPaths.isHealthy)
159+
.get(MeiliSearch.apiRoutes.isHealthy)
152160
.then(() => true)
153161
}
154162

@@ -162,7 +170,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
162170
* @method stats
163171
*/
164172
async stats(): Promise<Types.Stats> {
165-
const url = MeiliSearch.apiPaths.stats
173+
const url = MeiliSearch.apiRoutes.stats
166174
return await this.httpRequest.get<Types.Stats>(url)
167175
}
168176

@@ -172,7 +180,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
172180
* @method version
173181
*/
174182
async version(): Promise<Types.Version> {
175-
const url = MeiliSearch.apiPaths.version
183+
const url = MeiliSearch.apiRoutes.version
176184
return await this.httpRequest.get<Types.Version>(url)
177185
}
178186

@@ -186,7 +194,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
186194
* @method createDump
187195
*/
188196
async createDump(): Promise<Types.EnqueuedDump> {
189-
const url = MeiliSearch.apiPaths.createDump
197+
const url = MeiliSearch.apiRoutes.createDump
190198
return await this.httpRequest.post<undefined, Types.EnqueuedDump>(url)
191199
}
192200

@@ -196,7 +204,7 @@ class MeiliSearch implements Types.MeiliSearchInterface {
196204
* @method getDumpStatus
197205
*/
198206
async getDumpStatus(dumpUid: string): Promise<Types.EnqueuedDump> {
199-
const url = MeiliSearch.apiPathConstructors.getDumpStatus(dumpUid)
207+
const url = MeiliSearch.routeConstructors.getDumpStatus(dumpUid)
200208
return await this.httpRequest.get<Types.EnqueuedDump>(url)
201209
}
202210
}

0 commit comments

Comments
 (0)