Skip to content

Commit

Permalink
chore: pass referral id in every request header
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Dec 12, 2024
1 parent c98503d commit bffc4c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/consts/LocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const referralIdKey = "ref";
3 changes: 2 additions & 1 deletion src/context/Global.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -133,7 +134,7 @@ const GlobalProvider = (props: { children: JSX.Element }) => {
isMobile() ? "boltz_webapp_mobile" : "boltz_webapp_desktop",
),
{
name: "ref",
name: referralIdKey,
...stringSerializer,
},
);
Expand Down
12 changes: 11 additions & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -84,16 +85,25 @@ export const fetcher = async <T = unknown>(
url: string,
params?: Record<string, unknown>,
): Promise<T> => {
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) {
Expand Down

0 comments on commit bffc4c6

Please sign in to comment.