Skip to content

Commit 85cc249

Browse files
committed
feat: include client version header with API requests
1 parent 7bd5abf commit 85cc249

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
]
6060
},
6161
"dependencies": {
62+
"@jsbits/get-package-version": "^1.0.3",
6263
"@noble/secp256k1": "^1.5.2",
6364
"@stardazed/streams-polyfill": "^2.4.0",
6465
"@xmtp/proto": "^3.1.0",

src/ApiClient.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import { retry, sleep } from './utils'
44
import Long from 'long'
55
import AuthCache from './authn/AuthCache'
66
import { Authenticator } from './authn'
7+
import getPackageVersion from '@jsbits/get-package-version'
78
export const { MessageApi, SortDirection } = messageApi
89

910
const RETRY_SLEEP_TIME = 100
1011
const ERR_CODE_UNAUTHENTICATED = 16
1112

13+
const getClientVersion = () => 'xmtp-js/' + getPackageVersion()
14+
15+
const clientVersionHeaderKey = 'X-Client-Version'
16+
1217
export type GrpcError = Error & { code?: number }
1318

1419
export type QueryParams = {
@@ -89,7 +94,16 @@ export default class ApiClient {
8994
): ReturnType<typeof MessageApi.Query> {
9095
return retry(
9196
MessageApi.Query,
92-
[req, { pathPrefix: this.pathPrefix, mode: 'cors' }],
97+
[
98+
req,
99+
{
100+
pathPrefix: this.pathPrefix,
101+
mode: 'cors',
102+
headers: new Headers({
103+
[clientVersionHeaderKey]: getClientVersion(),
104+
}),
105+
},
106+
],
93107
this.maxRetries,
94108
RETRY_SLEEP_TIME
95109
)
@@ -109,7 +123,10 @@ export default class ApiClient {
109123
{
110124
pathPrefix: this.pathPrefix,
111125
mode: 'cors',
112-
headers: new Headers({ Authorization: `Bearer ${authToken}` }),
126+
headers: new Headers({
127+
Authorization: `Bearer ${authToken}`,
128+
[clientVersionHeaderKey]: getClientVersion(),
129+
}),
113130
},
114131
],
115132
this.maxRetries,
@@ -143,6 +160,9 @@ export default class ApiClient {
143160
pathPrefix: this.pathPrefix,
144161
signal: abortController.signal,
145162
mode: 'cors',
163+
headers: new Headers({
164+
[clientVersionHeaderKey]: getClientVersion(),
165+
}),
146166
}).catch(async (err: GrpcError) => {
147167
if (isAbortError(err)) {
148168
return

test/ApiClient.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Long from 'long'
55
import { sleep } from './helpers'
66
import { Authenticator } from '../src/authn'
77
import { PrivateKey } from '../src'
8+
import getPackageVersion from '@jsbits/get-package-version'
89
const { MessageApi } = messageApi
910

1011
const PATH_PREFIX = 'http://fake:5050'
@@ -51,6 +52,9 @@ describe('Query', () => {
5152
expect(apiMock).toHaveBeenCalledWith(expectedReq, {
5253
pathPrefix: PATH_PREFIX,
5354
mode: 'cors',
55+
headers: new Headers({
56+
'X-Client-Version': 'xmtp-js/' + getPackageVersion(),
57+
}),
5458
})
5559
})
5660

@@ -99,6 +103,9 @@ describe('Query', () => {
99103
expect(apiMock).toHaveBeenCalledWith(expectedReq, {
100104
pathPrefix: PATH_PREFIX,
101105
mode: 'cors',
106+
headers: new Headers({
107+
'X-Client-Version': 'xmtp-js/' + getPackageVersion(),
108+
}),
102109
})
103110
})
104111

@@ -124,6 +131,9 @@ describe('Query', () => {
124131
expect(apiMock).toHaveBeenLastCalledWith(expectedReq, {
125132
pathPrefix: PATH_PREFIX,
126133
mode: 'cors',
134+
headers: new Headers({
135+
'X-Client-Version': 'xmtp-js/' + getPackageVersion(),
136+
}),
127137
})
128138
})
129139
})
@@ -164,7 +174,10 @@ describe('Publish', () => {
164174
expect(publishMock).toHaveBeenCalledWith(expectedRequest, {
165175
pathPrefix: PATH_PREFIX,
166176
mode: 'cors',
167-
headers: new Headers({ Authorization: `Bearer ${AUTH_TOKEN}` }),
177+
headers: new Headers({
178+
Authorization: `Bearer ${AUTH_TOKEN}`,
179+
'X-Client-Version': 'xmtp-js/' + getPackageVersion(),
180+
}),
168181
})
169182
})
170183

@@ -237,6 +250,9 @@ describe('Subscribe', () => {
237250
pathPrefix: PATH_PREFIX,
238251
signal: expect.anything(),
239252
mode: 'cors',
253+
headers: new Headers({
254+
'X-Client-Version': 'xmtp-js/' + getPackageVersion(),
255+
}),
240256
})
241257
})
242258

0 commit comments

Comments
 (0)