From bffc4c65605368c3ab56db72f5adca3a8389c7bd Mon Sep 17 00:00:00 2001 From: michael1011 Date: Thu, 12 Dec 2024 19:57:54 +0100 Subject: [PATCH] chore: pass referral id in every request header --- src/consts/LocalStorage.ts | 1 + src/context/Global.tsx | 3 ++- src/utils/helper.ts | 12 +++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 src/consts/LocalStorage.ts diff --git a/src/consts/LocalStorage.ts b/src/consts/LocalStorage.ts new file mode 100644 index 00000000..ac3161fe --- /dev/null +++ b/src/consts/LocalStorage.ts @@ -0,0 +1 @@ +export const referralIdKey = "ref"; diff --git a/src/context/Global.tsx b/src/context/Global.tsx index 9db53cc3..e23da708 100644 --- a/src/context/Global.tsx +++ b/src/context/Global.tsx @@ -16,6 +16,7 @@ import type { JSX } from "solid-js"; import { config } from "../config"; import { Denomination } from "../consts/Enums"; +import { referralIdKey } from "../consts/LocalStorage"; import { swapStatusFinal } from "../consts/SwapStatus"; import { detectLanguage } from "../i18n/detect"; import dict, { DictKey } from "../i18n/i18n"; @@ -133,7 +134,7 @@ const GlobalProvider = (props: { children: JSX.Element }) => { isMobile() ? "boltz_webapp_mobile" : "boltz_webapp_desktop", ), { - name: "ref", + name: referralIdKey, ...stringSerializer, }, ); diff --git a/src/utils/helper.ts b/src/utils/helper.ts index c2aa5016..b1c4bcb2 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -4,6 +4,7 @@ import { ECPairInterface } from "ecpair"; import { chooseUrl, config } from "../config"; import { BTC, LN } from "../consts/Assets"; import { SwapType } from "../consts/Enums"; +import { referralIdKey } from "../consts/LocalStorage"; import { ChainPairTypeTaproot, Pairs, @@ -84,16 +85,25 @@ export const fetcher = async ( url: string, params?: Record, ): Promise => { - let opts = {}; + // We cannot use the context here, so we get the data directly from local storage + const referral = localStorage.getItem(referralIdKey); + let opts: RequestInit = { + headers: { + referral, + }, + }; + if (params) { opts = { method: "POST", headers: { + ...opts.headers, "Content-Type": "application/json", }, body: JSON.stringify(params), }; } + const apiUrl = getApiUrl() + url; const response = await fetch(apiUrl, opts); if (!response.ok) {