Skip to content

Commit c91efe3

Browse files
Allow the request "credentials" option to be configured (#1262)
1 parent fb394f0 commit c91efe3

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

.changeset/plenty-camels-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Add the possibility to configure the Request "credentials" option. Currently, the library only uses the default "same-site" value, which does not include cookies for cross-origin requests.

packages/browser/src/plugins/segmentio/__tests__/batched-dispatcher.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe('Batching', () => {
9797
"https://https://api.segment.io/b",
9898
{
9999
"body": "{"batch":[{"event":"first"},{"event":"second"},{"event":"third"}],"sentAt":"1993-06-09T00:00:00.000Z"}",
100+
"credentials": undefined,
100101
"headers": {
101102
"Content-Type": "text/plain",
102103
},
@@ -181,6 +182,7 @@ describe('Batching', () => {
181182
"https://https://api.segment.io/b",
182183
{
183184
"body": "{"batch":[{"event":"first"},{"event":"second"}],"sentAt":"1993-06-09T00:00:10.000Z"}",
185+
"credentials": undefined,
184186
"headers": {
185187
"Content-Type": "text/plain",
186188
},
@@ -217,6 +219,7 @@ describe('Batching', () => {
217219
"https://https://api.segment.io/b",
218220
{
219221
"body": "{"batch":[{"event":"first"}],"sentAt":"1993-06-09T00:00:10.000Z"}",
222+
"credentials": undefined,
220223
"headers": {
221224
"Content-Type": "text/plain",
222225
},
@@ -232,6 +235,7 @@ describe('Batching', () => {
232235
"https://https://api.segment.io/b",
233236
{
234237
"body": "{"batch":[{"event":"second"}],"sentAt":"1993-06-09T00:00:21.000Z"}",
238+
"credentials": undefined,
235239
"headers": {
236240
"Content-Type": "text/plain",
237241
},
@@ -264,6 +268,7 @@ describe('Batching', () => {
264268
"https://https://api.segment.io/b",
265269
{
266270
"body": "{"batch":[{"event":"first"},{"event":"second"}],"sentAt":"1993-06-09T00:00:00.000Z"}",
271+
"credentials": undefined,
267272
"headers": {
268273
"Content-Type": "text/plain",
269274
},

packages/browser/src/plugins/segmentio/__tests__/index.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ describe('Segment.io', () => {
7474
})
7575
})
7676

77+
describe('configuring fetch credentials', () => {
78+
it('should accept fetch credentials configuration', async () => {
79+
const analytics = new Analytics({ writeKey: 'foo' })
80+
81+
await analytics.register(
82+
await segmentio(analytics, {
83+
apiKey: '',
84+
deliveryStrategy: {
85+
config: {
86+
credentials: 'include',
87+
},
88+
},
89+
})
90+
)
91+
92+
await analytics.track('foo')
93+
const [_, params] = spyMock.mock.lastCall
94+
expect(params.credentials).toBe('include')
95+
})
96+
})
97+
7798
describe('configuring headers', () => {
7899
it('should accept additional headers', async () => {
79100
const analytics = new Analytics({ writeKey: 'foo' })

packages/browser/src/plugins/segmentio/batched-dispatcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default function batch(
7777
})
7878

7979
return fetch(`https://${apiHost}/b`, {
80+
credentials: config?.credentials,
8081
keepalive: config?.keepalive || pageUnloaded,
8182
headers: createHeaders(config?.headers),
8283
method: 'post',

packages/browser/src/plugins/segmentio/fetch-dispatcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function (config?: StandardDispatcherConfig): {
88
} {
99
function dispatch(url: string, body: object): Promise<unknown> {
1010
return fetch(url, {
11+
credentials: config?.credentials,
1112
keepalive: config?.keepalive,
1213
headers: createHeaders(config?.headers),
1314
method: 'post',

packages/browser/src/plugins/segmentio/shared-dispatcher.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ export type AdditionalHeaders =
1919
| (() => Record<string, string>)
2020

2121
export type RequestPriority = 'high' | 'low' | 'auto'
22+
export type RequestCredentials = 'include' | 'same-origin' | 'omit'
2223

2324
/**
2425
* These are the options that can be passed to the fetch dispatcher.
2526
* They more/less map to the Fetch RequestInit type.
2627
*/
2728
interface DispatchFetchConfig {
29+
/**
30+
* Request credentials configuration
31+
*
32+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
33+
*/
34+
credentials?: RequestCredentials
2835
/**
2936
* This is useful for ensuring that an event is sent even if the user navigates away from the page.
3037
* However, it may increase the likelihood of events being lost, as there is a 64kb limit for *all* fetch requests (not just ones to segment) with keepalive (which is why it's disabled by default). So, if you're sending a lot of data, this will likely cause events to be dropped.

0 commit comments

Comments
 (0)