Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5868a0a

Browse files
authored
Merge pull requestsegmentio#4989 from segmentio/MichaelGHSeg/LIBWEB-1395
Adding info about new http client
2 parents8a237a0 +a0b9c76 commit5868a0a

File tree

1 file changed

+66
-0
lines changed
  • src/connections/sources/catalog/libraries/server/node

1 file changed

+66
-0
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ Setting | Details
284284
`flushInterval` _number_ | The number of milliseconds to wait before flushing the queue automatically. The default is: `10000`
285285
`httpRequestTimeout` | The maximum number of milliseconds to wait for an http request. The default is: `10000`
286286
`disable` | Disable the analytics library for testing. The default is: `false`
287+
`httpClient` *Optional* | A custom AnalyticsHTTPClient implementation to support alternate libraries or proxies. Defaults to global fetch or node-fetch for older versions of node.
287288
288289
## Graceful shutdown
289290
Avoid losing events after shutting down your console. Call `.closeAndFlush()` to stop collecting new events and flush all existing events. If a callback on an event call is included, this also waits for all callbacks to be called, and any of their subsequent promises to be resolved.
@@ -553,7 +554,72 @@ Different parts of your application may require different types of batching, or
553554
const marketingAnalytics = new Analytics({ writeKey:'MARKETING_WRITE_KEY' });
554555
const appAnalytics = new Analytics({ writeKey:'APP_WRITE_KEY' });
555556
```
557+
## AnalyticsHTTPClient
556558
559+
Segment attempts to use the global `fetch` implementation if available in order to support several diverse environments. Some special cases (for example, http proxy) may require a different implementation for http communication. You can provide a customized wrapper in the Analytics configuration to support this. Here are a few approaches:
560+
561+
Use a custom fetch-like implementation with proxy (simple, recommended)
562+
```javascript
563+
import { HTTPFetchFn } from'../lib/http-client'
564+
import axios from'axios'
565+
566+
const httpClient: HTTPFetchFn = async (url, options) => {
567+
return axios({
568+
url,
569+
proxy: {
570+
protocol:'http',
571+
host:'proxy.example.com',
572+
port: 8886,
573+
auth: {
574+
username:'user',
575+
password:'pass',
576+
},
577+
},
578+
...options,
579+
})
580+
}
581+
582+
const analytics = new Analytics({
583+
writeKey:'<YOUR_WRITE_KEY>',
584+
httpClient,
585+
})
586+
```
587+
Augment the default HTTP Client
588+
```javascript
589+
import { FetchHTTPClient, HTTPClientRequest } from'@segment/analytics-node'
590+
591+
class MyClient extends FetchHTTPClient {
592+
async makeRequest(options: HTTPClientRequest) {
593+
return super.makeRequest({
594+
...options,
595+
headers: { ...options.headers, foo:'bar' }
596+
}})
597+
}
598+
}
599+
600+
const analytics = new Analytics({
601+
writeKey:'<YOUR_WRITE_KEY>',
602+
httpClient: new MyClient()
603+
})
604+
```
605+
Completely override the full HTTPClient (Advanced, you probably don't need todo this)
606+
```javascript
607+
import { HTTPClient, HTTPClientRequest } from'@segment/analytics-node'
608+
609+
class CustomClient implements HTTPClient {
610+
async makeRequest(options: HTTPClientRequest) {
611+
return someRequestLibrary(options.url, {
612+
method: options.method,
613+
body: JSON.stringify(options.data) // serialize data
614+
headers: options.headers,
615+
})
616+
}
617+
}
618+
const analytics = new Analytics({
619+
writeKey:'<YOUR_WRITE_KEY>',
620+
httpClient: newCustomClient()
621+
})
622+
```
557623
558624
## Troubleshooting
559625

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp