Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update clients to have different user agents #1595

Closed
9 changes: 8 additions & 1 deletion src/clients/browser-client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Config } from '../types'
import { Client } from './client'
import { PACKAGE_VERSION } from '../package-version'

class MeiliSearch extends Client {
constructor(config: Config) {
super(config)
super({
...config,
clientAgents: [
...(config.clientAgents ?? []),
`Meilisearch JavaScript (v${PACKAGE_VERSION})`,
],
})
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/clients/node-client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Client } from './client'
import { Config, TokenSearchRules, TokenOptions } from '../types'
import { Token } from '../token'
import { PACKAGE_VERSION } from '../package-version'

class MeiliSearch extends Client {
tokens: Token

constructor(config: Config) {
super(config)
super({
...config,
clientAgents: [
...(config.clientAgents ?? []),
`Meilisearch Node (v${PACKAGE_VERSION})`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that this package might be used by any other runtime like Deno and Bun and more, does it make sense to call it Node? Maybe it would be best if we'd only ship one client, one that runs everywhere, and is a lot more treeshakeable, but that's part of another issue.

],
})
this.tokens = new Token(config)
}

Expand Down
8 changes: 1 addition & 7 deletions src/http-requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Config, EnqueuedTaskObject } from './types'
import { PACKAGE_VERSION } from './package-version'

import {
MeiliSearchError,
Expand Down Expand Up @@ -58,7 +57,6 @@ function cloneAndParseHeaders(headers: HeadersInit): Record<string, string> {

function createHeaders(config: Config): Record<string, any> {
const agentHeader = 'X-Meilisearch-Client'
const packageAgent = `Meilisearch JavaScript (v${PACKAGE_VERSION})`
const contentType = 'Content-Type'
const authorization = 'Authorization'
const headers = cloneAndParseHeaders(config.requestConfig?.headers ?? {})
Expand All @@ -74,16 +72,12 @@ function createHeaders(config: Config): Record<string, any> {

// Creates the custom user agent with information on the package used.
if (config.clientAgents && Array.isArray(config.clientAgents)) {
const clients = config.clientAgents.concat(packageAgent)

headers[agentHeader] = clients.join(' ; ')
headers[agentHeader] = config.clientAgents.join(' ; ')
} else if (config.clientAgents && !Array.isArray(config.clientAgents)) {
// If the header is defined but not an array
throw new MeiliSearchError(
`Meilisearch: The header "${agentHeader}" should be an array of string(s).\n`
)
} else {
headers[agentHeader] = packageAgent
}

return headers
Expand Down
6 changes: 3 additions & 3 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
})

expect(client.httpRequest.headers['X-Meilisearch-Client']).toStrictEqual(
`Meilisearch JavaScript (v${PACKAGE_VERSION})`
`Meilisearch Node (v${PACKAGE_VERSION})`
)
})

Expand All @@ -278,7 +278,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
})

expect(client.httpRequest.headers['X-Meilisearch-Client']).toStrictEqual(
`Meilisearch JavaScript (v${PACKAGE_VERSION})`
`Meilisearch Node (v${PACKAGE_VERSION})`
)
})

Expand All @@ -291,7 +291,7 @@ describe.each([{ permission: 'Master' }, { permission: 'Admin' }])(
})

expect(client.httpRequest.headers['X-Meilisearch-Client']).toStrictEqual(
`random plugin 1 ; random plugin 2 ; Meilisearch JavaScript (v${PACKAGE_VERSION})`
`random plugin 1 ; random plugin 2 ; Meilisearch Node (v${PACKAGE_VERSION})`
)
})

Expand Down