Skip to content

Set custom header in fetch request #1370

@AydinHassan

Description

@AydinHassan

Summary

Hello, like in #267 we would like to add a custom header for authentication with our proxy server. As suggested in that issue I don't think it's possible to replace the transport provider in order to provide a new transport any more. We could use the api_key field but we also need to update that when the token is refreshed so that's no ideal.

In an perfect world we would just extend/swap the fetch transport and add our header there but my attempts failed. Maybe there is an easy way that I am missing :)

I tried like this:

import * as amplitude from '@amplitude/analytics-browser';

amplitude.init('placeholder-apikey', undefined, {
    autocapture: false,
    serverZone: 'EU',
    appVersion: '',
    trackingOptions: {
        ipAddress: false,
        language: false,
        platform: false,
    },
    fetchRemoteConfig: false,
    serverUrl: 'http://proxy-url',
    transportProvider: (transport?: TransportType): Transport => {
        if (transport === 'beacon') {
            return new SendBeaconTransport();
        }

        return new class extends BaseTransport implements Transport {
            async send(serverUrl: string, payload: Payload): Promise<Response | null> {
                //...
            }
        }
    },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions