Skip to content

Commit

Permalink
Merge #1595
Browse files Browse the repository at this point in the history
1595: refactor: update clients to have different user agents r=brunoocasali a=Botseer

# Pull Request

## Related issue
Fixes #1437

## What does this PR do?
The users agents for BrowserClient and NodeClient have been differentiated as per the issue for better metrics.

As for the code changes, I have made the change such that the necessary user agents are provided while constructing the respective client itself. Therefore there is no need to handle user agents in the `http-request` file.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Botseer <[email protected]>
  • Loading branch information
meili-bors[bot] and Botseer committed Oct 31, 2023
2 parents bf54981 + c767786 commit ec84e24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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})`,
],
})
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 @@ -55,7 +54,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 @@ -71,16 +69,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

0 comments on commit ec84e24

Please sign in to comment.