Skip to content

Commit 496b6b7

Browse files
authored
allow for additional header and set correct content type for requests (#546)
* feat: improved header handling * fix: use types and set property directly Co-authored-by: hazeglide <[email protected]>
1 parent 6b254d3 commit 496b6b7

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/meili-axios-wrapper.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ class MeiliAxiosWrapper implements Types.MeiliAxiosWrapperInterface {
2121
cancelTokenSource: CancelTokenSource
2222

2323
constructor(config: Types.Config) {
24+
const headers: { [index: string]: any } = {
25+
...(config.headers || {}),
26+
'Content-Type': 'application/json',
27+
}
2428
if (config.apiKey !== undefined) {
25-
this.instance = instance.create({
26-
baseURL: config.host,
27-
headers: {
28-
'X-Meili-API-Key': config.apiKey,
29-
},
30-
})
31-
} else {
32-
this.instance = instance.create({
33-
baseURL: config.host,
34-
})
29+
headers['X-Meili-API-Key'] = config.apiKey
3530
}
31+
this.instance = instance.create({
32+
baseURL: config.host,
33+
headers: headers,
34+
})
3635
this.cancelTokenSource = instance.CancelToken.source()
3736
this.instance.interceptors.response.use((response) => response.data)
3837
this.instance.interceptors.request.use((request) => {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export { MeiliSearchTimeOutError }
2626
export interface Config {
2727
host: string
2828
apiKey?: string
29+
headers?: object
2930
}
3031

3132
///

0 commit comments

Comments
 (0)