Skip to content

Commit

Permalink
refactor: update clients to have different user agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Botseer authored Oct 27, 2023
1 parent 8fea5e4 commit c767786
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 c767786

Please sign in to comment.