Skip to content

Commit

Permalink
✨[RUM-2158] Allow more flexible proxy URL (#2502)
Browse files Browse the repository at this point in the history
* ✨Allow more flexible proxy URL

* 👌use options parameter
  • Loading branch information
bcaudan authored Nov 17, 2023
1 parent 3e9748b commit 4d7e114
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface InitConfiguration {
storeContextsAcrossPages?: boolean | undefined

// transport options
proxy?: string | undefined
proxy?: string | ProxyFn | undefined
site?: string | undefined

// tag and context options
Expand All @@ -49,6 +49,7 @@ export interface InitConfiguration {
enableExperimentalFeatures?: string[] | undefined
replica?: ReplicaUserConfiguration | undefined
datacenter?: string
// TODO next major: remove this option and replace usages by proxyFn
internalAnalyticsSubdomain?: string

telemetryConfigurationSampleRate?: number
Expand All @@ -58,6 +59,12 @@ export interface InitConfiguration {
// for this option.
type GenericBeforeSendCallback = (event: any, context?: any) => unknown

/**
* path: /api/vX/product
* parameters: xxx=yyy&zzz=aaa
*/
type ProxyFn = (options: { path: string; parameters: string }) => string

interface ReplicaUserConfiguration {
applicationId?: string
clientToken: string
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/domain/configuration/endpointBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ describe('endpointBuilder', () => {
)
).toBeTrue()
})

it('should allow to fully control the proxy url', () => {
const proxyFn = (options: { path: string; parameters: string }) =>
`https://proxy.io/prefix${options.path}/suffix?${options.parameters}`
expect(
createEndpointBuilder({ ...initConfiguration, proxy: proxyFn }, 'rum', []).build('xhr', DEFAULT_PAYLOAD)
).toMatch(
`https://proxy.io/prefix/api/v2/rum/suffix\\?ddsource=(.*)&ddtags=(.*)&dd-api-key=${clientToken}&dd-evp-origin-version=(.*)&dd-evp-origin=browser&dd-request-id=(.*)&batch_time=(.*)`
)
})
})

describe('tags', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ function createEndpointUrlWithParametersBuilder(
): (parameters: string) => string {
const path = `/api/v2/${trackType}`
const proxy = initConfiguration.proxy
if (proxy) {
if (typeof proxy === 'string') {
const normalizedProxyUrl = normalizeUrl(proxy)
return (parameters) => `${normalizedProxyUrl}?ddforward=${encodeURIComponent(`${path}?${parameters}`)}`
}
if (typeof proxy === 'function') {
return (parameters) => proxy({ path, parameters })
}
const host = buildEndpointHost(initConfiguration)
return (parameters) => `https://${host}${path}?${parameters}`
}
Expand Down

0 comments on commit 4d7e114

Please sign in to comment.