Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
VerifyEmailRequest,
VerifyEmailResponse,
} from "@/api/types";
import { AUTH_ENDPOINTS } from "@/lib/constants";
import { AUTH_ENDPOINTS, CLIENT_HEADER, CLIENT_HEADER_VALUE } from "@/lib/constants";
import { accessTokenStorage, refreshTokenStorage } from "@/lib/storage";
import {
loginResponseSchema,
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function refreshAccessToken(): Promise<boolean> {
try {
const res = await fetch(AUTH_ENDPOINTS.deviceRefresh, {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", [CLIENT_HEADER]: CLIENT_HEADER_VALUE },
body: JSON.stringify({ refresh_token: refreshToken }),
signal: AbortSignal.timeout(10_000),
});
Expand Down
7 changes: 5 additions & 2 deletions src/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ApiErrorResponse } from "@/api/types";
import { AUTH_ENDPOINTS } from "@/lib/constants";
import { AUTH_ENDPOINTS, CLIENT_HEADER, CLIENT_HEADER_VALUE } from "@/lib/constants";
import { ApiError, isOffline, NetworkError } from "@/lib/errors";
import {
accessTokenStorage,
Expand Down Expand Up @@ -43,7 +43,7 @@ async function refreshAccessToken(): Promise<string | null> {
try {
const res = await fetch(AUTH_ENDPOINTS.deviceRefresh, {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", [CLIENT_HEADER]: CLIENT_HEADER_VALUE },
body: JSON.stringify({ refresh_token: refreshToken }),
});

Expand Down Expand Up @@ -88,6 +88,9 @@ export async function request<T>(
}
}

// Client attribution header (all request() URLs point at the spoo.me backend)
headers[CLIENT_HEADER] = CLIENT_HEADER_VALUE;

// Auth header
if (!noAuth) {
const authHeader = await getAuthHeader();
Expand Down
4 changes: 2 additions & 2 deletions src/api/export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ExportQuery } from "@/api/types";
import { API_V1 } from "@/lib/constants";
import { API_V1, CLIENT_HEADER, CLIENT_HEADER_VALUE } from "@/lib/constants";
import { isOffline, NetworkError } from "@/lib/errors";
import { accessTokenStorage, apiKeyStorage, authModeStorage } from "@/lib/storage";

Expand All @@ -17,7 +17,7 @@ export async function exportStats(query: ExportQuery): Promise<Blob> {
}
}

const headers: Record<string, string> = {};
const headers: Record<string, string> = { [CLIENT_HEADER]: CLIENT_HEADER_VALUE };
const mode = await authModeStorage.getValue();
if (mode === "apikey") {
const key = await apiKeyStorage.getValue();
Expand Down
4 changes: 2 additions & 2 deletions src/api/stats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from "@/api/client";
import type { StatsQuery, StatsResponse } from "@/api/types";
import { API_BASE_URL, API_V1 } from "@/lib/constants";
import { API_BASE_URL, API_V1, CLIENT_HEADER, CLIENT_HEADER_VALUE } from "@/lib/constants";
import { ApiError } from "@/lib/errors";
import { statsResponseSchema } from "@/schemas/api";

Expand Down Expand Up @@ -50,7 +50,7 @@ export interface V0StatsResponse {
*/
export async function getStatsV0(shortCode: string, password?: string): Promise<V0StatsResponse> {
const url = `${API_BASE_URL}/stats/${shortCode}`;
const headers: Record<string, string> = {};
const headers: Record<string, string> = { [CLIENT_HEADER]: CLIENT_HEADER_VALUE };

const body = password ? new URLSearchParams({ password }) : undefined;
if (body) {
Expand Down
4 changes: 3 additions & 1 deletion src/entrypoints/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ACCESS_TOKEN_TTL_MS,
API_BASE_URL,
AUTH_ENDPOINTS,
CLIENT_HEADER,
CLIENT_HEADER_VALUE,
HISTORY_MAX_ITEMS,
QR_BRAND,
TOKEN_REFRESH_BUFFER_MS,
Expand Down Expand Up @@ -139,7 +141,7 @@ async function handleTokenRefresh(): Promise<void> {
async function exchangeDeviceCode(code: string): Promise<void> {
const res = await fetch(AUTH_ENDPOINTS.deviceToken, {
method: "POST",
headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json", [CLIENT_HEADER]: CLIENT_HEADER_VALUE },
body: JSON.stringify({ code }),
});

Expand Down
4 changes: 4 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const QR_API_BASE_URL = import.meta.env.VITE_QR_API_BASE_URL || "https://
export const API_V1 = `${API_BASE_URL}/api/v1`;
export const QR_API_V1 = `${QR_API_BASE_URL}/api/v1`;

/** Identifies this client to the spoo.me API. Never sent to third-party hosts. */
export const CLIENT_HEADER = "X-Spoo-Client";
export const CLIENT_HEADER_VALUE = `snap/${browser.runtime.getManifest().version}`;

export const AUTH_ENDPOINTS = {
login: `${API_BASE_URL}/auth/login`,
register: `${API_BASE_URL}/auth/register`,
Expand Down
Loading