Files
Project_Velocity/app/src/lib/velocitySession.ts

38 lines
1.0 KiB
TypeScript

export const VELOCITY_TOKEN_KEY = 'velocity-api-token';
export function getVelocityToken(): string | null {
if (typeof window === 'undefined') {
return null;
}
return window.localStorage.getItem(VELOCITY_TOKEN_KEY);
}
export function setVelocityToken(token: string) {
if (typeof window === 'undefined') {
return;
}
window.localStorage.setItem(VELOCITY_TOKEN_KEY, token);
}
export function clearVelocityToken() {
if (typeof window === 'undefined') {
return;
}
window.localStorage.removeItem(VELOCITY_TOKEN_KEY);
}
export function buildVelocityHeaders(init?: HeadersInit, includeJson = true): Headers {
const headers = new Headers(init);
if (includeJson && !headers.has('Content-Type')) {
headers.set('Content-Type', 'application/json');
}
if (!headers.has('Accept')) {
headers.set('Accept', 'application/json');
}
const token = getVelocityToken();
if (token && !headers.has('Authorization')) {
headers.set('Authorization', `Bearer ${token}`);
}
return headers;
}