Skip to content

Commit 4ee190a

Browse files
authored
Updating samples
1 parent 8104d33 commit 4ee190a

File tree

1 file changed

+54
-27
lines changed
  • src/connections/sources/catalog/libraries/server/node

1 file changed

+54
-27
lines changed

src/connections/sources/catalog/libraries/server/node/index.md

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -556,40 +556,67 @@ const appAnalytics = new Analytics({ writeKey: 'APP_WRITE_KEY' });
556556
```
557557
## AnalyticsHTTPClient
558558
559-
In some cases such as supporting an environment with an http proxy in place, you may need to use something other than our multiplatform `fetch` call. You can create a custom `AnalyticsHTTPClient` implementation and pass it in to the Analytics Configuration.
559+
We attempt to use the global `fetch` implementation if available in order to support several diverse environments. Some special cases (e.g. http proxy) may require a different implementation for http communication. A customized wrapper can be provided in the Analytics configuration to support this. Here are a few approaches:
560560
561-
An example of supporting proxies with a popular library, axios:
561+
Use a custom fetch-like implementation with proxy (simple, recommended)
562+
```
563+
import { HTTPFetchFn } from '../lib/http-client'
564+
565+
const httpClient: HTTPFetchFn = async (url, options) => {
566+
return axios({
567+
url,
568+
proxy: {
569+
protocol: 'http',
570+
host: 'proxy.example.com',
571+
port: 8886,
572+
auth: {
573+
username: 'user',
574+
password: 'pass',
575+
},
576+
},
577+
...options,
578+
})
579+
}
580+
581+
new Analytics({
582+
writeKey: '<YOUR_WRITE_KEY>',
583+
httpClient,
584+
})
585+
```
586+
Augment the default HTTP Client
562587
```javascript
563-
const axios = require('axios')
564-
565-
export class ProxyAxiosClient implements AnalyticsHTTPClient {
566-
public proxy = null
567-
async send(
568-
url: string,
569-
options: AnalyticsHTTPClientOptions
570-
): Promise<AnalyticsHTTPClientResponse> {
571-
const proxyoptions = Object.assign({}, options, this.proxy)
572-
return await axios.get(url, proxyoptions)
588+
import { FetchHTTPClient, HTTPClientRequest } from '@segment/analytics-node'
589+
590+
class MyClient extends FetchHTTPClient {
591+
async makeRequest(options: HTTPClientRequest) {
592+
return super.makeRequest({
593+
...options,
594+
headers: { ...options.headers, foo: 'bar' }
595+
}})
573596
}
574597
}
598+
599+
const analytics = new Analytics({
600+
writeKey: '<YOUR_WRITE_KEY>',
601+
httpClient: new MyClient()
602+
})
575603
```
576-
And then in your initialization:
604+
Completely override the full HTTPClient (Advanced, you probably don't need to do this)
577605
```javascript
578-
const proxyHttpClient = new ProxyAxiosClient()
579-
proxyHttpClient.proxy = {
580-
proxy: {
581-
protocol: 'http',
582-
host: 'proxy.example.com',
583-
port: 8886,
584-
auth: {
585-
username: 'user',
586-
password: 'pass',
587-
},
588-
},
606+
import { HTTPClient, HTTPClientRequest } from '@segment/analytics-node'
607+
608+
class CustomClient implements HTTPClient {
609+
async makeRequest(options: HTTPClientRequest) {
610+
return someRequestLibrary(options.url, {
611+
method: options.method,
612+
body: JSON.stringify(options.data) // serialize data
613+
headers: options.headers,
614+
})
615+
}
589616
}
590-
const analytics = new Analytics({
591-
writeKey: '<YOUR_WRITE_KEY>',
592-
httpClient: proxyHttpClient,
617+
const analytics = new Analytics({
618+
writeKey: '<YOUR_WRITE_KEY>',
619+
httpClient: new CustomClient()
593620
})
594621
```
595622

0 commit comments

Comments
 (0)